web-speech-cognitive-services 7.1.4-master.9e026d9 → 8.0.0-main.85313be

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/dist/web-speech-cognitive-services.d.mts +186 -0
  2. package/dist/web-speech-cognitive-services.d.ts +186 -0
  3. package/dist/web-speech-cognitive-services.development.js +25677 -0
  4. package/dist/web-speech-cognitive-services.development.js.map +1 -0
  5. package/dist/web-speech-cognitive-services.js +1267 -0
  6. package/dist/web-speech-cognitive-services.js.map +1 -0
  7. package/dist/web-speech-cognitive-services.mjs +1232 -0
  8. package/dist/web-speech-cognitive-services.mjs.map +1 -0
  9. package/dist/web-speech-cognitive-services.production.min.js +31 -0
  10. package/dist/web-speech-cognitive-services.production.min.js.map +1 -0
  11. package/package.json +54 -40
  12. package/CHANGELOG.md +0 -372
  13. package/lib/BingSpeech/SpeechToText/SpeechGrammarList.js +0 -94
  14. package/lib/BingSpeech/SpeechToText/SpeechGrammarList.js.map +0 -1
  15. package/lib/BingSpeech/SpeechToText/createSpeechRecognitionPonyfill.js +0 -483
  16. package/lib/BingSpeech/SpeechToText/createSpeechRecognitionPonyfill.js.map +0 -1
  17. package/lib/BingSpeech/SpeechToText.js +0 -14
  18. package/lib/BingSpeech/SpeechToText.js.map +0 -1
  19. package/lib/BingSpeech/TextToSpeech/AudioContextConsumer.js +0 -122
  20. package/lib/BingSpeech/TextToSpeech/AudioContextConsumer.js.map +0 -1
  21. package/lib/BingSpeech/TextToSpeech/AudioContextQueue.js +0 -104
  22. package/lib/BingSpeech/TextToSpeech/AudioContextQueue.js.map +0 -1
  23. package/lib/BingSpeech/TextToSpeech/SpeechSynthesisUtterance.js +0 -264
  24. package/lib/BingSpeech/TextToSpeech/SpeechSynthesisUtterance.js.map +0 -1
  25. package/lib/BingSpeech/TextToSpeech/SpeechSynthesisVoice.js +0 -61
  26. package/lib/BingSpeech/TextToSpeech/SpeechSynthesisVoice.js.map +0 -1
  27. package/lib/BingSpeech/TextToSpeech/buildSSML.js +0 -32
  28. package/lib/BingSpeech/TextToSpeech/buildSSML.js.map +0 -1
  29. package/lib/BingSpeech/TextToSpeech/createSpeechSynthesisPonyfill.js +0 -220
  30. package/lib/BingSpeech/TextToSpeech/createSpeechSynthesisPonyfill.js.map +0 -1
  31. package/lib/BingSpeech/TextToSpeech/fetchSpeechData.js +0 -74
  32. package/lib/BingSpeech/TextToSpeech/fetchSpeechData.js.map +0 -1
  33. package/lib/BingSpeech/TextToSpeech/fetchVoices.js +0 -335
  34. package/lib/BingSpeech/TextToSpeech/fetchVoices.js.map +0 -1
  35. package/lib/BingSpeech/TextToSpeech/isSSML.js +0 -13
  36. package/lib/BingSpeech/TextToSpeech/isSSML.js.map +0 -1
  37. package/lib/BingSpeech/TextToSpeech/subscribeEvent.js +0 -14
  38. package/lib/BingSpeech/TextToSpeech/subscribeEvent.js.map +0 -1
  39. package/lib/BingSpeech/TextToSpeech.js +0 -14
  40. package/lib/BingSpeech/TextToSpeech.js.map +0 -1
  41. package/lib/BingSpeech/Util/DOMEventEmitter.js +0 -61
  42. package/lib/BingSpeech/Util/DOMEventEmitter.js.map +0 -1
  43. package/lib/BingSpeech/Util/createFetchTokenUsingSubscriptionKey.js +0 -41
  44. package/lib/BingSpeech/Util/createFetchTokenUsingSubscriptionKey.js.map +0 -1
  45. package/lib/BingSpeech/fetchAuthorizationToken.js +0 -57
  46. package/lib/BingSpeech/fetchAuthorizationToken.js.map +0 -1
  47. package/lib/BingSpeech/index.js +0 -84
  48. package/lib/BingSpeech/index.js.map +0 -1
  49. package/lib/SpeechServices/SpeechSDK.js +0 -19
  50. package/lib/SpeechServices/SpeechSDK.js.map +0 -1
  51. package/lib/SpeechServices/SpeechToText/SpeechGrammarList.js +0 -45
  52. package/lib/SpeechServices/SpeechToText/SpeechGrammarList.js.map +0 -1
  53. package/lib/SpeechServices/SpeechToText/cognitiveServiceEventResultToWebSpeechRecognitionResultList.js +0 -56
  54. package/lib/SpeechServices/SpeechToText/cognitiveServiceEventResultToWebSpeechRecognitionResultList.js.map +0 -1
  55. package/lib/SpeechServices/SpeechToText/createSpeechRecognitionPonyfill.js +0 -984
  56. package/lib/SpeechServices/SpeechToText/createSpeechRecognitionPonyfill.js.map +0 -1
  57. package/lib/SpeechServices/SpeechToText.js +0 -24
  58. package/lib/SpeechServices/SpeechToText.js.map +0 -1
  59. package/lib/SpeechServices/TextToSpeech/AudioContextConsumer.js +0 -92
  60. package/lib/SpeechServices/TextToSpeech/AudioContextConsumer.js.map +0 -1
  61. package/lib/SpeechServices/TextToSpeech/AudioContextQueue.js +0 -111
  62. package/lib/SpeechServices/TextToSpeech/AudioContextQueue.js.map +0 -1
  63. package/lib/SpeechServices/TextToSpeech/SpeechSynthesisEvent.js +0 -40
  64. package/lib/SpeechServices/TextToSpeech/SpeechSynthesisEvent.js.map +0 -1
  65. package/lib/SpeechServices/TextToSpeech/SpeechSynthesisUtterance.js +0 -283
  66. package/lib/SpeechServices/TextToSpeech/SpeechSynthesisUtterance.js.map +0 -1
  67. package/lib/SpeechServices/TextToSpeech/SpeechSynthesisVoice.js +0 -63
  68. package/lib/SpeechServices/TextToSpeech/SpeechSynthesisVoice.js.map +0 -1
  69. package/lib/SpeechServices/TextToSpeech/buildSSML.js +0 -32
  70. package/lib/SpeechServices/TextToSpeech/buildSSML.js.map +0 -1
  71. package/lib/SpeechServices/TextToSpeech/createSpeechSynthesisPonyfill.js +0 -282
  72. package/lib/SpeechServices/TextToSpeech/createSpeechSynthesisPonyfill.js.map +0 -1
  73. package/lib/SpeechServices/TextToSpeech/fetchCustomVoices.js +0 -110
  74. package/lib/SpeechServices/TextToSpeech/fetchCustomVoices.js.map +0 -1
  75. package/lib/SpeechServices/TextToSpeech/fetchSpeechData.js +0 -127
  76. package/lib/SpeechServices/TextToSpeech/fetchSpeechData.js.map +0 -1
  77. package/lib/SpeechServices/TextToSpeech/fetchVoices.js +0 -87
  78. package/lib/SpeechServices/TextToSpeech/fetchVoices.js.map +0 -1
  79. package/lib/SpeechServices/TextToSpeech/isSSML.js +0 -13
  80. package/lib/SpeechServices/TextToSpeech/isSSML.js.map +0 -1
  81. package/lib/SpeechServices/TextToSpeech/subscribeEvent.js +0 -14
  82. package/lib/SpeechServices/TextToSpeech/subscribeEvent.js.map +0 -1
  83. package/lib/SpeechServices/TextToSpeech.js +0 -14
  84. package/lib/SpeechServices/TextToSpeech.js.map +0 -1
  85. package/lib/SpeechServices/fetchAuthorizationToken.js +0 -58
  86. package/lib/SpeechServices/fetchAuthorizationToken.js.map +0 -1
  87. package/lib/SpeechServices/patchOptions.js +0 -213
  88. package/lib/SpeechServices/patchOptions.js.map +0 -1
  89. package/lib/SpeechServices/resolveFunctionOrReturnValue.js +0 -11
  90. package/lib/SpeechServices/resolveFunctionOrReturnValue.js.map +0 -1
  91. package/lib/SpeechServices.js +0 -73
  92. package/lib/SpeechServices.js.map +0 -1
  93. package/lib/Util/arrayToMap.js +0 -28
  94. package/lib/Util/arrayToMap.js.map +0 -1
  95. package/lib/Util/createPromiseQueue.js +0 -40
  96. package/lib/Util/createPromiseQueue.js.map +0 -1
  97. package/lib/index.js +0 -14
  98. package/lib/index.js.map +0 -1
  99. package/umd/web-speech-cognitive-services.development.js +0 -4740
  100. package/umd/web-speech-cognitive-services.production.min.js +0 -2
