-
Notifications
You must be signed in to change notification settings - Fork 12
/
BingSpeech.ts
1144 lines (1017 loc) · 46.4 KB
/
BingSpeech.ts
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
998
999
1000
// Based on Bing Speech API documentation
// https://www.microsoft.com/cognitive-services/en-us/speech-api/documentation/overview
module BingSpeech {
interface HttpHeader {
name: string;
value: string;
}
interface SpeechItem {
isReadyToPlay: boolean;
data: ArrayBuffer;
index: number;
text: string;
locale: SupportedLocales;
outputFormat: OutputFormat;
callback: () => void;
}
interface RecognitionItem {
hasBeenProcessed: boolean;
requestId: string;
text: string;
error: boolean;
}
type OutputFormatValues =
"raw-8khz-8bit-mono-mulaw"
| "raw-16khz-16bit-mono-pcm"
| "riff-8khz-8bit-mono-mulaw"
| "riff-16khz-16bit-mono-pcm";
export enum OutputFormat {
/**
* Warning: not supported by Web Audio
*/
Raw8khz8bit,
/**
* Warning: not supported by Web Audio
*/
Raw16khz16bit,
Riff8khz8bit,
/**
* Default value
*/
Riff16khz16bit
};
type SupportedLocalesValues = "Microsoft Server Speech Text to Speech Voice (ar-EG, Hoda)" |
"Microsoft Server Speech Text to Speech Voice (de-DE, Hedda)" |
"Microsoft Server Speech Text to Speech Voice (de-DE, Stefan, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (en-AU, Catherine)" |
"Microsoft Server Speech Text to Speech Voice (en-CA, Linda)" |
"Microsoft Server Speech Text to Speech Voice (en-GB, Susan, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (en-GB, George, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (en-IN, Ravi, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)" |
"Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)" |
"Microsoft Server Speech Text to Speech Voice (es-ES, Laura, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (es-MX, Raul, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (fr-CA, Caroline)" |
"Microsoft Server Speech Text to Speech Voice (fr-FR, Julie, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (fr-FR, Paul, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (it-IT, Cosimo, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (ja-JP, Ayumi, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (ja-JP, Ichiro, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (pt-BR, Daniel, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (ru-RU, Irina, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (ru-RU, Pavel, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (zh-CN, HuihuiRUS)" |
"Microsoft Server Speech Text to Speech Voice (zh-CN, Yaoyao, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (zh-CN, Kangkang, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (zh-HK, Tracy, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (zh-HK, Danny, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (zh-TW, Yating, Apollo)" |
"Microsoft Server Speech Text to Speech Voice (zh-TW, Zhiwei, Apollo)";
export enum SupportedLocales {
arEG_Female, deDE_Female, deDE_Male, enAU_Female, enCA_Female, enGB_Female, enGB_Male, enIN_Male, enUS_Female, enUS_Male, esES_Female, esES_Male, esMX_Male, frCA_Female,
frFR_Female, frFR_Male, itIT_Male, jaJP_Female, jaJP_Male, ptBR_Male, ruRU_Female, ruRU_Male, zhCN_Female, zhCN_Female2, zhCN_Male, zhHK_Female, zhHK_Male, zhTW_Female, zhTW_Male
}
class Tools {
public authToken = "";
public audioContext: AudioContext;
public canUseWebAudio = false;
public apiKey: string;
private _tokenTime;
/**
* @param apiKey should be your Bing Speech API key
* Note: The way to get api key:
* https://www.microsoft.com/cognitive-services/en-us/subscriptions?productId=/products/Bing.Speech.Preview
* Paid: https://portal.azure.com/#create/Microsoft.CognitiveServices/apitype/Bing.Speech/pricingtier/S0
*/
public constructor(apiKey: string) {
if (!apiKey) {
throw "Please provide a valid Bing Speech API key";
}
this.apiKey = apiKey;
try {
if (typeof window.AudioContext !== 'undefined' || typeof window.webkitAudioContext !== 'undefined') {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
this.audioContext = new AudioContext();
this.canUseWebAudio = true;
// iOS requires a touch interaction before unlocking its web audio stack
if (/iPad|iPhone|iPod/.test(navigator.platform)) {
this._unlockiOSaudio();
}
}
}
catch (e) {
console.error("Cannot initialize Web Audio Context.");
}
}
private _unlockiOSaudio() {
var unlockaudio = () => {
var buffer = this.audioContext.createBuffer(1, 1, 22050);
var source = this.audioContext.createBufferSource();
source.buffer = buffer;
source.connect(this.audioContext.destination);
source.start(0);
setTimeout(() => {
if (((<any>source).playbackState === (<any>source).PLAYING_STATE || (<any>source).playbackState === (<any>source).FINISHED_STATE)) {
window.removeEventListener('touchend', unlockaudio, false);
}
}, 0);
};
window.addEventListener('touchend', unlockaudio, false);
}
public async checkAuthToken() {
var timeElapsed = (Date.now() - this._tokenTime) / 1000;
// Doc: https://www.microsoft.com/cognitive-services/en-us/Speech-api/documentation/API-Reference-REST/BingVoiceRecognition says
// that "the token has an expiry time of 10 minutes". Let's generate it after 500s to be secure.
if (this.authToken === "" || timeElapsed > 500) {
var newAuthToken = "";
var optionalHeaders: HttpHeader[] = [{ name: "Ocp-Apim-Subscription-Key", value: this.apiKey },
// required for Firefox otherwise a CORS error is raised
{ name: "Access-Control-Allow-Origin", value: "*" }]
try {
var resultsText = await this.makeHttpRequest("POST", "https://api.cognitive.microsoft.com/sts/v1.0/issueToken", false, optionalHeaders);
newAuthToken = resultsText;
this._tokenTime = Date.now();
console.log("New authentication token generated.");
}
catch (ex) {
console.error("Error issuing token. Did you provide a valid Bing Speech API key?");
}
this.authToken = newAuthToken;
}
}
public async makeHttpRequest(actionType: string, url: string, isArrayBuffer: boolean = false, optionalHeaders?: HttpHeader[], dataToSend?): Promise<any> {
return new Promise<any>((resolve, reject) => {
var xhr = new XMLHttpRequest();
if (isArrayBuffer) {
xhr.responseType = 'arraybuffer';
}
xhr.onreadystatechange = function (event) {
if (xhr.readyState !== 4) return;
if (xhr.status >= 200 && xhr.status < 300) {
if (!isArrayBuffer) {
resolve(xhr.responseText);
}
else {
resolve(xhr.response);
}
} else {
reject(xhr.status);
}
};
try {
xhr.open(actionType, url, true);
if (optionalHeaders) {
optionalHeaders.forEach((header) => {
xhr.setRequestHeader(header.name, header.value);
});
}
if (dataToSend) {
xhr.send(dataToSend);
}
else {
xhr.send();
}
}
catch (ex) {
reject(ex)
}
});
}
}
class Guid {
public static generateString(): string {
return Guid.getRnd4HexOctet() + Guid.getRnd4HexOctet() + "-" + Guid.getRnd4HexOctet() + "-" + Guid.getRndGuidTimeHiAndVersionHex() + "-" + Guid.getRndGuidClockSeqHiAndReservedHex() + "-" + Guid.getRnd4HexOctet() + Guid.getRnd4HexOctet() + Guid.getRnd4HexOctet();
}
public static generate16bitRnd(): number {
return Math.random() * 0x10000 | 0;
}
public static getRnd4HexOctet(): string {
return ("0000" + Guid.generate16bitRnd().toString(16)).slice(-4);
}
public static getRndGuidTimeHiAndVersionHex(): string {
return (Guid.generate16bitRnd() & 0x0FFF | 0x4000).toString(16);
}
public static getRndGuidClockSeqHiAndReservedHex(): string {
return (Guid.generate16bitRnd() & 0x3FFF | 0x8000).toString(16);
}
}
export class RecognitionClient {
private _tools: Tools;
private _actualRequests: RecognitionItem[] = [];
private _waitingForAnswers = false;
public onFinalResponseReceived: (response) => void;
public onError: (code, requestId) => void;
public onVoiceDetected: () => void;
public onVoiceEnded: () => void;
public onNetworkActivityStarted: () => void;
public onNetworkActivityEnded: () => void;
private _vad = null;
private _continuous = false;
private _audioStream = null;
private _locale = "en-us";
/**
* @param apiKey should be your Bing Speech API key
* Note: The way to get api key:
* https://www.microsoft.com/cognitive-services/en-us/subscriptions?productId=/products/Bing.Speech.Preview
* Paid: https://portal.azure.com/#create/Microsoft.CognitiveServices/apitype/Bing.Speech/pricingtier/S0
*/
public constructor(apiKey: string, locale: string = "en-us") {
if (!apiKey) {
throw "Please provide a valid Bing Speech API key";
}
this._tools = new Tools(apiKey);
this._tools.checkAuthToken();
this._locale = locale;
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia || navigator.msGetUserMedia;
if (!navigator.getUserMedia) {
throw "Sorry, your browser doesn't have microphone support.";
}
}
public startMicAndContinuousRecognition() {
if (typeof window.VADRecord !== "undefined") {
if (!this._continuous) {
this._continuous = true;
this._continueListening();
}
}
else {
console.warn("You need to reference vadrecord.js");
}
}
public endMicAndContinuousRecognition() {
this._continuous = false;
if (this._audioStream) {
this._audioStream.getAudioTracks()[0].stop();
}
if (this.onVoiceEnded) {
this.onVoiceEnded();
}
if (this.onNetworkActivityEnded) {
this.onNetworkActivityEnded();
}
if (this._vad) {
this._vad.dispose();
}
}
private _continueListening() {
navigator.getUserMedia({ audio: true }, (stream) => {
this._startVoiceDetection(stream);
}, function (e) {
console.log("No live audio input in this browser: " + e);
});
}
private _startVoiceDetection(stream: MediaStream) {
this._audioStream = stream;
var source = this._tools.audioContext.createMediaStreamSource(stream);
var options = {
source: source,
voice_stop: (buffer) => {
if (this.onVoiceEnded) {
this.onVoiceEnded();
}
this.recognizeAudio(buffer, this._locale);
},
voice_start: () => {
if (this.onVoiceDetected) {
this.onVoiceDetected();
}
}
};
this._vad = new window.VADRecord(options);
}
private async recognizeAudio(audio: ArrayBuffer, locale: string) {
var requestId = Guid.generateString();
var newRecognitionItem: RecognitionItem = { hasBeenProcessed: false, requestId: requestId, text: "", error: false };
this._actualRequests.push(newRecognitionItem);
if (this.onNetworkActivityStarted) {
this.onNetworkActivityStarted();
}
await this._tools.checkAuthToken();
var optionalHeaders;
var response;
var textAnswer;
var error = false;
optionalHeaders = [{ name: "Content-type", value: 'audio/wav; samplerate=16000' },
{ name: "Authorization", value: "Bearer " + this._tools.authToken }]
try {
var blob = new Blob([audio], { type: 'audio/wav' });
var requestParameters = "?scenarios=smd&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&locale=" + this._locale + "&device.os=wp7&version=3.0&format=json&instanceid=b2c95ede-97eb-4c88-81e4-80f32d6aee54&requestid=" + requestId;
response = await this._tools.makeHttpRequest("POST", "https://speech.platform.bing.com/recognize" + requestParameters, false, optionalHeaders, blob);
var parsedReponse = JSON.parse(response);
if (parsedReponse.header && parsedReponse.header.status !== "error") {
textAnswer = parsedReponse.header.name;
}
else {
error = true;
textAnswer = "!Error during recognition!";
}
let index = 0;
for (index = 0; index < this._actualRequests.length; index++) {
if (this._actualRequests[index].requestId === requestId) {
this._actualRequests[index].text = textAnswer;
this._actualRequests[index].hasBeenProcessed = true;
this._actualRequests[index].error = error;
break;
}
}
if (index === 0 && !this._waitingForAnswers) {
this._waitingForAnswers = true;
this._returnAnswer();
}
}
catch (ex) {
console.warn("Error while calling Bing Speech Recognition API: " + ex);
let index = 0;
for (index = 0; index < this._actualRequests.length; index++) {
if (this._actualRequests[index].requestId === requestId) {
break;
}
}
this._actualRequests.splice(index, 1);
if (this.onError) {
this.onError(ex, requestId);
}
}
}
private _returnAnswer() {
// Checking the next item to handle
var recognitionItem = this._actualRequests[0];
if (!recognitionItem) {
return;
}
// If it's not ready yet, let's wait for 100ms more
if (!recognitionItem.hasBeenProcessed) {
window.setTimeout(() => {
this._returnAnswer();
}, 100);
return;
}
if (this.onFinalResponseReceived && !recognitionItem.error) {
this.onFinalResponseReceived(recognitionItem.text);
}
if (this.onError && recognitionItem.error) {
this.onError(recognitionItem.text, recognitionItem.requestId);
}
this._actualRequests.splice(0, 1);
if (this._actualRequests.length > 0) {
this._returnAnswer();
}
else {
if (this.onNetworkActivityEnded) {
this.onNetworkActivityEnded();
}
this._waitingForAnswers = false;
}
}
}
export class TTSClient {
// By default, doing multiple XHR in parallel to download Bing Speech generated wav
// Set to false to serialize requests
public multipleXHR: boolean = true;
private _playing: boolean;
private _globalLocale: SupportedLocales;
private _waitingQueue: SpeechItem[] = [];
private _waitingQueueIndex: number[] = [];
private _nbWaitingItems: number = 0;
private _requestsInProgress = 0;
private _tools: Tools;
/**
* @param apiKey should be your Bing Speech API key
* Note: The way to get api key:
* https://www.microsoft.com/cognitive-services/en-us/subscriptions?productId=/products/Bing.Speech.Preview
* Paid: https://portal.azure.com/#create/Microsoft.CognitiveServices/apitype/Bing.Speech/pricingtier/S0
*/
public constructor(apiKey: string, locale: SupportedLocales = SupportedLocales.enUS_Male) {
if (!apiKey) {
throw "Please provide a valid Bing Speech API key";
}
this._tools = new Tools(apiKey);
this._globalLocale = locale;
}
public async synthesize(text: string, locale?: SupportedLocales, callback?: () => void, outputFormat: OutputFormat = OutputFormat.Riff16khz16bit) {
if (!this._tools.canUseWebAudio) {
console.error("You need Web Audio to use this API.");
return;
}
// Let's use that as a key to retrieve our item after the XHR callback
let speechItemIndex = this._nbWaitingItems;
this._nbWaitingItems++;
var newSpeechItem: SpeechItem = { isReadyToPlay: false, data: null, index: speechItemIndex, text: text, locale: locale, outputFormat: outputFormat, callback: callback};
this._waitingQueue.push(newSpeechItem);
this._waitingQueueIndex.push(speechItemIndex);
if (this.multipleXHR || this._requestsInProgress == 0) {
this._getBingSpeechData(text, locale, outputFormat, speechItemIndex);
}
}
private async _getBingSpeechData(text: string, locale: SupportedLocales, outputFormat: OutputFormat, speechItemIndex: number) {
this._requestsInProgress++;
// ---- This whole code block is async ------
await this._tools.checkAuthToken();
var optionalHeaders;
var outputFormatValue = this._getFormatValue(outputFormat);
optionalHeaders = [{ name: "Content-type", value: 'application/ssml+xml' },
{ name: "X-Microsoft-OutputFormat", value: outputFormatValue },
{ name: "Authorization", value: this._tools.authToken },
{ name: "X-Search-AppIde", value: '07D3234E49CE426DAA29772419F436CA' },
{ name: "X-Search-ClientID", value: '1ECFAE91408841A480F00935DC390960' },
{ name: "Ocp-Apim-Subscription-Key", value: this._tools.apiKey }]
var SSML = this.makeSSML(text, locale);
try {
var blobReponse = await this._tools.makeHttpRequest("POST", "https://speech.platform.bing.com/synthesize", true, optionalHeaders, SSML);
}
catch (ex) {
console.warn("Error while calling Bing Speech API. Ignoring this item: '" + text + "'");
}
this._requestsInProgress--;
// ---------------------------------------------
// In case of multiple XHR, we can reach this line by anytime
// that's why we kept a copy of the speech item index to remember its order in the queue
let indexInQueue = this._waitingQueueIndex.indexOf(speechItemIndex);
let nextIndex = indexInQueue + 1;
// If we had data back, it will be added to the queue to be decoded & play later
if (blobReponse) {
this._waitingQueue[indexInQueue].data = blobReponse;
this._waitingQueue[indexInQueue].isReadyToPlay = true;
}
// If not, in case of error or timeout for instance,
// we will simply ignore this speech item and remove it from the queue
else {
this._waitingQueue.splice(indexInQueue, 1);
this._waitingQueueIndex.splice(indexInQueue, 1);
nextIndex--;
}
// If mono-XHR, let's at least launch the next XHR in background while playing this text
if (!this.multipleXHR && (nextIndex < this._waitingQueue.length)) {
this._getBingSpeechData(this._waitingQueue[nextIndex].text, this._waitingQueue[nextIndex].locale, this._waitingQueue[nextIndex].outputFormat, this._waitingQueue[nextIndex].index);
}
if (!this._playing) {
this._play();
}
}
private _play() {
// Checking the next speech item to handle
var speechItem = this._waitingQueue[0];
if (!speechItem) {
return;
}
// If it's not ready yet, let's wait for 100ms more
if (!speechItem.isReadyToPlay) {
window.setTimeout(() => {
this._play();
}, 100);
return;
}
// If it's ready to be decoded & played
if (!this._playing) {
this._playing = true;
this._tools.audioContext.decodeAudioData(speechItem.data, (buffer) => {
var source = this._tools.audioContext.createBufferSource();
source.buffer = buffer;
source.connect(this._tools.audioContext.destination);
source.start(0);
source.onended = (evt: Event) => {
this._playing = false;
this._waitingQueue.splice(0, 1);
this._waitingQueueIndex.splice(0, 1);
if (speechItem.callback) {
speechItem.callback();
}
if (this._waitingQueue.length > 0) {
this._play();
}
else {
this._nbWaitingItems = 0;
}
};
});
}
}
private _getFormatValue(outputFormat: OutputFormat): OutputFormatValues {
var outputFormatValue;
switch (outputFormat) {
case OutputFormat.Raw16khz16bit:
outputFormatValue = "raw-16khz-16bit-mono-pcm";
break;
case OutputFormat.Raw8khz8bit:
outputFormatValue = "raw-8khz-8bit-mono-mulaw";
break;
case OutputFormat.Riff16khz16bit:
outputFormatValue = "riff-16khz-16bit-mono-pcm";
break;
case OutputFormat.Riff8khz8bit:
outputFormatValue = "riff-8khz-8bit-mono-mulaw";
break;
default:
outputFormatValue = "riff-16khz-16bit-mono-pcm";
}
return outputFormatValue;
}
private makeSSML(text: string, localeAsked: SupportedLocales): string {
var locale: string;
var gender: string;
var supportedLocaleValue: SupportedLocalesValues;
var SSML: string = "<speak version='1.0' xml:lang=";
if (!localeAsked) {
localeAsked = this._globalLocale;
}
switch (localeAsked) {
case SupportedLocales.arEG_Female:
locale = "'ar-eg'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (ar-EG, Hoda)";
break;
case SupportedLocales.deDE_Female:
locale = "'de-de'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (de-DE, Hedda)";
break;
case SupportedLocales.deDE_Male:
locale = "'de-de'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (de-DE, Stefan, Apollo)";
break;
case SupportedLocales.enAU_Female:
locale = "'en-au'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (en-AU, Catherine)";
break;
case SupportedLocales.enCA_Female:
locale = "'en-ca'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (en-CA, Linda)";
break;
case SupportedLocales.enGB_Female:
locale = "'en-gb'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (en-GB, Susan, Apollo)";
break;
case SupportedLocales.enGB_Male:
locale = "'en-gb'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (en-GB, George, Apollo)";
break;
case SupportedLocales.enIN_Male:
locale = "'en-in'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (en-IN, Ravi, Apollo)";
break;
case SupportedLocales.enUS_Female:
locale = "'en-us'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)";
break;
case SupportedLocales.enUS_Male:
locale = "'en-us'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)";
break;
case SupportedLocales.esES_Female:
locale = "'es-es'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (es-ES, Laura, Apollo)";
break;
case SupportedLocales.esES_Male:
locale = "'es-es'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)";
break;
case SupportedLocales.esMX_Male:
locale = "'es-mx'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (es-MX, Raul, Apollo)";
break;
case SupportedLocales.frCA_Female:
locale = "'fr-ca'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (fr-CA, Caroline)";
break;
case SupportedLocales.frFR_Female:
locale = "'fr-fr'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (fr-FR, Julie, Apollo)";
break;
case SupportedLocales.frFR_Male:
locale = "'fr-fr'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (fr-FR, Paul, Apollo)";
break;
case SupportedLocales.itIT_Male:
locale = "'it-it'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (it-IT, Cosimo, Apollo)";
break;
case SupportedLocales.jaJP_Female:
locale = "'ja-jp'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (ja-JP, Ayumi, Apollo)";
break;
case SupportedLocales.jaJP_Male:
locale = "'ja-jp'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (ja-JP, Ichiro, Apollo)";
break;
case SupportedLocales.ptBR_Male:
locale = "'pt-br'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (pt-BR, Daniel, Apollo)";
break;
case SupportedLocales.ruRU_Female:
locale = "'ru-ru'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (ru-RU, Irina, Apollo)";
break;
case SupportedLocales.ruRU_Male:
locale = "'ru-ru'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (ru-RU, Pavel, Apollo)";
break;
case SupportedLocales.zhCN_Female:
locale = "'zh-cn'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (zh-CN, HuihuiRUS)";
break;
case SupportedLocales.zhCN_Female2:
locale = "'zh-cn'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (zh-CN, Yaoyao, Apollo)";
break;
case SupportedLocales.zhCN_Male:
locale = "'zh-cn'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (zh-CN, Kangkang, Apollo)";
break;
case SupportedLocales.zhHK_Female:
locale = "'zh-hk'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (zh-HK, Tracy, Apollo)";
break;
case SupportedLocales.zhHK_Male:
locale = "'zh-hk'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (zh-HK, Danny, Apollo)";
break;
case SupportedLocales.zhTW_Female:
locale = "'zh-tw'";
gender = "'Female'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (zh-TW, Yating, Apollo)";
break;
case SupportedLocales.zhTW_Male:
locale = "'zh-tw'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (zh-TW, Zhiwei, Apollo)";
break;
default:
locale = "'en-us'";
gender = "'Male'";
supportedLocaleValue = "Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)";
}
SSML += locale + "><voice xml:lang=" + locale + " xml:gender=" + gender + " name='" + supportedLocaleValue + "'>" + `${text.encodeHTML()}` + "</voice></speak>" ;
return SSML;
}
}
}
interface Window {
AudioContext(func: any): any;
webkitAudioContext(func: any): any;
}
interface String {
encodeHTML;
}
if (!String.prototype.encodeHTML) {
String.prototype.encodeHTML = function () {
return this.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
};
}
(function (window) {
var VADRecord = function (options) {
var currentIndex = 0;
var startIndex = 0;
var recLength = 0;
var recBuffers = [];
var numChannels = 2;
var sampleRate = null;
function mergeBuffers(channelBuffer, recordingLength, startIndex, bufferLen) {
var result = new Float32Array(recordingLength - startIndex);
var offset = 0;
var lng = channelBuffer.length;
var startI = startIndex / bufferLen;
for (var i = startI; i < lng; i++) {
var buffer = channelBuffer[i];
result.set(buffer, offset);
offset += buffer.length;
}
return result;
}
function interleave(inputL, inputR) {
let length = inputL.length + inputR.length;
let result = new Float32Array(length);
let index = 0,
inputIndex = 0;
while (index < length) {
result[index++] = inputL[inputIndex];
result[index++] = inputR[inputIndex];
inputIndex++;
}
return result;
}
function writeUTFBytes(view, offset, string) {
var lng = string.length;
for (var i = 0; i < lng; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
function floatTo16BitPCM(output, offset, input) {
for (let i = 0; i < input.length; i++ , offset += 2) {
let s = Math.max(-1, Math.min(1, input[i]));
output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
}
}
function encodeWAV(samples) {
var buffer = new ArrayBuffer(44 + samples.length * 2);
let view = new DataView(buffer);
/* RIFF identifier */
writeString(view, 0, 'RIFF');
/* RIFF chunk length */
view.setUint32(4, 36 + samples.length * 2, true);
/* RIFF type */
writeString(view, 8, 'WAVE');
/* format chunk identifier */
writeString(view, 12, 'fmt ');
/* format chunk length */
view.setUint32(16, 16, true);
/* sample format (raw) */
view.setUint16(20, 1, true);
/* channel count */
view.setUint16(22, numChannels, true);
/* sample rate */
view.setUint32(24, sampleRate, true);
/* byte rate (sample rate * block align) */
view.setUint32(28, sampleRate * 4, true);
/* block align (channel count * bytes per sample) */
view.setUint16(32, numChannels * 2, true);
/* bits per sample */
view.setUint16(34, 16, true);
/* data chunk identifier */
writeString(view, 36, 'data');
/* data chunk length */
view.setUint32(40, samples.length * 2, true);
floatTo16BitPCM(view, 44, samples);
return buffer;
}
function clear() {
recLength = 0;
recBuffers = [];
initBuffers();
}
function initBuffers() {
for (let channel = 0; channel < numChannels; channel++) {
recBuffers[channel] = [];
}
}
function writeString(view, offset, string) {
for (let i = 0; i < string.length; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
this.dispose = function () {
if (this.options.source) {
this.options.source.disconnect();
}
if (this.analyser) {
this.analyser.disconnect();
}
if (this.scriptProcessorNode) {
this.scriptProcessorNode.onaudioprocess = null;
this.scriptProcessorNode.disconnect();
}
this.options.source = null;
this.analyser = null;
this.scriptProcessorNode = null;
}
this.buildAudioRecord = function () {
let buffers = [];
for (let channel = 0; channel < numChannels; channel++) {
buffers.push(mergeBuffers(recBuffers[channel], recLength, startIndex, this.options.bufferLen));
}
var interleaved;
if (numChannels === 2) {
interleaved = interleave(buffers[0], buffers[1]);
} else {
interleaved = buffers[0];
}
var buffer = encodeWAV(interleaved);
return buffer;
}
// Default options
this.options = {
fftSize: 512,
bufferLen: 512,
voice_stop: function () { },
voice_start: function () { },
smoothingTimeConstant: 0.99,
energy_offset: 1e-8, // The initial offset.
energy_threshold_ratio_pos: 2, // Signal must be twice the offset
energy_threshold_ratio_neg: 0.5, // Signal must be half the offset
energy_integration: 1, // Size of integration change compared to the signal per second.
filter: [
{ f: 200, v: 0 }, // 0 -> 200 is 0
{ f: 2000, v: 1 } // 200 -> 2k is 1
],
source: null,
context: null
};
// User options
for (var option in options) {
if (options.hasOwnProperty(option)) {
this.options[option] = options[option];
}
}
clear();
// Require source
if (!this.options.source)
throw new Error("The options must specify a MediaStreamAudioSourceNode.");
// Set this.options.context
this.options.context = this.options.source.context;
// Calculate time relationships
this.hertzPerBin = this.options.context.sampleRate / this.options.fftSize;
this.iterationFrequency = this.options.context.sampleRate / this.options.bufferLen;
this.iterationPeriod = 1 / this.iterationFrequency;
sampleRate = this.options.context.sampleRate;
var DEBUG = false;
if (DEBUG) console.log(
'Vad' +
' | sampleRate: ' + this.options.context.sampleRate +
' | hertzPerBin: ' + this.hertzPerBin +
' | iterationFrequency: ' + this.iterationFrequency +
' | iterationPeriod: ' + this.iterationPeriod
);
this.setFilter = function (shape) {
this.filter = [];
for (var i = 0, iLen = this.options.fftSize / 2; i < iLen; i++) {
this.filter[i] = 0;
for (var j = 0, jLen = shape.length; j < jLen; j++) {
if (i * this.hertzPerBin < shape[j].f) {
this.filter[i] = shape[j].v;
break; // Exit j loop
}
}
}
}
this.setFilter(this.options.filter);
this.ready = {};
this.vadState = false; // True when Voice Activity Detected
// Energy detector props
this.energy_offset = this.options.energy_offset;
this.energy_threshold_pos = this.energy_offset * this.options.energy_threshold_ratio_pos;
this.energy_threshold_neg = this.energy_offset * this.options.energy_threshold_ratio_neg;
this.voiceTrend = 0;
this.voiceTrendMax = 10;
this.voiceTrendMin = -10;
this.voiceTrendStart = 5;
this.voiceTrendEnd = -5;
// Create analyser
this.analyser = this.options.context.createAnalyser();
this.analyser.smoothingTimeConstant = this.options.smoothingTimeConstant; // 0.99;
this.analyser.fftSize = this.options.fftSize;
this.floatFrequencyData = new Float32Array(this.analyser.frequencyBinCount);
// Setup local storage of the Linear FFT data
this.floatFrequencyDataLinear = new Float32Array(this.floatFrequencyData.length);
// Connect this.analyser
this.options.source.connect(this.analyser);
// Create ScriptProcessorNode
this.scriptProcessorNode = this.options.context.createScriptProcessor(this.options.bufferLen, numChannels, numChannels);
// Connect scriptProcessorNode (Theretically, not required)
this.scriptProcessorNode.connect(this.options.context.destination);
// Create callback to update/analyze floatFrequencyData
var self = this;
this.scriptProcessorNode.onaudioprocess = function (event) {
self.analyser.getFloatFrequencyData(self.floatFrequencyData);
self.update();
self.store(event);
self.monitor();