-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.cpp
997 lines (856 loc) · 26 KB
/
speech.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
#include "speech.hpp"
#include "exception.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QCryptographicHash>
#include <QTimer>
#include <QDebug>
#include <libgen.h>
namespace Bing {
const QString FETCH_TOKEN_URI = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
const QString RECOGNITION_URL = "https://speech.platform.bing.com/speech/recognition/";
const QString SYNTHESIZE_URL = "https://speech.platform.bing.com/synthesize";
const int RENEW_TOKEN_INTERVAL = 9; // Minutes before renewing token
Speech *Speech::mInstance;
SoupSession *Speech::mSession;
QTimer *Speech::mRenewTokenTimer;
QString Speech::mRecognizerSubscriptionKey;
QString Speech::mRecognizerToken;
QString Speech::mSynthesizerSubscriptionKey;
QString Speech::mSynthesizerToken;
QString Speech::mConnectionId;
QString Speech::mEndpointId;
bool Speech::mCache;
void Speech::init(int log)
{
if (mSession) {
return;
}
SoupLogger *logger;
SoupLoggerLogLevel logLevel;
if (log <= 0) {
logLevel = SOUP_LOGGER_LOG_NONE;
} else if (log == 1) {
logLevel = SOUP_LOGGER_LOG_MINIMAL;
} else if (log == 2) {
logLevel = SOUP_LOGGER_LOG_HEADERS;
} else {
logLevel = SOUP_LOGGER_LOG_BODY;
}
mSession = soup_session_new_with_options(SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_SNIFFER, NULL);
logger = soup_logger_new(logLevel, -1);
soup_session_add_feature(mSession, SOUP_SESSION_FEATURE(logger));
g_object_unref(logger);
}
void Speech::destroy()
{
if (mSession) {
g_object_unref(mSession);
mSession = NULL;
}
mRecognizerToken.clear();
mSynthesizerToken.clear();
delete mInstance;
}
Speech *Speech::instance()
{
if (mInstance) {
return mInstance;
}
return new Speech();
}
void Speech::authenticate(const QString &recognizerSubscriptionKey, const QString &synthesizerSubscriptionKey)
{
mRecognizerSubscriptionKey = recognizerSubscriptionKey;
mSynthesizerSubscriptionKey = synthesizerSubscriptionKey;
Speech::fetchToken();
delete mRenewTokenTimer;
mRenewTokenTimer = new QTimer(this);
connect(mRenewTokenTimer, &QTimer::timeout, this, &Speech::renewToken);
mRenewTokenTimer->start(RENEW_TOKEN_INTERVAL * 60 * 1000);
}
void Speech::fetchToken()
{
SoupMessage *msg;
SoupMessageBody *body;
mRecognizerToken.clear();
mSynthesizerToken.clear();
// Recognizer
if (mEndpointId.isEmpty()) {
msg = soup_message_new("POST", FETCH_TOKEN_URI.toUtf8().data());
} else {
msg = soup_message_new("POST", "https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken");
}
soup_message_headers_append(msg->request_headers, "Content-Length", "0");
soup_message_headers_append(msg->request_headers, "Ocp-Apim-Subscription-Key", mRecognizerSubscriptionKey.toUtf8().data());
soup_session_send_message(mSession, msg);
g_object_get(msg, "response-body", &body, NULL);
mRecognizerToken = QByteArray(body->data, body->length);
// Synthesizer
msg = soup_message_new("POST", FETCH_TOKEN_URI.toUtf8().data());
soup_message_headers_append(msg->request_headers, "Content-Length", "0");
soup_message_headers_append(msg->request_headers, "Ocp-Apim-Subscription-Key", mSynthesizerSubscriptionKey.toUtf8().data());
soup_session_send_message(mSession, msg);
g_object_get(msg, "response-body", &body, NULL);
mSynthesizerToken = QByteArray(body->data, body->length);
}
void Speech::setCache(bool cache)
{
mCache = cache;
}
void Speech::setEndpointId(const QString &endpointId)
{
mEndpointId = endpointId;
}
void Speech::setTimeout(unsigned int secs)
{
if (!mSession) {
return;
}
g_object_set(mSession, SOUP_SESSION_TIMEOUT, secs, NULL);
g_object_set(mSession, SOUP_SESSION_IDLE_TIMEOUT, secs, NULL);
soup_session_abort(mSession);
}
void Speech::renewToken()
{
Speech::fetchToken();
fprintf(stdout, "%s\n", "Renewed access token");
}
Speech::RecognitionResponse Speech::recognize(const QByteArray &data, RecognitionLanguage language, RecognitionMode mode)
{
Speech::RecognitionResponse res;
SoupMessage *msg;
SoupMessageBody *body;
QString modeString;
switch (mode) {
default:
case RecognitionMode::Interactive:
modeString = "interactive";
break;
case RecognitionMode::Dictation:
modeString = "dictation";
break;
case RecognitionMode::Conversation:
modeString = "conversation";
break;
}
QString url;
if (mEndpointId.isEmpty()) {
url = RECOGNITION_URL + modeString + "/cognitiveservices/v1?language=" + recognitionLanguageString(language) + "&format=detailed";
} else {
url = "https://westus.stt.speech.microsoft.com/speech/recognition/" + modeString + "/cognitiveservices/v1?cid=" + mEndpointId + "&format=detailed";
}
QString auth = "Bearer " + mRecognizerToken;
// Do POST request
msg = soup_message_new("POST", url.toUtf8().data());
if (mEndpointId.isEmpty()) {
soup_message_set_request(msg, "audio/wav; codec=\"\"audio/pcm\"\"; samplerate=16000", SOUP_MEMORY_COPY, data.data(), data.size());
} else {
QByteArray tmp = data;
unsigned char wav_header_bin[] = {
0x52, 0x49, 0x46, 0x46, 0xc4, 0x09, 0x01, 0x00, 0x57, 0x41, 0x56,
0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
0x01, 0x00, 0x80, 0x3e, 0x00, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x02,
0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0xa0, 0x09, 0x01, 0x00
};
tmp.prepend(QByteArray(reinterpret_cast<const char *>(wav_header_bin), 44));
soup_message_set_request(msg, "application/octet-stream", SOUP_MEMORY_COPY, tmp.data(), tmp.size());
}
soup_message_headers_append(msg->request_headers, "Authorization", auth.toUtf8().data());
int httpStatusCode = soup_session_send_message(mSession, msg);
if (SOUP_STATUS_IS_CLIENT_ERROR(httpStatusCode) || SOUP_STATUS_IS_SERVER_ERROR(httpStatusCode)) {
throw Exception(HTTPError);
} else if (SOUP_STATUS_IS_TRANSPORT_ERROR(httpStatusCode)) {
throw Exception(IOError);
}
g_object_get(msg, "response-body", &body, NULL);
return parseRecognitionResponse(QByteArray(body->data, body->length));
}
Speech::RecognitionResponse Speech::parseRecognitionResponse(const QByteArray &data)
{
Speech::RecognitionResponse res;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
QJsonObject root;
if (error.error != QJsonParseError::NoError) {
return res;
}
root = doc.object();
res.recognitionStatus = root["RecognitionStatus"].toString();
res.offset = root["Offset"].toInt();
res.duration = root["Duration"].toInt();
auto nbest = root["NBest"].toArray();
for (auto i = 0; i < nbest.size(); i++) {
RecognitionResult result;
QJsonObject item = nbest[i].toObject();
result.confidence = item["Confidence"].toDouble();
result.lexical = item["Lexical"].toString();
result.itn = item["ITN"].toString();
result.maskedItn = item["MaskedITN"].toString();
result.display = item["Display"].toString();
res.nbest.push_back(result);
}
return res;
}
bool Speech::hasSynthesizeCache(const QString &text, const Voice::Font &font) const
{
auto path = Speech::cachePath(text, font);
QFile file(path);
return file.exists();
}
QByteArray Speech::loadSynthesizeCache(const QString &text, const Voice::Font &font)
{
QFile file(cachePath(text, font));
file.open(QIODevice::ReadOnly);
return file.readAll();
}
bool Speech::saveSynthesizeCache(const QByteArray &data, const QString &text, const Voice::Font &font)
{
if (data.isEmpty()) {
return false;
}
auto path = cachePath(text, font);
auto pathDup = strdup(path.toUtf8().data());
QFile file(path);
QDir dir(QString(dirname(pathDup)));
free(pathDup);
if (!dir.exists()) {
dir.mkpath(dir.path());
}
if (!file.open(QIODevice::WriteOnly)) {
return false;
}
return file.write(data) >= 0;
}
QString Speech::cachePath(const QString &text, const Voice::Font &font)
{
QString filePath;
QString cacheFilename = QString("%1").arg(QString(QCryptographicHash::hash(text.toUtf8(), QCryptographicHash::Sha1).toHex()));
qDebug() << "bing: speech.cpp: Cache path for" << text << "is" << cacheFilename;
filePath.append("/var/cache/bing/");
filePath.append(font.lang + "/");
filePath.append(font.gender + "/");
filePath.append(font.name + "/");
filePath.append(cacheFilename);
return filePath;
}
bool Speech::RecognitionResponse::hasMatch() const
{
return recognitionStatus == "Success";
}
bool Speech::RecognitionResponse::isSilent() const
{
return recognitionStatus == "InitialSilenceTimeout";
}
void Speech::RecognitionResponse::print() const
{
fprintf(stdout, "RecognitionStatus: %s\n", recognitionStatus.toUtf8().data());
fprintf(stdout, "Offset: %d\n", offset);
fprintf(stdout, "Duration: %d\n", duration);
fprintf(stdout, "\n");
for (auto i = 0; i < nbest.size(); i++) {
fprintf(stdout, "NBest #%d\n", i);
fprintf(stdout, "-------------------\n");
fprintf(stdout, "Confidence: %.8f\n", nbest[i].confidence);
fprintf(stdout, "Lexical: %s\n", nbest[i].lexical.toUtf8().data());
fprintf(stdout, "ITN: %s\n", nbest[i].itn.toUtf8().data());
fprintf(stdout, "MaskedITN: %s\n", nbest[i].maskedItn.toUtf8().data());
fprintf(stdout, "Display: %s\n", nbest[i].display.toUtf8().data());
fprintf(stdout, "\n");
}
fprintf(stdout, "\n");
}
////////////////
// Synthesize //
////////////////
namespace Voice {
namespace ar_EG {
Font Hoda {
"ar-EG",
"Female",
"Microsoft Server Speech Text to Speech Voice (ar-EG, Hoda)"
};
}
namespace ar_SA {
Font Naayf {
"ar-SA",
"Male",
"Microsoft Server Speech Text to Speech Voice (ar-SA, Naayf)"
};
}
namespace bg_BG {
Font Ivan {
"bg-BG",
"Male",
"Microsoft Server Speech Text to Speech Voice (bg-BG, Ivan)"
};
}
namespace ca_ES {
Font HerenaRUS {
"ca-ES",
"Female",
"Microsoft Server Speech Text to Speech Voice (ca-ES, HerenaRUS)"
};
}
namespace ca_CZ {
Font Jakub {
"ca-CZ",
"Male",
"Microsoft Server Speech Text to Speech Voice (cs-CZ, Jakub)"
};
}
namespace da_DK {
Font HelleRUS {
"da-DK",
"Female",
"Microsoft Server Speech Text to Speech Voice (da-DK, HelleRUS)"
};
}
namespace de_AT {
Font Michael {
"de-AT",
"Male",
"Microsoft Server Speech Text to Speech Voice (de-AT, Michael)"
};
}
namespace de_CH {
Font Karsten {
"de-CH",
"Male",
"Microsoft Server Speech Text to Speech Voice (de-CH, Karsten)"
};
}
namespace de_DE {
Font Hedda {
"de-DE",
"Female",
"Microsoft Server Speech Text to Speech Voice (de-DE, Hedda)"
};
Font HeddaRUS {
"de-DE",
"Female",
"Microsoft Server Speech Text to Speech Voice (de-DE, HeddaRUS)"
};
Font StefanApollo {
"de-DE",
"Male",
"Microsoft Server Speech Text to Speech Voice (de-DE, Stefan, Apollo)"
};
}
namespace el_GR {
Font Stefanos {
"el-GR",
"Male",
"Microsoft Server Speech Text to Speech Voice (el-GR, Stefanos)"
};
}
namespace en_AU {
Font Catherine {
"en-AU",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-AU, Catherine)"
};
Font HayleyRUS {
"en-AU",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-AU, HayleyRUS)"
};
}
namespace en_CA {
Font Linda {
"en-CA",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-CA, Linda)"
};
Font HeatherRUS {
"en-CA",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-CA, HeatherRUS)"
};
}
namespace en_GB {
Font SusanApollo {
"en-GB",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-GB, Susan, Apollo)"
};
Font HazelRUS {
"en-GB",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-GB, HazelRUS)"
};
Font GeorgeApollo {
"en-GB",
"Male",
"Microsoft Server Speech Text to Speech Voice (en-GB, George, Apollo)"
};
}
namespace en_IE {
Font Sean {
"en-IE",
"Male",
"Microsoft Server Speech Text to Speech Voice (en-IE, Sean)"
};
}
namespace en_IN {
Font HeeraApollo {
"en-IN",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-IN, Heera, Apollo)"
};
Font PriyaRUS {
"en-IN",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-IN, PriyaRUS)"
};
Font RaviApollo {
"en-IN",
"Male",
"Microsoft Server Speech Text to Speech Voice (en-IN, Ravi, Apollo)"
};
}
namespace en_US {
Font ZiraRUS {
"en-US",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)"
};
Font JessaRUS {
"en-US",
"Female",
"Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)"
};
Font BenjaminRUS {
"en-US",
"Male",
"Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)"
};
}
namespace es_ES {
Font LauraApollo {
"es-ES",
"Female",
"Microsoft Server Speech Text to Speech Voice (es-ES, Laura, Apollo)"
};
Font HelenaRUS {
"es-ES",
"Female",
"Microsoft Server Speech Text to Speech Voice (es-ES, HelenaRUS)"
};
Font PabloApollo {
"es-ES",
"Male",
"Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)"
};
}
namespace es_MX {
Font HildaRUS {
"es-MX",
"Female",
"Microsoft Server Speech Text to Speech Voice (es-MX, HildaRUS)"
};
Font RaulApollo {
"es-MX",
"Male",
"Microsoft Server Speech Text to Speech Voice (es-MX, Raul, Apollo)"
};
}
namespace fi_FI {
Font HeidiRUS {
"fi-FI",
"Female",
"Microsoft Server Speech Text to Speech Voice (fi-FI, HeidiRUS)"
};
}
namespace fr_CA {
Font Caroline {
"fr-CA",
"Female",
"Microsoft Server Speech Text to Speech Voice (fr-CA, Caroline)"
};
Font HarmonieRUS {
"fr-CA",
"Female",
"Microsoft Server Speech Text to Speech Voice (fr-CA, HarmonieRUS)"
};
}
namespace fr_CH {
Font Guillaume {
"fr-CH",
"Male",
"Microsoft Server Speech Text to Speech Voice (fr-CH, Guillaume)"
};
}
namespace fr_FR {
Font JulieApollo {
"fr-FR",
"Female",
"Microsoft Server Speech Text to Speech Voice (fr-FR, JulieApollo)"
};
Font HortenseRUS {
"fr-FR",
"Female",
"Microsoft Server Speech Text to Speech Voice (fr-FR, HortenseRUS)"
};
Font PaulApollo {
"fr-FR",
"Male",
"Microsoft Server Speech Text to Speech Voice (fr-FR, PaulApollo)"
};
}
namespace he_IL {
Font Asaf {
"he-IL",
"Male",
"Microsoft Server Speech Text to Speech Voice (he-IL, Asaf)"
};
}
namespace hi_IN {
Font KalpanaApollo {
"hi-IN",
"Female",
"Microsoft Server Speech Text to Speech Voice (hi-IN, Kalpana, Apollo)"
};
Font Kalpana {
"hi-IN",
"Female",
"Microsoft Server Speech Text to Speech Voice (hi-IN, Kalpana)"
};
Font Hemant {
"hi-IN",
"Male",
"Microsoft Server Speech Text to Speech Voice (hi-IN, Hemant)"
};
}
namespace hr_HR {
Font Matej {
"hr-HR",
"Male",
"Microsoft Server Speech Text to Speech Voice (hr-HR, Matej)"
};
}
namespace hu_HU {
Font Szabolcs {
"hu-HU",
"Male",
"Microsoft Server Speech Text to Speech Voice (hu-HU, Szabolcs)"
};
}
namespace id_ID {
Font Andika {
"id-ID",
"Male",
"Microsoft Server Speech Text to Speech Voice (id-ID, Andika)"
};
}
namespace it_IT {
Font CosimaApollo {
"it-IT",
"Male",
"Microsoft Server Speech Text to Speech Voice (it-IT, Cosimo, Apollo)"
};
}
namespace ja_JP {
Font AyumiApollo {
"ja-JP",
"Female",
"Microsoft Server Speech Text to Speech Voice (ja-JP, Ayumi, Apollo)"
};
Font IchiroApollo {
"ja-JP",
"Male",
"Microsoft Server Speech Text to Speech Voice (ja-JP, Ichiro, Apollo)"
};
Font HarukaRUS {
"ja-JP",
"Female",
"Microsoft Server Speech Text to Speech Voice (ja-JP, HarukaRUS)"
};
Font LuciaRUS {
"ja-JP",
"Female",
"Microsoft Server Speech Text to Speech Voice (ja-JP, LuciaRUS)"
};
Font EkaterinaRUS {
"ja-JP",
"Male",
"Microsoft Server Speech Text to Speech Voice (ja-JP, EkaterinaRUS)"
};
}
namespace ko_KR {
Font HeamiRUS {
"ko-KR",
"Female",
"Microsoft Server Speech Text to Speech Voice (ko-KR, HeamiRUS)"
};
}
namespace ms_MY {
Font Rizwan {
"ms-MY",
"Male",
"Microsoft Server Speech Text to Speech Voice (ms-MY, Rizwan)"
};
}
namespace nb_NO {
Font HuldaRUS {
"nb-NO",
"Female",
"Microsoft Server Speech Text to Speech Voice (nb-NO, HuldaRUS)"
};
}
namespace nl_NL {
Font HannaRUS {
"nl-NL",
"Female",
"Microsoft Server Speech Text to Speech Voice (nl-NL, HannaRUS)"
};
}
namespace pl_PL {
Font PaulinaRUS {
"pl-PL",
"Female",
"Microsoft Server Speech Text to Speech Voice (pl-PL, PaulinaRUS)"
};
}
namespace pt_BR {
Font HeloisaRUS {
"pt-BR",
"Female",
"Microsoft Server Speech Text to Speech Voice (pt-BR, HeloisaRUS)"
};
Font DanielApollo {
"pt-BR",
"Female",
"Microsoft Server Speech Text to Speech Voice (pt-BR, DanielApollo)"
};
}
namespace pt_PT {
Font HeliaRUS {
"pt-PT",
"Female",
"Microsoft Server Speech Text to Speech Voice (pt-PT, HeliaRUS)"
};
}
namespace ro_RO {
Font Andrei {
"ro-RO",
"Male",
"Microsoft Server Speech Text to Speech Voice (ro-RO, Andrei)"
};
}
namespace ru_RU {
Font IrinaApollo {
"ru-RU",
"Female",
"Microsoft Server Speech Text to Speech Voice (ru-RU, Irina, Apollo)"
};
Font PavelApollo {
"ru-RU",
"Male",
"Microsoft Server Speech Text to Speech Voice (ru-RU, Pavel, Apollo)"
};
}
namespace sk_SK {
Font Filip {
"sk-SK",
"Male",
"Microsoft Server Speech Text to Speech Voice (sk-SK, Filip)"
};
}
namespace sl_SI {
Font Lado {
"sl-SI",
"Male",
"Microsoft Server Speech Text to Speech Voice (sl-SI, Lado)"
};
}
namespace sv_SE {
Font HedvigRUS {
"sv-SE",
"Female",
"Microsoft Server Speech Text to Speech Voice (sv-SE, HedvigRUS)"
};
}
namespace ta_IN {
Font Valluvar {
"ta-IN",
"Male",
"Microsoft Server Speech Text to Speech Voice (ta-IN, Valluvar)"
};
}
namespace th_TH {
Font Pattara {
"th-TH",
"Male",
"Microsoft Server Speech Text to Speech Voice (th-TH, Pattara)"
};
}
namespace tr_TR {
Font SedaRUS {
"tr-TR",
"Female",
"Microsoft Server Speech Text to Speech Voice (tr-TR, SedaRUS)"
};
}
namespace vi_VN {
Font An {
"vi-VN",
"Male",
"Microsoft Server Speech Text to Speech Voice (vi-VN, An)"
};
}
namespace zh_CN {
Font HuihuiRUS {
"zh-CN",
"Female",
"Microsoft Server Speech Text to Speech Voice (zh-CN, HuihuiRUS)"
};
Font YaoyaoApollo {
"zh-CN",
"Female",
"Microsoft Server Speech Text to Speech Voice (zh-CN, Yaoyao, Apollo)"
};
Font KangkangApollo {
"zh-CN",
"Male",
"Microsoft Server Speech Text to Speech Voice (zh-CN, Kangkang, Apollo)"
};
}
namespace zh_HK {
Font TracyApollo {
"zh-HK",
"Female",
"Microsoft Server Speech Text to Speech Voice (zh-HK, Tracy, Apollo)"
};
Font TracyRUS {
"zh-HK",
"Female",
"Microsoft Server Speech Text to Speech Voice (zh-HK, TracyRUS)"
};
Font DannyApollo {
"zh-HK",
"Male",
"Microsoft Server Speech Text to Speech Voice (zh-HK, Danny, Apollo)"
};
}
namespace zh_TW {
Font YatingApollo {
"zh-TW",
"Female",
"Microsoft Server Speech Text to Speech Voice (zh-TW, Yating, Apollo)"
};
Font HanHanRUS {
"zh-TW",
"Female",
"Microsoft Server Speech Text to Speech Voice (zh-TW, HanHanRUS)"
};
Font ZhiweiApollo {
"zh-TW",
"Male",
"Microsoft Server Speech Text to Speech Voice (zh-TW, Zhiwei, Apollo)"
};
}
}
QByteArray Speech::synthesize(const QString &text, Voice::Font font)
{
QByteArray result;
if (mCache && hasSynthesizeCache(text, font)) {
return loadSynthesizeCache(text, font);
}
SoupMessage *msg;
SoupMessageBody *body;
QString auth = "Bearer " + mSynthesizerToken;
QString format = "raw-16khz-16bit-mono-pcm";
QString dataStr = "<speak version='1.0' xml:lang='en-US'><voice xml:lang='" + font.lang + "' xml:gender='" + font.gender + "' name='" + font.name + "'>" + text + "</voice></speak>";
QByteArray data = dataStr.toUtf8();
// Do POST request
msg = soup_message_new("POST", SYNTHESIZE_URL.toUtf8().data());
soup_message_set_request(msg, "application/ssml+xml", SOUP_MEMORY_COPY, data.data(), data.size());
soup_message_headers_append(msg->request_headers, "Authorization", auth.toUtf8().data());
soup_message_headers_append(msg->request_headers, "X-Microsoft-OutputFormat", format.toUtf8().data());
soup_message_headers_append(msg->request_headers, "User-Agent", "libbing");
int httpStatusCode = soup_session_send_message(mSession, msg);
if (httpStatusCode >= 400) {
throw Exception(HTTPError);
}
g_object_get(msg, "response-body", &body, NULL);
result = QByteArray(body->data, body->length);
if (mCache) {
if (!saveSynthesizeCache(result, text, font)) {
throw Exception(IOError);
}
}
return result;
}
QString Speech::recognitionLanguageString(RecognitionLanguage language)
{
switch (language) {
case ArabicEgypt:
return "ar-EG";
case CatalanSpain:
return "ca-ES";
case DanishDenmark:
return "da-DK";
case GermanGermany:
return "de-DE";
case EnglishAustralia:
return "en-AU";
case EnglishCanada:
return "en-CA";
case EnglishUnitedKingdom:
return "en-GB";
case EnglishIndia:
return "en-IN";
case EnglishNewZealand:
return "en-NZ";
default:
case EnglishUnitedStates:
return "en-US";
case SpanishSpain:
return "es-ES";
case SpanishMexico:
return "es-MX";
case FinnishFinland:
return "fi-FI";
case FrenchCanada:
return "fi-CA";
case FrenchFrance:
return "fi-FR";
case HindiIndia:
return "hi-IN";
case ItalianItaly:
return "it-IT";
case JapaneseJapan:
return "ja-JP";
case KoreanKorea:
return "ko-KR";
case NorwegianNorway:
return "nb-NO";
case DutchNetherlands:
return "nl-NL";
case PolishPoland:
return "pl-PL";
case PortugueseBrazil:
return "pl-BR";
case PortuguesePortugal:
return "pl-PT";
case RussianRussia:
return "ru-RU";
case SwedishSweden:
return "sv-SE";
case ChineseChina:
return "zh-CN";
case ChineseHongKong:
return "zh-HK";
case ChineseTaiwan:
return "zh-TW";
}
}
}