Showing the changes to interface/src/scripting/LimitlessVoiceRecognitionScriptingInterface.cpp (1 of 4 files). Show all
Unified · Split
| @@ -0,0 +1,64 @@ | ||
| 1 | +// | |
| 2 | +// SpeechRecognitionScriptingInterface.h | |
| 3 | +// interface/src/scripting | |
| 4 | +// | |
| 5 | +// Created by Trevor Berninger on 3/20/17. | |
| 6 | +// Copyright 2017 Limitless ltd. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include <src/InterfaceLogging.h> | |
| 13 | +#include <src/ui/AvatarInputs.h> | |
| 14 | +#include <QtConcurrent/QtConcurrentRun> | |
| 15 | +#include "LimitlessVoiceRecognitionScriptingInterface.h" | |
| 16 | + | |
| 17 | +const float LimitlessVoiceRecognitionScriptingInterface::_audioLevelThreshold = 0.33f; | |
| 18 | +const int LimitlessVoiceRecognitionScriptingInterface::_voiceTimeoutDuration = 2000; | |
| 19 | + | |
| 20 | +LimitlessVoiceRecognitionScriptingInterface::LimitlessVoiceRecognitionScriptingInterface() : | |
| 21 | + _shouldStartListeningForVoice(false) | |
| 22 | +{ | |
| 23 | + _voiceTimer.setSingleShot(true); | |
| 24 | + connect(&_voiceTimer, &QTimer::timeout, this, &LimitlessVoiceRecognitionScriptingInterface::voiceTimeout); | |
| 25 | + connect(&_connection, &LimitlessConnection::onReceivedTranscription, this, [this](QString transcription){emit onReceivedTranscription(transcription);}); | |
| 26 | + connect(&_connection, &LimitlessConnection::onFinishedSpeaking, this, [this](QString transcription){emit onFinishedSpeaking(transcription);}); | |
| 27 | + _connection.moveToThread(&_connectionThread); | |
| 28 | + _connectionThread.setObjectName("Limitless Connection"); | |
| 29 | + _connectionThread.start(); | |
| 30 | +} | |
| 31 | + | |
| 32 | +void LimitlessVoiceRecognitionScriptingInterface::update() { | |
| 33 | + const float audioLevel = AvatarInputs::getInstance()->loudnessToAudioLevel(DependencyManager::get<AudioClient>()->getAudioAverageInputLoudness()); | |
| 34 | + | |
| 35 | + if (_shouldStartListeningForVoice) { | |
| 36 | + if (_connection._streamingAudioForTranscription) { | |
| 37 | + if (audioLevel > _audioLevelThreshold) { | |
| 38 | + if (_voiceTimer.isActive()) { | |
| 39 | + _voiceTimer.stop(); | |
| 40 | + } | |
| 41 | + } else if (!_voiceTimer.isActive()){ | |
| 42 | + _voiceTimer.start(_voiceTimeoutDuration); | |
| 43 | + } | |
| 44 | + } else if (audioLevel > _audioLevelThreshold) { | |
| 45 | + // to make sure invoke doesn't get called twice before the method actually gets called | |
| 46 | + _connection._streamingAudioForTranscription = true; | |
| 47 | + QMetaObject::invokeMethod(&_connection, "startListening", Q_ARG(QString, authCode)); | |
| 48 | + } | |
| 49 | + } | |
| 50 | +} | |
| 51 | + | |
| 52 | +void LimitlessVoiceRecognitionScriptingInterface::setListeningToVoice(bool listening) { | |
| 53 | + _shouldStartListeningForVoice = listening; | |
| 54 | +} | |
| 55 | + | |
| 56 | +void LimitlessVoiceRecognitionScriptingInterface::setAuthKey(QString key) { | |
| 57 | + authCode = key; | |
| 58 | +} | |
| 59 | + | |
| 60 | +void LimitlessVoiceRecognitionScriptingInterface::voiceTimeout() { | |
| 61 | + if (_connection._streamingAudioForTranscription) { | |
| 62 | + QMetaObject::invokeMethod(&_connection, "stopListening"); | |
| 63 | + } | |
| 64 | +} | |