web-speech-cognitive-services 7.1.4-master.9e026d9 → 8.0.0-main.15b930d

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