@@ -0,0 +1,1232 @@
1
+ // src/SpeechServices/SpeechToText/createSpeechRecognitionPonyfill.js
2
+ import { Event, EventTarget, getEventAttributeValue, setEventAttributeValue } from "event-target-shim";
3
+
4
+ // src/Util/arrayToMap.js
5
+ function arrayToMap_default(array, extras) {
6
+ const map = {
7
+ ...[].reduce.call(
8
+ array,
9
+ (map2, value, index) => {
10
+ map2[index] = value;
11
+ return map2;
12
+ },
13
+ {}
14
+ ),
15
+ ...extras,
16
+ length: array.length,
17
+ [Symbol.iterator]: () => [].slice.call(map)[Symbol.iterator]()
18
+ };
19
+ return map;
20
+ }
21
+
22
+ // src/SpeechServices/SpeechSDK.js
23
+ import {
24
+ AudioConfig,
25
+ OutputFormat,
26
+ ResultReason,
27
+ SpeechConfig,
28
+ SpeechRecognizer
29
+ } from "microsoft-cognitiveservices-speech-sdk/distrib/lib/microsoft.cognitiveservices.speech.sdk";
30
+ var SpeechSDK_default = {
31
+ AudioConfig,
32
+ OutputFormat,
33
+ ResultReason,
34
+ SpeechConfig,
35
+ SpeechRecognizer
36
+ };
37
+
38
+ // src/SpeechServices/SpeechToText/cognitiveServiceEventResultToWebSpeechRecognitionResultList.js
39
+ var {
40
+ ResultReason: { RecognizingSpeech, RecognizedSpeech }
41
+ } = SpeechSDK_default;
42
+ function cognitiveServiceEventResultToWebSpeechRecognitionResultList_default(result, { maxAlternatives = Infinity, textNormalization = "display" } = {}) {
43
+ if (result.reason === RecognizingSpeech || result.reason === RecognizedSpeech && !result.json.NBest) {
44
+ const resultList = [
45
+ {
46
+ confidence: 0.5,
47
+ transcript: result.text
48
+ }
49
+ ];
50
+ if (result.reason === RecognizedSpeech) {
51
+ resultList.isFinal = true;
52
+ }
53
+ return resultList;
54
+ } else if (result.reason === RecognizedSpeech) {
55
+ const resultList = arrayToMap_default(
56
+ (result.json.NBest || []).slice(0, maxAlternatives).map(({ Confidence: confidence, Display: display, ITN: itn, Lexical: lexical, MaskedITN: maskedITN }) => ({
57
+ confidence,
58
+ transcript: textNormalization === "itn" ? itn : textNormalization === "lexical" ? lexical : textNormalization === "maskeditn" ? maskedITN : display
59
+ })),
60
+ { isFinal: true }
61
+ );
62
+ return resultList;
63
+ }
64
+ return [];
65
+ }
66
+
67
+ // ../../node_modules/p-defer/index.js
68
+ function pDefer() {
69
+ const deferred = {};
70
+ deferred.promise = new Promise((resolve, reject) => {
71
+ deferred.resolve = resolve;
72
+ deferred.reject = reject;
73
+ });
74
+ return deferred;
75
+ }
76
+
77
+ // src/Util/createPromiseQueue.js
78
+ function createPromiseQueue_default() {
79
+ let shiftDeferred;
80
+ const queue = [];
81
+ const push = (value) => {
82
+ if (shiftDeferred) {
83
+ const { resolve } = shiftDeferred;
84
+ shiftDeferred = null;
85
+ resolve(value);
86
+ } else {
87
+ queue.push(value);
88
+ }
89
+ };
90
+ const shift = () => {
91
+ if (queue.length) {
92
+ return Promise.resolve(queue.shift());
93
+ }
94
+ return (shiftDeferred || (shiftDeferred = pDefer())).promise;
95
+ };
96
+ return {
97
+ push,
98
+ shift
99
+ };
100
+ }
101
+
102
+ // src/SpeechServices/resolveFunctionOrReturnValue.ts
103
+ function isFunction(value) {
104
+ return typeof value === "function";
105
+ }
106
+ function resolveFunctionOrReturnValue(fnOrValue) {
107
+ return isFunction(fnOrValue) ? fnOrValue() : fnOrValue;
108
+ }
109
+
110
+ // src/SpeechServices/patchOptions.js
111
+ var shouldWarnOnSubscriptionKey = true;
112
+ function patchOptions({
113
+ authorizationToken,
114
+ credentials,
115
+ looseEvent,
116
+ looseEvents,
117
+ region = "westus",
118
+ subscriptionKey,
119
+ ...otherOptions
120
+ } = {}) {
121
+ if (typeof looseEvent !== "undefined") {
122
+ console.warn('web-speech-cognitive-services: The option "looseEvent" should be named as "looseEvents".');
123
+ looseEvents = looseEvent;
124
+ }
125
+ if (!credentials) {
126
+ if (!authorizationToken && !subscriptionKey) {
127
+ throw new Error("web-speech-cognitive-services: Credentials must be specified.");
128
+ } else {
129
+ console.warn(
130
+ "web-speech-cognitive-services: We are deprecating authorizationToken, region, and subscriptionKey. Please use credentials instead. The deprecated option will be removed on or after 2020-11-14."
131
+ );
132
+ credentials = async () => authorizationToken ? { authorizationToken: await resolveFunctionOrReturnValue(authorizationToken), region } : { region, subscriptionKey: await resolveFunctionOrReturnValue(subscriptionKey) };
133
+ }
134
+ }
135
+ return {
136
+ ...otherOptions,
137
+ fetchCredentials: async () => {
138
+ const {
139
+ authorizationToken: authorizationToken2,
140
+ customVoiceHostname,
141
+ region: region2,
142
+ speechRecognitionHostname,
143
+ speechSynthesisHostname,
144
+ subscriptionKey: subscriptionKey2
145
+ } = await resolveFunctionOrReturnValue(credentials);
146
+ if (!authorizationToken2 && !subscriptionKey2 || authorizationToken2 && subscriptionKey2) {
147
+ throw new Error(
148
+ 'web-speech-cognitive-services: Either "authorizationToken" or "subscriptionKey" must be provided.'
149
+ );
150
+ } else if (!region2 && !(speechRecognitionHostname && speechSynthesisHostname)) {
151
+ throw new Error(
152
+ 'web-speech-cognitive-services: Either "region" or "speechRecognitionHostname" and "speechSynthesisHostname" must be set.'
153
+ );
154
+ } else if (region2 && (customVoiceHostname || speechRecognitionHostname || speechSynthesisHostname)) {
155
+ throw new Error(
156
+ 'web-speech-cognitive-services: Only either "region" or "customVoiceHostname", "speechRecognitionHostname" and "speechSynthesisHostname" can be set.'
157
+ );
158
+ } else if (authorizationToken2) {
159
+ if (typeof authorizationToken2 !== "string") {
160
+ throw new Error('web-speech-cognitive-services: "authorizationToken" must be a string.');
161
+ }
162
+ } else if (typeof subscriptionKey2 !== "string") {
163
+ throw new Error('web-speech-cognitive-services: "subscriptionKey" must be a string.');
164
+ }
165
+ if (shouldWarnOnSubscriptionKey && subscriptionKey2) {
166
+ console.warn(
167
+ "web-speech-cognitive-services: In production environment, subscription key should not be used, authorization token should be used instead."
168
+ );
169
+ shouldWarnOnSubscriptionKey = false;
170
+ }
171
+ const resolvedCredentials = authorizationToken2 ? { authorizationToken: authorizationToken2 } : { subscriptionKey: subscriptionKey2 };
172
+ if (region2) {
173
+ resolvedCredentials.region = region2;
174
+ } else {
175
+ resolvedCredentials.customVoiceHostname = customVoiceHostname;
176
+ resolvedCredentials.speechRecognitionHostname = speechRecognitionHostname;
177
+ resolvedCredentials.speechSynthesisHostname = speechSynthesisHostname;
178
+ }
179
+ return resolvedCredentials;
180
+ },
181
+ looseEvents
182
+ };
183
+ }
184
+
185
+ // src/SpeechServices/SpeechToText/SpeechGrammarList.js
186
+ var SpeechGrammarList_default = class {
187
+ constructor() {
188
+ this._phrases = [];
189
+ }
190
+ addFromString() {
191
+ throw new Error("JSGF is not supported");
192
+ }
193
+ get phrases() {
194
+ return this._phrases;
195
+ }
196
+ set phrases(value) {
197
+ if (Array.isArray(value)) {
198
+ this._phrases = value;
199
+ } else if (typeof value === "string") {
200
+ this._phrases = [value];
201
+ } else {
202
+ throw new Error(`The provided value is not an array or of type 'string'`);
203
+ }
204
+ }
205
+ };
206
+
207
+ // src/SpeechServices/SpeechToText/createSpeechRecognitionPonyfill.js
208
+ var { AudioConfig: AudioConfig2, OutputFormat: OutputFormat2, ResultReason: ResultReason2, SpeechConfig: SpeechConfig2, SpeechRecognizer: SpeechRecognizer2 } = SpeechSDK_default;
209
+ function serializeRecognitionResult({ duration, errorDetails, json, offset, properties, reason, resultId, text }) {
210
+ return {
211
+ duration,
212
+ errorDetails,
213
+ json: JSON.parse(json),
214
+ offset,
215
+ properties,
216
+ reason,
217
+ resultId,
218
+ text
219
+ };
220
+ }
221
+ function averageAmplitude(arrayBuffer) {
222
+ const array = new Int16Array(arrayBuffer);
223
+ return [].reduce.call(array, (averageAmplitude2, amplitude) => averageAmplitude2 + Math.abs(amplitude), 0) / array.length;
224
+ }
225
+ function cognitiveServicesAsyncToPromise(fn) {
226
+ return (...args) => new Promise((resolve, reject) => fn(...args, resolve, reject));
227
+ }
228
+ var SpeechRecognitionEvent = class extends Event {
229
+ constructor(type, { data, emma, interpretation, resultIndex, results } = {}) {
230
+ super(type);
231
+ this.data = data;
232
+ this.emma = emma;
233
+ this.interpretation = interpretation;
234
+ this.resultIndex = resultIndex;
235
+ this.results = results;
236
+ }
237
+ };
238
+ function prepareAudioConfig(audioConfig) {
239
+ const originalAttach = audioConfig.attach;
240
+ const boundOriginalAttach = audioConfig.attach.bind(audioConfig);
241
+ let firstChunk;
242
+ let muted;
243
+ audioConfig.attach = async () => {
244
+ const reader = await boundOriginalAttach();
245
+ return {
246
+ ...reader,
247
+ read: async () => {
248
+ const chunk = await reader.read();
249
+ if (!firstChunk && averageAmplitude(chunk.buffer) > 150) {
250
+ audioConfig.events.onEvent({ name: "FirstAudibleChunk" });
251
+ firstChunk = true;
252
+ }
253
+ if (muted) {
254
+ return { buffer: new ArrayBuffer(0), isEnd: true, timeReceived: Date.now() };
255
+ }
256
+ return chunk;
257
+ }
258
+ };
259
+ };
260
+ return {
261
+ audioConfig,
262
+ pause: () => {
263
+ muted = true;
264
+ },
265
+ unprepare: () => {
266
+ audioConfig.attach = originalAttach;
267
+ }
268
+ };
269
+ }
270
+ function createSpeechRecognitionPonyfillFromRecognizer({
271
+ createRecognizer,
272
+ enableTelemetry,
273
+ looseEvents,
274
+ referenceGrammars,
275
+ textNormalization
276
+ }) {
277
+ SpeechRecognizer2.enableTelemetry(enableTelemetry !== false);
278
+ class SpeechRecognition extends EventTarget {
279
+ constructor() {
280
+ super();
281
+ this._continuous = false;
282
+ this._interimResults = false;
283
+ this._lang = typeof window !== "undefined" ? window.document.documentElement.getAttribute("lang") || window.navigator.language : "en-US";
284
+ this._grammars = new SpeechGrammarList_default();
285
+ this._maxAlternatives = 1;
286
+ }
287
+ emitCognitiveServices(type, event) {
288
+ this.dispatchEvent(
289
+ new SpeechRecognitionEvent("cognitiveservices", {
290
+ data: {
291
+ ...event,
292
+ type
293
+ }
294
+ })
295
+ );
296
+ }
297
+ get continuous() {
298
+ return this._continuous;
299
+ }
300
+ set continuous(value) {
301
+ this._continuous = value;
302
+ }
303
+ get grammars() {
304
+ return this._grammars;
305
+ }
306
+ set grammars(value) {
307
+ if (value instanceof SpeechGrammarList_default) {
308
+ this._grammars = value;
309
+ } else {
310
+ throw new Error(`The provided value is not of type 'SpeechGrammarList'`);
311
+ }
312
+ }
313
+ get interimResults() {
314
+ return this._interimResults;
315
+ }
316
+ set interimResults(value) {
317
+ this._interimResults = value;
318
+ }
319
+ get maxAlternatives() {
320
+ return this._maxAlternatives;
321
+ }
322
+ set maxAlternatives(value) {
323
+ this._maxAlternatives = value;
324
+ }
325
+ get lang() {
326
+ return this._lang;
327
+ }
328
+ set lang(value) {
329
+ this._lang = value;
330
+ }
331
+ get onaudioend() {
332
+ return getEventAttributeValue(this, "audioend");
333
+ }
334
+ set onaudioend(value) {
335
+ setEventAttributeValue(this, "audioend", value);
336
+ }
337
+ get onaudiostart() {
338
+ return getEventAttributeValue(this, "audiostart");
339
+ }
340
+ set onaudiostart(value) {
341
+ setEventAttributeValue(this, "audiostart", value);
342
+ }
343
+ get oncognitiveservices() {
344
+ return getEventAttributeValue(this, "cognitiveservices");
345
+ }
346
+ set oncognitiveservices(value) {
347
+ setEventAttributeValue(this, "cognitiveservices", value);
348
+ }
349
+ get onend() {
350
+ return getEventAttributeValue(this, "end");
351
+ }
352
+ set onend(value) {
353
+ setEventAttributeValue(this, "end", value);
354
+ }
355
+ get onerror() {
356
+ return getEventAttributeValue(this, "error");
357
+ }
358
+ set onerror(value) {
359
+ setEventAttributeValue(this, "error", value);
360
+ }
361
+ get onresult() {
362
+ return getEventAttributeValue(this, "result");
363
+ }
364
+ set onresult(value) {
365
+ setEventAttributeValue(this, "result", value);
366
+ }
367
+ get onsoundend() {
368
+ return getEventAttributeValue(this, "soundend");
369
+ }
370
+ set onsoundend(value) {
371
+ setEventAttributeValue(this, "soundend", value);
372
+ }
373
+ get onsoundstart() {
374
+ return getEventAttributeValue(this, "soundstart");
375
+ }
376
+ set onsoundstart(value) {
377
+ setEventAttributeValue(this, "soundstart", value);
378
+ }
379
+ get onspeechend() {
380
+ return getEventAttributeValue(this, "speechend");
381
+ }
382
+ set onspeechend(value) {
383
+ setEventAttributeValue(this, "speechend", value);
384
+ }
385
+ get onspeechstart() {
386
+ return getEventAttributeValue(this, "speechstart");
387
+ }
388
+ set onspeechstart(value) {
389
+ setEventAttributeValue(this, "speechstart", value);
390
+ }
391
+ get onstart() {
392
+ return getEventAttributeValue(this, "start");
393
+ }
394
+ set onstart(value) {
395
+ setEventAttributeValue(this, "start", value);
396
+ }
397
+ start() {
398
+ this._startOnce().catch((err) => {
399
+ this.dispatchEvent(new ErrorEvent("error", { error: err, message: err && (err.stack || err.message) }));
400
+ });
401
+ }
402
+ async _startOnce() {
403
+ const recognizer = await createRecognizer(this.lang);
404
+ const { pause, unprepare } = prepareAudioConfig(recognizer.audioConfig);
405
+ try {
406
+ const queue = createPromiseQueue_default();
407
+ let soundStarted;
408
+ let speechStarted;
409
+ let stopping;
410
+ const { detach: detachAudioConfigEvent } = recognizer.audioConfig.events.attach((event) => {
411
+ const { name } = event;
412
+ if (name === "AudioSourceReadyEvent") {
413
+ queue.push({ audioSourceReady: {} });
414
+ } else if (name === "AudioSourceOffEvent") {
415
+ queue.push({ audioSourceOff: {} });
416
+ } else if (name === "FirstAudibleChunk") {
417
+ queue.push({ firstAudibleChunk: {} });
418
+ }
419
+ });
420
+ recognizer.canceled = (_, { errorDetails, offset, reason, sessionId }) => {
421
+ queue.push({
422
+ canceled: {
423
+ errorDetails,
424
+ offset,
425
+ reason,
426
+ sessionId
427
+ }
428
+ });
429
+ };
430
+ recognizer.recognized = (_, { offset, result, sessionId }) => {
431
+ queue.push({
432
+ recognized: {
433
+ offset,
434
+ result: serializeRecognitionResult(result),
435
+ sessionId
436
+ }
437
+ });
438
+ };
439
+ recognizer.recognizing = (_, { offset, result, sessionId }) => {
440
+ queue.push({
441
+ recognizing: {
442
+ offset,
443
+ result: serializeRecognitionResult(result),
444
+ sessionId
445
+ }
446
+ });
447
+ };
448
+ recognizer.sessionStarted = (_, { sessionId }) => {
449
+ queue.push({ sessionStarted: { sessionId } });
450
+ };
451
+ recognizer.sessionStopped = (_, { sessionId }) => {
452
+ queue.push({ sessionStopped: { sessionId } });
453
+ };
454
+ recognizer.speechStartDetected = (_, { offset, sessionId }) => {
455
+ queue.push({ speechStartDetected: { offset, sessionId } });
456
+ };
457
+ recognizer.speechEndDetected = (_, { sessionId }) => {
458
+ queue.push({ speechEndDetected: { sessionId } });
459
+ };
460
+ const { phrases } = this.grammars;
461
+ const { dynamicGrammar } = recognizer.privReco;
462
+ referenceGrammars && referenceGrammars.length && dynamicGrammar.addReferenceGrammar(referenceGrammars);
463
+ phrases && phrases.length && dynamicGrammar.addPhrase(phrases);
464
+ await cognitiveServicesAsyncToPromise(recognizer.startContinuousRecognitionAsync.bind(recognizer))();
465
+ if (recognizer.stopContinuousRecognitionAsync) {
466
+ this.abort = () => queue.push({ abort: {} });
467
+ this.stop = () => queue.push({ stop: {} });
468
+ } else {
469
+ this.abort = this.stop = void 0;
470
+ }
471
+ let audioStarted;
472
+ let finalEvent;
473
+ let finalizedResults = [];
474
+ for (let loop = 0; !stopping || audioStarted; loop++) {
475
+ const event = await queue.shift();
476
+ const {
477
+ abort,
478
+ audioSourceOff,
479
+ audioSourceReady,
480
+ canceled,
481
+ firstAudibleChunk,
482
+ recognized,
483
+ recognizing,
484
+ stop
485
+ } = event;
486
+ Object.keys(event).forEach((name) => this.emitCognitiveServices(name, event[name]));
487
+ const errorMessage = canceled && canceled.errorDetails;
488
+ if (/Permission\sdenied/u.test(errorMessage || "")) {
489
+ finalEvent = {
490
+ error: "not-allowed",
491
+ type: "error"
492
+ };
493
+ break;
494
+ }
495
+ if (!loop) {
496
+ this.dispatchEvent(new SpeechRecognitionEvent("start"));
497
+ }
498
+ if (errorMessage) {
499
+ if (/1006/u.test(errorMessage)) {
500
+ if (!audioStarted) {
501
+ this.dispatchEvent(new SpeechRecognitionEvent("audiostart"));
502
+ this.dispatchEvent(new SpeechRecognitionEvent("audioend"));
503
+ }
504
+ finalEvent = {
505
+ error: "network",
506
+ type: "error"
507
+ };
508
+ } else {
509
+ finalEvent = {
510
+ error: "unknown",
511
+ type: "error"
512
+ };
513
+ }
514
+ break;
515
+ } else if (abort || stop) {
516
+ if (abort) {
517
+ finalEvent = {
518
+ error: "aborted",
519
+ type: "error"
520
+ };
521
+ stopping = "abort";
522
+ } else {
523
+ pause();
524
+ stopping = "stop";
525
+ }
526
+ if (abort && recognizer.stopContinuousRecognitionAsync) {
527
+ await cognitiveServicesAsyncToPromise(recognizer.stopContinuousRecognitionAsync.bind(recognizer))();
528
+ }
529
+ } else if (audioSourceReady) {
530
+ this.dispatchEvent(new SpeechRecognitionEvent("audiostart"));
531
+ audioStarted = true;
532
+ } else if (firstAudibleChunk) {
533
+ this.dispatchEvent(new SpeechRecognitionEvent("soundstart"));
534
+ soundStarted = true;
535
+ } else if (audioSourceOff) {
536
+ speechStarted && this.dispatchEvent(new SpeechRecognitionEvent("speechend"));
537
+ soundStarted && this.dispatchEvent(new SpeechRecognitionEvent("soundend"));
538
+ audioStarted && this.dispatchEvent(new SpeechRecognitionEvent("audioend"));
539
+ audioStarted = soundStarted = speechStarted = false;
540
+ break;
541
+ } else if (stopping !== "abort") {
542
+ if (recognized && recognized.result && recognized.result.reason === ResultReason2.NoMatch) {
543
+ finalEvent = {
544
+ error: "no-speech",
545
+ type: "error"
546
+ };
547
+ } else if (recognized || recognizing) {
548
+ if (!audioStarted) {
549
+ this.dispatchEvent(new SpeechRecognitionEvent("audiostart"));
550
+ audioStarted = true;
551
+ }
552
+ if (!soundStarted) {
553
+ this.dispatchEvent(new SpeechRecognitionEvent("soundstart"));
554
+ soundStarted = true;
555
+ }
556
+ if (!speechStarted) {
557
+ this.dispatchEvent(new SpeechRecognitionEvent("speechstart"));
558
+ speechStarted = true;
559
+ }
560
+ if (recognized) {
561
+ const result = cognitiveServiceEventResultToWebSpeechRecognitionResultList_default(recognized.result, {
562
+ maxAlternatives: this.maxAlternatives,
563
+ textNormalization
564
+ });
565
+ const recognizable = !!result[0].transcript;
566
+ if (recognizable) {
567
+ finalizedResults = [...finalizedResults, result];
568
+ this.continuous && this.dispatchEvent(
569
+ new SpeechRecognitionEvent("result", {
570
+ results: finalizedResults
571
+ })
572
+ );
573
+ }
574
+ if (this.continuous && recognizable) {
575
+ finalEvent = null;
576
+ } else {
577
+ finalEvent = {
578
+ results: finalizedResults,
579
+ type: "result"
580
+ };
581
+ }
582
+ if (!this.continuous && recognizer.stopContinuousRecognitionAsync) {
583
+ await cognitiveServicesAsyncToPromise(recognizer.stopContinuousRecognitionAsync.bind(recognizer))();
584
+ }
585
+ if (looseEvents && finalEvent && recognizable) {
586
+ this.dispatchEvent(new SpeechRecognitionEvent(finalEvent.type, finalEvent));
587
+ finalEvent = null;
588
+ }
589
+ } else if (recognizing) {
590
+ this.interimResults && this.dispatchEvent(
591
+ new SpeechRecognitionEvent("result", {
592
+ results: [
593
+ ...finalizedResults,
594
+ cognitiveServiceEventResultToWebSpeechRecognitionResultList_default(recognizing.result, {
595
+ maxAlternatives: this.maxAlternatives,
596
+ textNormalization
597
+ })
598
+ ]
599
+ })
600
+ );
601
+ }
602
+ }
603
+ }
604
+ }
605
+ if (speechStarted) {
606
+ this.dispatchEvent(new SpeechRecognitionEvent("speechend"));
607
+ }
608
+ if (soundStarted) {
609
+ this.dispatchEvent(new SpeechRecognitionEvent("soundend"));
610
+ }
611
+ if (audioStarted) {
612
+ this.dispatchEvent(new SpeechRecognitionEvent("audioend"));
613
+ }
614
+ if (finalEvent) {
615
+ if (finalEvent.type === "result" && !finalEvent.results.length) {
616
+ finalEvent = {
617
+ error: "no-speech",
618
+ type: "error"
619
+ };
620
+ }
621
+ if (finalEvent.type === "error") {
622
+ this.dispatchEvent(new ErrorEvent("error", finalEvent));
623
+ } else {
624
+ this.dispatchEvent(new SpeechRecognitionEvent(finalEvent.type, finalEvent));
625
+ }
626
+ }
627
+ this.dispatchEvent(new SpeechRecognitionEvent("end"));
628
+ detachAudioConfigEvent();
629
+ } catch (err) {
630
+ console.error(err);
631
+ throw err;
632
+ } finally {
633
+ unprepare();
634
+ recognizer.dispose();
635
+ }
636
+ }
637
+ }
638
+ return {
639
+ SpeechGrammarList: SpeechGrammarList_default,
640
+ SpeechRecognition,
641
+ SpeechRecognitionEvent
642
+ };
643
+ }
644
+ var createSpeechRecognitionPonyfill_default = (options) => {
645
+ const {
646
+ audioConfig = AudioConfig2.fromDefaultMicrophoneInput(),
647
+ // We set telemetry to true to honor the default telemetry settings of Speech SDK
648
+ // https://github.com/Microsoft/cognitive-services-speech-sdk-js#data--telemetry
649
+ enableTelemetry = true,
650
+ fetchCredentials,
651
+ looseEvents,
652
+ referenceGrammars,
653
+ speechRecognitionEndpointId,
654
+ textNormalization = "display"
655
+ } = patchOptions(options);
656
+ if (!audioConfig && (!window.navigator.mediaDevices || !window.navigator.mediaDevices.getUserMedia)) {
657
+ console.warn(
658
+ "web-speech-cognitive-services: This browser does not support WebRTC and it will not work with Cognitive Services Speech Services."
659
+ );
660
+ return {};
661
+ }
662
+ const createRecognizer = async (lang) => {
663
+ const { authorizationToken, region, speechRecognitionHostname, subscriptionKey } = await fetchCredentials();
664
+ let speechConfig;
665
+ if (speechRecognitionHostname) {
666
+ const host = { hostname: speechRecognitionHostname, port: 443, protocol: "wss:" };
667
+ if (authorizationToken) {
668
+ speechConfig = SpeechConfig2.fromHost(host);
669
+ speechConfig.authorizationToken = authorizationToken;
670
+ } else {
671
+ speechConfig = SpeechConfig2.fromHost(host, subscriptionKey);
672
+ }
673
+ } else {
674
+ speechConfig = authorizationToken ? SpeechConfig2.fromAuthorizationToken(authorizationToken, region) : SpeechConfig2.fromSubscription(subscriptionKey, region);
675
+ }
676
+ if (speechRecognitionEndpointId) {
677
+ speechConfig.endpointId = speechRecognitionEndpointId;
678
+ }
679
+ speechConfig.outputFormat = OutputFormat2.Detailed;
680
+ speechConfig.speechRecognitionLanguage = lang || "en-US";
681
+ return new SpeechRecognizer2(speechConfig, audioConfig);
682
+ };
683
+ return createSpeechRecognitionPonyfillFromRecognizer({
684
+ audioConfig,
685
+ createRecognizer,
686
+ enableTelemetry,
687
+ looseEvents,
688
+ referenceGrammars,
689
+ textNormalization
690
+ });
691
+ };
692
+
693
+ // src/SpeechServices/SpeechToText.js
694
+ var SpeechToText_default = createSpeechRecognitionPonyfill_default;
695
+
696
+ // src/SpeechServices/TextToSpeech/createSpeechSynthesisPonyfill.js
697
+ import { EventTarget as EventTarget3, getEventAttributeValue as getEventAttributeValue3, setEventAttributeValue as setEventAttributeValue3 } from "event-target-shim";
698
+ import onErrorResumeNext from "on-error-resume-next";
699
+
700
+ // src/SpeechServices/TextToSpeech/AudioContextQueue.js
701
+ import memoize from "memoize-one";
702
+
703
+ // src/SpeechServices/TextToSpeech/AudioContextConsumer.js
704
+ var AudioContextConsumer_default = class {
705
+ constructor(audioContext) {
706
+ this.audioContext = audioContext;
707
+ }
708
+ pause() {
709
+ this.audioContext && this.audioContext.suspend();
710
+ this.playingUtterance && this.playingUtterance.dispatchEvent(new CustomEvent("pause"));
711
+ }
712
+ resume() {
713
+ this.audioContext && this.audioContext.resume();
714
+ this.playingUtterance && this.playingUtterance.dispatchEvent(new CustomEvent("resume"));
715
+ }
716
+ async start(queue) {
717
+ let utterance;
718
+ while (utterance = queue.shift()) {
719
+ this.playingUtterance = utterance;
720
+ await utterance.play(this.audioContext);
721
+ this.playingUtterance = null;
722
+ }
723
+ }
724
+ stop() {
725
+ this.playingUtterance && this.playingUtterance.stop();
726
+ if (this.audioContext.state === "suspended") {
727
+ this.audioContext.resume();
728
+ }
729
+ }
730
+ };
731
+
732
+ // src/SpeechServices/TextToSpeech/AudioContextQueue.js
733
+ var AudioContextQueue_default = class {
734
+ constructor({ audioContext, ponyfill }) {
735
+ this.consumer = null;
736
+ this.paused = false;
737
+ this.queue = [];
738
+ this.getAudioContext = memoize(() => audioContext || new ponyfill.AudioContext());
739
+ }
740
+ pause() {
741
+ this.paused = true;
742
+ this.consumer && this.consumer.pause();
743
+ }
744
+ push(utterance) {
745
+ this.queue.push(utterance);
746
+ this.startConsumer();
747
+ }
748
+ resume() {
749
+ this.paused = false;
750
+ if (this.consumer) {
751
+ this.consumer.resume();
752
+ } else {
753
+ this.startConsumer();
754
+ }
755
+ }
756
+ get speaking() {
757
+ return !!this.consumer;
758
+ }
759
+ async startConsumer() {
760
+ while (!this.paused && this.queue.length && !this.consumer) {
761
+ this.consumer = new AudioContextConsumer_default(this.getAudioContext());
762
+ await this.consumer.start(this.queue);
763
+ this.consumer = null;
764
+ }
765
+ }
766
+ stop() {
767
+ this.queue.splice(0);
768
+ this.consumer && this.consumer.stop();
769
+ }
770
+ };
771
+
772
+ // src/SpeechServices/TextToSpeech/SpeechSynthesisVoice.js
773
+ var SpeechSynthesisVoice_default = class {
774
+ constructor({ gender, lang, voiceURI }) {
775
+ this._default = false;
776
+ this._gender = gender;
777
+ this._lang = lang;
778
+ this._localService = false;
779
+ this._name = voiceURI;
780
+ this._voiceURI = voiceURI;
781
+ }
782
+ get default() {
783
+ return this._default;
784
+ }
785
+ get gender() {
786
+ return this._gender;
787
+ }
788
+ get lang() {
789
+ return this._lang;
790
+ }
791
+ get localService() {
792
+ return this._localService;
793
+ }
794
+ get name() {
795
+ return this._name;
796
+ }
797
+ get voiceURI() {
798
+ return this._voiceURI;
799
+ }
800
+ };
801
+
802
+ // src/SpeechServices/TextToSpeech/fetchCustomVoices.js
803
+ async function fetchCustomVoices({ customVoiceHostname, deploymentId, region, subscriptionKey }) {
804
+ const hostname = customVoiceHostname || `${region}.customvoice.api.speech.microsoft.com`;
805
+ const res = await fetch(
806
+ `https://${encodeURI(hostname)}/api/texttospeech/v2.0/endpoints/${encodeURIComponent(deploymentId)}`,
807
+ {
808
+ headers: {
809
+ accept: "application/json",
810
+ "ocp-apim-subscription-key": subscriptionKey
811
+ }
812
+ }
813
+ );
814
+ if (!res.ok) {
815
+ throw new Error("Failed to fetch custom voices");
816
+ }
817
+ return res.json();
818
+ }
819
+ async function fetchCustomVoices_default({ customVoiceHostname, deploymentId, region, subscriptionKey }) {
820
+ const { models } = await fetchCustomVoices({ customVoiceHostname, deploymentId, region, subscriptionKey });
821
+ return models.map(
822
+ ({ properties: { Gender: gender }, locale: lang, name: voiceURI }) => new SpeechSynthesisVoice_default({ gender, lang, voiceURI })
823
+ ).sort(({ name: x }, { name: y }) => x > y ? 1 : x < y ? -1 : 0);
824
+ }
825
+
826
+ // src/SpeechServices/TextToSpeech/fetchVoices.js
827
+ async function fetchVoices({ authorizationToken, region, speechSynthesisHostname, subscriptionKey }) {
828
+ const hostname = speechSynthesisHostname || `${encodeURI(region)}.tts.speech.microsoft.com`;
829
+ const res = await fetch(`https://${hostname}/cognitiveservices/voices/list`, {
830
+ headers: {
831
+ "content-type": "application/json",
832
+ ...authorizationToken ? {
833
+ authorization: `Bearer ${authorizationToken}`
834
+ } : {
835
+ "Ocp-Apim-Subscription-Key": subscriptionKey
836
+ }
837
+ }
838
+ });
839
+ if (!res.ok) {
840
+ throw new Error("Failed to fetch voices");
841
+ }
842
+ const voices = await res.json();
843
+ return voices.map(({ Gender: gender, Locale: lang, Name: voiceURI }) => new SpeechSynthesisVoice_default({ gender, lang, voiceURI })).sort(({ name: x }, { name: y }) => x > y ? 1 : x < y ? -1 : 0);
844
+ }
845
+
846
+ // src/SpeechServices/TextToSpeech/SpeechSynthesisEvent.js
847
+ import { Event as Event2 } from "event-target-shim";
848
+ var SpeechSynthesisEvent = class extends Event2 {
849
+ constructor(type) {
850
+ super(type);
851
+ }
852
+ };
853
+
854
+ // src/SpeechServices/TextToSpeech/SpeechSynthesisUtterance.js
855
+ import { EventTarget as EventTarget2, getEventAttributeValue as getEventAttributeValue2, setEventAttributeValue as setEventAttributeValue2 } from "event-target-shim";
856
+ import EventAsPromise from "event-as-promise";
857
+
858
+ // src/SpeechServices/TextToSpeech/fetchSpeechData.js
859
+ import { decode } from "base64-arraybuffer";
860
+
861
+ // src/SpeechServices/TextToSpeech/buildSSML.js
862
+ function relativePercentage(value) {
863
+ let relative = Math.round((value - 1) * 100);
864
+ if (relative >= 0) {
865
+ relative = "+" + relative;
866
+ }
867
+ return relative + "%";
868
+ }
869
+ function buildSSML({ lang, pitch = 1, rate = 1, text, voice, volume }) {
870
+ return `<speak version="1.0" xml:lang="${lang}">
871
+ <voice xml:lang="${lang}" name="${voice}">
872
+ <prosody pitch="${relativePercentage(pitch)}" rate="${relativePercentage(rate)}" volume="${relativePercentage(
873
+ volume
874
+ )}">
875
+ ${text}
876
+ </prosody>
877
+ </voice>
878
+ </speak>`;
879
+ }
880
+
881
+ // src/SpeechServices/TextToSpeech/isSSML.js
882
+ var SPEAK_TAG_PATTERN = /^\s*<speak(\s|\/?>)/u;
883
+ var XML_PROLOG_PATTERN = /^\s*<\?xml\s/u;
884
+ function isSSML(text) {
885
+ return SPEAK_TAG_PATTERN.test(text) || XML_PROLOG_PATTERN.test(text);
886
+ }
887
+
888
+ // src/SpeechServices/TextToSpeech/fetchSpeechData.js
889
+ var DEFAULT_LANGUAGE = "en-US";
890
+ var DEFAULT_OUTPUT_FORMAT = "riff-16khz-16bit-mono-pcm";
891
+ var DEFAULT_VOICE = "Microsoft Server Speech Text to Speech Voice (en-US, AriaNeural)";
892
+ var EMPTY_MP3_BASE64 = "SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU3LjU2LjEwMQAAAAAAAAAAAAAA//tAwAAAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8AAAACAAABhgC7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7//////////////////////////////////////////////////////////////////8AAAAATGF2YzU3LjY0AAAAAAAAAAAAAAAAJAUHAAAAAAAAAYYoRBqpAAAAAAD/+xDEAAPAAAGkAAAAIAAANIAAAARMQU1FMy45OS41VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/7EMQpg8AAAaQAAAAgAAA0gAAABFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV";
893
+ async function fetchSpeechData_default({
894
+ deploymentId,
895
+ fetchCredentials,
896
+ lang = DEFAULT_LANGUAGE,
897
+ outputFormat = DEFAULT_OUTPUT_FORMAT,
898
+ pitch,
899
+ rate,
900
+ text,
901
+ voice = DEFAULT_VOICE,
902
+ volume
903
+ }) {
904
+ if (!text) {
905
+ return decode(EMPTY_MP3_BASE64);
906
+ }
907
+ const { authorizationToken, region, speechSynthesisHostname, subscriptionKey } = await fetchCredentials();
908
+ if (authorizationToken && subscriptionKey || !authorizationToken && !subscriptionKey) {
909
+ throw new Error('Only "authorizationToken" or "subscriptionKey" should be set.');
910
+ } else if (region && speechSynthesisHostname || !region && !speechSynthesisHostname) {
911
+ throw new Error('Only "region" or "speechSynthesisHostnamename" should be set.');
912
+ }
913
+ const ssml = isSSML(text) ? text : buildSSML({ lang, pitch, rate, text, voice, volume });
914
+ const hostname = speechSynthesisHostname || (deploymentId ? `${encodeURI(region)}.voice.speech.microsoft.com` : `${encodeURI(region)}.tts.speech.microsoft.com`);
915
+ const search = deploymentId ? `?deploymentId=${encodeURI(deploymentId)}` : "";
916
+ const url = `https://${hostname}/cognitiveservices/v1${search}`;
917
+ const res = await fetch(url, {
918
+ headers: {
919
+ "Content-Type": "application/ssml+xml",
920
+ "X-Microsoft-OutputFormat": outputFormat,
921
+ ...authorizationToken ? {
922
+ Authorization: `Bearer ${authorizationToken}`
923
+ } : {
924
+ "Ocp-Apim-Subscription-Key": subscriptionKey
925
+ }
926
+ },
927
+ method: "POST",
928
+ body: ssml
929
+ });
930
+ if (!res.ok) {
931
+ throw new Error(`web-speech-cognitive-services: Failed to syntheis speech, server returned ${res.status}`);
932
+ }
933
+ return res.arrayBuffer();
934
+ }
935
+
936
+ // src/SpeechServices/TextToSpeech/subscribeEvent.js
937
+ function subscribeEvent(target, name, handler) {
938
+ target.addEventListener(name, handler);
939
+ return () => target.removeEventListener(name, handler);
940
+ }
941
+
942
+ // src/SpeechServices/TextToSpeech/SpeechSynthesisUtterance.js
943
+ function asyncDecodeAudioData(audioContext, arrayBuffer) {
944
+ return new Promise((resolve, reject) => {
945
+ const promise = audioContext.decodeAudioData(arrayBuffer, resolve, reject);
946
+ promise && typeof promise.then === "function" && resolve(promise);
947
+ });
948
+ }
949
+ function playDecoded(audioContext, audioBuffer, source) {
950
+ return new Promise((resolve, reject) => {
951
+ const audioContextClosed = new EventAsPromise();
952
+ const sourceEnded = new EventAsPromise();
953
+ const unsubscribe = subscribeEvent(
954
+ audioContext,
955
+ "statechange",
956
+ ({ target: { state } }) => state === "closed" && audioContextClosed.eventListener()
957
+ );
958
+ try {
959
+ source.buffer = audioBuffer;
960
+ source.onended = sourceEnded.eventListener;
961
+ source.connect(audioContext.destination);
962
+ source.start(0);
963
+ Promise.race([audioContextClosed.upcoming(), sourceEnded.upcoming()]).then(resolve);
964
+ } catch (err) {
965
+ reject(err);
966
+ } finally {
967
+ unsubscribe();
968
+ }
969
+ });
970
+ }
971
+ var SpeechSynthesisUtterance = class extends EventTarget2 {
972
+ constructor(text) {
973
+ super();
974
+ this._lang = null;
975
+ this._pitch = 1;
976
+ this._rate = 1;
977
+ this._voice = null;
978
+ this._volume = 1;
979
+ this.text = text;
980
+ this.onboundary = null;
981
+ this.onend = null;
982
+ this.onerror = null;
983
+ this.onmark = null;
984
+ this.onpause = null;
985
+ this.onresume = null;
986
+ this.onstart = null;
987
+ }
988
+ get lang() {
989
+ return this._lang;
990
+ }
991
+ set lang(value) {
992
+ this._lang = value;
993
+ }
994
+ get onboundary() {
995
+ return getEventAttributeValue2(this, "boundary");
996
+ }
997
+ set onboundary(value) {
998
+ setEventAttributeValue2(this, "boundary", value);
999
+ }
1000
+ get onend() {
1001
+ return getEventAttributeValue2(this, "end");
1002
+ }
1003
+ set onend(value) {
1004
+ setEventAttributeValue2(this, "end", value);
1005
+ }
1006
+ get onerror() {
1007
+ return getEventAttributeValue2(this, "error");
1008
+ }
1009
+ set onerror(value) {
1010
+ setEventAttributeValue2(this, "error", value);
1011
+ }
1012
+ get onmark() {
1013
+ return getEventAttributeValue2(this, "mark");
1014
+ }
1015
+ set onmark(value) {
1016
+ setEventAttributeValue2(this, "mark", value);
1017
+ }
1018
+ get onpause() {
1019
+ return getEventAttributeValue2(this, "pause");
1020
+ }
1021
+ set onpause(value) {
1022
+ setEventAttributeValue2(this, "pause", value);
1023
+ }
1024
+ get onresume() {
1025
+ return getEventAttributeValue2(this, "resume");
1026
+ }
1027
+ set onresume(value) {
1028
+ setEventAttributeValue2(this, "resume", value);
1029
+ }
1030
+ get onstart() {
1031
+ return getEventAttributeValue2(this, "start");
1032
+ }
1033
+ set onstart(value) {
1034
+ setEventAttributeValue2(this, "start", value);
1035
+ }
1036
+ get pitch() {
1037
+ return this._pitch;
1038
+ }
1039
+ set pitch(value) {
1040
+ this._pitch = value;
1041
+ }
1042
+ get rate() {
1043
+ return this._rate;
1044
+ }
1045
+ set rate(value) {
1046
+ this._rate = value;
1047
+ }
1048
+ get voice() {
1049
+ return this._voice;
1050
+ }
1051
+ set voice(value) {
1052
+ this._voice = value;
1053
+ }
1054
+ get volume() {
1055
+ return this._volume;
1056
+ }
1057
+ set volume(value) {
1058
+ this._volume = value;
1059
+ }
1060
+ preload({ deploymentId, fetchCredentials, outputFormat }) {
1061
+ this.arrayBufferPromise = fetchSpeechData_default({
1062
+ fetchCredentials,
1063
+ deploymentId,
1064
+ lang: this.lang || window.navigator.language,
1065
+ outputFormat,
1066
+ pitch: this.pitch,
1067
+ rate: this.rate,
1068
+ text: this.text,
1069
+ voice: this.voice && this.voice.voiceURI,
1070
+ volume: this.volume
1071
+ });
1072
+ this.arrayBufferPromise.catch();
1073
+ }
1074
+ async play(audioContext) {
1075
+ try {
1076
+ this.dispatchEvent(new SpeechSynthesisEvent("start"));
1077
+ const source = audioContext.createBufferSource();
1078
+ const audioBuffer = await asyncDecodeAudioData(audioContext, await this.arrayBufferPromise);
1079
+ this._playingSource = source;
1080
+ await playDecoded(audioContext, audioBuffer, source);
1081
+ this._playingSource = null;
1082
+ this.dispatchEvent(new SpeechSynthesisEvent("end"));
1083
+ } catch (error) {
1084
+ this.dispatchEvent(new ErrorEvent("error", { error: "synthesis-failed", message: error.stack }));
1085
+ }
1086
+ }
1087
+ stop() {
1088
+ this._playingSource && this._playingSource.stop();
1089
+ }
1090
+ };
1091
+ var SpeechSynthesisUtterance_default = SpeechSynthesisUtterance;
1092
+
1093
+ // src/SpeechServices/TextToSpeech/createSpeechSynthesisPonyfill.js
1094
+ var DEFAULT_OUTPUT_FORMAT2 = "audio-24khz-160kbitrate-mono-mp3";
1095
+ var EMPTY_ARRAY = [];
1096
+ var createSpeechSynthesisPonyfill_default = (options) => {
1097
+ const {
1098
+ audioContext,
1099
+ fetchCredentials,
1100
+ ponyfill = {
1101
+ AudioContext: window.AudioContext || window.webkitAudioContext
1102
+ },
1103
+ speechSynthesisDeploymentId,
1104
+ speechSynthesisOutputFormat = DEFAULT_OUTPUT_FORMAT2
1105
+ } = patchOptions(options);
1106
+ if (!audioContext && !ponyfill.AudioContext) {
1107
+ console.warn(
1108
+ "web-speech-cognitive-services: This browser does not support Web Audio and it will not work with Cognitive Services Speech Services."
1109
+ );
1110
+ return {};
1111
+ }
1112
+ class SpeechSynthesis extends EventTarget3 {
1113
+ constructor() {
1114
+ super();
1115
+ this.queue = new AudioContextQueue_default({ audioContext, ponyfill });
1116
+ this.updateVoices();
1117
+ }
1118
+ cancel() {
1119
+ this.queue.stop();
1120
+ }
1121
+ getVoices() {
1122
+ return EMPTY_ARRAY;
1123
+ }
1124
+ get onvoiceschanged() {
1125
+ return getEventAttributeValue3(this, "voiceschanged");
1126
+ }
1127
+ set onvoiceschanged(value) {
1128
+ setEventAttributeValue3(this, "voiceschanged", value);
1129
+ }
1130
+ pause() {
1131
+ this.queue.pause();
1132
+ }
1133
+ resume() {
1134
+ this.queue.resume();
1135
+ }
1136
+ speak(utterance) {
1137
+ if (!(utterance instanceof SpeechSynthesisUtterance_default)) {
1138
+ throw new Error("invalid utterance");
1139
+ }
1140
+ const { reject, resolve, promise } = pDefer();
1141
+ const handleError = ({ error: errorCode, message }) => {
1142
+ const error = new Error(errorCode);
1143
+ error.stack = message;
1144
+ reject(error);
1145
+ };
1146
+ utterance.addEventListener("end", resolve);
1147
+ utterance.addEventListener("error", handleError);
1148
+ utterance.preload({
1149
+ deploymentId: speechSynthesisDeploymentId,
1150
+ fetchCredentials,
1151
+ outputFormat: speechSynthesisOutputFormat
1152
+ });
1153
+ this.queue.push(utterance);
1154
+ return promise.finally(() => {
1155
+ utterance.removeEventListener("end", resolve);
1156
+ utterance.removeEventListener("error", handleError);
1157
+ });
1158
+ }
1159
+ get speaking() {
1160
+ return this.queue.speaking;
1161
+ }
1162
+ async updateVoices() {
1163
+ const { customVoiceHostname, region, speechSynthesisHostname, subscriptionKey } = await fetchCredentials();
1164
+ if (speechSynthesisDeploymentId) {
1165
+ if (subscriptionKey) {
1166
+ console.warn(
1167
+ "web-speech-cognitive-services: Listing of custom voice models are only available when using subscription key."
1168
+ );
1169
+ await onErrorResumeNext(async () => {
1170
+ const voices = await fetchCustomVoices_default({
1171
+ customVoiceHostname,
1172
+ deploymentId: speechSynthesisDeploymentId,
1173
+ region,
1174
+ speechSynthesisHostname,
1175
+ subscriptionKey
1176
+ });
1177
+ this.getVoices = () => voices;
1178
+ });
1179
+ }
1180
+ } else {
1181
+ await onErrorResumeNext(async () => {
1182
+ const voices = await fetchVoices(await fetchCredentials());
1183
+ this.getVoices = () => voices;
1184
+ });
1185
+ }
1186
+ this.dispatchEvent(new SpeechSynthesisEvent("voiceschanged"));
1187
+ }
1188
+ }
1189
+ return {
1190
+ speechSynthesis: new SpeechSynthesis(),
1191
+ SpeechSynthesisEvent,
1192
+ SpeechSynthesisUtterance: SpeechSynthesisUtterance_default
1193
+ };
1194
+ };
1195
+
1196
+ // src/SpeechServices/TextToSpeech.js
1197
+ var TextToSpeech_default = createSpeechSynthesisPonyfill_default;
1198
+
1199
+ // src/SpeechServices/fetchAuthorizationToken.js
1200
+ var TOKEN_URL_TEMPLATE = "https://{region}.api.cognitive.microsoft.com/sts/v1.0/issueToken";
1201
+ async function fetchAuthorizationToken_default({ region, subscriptionKey }) {
1202
+ const res = await fetch(TOKEN_URL_TEMPLATE.replace(/\{region\}/u, region), {
1203
+ headers: {
1204
+ "Ocp-Apim-Subscription-Key": subscriptionKey
1205
+ },
1206
+ method: "POST"
1207
+ });
1208
+ if (!res.ok) {
1209
+ throw new Error(`Failed to fetch authorization token, server returned ${res.status}`);
1210
+ }
1211
+ return res.text();
1212
+ }
1213
+
1214
+ // src/SpeechServices.js
1215
+ function createSpeechServicesPonyfill(options = {}, ...args) {
1216
+ return {
1217
+ ...SpeechToText_default(options, ...args),
1218
+ ...TextToSpeech_default(options, ...args)
1219
+ };
1220
+ }
1221
+ var meta = document.createElement("meta");
1222
+ meta.setAttribute("name", "web-speech-cognitive-services");
1223
+ meta.setAttribute("content", `version=${"8.0.0-main.85313be"}`);
1224
+ document.head.appendChild(meta);
1225
+ export {
1226
+ SpeechToText_default as createSpeechRecognitionPonyfill,
1227
+ createSpeechRecognitionPonyfillFromRecognizer,
1228
+ createSpeechServicesPonyfill,
1229
+ TextToSpeech_default as createSpeechSynthesisPonyfill,
1230
+ fetchAuthorizationToken_default as fetchAuthorizationToken
1231
+ };
1232
+ //# sourceMappingURL=web-speech-cognitive-services.mjs.map