-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAzureSpeechAPI.cpp
More file actions
232 lines (202 loc) · 7.28 KB
/
AzureSpeechAPI.cpp
File metadata and controls
232 lines (202 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#pragma once
#include "stdafx.h"
#include "AzureSpeechAPI.h"
#include "GetDefaultOutputDeviceID.cpp"
#include "RtAudioCallbackFunction.h"
AzureSpeechAPI* AzureSpeechAPI::instance = 0;
AzureSpeechAPI::AzureSpeechAPI(){
this->updater = nullptr;
}
AzureSpeechAPI::~AzureSpeechAPI(){}
void AzureSpeechAPI::stopRecognition() {
if (DISPOSE_FLAG == 0) {
azureRecognizer->StopContinuousRecognitionAsync().get();
}else {
azureTranslator->StopContinuousRecognitionAsync().get();
}
}
void AzureSpeechAPI::startRecognition(subtitleUpdater* updater)
{
this->updater = updater;
//find virtual mic that monitor the speaker output
CoInitializeEx(NULL, COINIT_MULTITHREADED);
LPWSTR routing = ListEndpoints();
std::wstring ws(routing);
std::string virtualMic = std::string(ws.begin(), ws.end());
auto audioConfig = AudioConfig::FromMicrophoneInput(virtualMic);
if (ENGINE__MODE==0) {
lunchRecognitionMode(audioConfig);
}
else {
lunchTranslationMode(audioConfig);
}
DISPOSE_FLAG = ENGINE__MODE;
std::promise<void> recognitionEnd;
//stream speaker output with RtAudio
if (!loopback->isStreamOpen() && !playback->isStreamOpen()) {
RtAudio::StreamParameters iParams, oParams;
iParams.deviceId = loopback->getDefaultOutputDevice();
iParams.nChannels = 2;
unsigned int devices = playback->getDeviceCount();
RtAudio::DeviceInfo info;
unsigned int speaker = 0;
for (unsigned int i = 0; i < devices; i++) {
info = playback->getDeviceInfo(i);
std::size_t found = info.name.find("CABLE Input");
if (found != std::string::npos) {
speaker = i;
}
}
oParams.deviceId = speaker;
oParams.nChannels = 2;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 100;
loopback->openStream(nullptr, &iParams, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &readSpeakerLoopback);
playback->openStream(&oParams, nullptr, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &playSpeakerSound);
loopback->startStream();
playback->startStream();
}
recognitionEnd.get_future().get();
}
void AzureSpeechAPI::lunchRecognitionMode(auto audioConfig) {
auto recognitionConfig = SpeechConfig::FromSubscription(SPEECH__SUBSCRIPTION__KEY, SPEECH__SERVICE__REGION);
recognitionConfig->SetSpeechRecognitionLanguage(detectionLanguage);
azureRecognizer = SpeechRecognizer::FromConfig(recognitionConfig, audioConfig);
azureRecognizer->Recognizing.Connect([&](const SpeechRecognitionEventArgs& e)
{
updater->updateRecognition(QString::fromStdString(e.Result->Text));
});
if (SAVING__RESULT__STATE) {
azureRecognizer->Recognized.Connect([&](const SpeechRecognitionEventArgs& e)
{
std::string recognitionLanguage;
std::string recognizedText = e.Result->Text;
if (recognizedText == "")return;
for (auto& index : supportedLanguage) {
if (index.second.recognitionCode == detectionLanguage) {
recognitionLanguage = index.second.name;
}
}
DBAPI->save_recognition_data(time(0), recognitionLanguage, recognizedText);
});
}
azureRecognizer->StartContinuousRecognitionAsync().get();
}
void AzureSpeechAPI::lunchTranslationMode(auto audioConfig) {
//setup azure connection
auto translationConfig = SpeechTranslationConfig::FromSubscription(SPEECH__SUBSCRIPTION__KEY, SPEECH__SERVICE__REGION);
translationConfig->SetSpeechRecognitionLanguage(detectionLanguage);
auto toLanguages = { translationLanguage };
for (auto language : toLanguages) {
translationConfig->AddTargetLanguage(language);
}
azureTranslator = TranslationRecognizer::FromConfig(translationConfig, audioConfig);
//prepare callback
if (SHOW__SINGLE__LANGUAGE) {
azureTranslator->Recognizing.Connect([&](const TranslationRecognitionEventArgs& e)
{
for (auto pair : e.Result->Translations)
{
auto language = pair.first;
auto translation = pair.second;
updater->updateRecognition(QString::fromStdString(translation));
}
});
}
else {
azureTranslator->Recognizing.Connect([&](const TranslationRecognitionEventArgs& e)
{
updater->updateRecognition(QString::fromStdString(e.Result->Text));
for (auto pair : e.Result->Translations)
{
auto language = pair.first;
auto translation = pair.second;
updater->updateTranslation(QString::fromStdString(translation));
}
});
}
if (SAVING__RESULT__STATE) {
azureTranslator->Recognized.Connect([&](const TranslationRecognitionEventArgs& e) {
std::string fromLanguage;
std::string recognizedText = e.Result->Text;
std::string toLanguage;
std::string translatedText;
for (auto pair : e.Result->Translations)
{
toLanguage = pair.first;
translatedText = pair.second;
}
if (recognizedText == translatedText)return;
for (auto& index : supportedLanguage) {
if (index.second.translationCode == toLanguage) {
toLanguage = index.second.name;
}
if (index.second.recognitionCode == detectionLanguage) {
fromLanguage = index.second.name;
}
}
DBAPI->save_translation_data(time(0), fromLanguage, toLanguage, recognizedText, translatedText);
});
}
azureTranslator->StartContinuousRecognitionAsync().get();
}
std::string AzureSpeechAPI::getKey()
{
return SPEECH__SUBSCRIPTION__KEY;
}
std::string AzureSpeechAPI::getRegion()
{
return SPEECH__SERVICE__REGION;
}
std::string AzureSpeechAPI::getDetectionLanguageCode()
{
return detectionLanguage;
}
std::string AzureSpeechAPI::getTranslationLanguageCode()
{
return translationLanguage;
}
bool AzureSpeechAPI::getSavingResultState()
{
return this->SAVING__RESULT__STATE;
}
void AzureSpeechAPI::setKey(std::string SPEECH__SUBSCRIPTION__KEY)
{
this->SPEECH__SUBSCRIPTION__KEY = SPEECH__SUBSCRIPTION__KEY;
}
void AzureSpeechAPI::setRegion(std::string SPEECH__SERVICE__REGION)
{
this->SPEECH__SERVICE__REGION = SPEECH__SERVICE__REGION;
}
void AzureSpeechAPI::setDetectionLanguageCode(std::string detectionLanguage)
{
this->detectionLanguage = detectionLanguage;
}
void AzureSpeechAPI::setTransaltionLanguageCode(std::string translationLanguage)
{
this->translationLanguage = translationLanguage;
}
void AzureSpeechAPI::setSavingResultState(bool state)
{
this->SAVING__RESULT__STATE = state;
}
int AzureSpeechAPI::getEngineMode()
{
return ENGINE__MODE;
}
bool AzureSpeechAPI::getShowSingleLanguage()
{
return SHOW__SINGLE__LANGUAGE;
}
void AzureSpeechAPI::setShowSingleLanguage(bool state)
{
this->SHOW__SINGLE__LANGUAGE = state;
}
void AzureSpeechAPI::setEngineMode(int mode)
{
this->ENGINE__MODE = mode;
}
std::map<std::string, language> AzureSpeechAPI::getSupportedLanguage()
{
return supportedLanguage;
}