web-speech-cognitive-services 8.0.0 → 8.0.1-main.7b3a347

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.
@@ -1,3 +1,9 @@
1
+ // src/SpeechServices/SpeechToText/createSpeechRecognitionPonyfill.ts
2
+ import { PropertyId } from "microsoft-cognitiveservices-speech-sdk";
3
+
4
+ // src/SpeechServices/patchOptions.ts
5
+ import { parse } from "valibot";
6
+
1
7
  // src/SpeechServices/resolveFunctionOrReturnValue.ts
2
8
  function isFunction(value) {
3
9
  return typeof value === "function";
@@ -6,103 +12,151 @@ function resolveFunctionOrReturnValue(fnOrValue) {
6
12
  return isFunction(fnOrValue) ? fnOrValue() : fnOrValue;
7
13
  }
8
14
 
15
+ // src/SpeechServices/SpeechSDK.ts
16
+ import {
17
+ AudioConfig,
18
+ OutputFormat,
19
+ ResultReason,
20
+ SpeechConfig,
21
+ SpeechRecognizer
22
+ } from "microsoft-cognitiveservices-speech-sdk/distrib/lib/microsoft.cognitiveservices.speech.sdk";
23
+ var SpeechSDK_default = {
24
+ AudioConfig,
25
+ OutputFormat,
26
+ ResultReason,
27
+ SpeechConfig,
28
+ SpeechRecognizer
29
+ };
30
+
31
+ // src/SpeechServices/SpeechToText/validation/credentialsSchema.ts
32
+ import { intersect, object, optional, pipe, readonly, string, undefined_, union } from "valibot";
33
+ var credentialsSchema = pipe(
34
+ intersect([
35
+ union(
36
+ [
37
+ object({
38
+ authorizationToken: string(),
39
+ subscriptionKey: optional(undefined_('"subscriptionKey" must be unset when "authorizationToken" is set.'))
40
+ }),
41
+ object({
42
+ authorizationToken: optional(undefined_('"authorizationToken" must be unset when "subscriptionKey" is set.')),
43
+ subscriptionKey: string()
44
+ })
45
+ ],
46
+ 'The object must either have either "authorizationToken" or "subscriptionKey" set, but not both.'
47
+ ),
48
+ union(
49
+ [
50
+ object({
51
+ customVoiceHostname: optional(undefined_('"customVoiceHostname" must be unest when "region" is set.')),
52
+ region: string(),
53
+ speechRecognitionHostname: optional(
54
+ undefined_('"speechRecognitionHostname" must be unest when "region" is set.')
55
+ ),
56
+ speechSynthesisHostname: optional(undefined_('"speechSynthesisHostname" must be unest when "region" is set.'))
57
+ }),
58
+ object({
59
+ customVoiceHostname: optional(union([string(), undefined_()])),
60
+ region: optional(undefined_('"region" must be unset when "*Hostname" is set.')),
61
+ speechRecognitionHostname: string(),
62
+ speechSynthesisHostname: string()
63
+ })
64
+ ],
65
+ 'The object must either have either "region" or "*Hostname" set, but not both.'
66
+ )
67
+ ]),
68
+ readonly()
69
+ );
70
+ var credentialsSchema_default = credentialsSchema;
71
+
72
+ // src/SpeechServices/SpeechToText/validation/enableTelemetrySchema.ts
73
+ import { boolean, optional as optional2 } from "valibot";
74
+ var enableTelemetrySchema = optional2(boolean());
75
+ var enableTelemetrySchema_default = enableTelemetrySchema;
76
+
77
+ // src/SpeechServices/SpeechToText/validation/initialSilenceTimeoutSchema.ts
78
+ import { maxValue, minValue, number, optional as optional3, pipe as pipe2 } from "valibot";
79
+ var initialSilenceTimeoutSchema = optional3(pipe2(number(), minValue(1), maxValue(6e4)));
80
+ var initialSilenceTimeoutSchema_default = initialSilenceTimeoutSchema;
81
+
82
+ // src/SpeechServices/SpeechToText/validation/looseEventsSchema.ts
83
+ import { boolean as boolean2, optional as optional4 } from "valibot";
84
+ var looseEventsSchema = optional4(boolean2(), false);
85
+ var looseEventsSchema_default = looseEventsSchema;
86
+
87
+ // src/SpeechServices/SpeechToText/validation/referenceGrammarsSchema.ts
88
+ import { array, optional as optional5, pipe as pipe3, string as string2, transform } from "valibot";
89
+ var referenceGrammarsSchema = pipe3(
90
+ optional5(array(string2()), []),
91
+ // any(),
92
+ // array(string()),
93
+ // transform<string[], readonly string[]>(value => (Object.isFrozen(value) ? value : Object.freeze([...value])))
94
+ transform((value) => Object.isFrozen(value) ? value : Object.freeze([...value]))
95
+ );
96
+ var referenceGrammarsSchema_default = referenceGrammarsSchema;
97
+
98
+ // src/SpeechServices/SpeechToText/validation/speechRecognitionEndpointIdSchema.ts
99
+ import { optional as optional6, string as string3 } from "valibot";
100
+ var speechRecognitionEndpointIdSchema = optional6(string3());
101
+ var speechRecognitionEndpointIdSchema_default = speechRecognitionEndpointIdSchema;
102
+
103
+ // src/SpeechServices/SpeechToText/validation/textNormalizationSchema.ts
104
+ import { enum_, optional as optional7 } from "valibot";
105
+ var textNormalizationSchema = optional7(
106
+ enum_({
107
+ display: "display",
108
+ itn: "itn",
109
+ lexical: "lexical",
110
+ maskeditn: "maskeditn"
111
+ }),
112
+ "display"
113
+ );
114
+ var textNormalizationSchema_default = textNormalizationSchema;
115
+
9
116
  // src/SpeechServices/patchOptions.ts
117
+ var { AudioConfig: AudioConfig2 } = SpeechSDK_default;
10
118
  var shouldWarnOnSubscriptionKey = true;
11
119
  function patchOptions(init) {
12
120
  const {
13
121
  audioConfig,
14
- authorizationToken,
122
+ credentials,
15
123
  enableTelemetry,
124
+ initialSilenceTimeout,
16
125
  looseEvent,
17
126
  referenceGrammars,
18
- region = "westus",
19
127
  speechRecognitionEndpointId,
20
- subscriptionKey,
21
128
  textNormalization
22
129
  } = init;
23
- let { credentials, looseEvents } = init;
130
+ let { looseEvents } = init;
24
131
  if (typeof looseEvent !== "undefined") {
25
132
  console.warn('web-speech-cognitive-services: The option "looseEvent" should be named as "looseEvents".');
26
133
  looseEvents = looseEvent;
27
134
  }
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
135
  return Object.freeze({
39
- audioConfig,
40
- enableTelemetry,
136
+ audioConfig: audioConfig || AudioConfig2.fromDefaultMicrophoneInput(),
137
+ // We set telemetry to true to honor the default telemetry settings of Speech SDK
138
+ // https://github.com/Microsoft/cognitive-services-speech-sdk-js#data--telemetry
139
+ enableTelemetry: parse(enableTelemetrySchema_default, enableTelemetry),
41
140
  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) {
141
+ const parsedCredentials = parse(credentialsSchema_default, await resolveFunctionOrReturnValue(credentials));
142
+ if (shouldWarnOnSubscriptionKey && parsedCredentials.subscriptionKey) {
70
143
  console.warn(
71
144
  "web-speech-cognitive-services: In production environment, subscription key should not be used, authorization token should be used instead."
72
145
  );
73
146
  shouldWarnOnSubscriptionKey = false;
74
147
  }
75
- return {
76
- ...typeof authorizationToken2 !== "undefined" ? { authorizationToken: authorizationToken2 } : { subscriptionKey: subscriptionKey2 },
77
- ...typeof region2 !== "undefined" ? { region: region2 } : {
78
- customVoiceHostname,
79
- speechRecognitionHostname,
80
- speechSynthesisHostname
81
- }
82
- };
148
+ return parsedCredentials;
83
149
  },
84
- looseEvents: !!looseEvents,
85
- referenceGrammars: referenceGrammars && Object.freeze([...referenceGrammars]),
86
- speechRecognitionEndpointId,
87
- textNormalization
150
+ initialSilenceTimeout: parse(initialSilenceTimeoutSchema_default, initialSilenceTimeout),
151
+ looseEvents: parse(looseEventsSchema_default, looseEvents),
152
+ referenceGrammars: parse(referenceGrammarsSchema_default, referenceGrammars),
153
+ speechRecognitionEndpointId: parse(speechRecognitionEndpointIdSchema_default, speechRecognitionEndpointId),
154
+ textNormalization: parse(textNormalizationSchema_default, textNormalization)
88
155
  });
89
156
  }
90
157
 
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
- };
158
+ // src/SpeechServices/SpeechToText/createSpeechRecognitionPonyfillFromRecognizer.ts
159
+ import { boolean as boolean3, function_, parse as parse2, undefined_ as undefined_2, union as union2 } from "valibot";
106
160
 
107
161
  // ../../node_modules/p-defer/index.js
108
162
  function pDefer() {
@@ -157,16 +211,16 @@ var SpeechRecognitionAlternative = class {
157
211
 
158
212
  // src/SpeechServices/SpeechToText/private/FakeArray.ts
159
213
  var FakeArray = class {
160
- constructor(array) {
161
- if (!array) {
214
+ constructor(array2) {
215
+ if (!array2) {
162
216
  throw new Error("array must be set.");
163
217
  }
164
- this.#array = array;
165
- for (const key in array) {
218
+ this.#array = array2;
219
+ for (const key in array2) {
166
220
  Object.defineProperty(this, key, {
167
221
  enumerable: true,
168
222
  get() {
169
- return array[key];
223
+ return array2[key];
170
224
  }
171
225
  });
172
226
  }
@@ -257,8 +311,8 @@ import { AudioSourceEvent } from "microsoft-cognitiveservices-speech-sdk/distrib
257
311
 
258
312
  // src/SpeechServices/SpeechToText/private/averageAmplitude.ts
259
313
  function averageAmplitude(arrayBuffer) {
260
- const array = Array.from(new Int16Array(arrayBuffer));
261
- return array.reduce((averageAmplitude2, amplitude) => averageAmplitude2 + Math.abs(amplitude), 0) / array.length;
314
+ const array2 = Array.from(new Int16Array(arrayBuffer));
315
+ return array2.reduce((averageAmplitude2, amplitude) => averageAmplitude2 + Math.abs(amplitude), 0) / array2.length;
262
316
  }
263
317
 
264
318
  // src/SpeechServices/SpeechToText/private/prepareAudioConfig.ts
@@ -406,6 +460,7 @@ var SpeechRecognitionEvent = class extends Event {
406
460
 
407
461
  // src/SpeechServices/SpeechToText/createSpeechRecognitionPonyfillFromRecognizer.ts
408
462
  var { ResultReason: ResultReason2, SpeechRecognizer: SpeechRecognizer2 } = SpeechSDK_default;
463
+ var enableTelemetrySchema2 = union2([boolean3(), undefined_2()]);
409
464
  function createSpeechRecognitionPonyfillFromRecognizer({
410
465
  createRecognizer,
411
466
  enableTelemetry,
@@ -413,7 +468,12 @@ function createSpeechRecognitionPonyfillFromRecognizer({
413
468
  referenceGrammars,
414
469
  textNormalization
415
470
  }) {
416
- SpeechRecognizer2.enableTelemetry(enableTelemetry !== false);
471
+ createRecognizer = parse2(function_(), createRecognizer);
472
+ enableTelemetry = parse2(enableTelemetrySchema2, enableTelemetry);
473
+ looseEvents = parse2(boolean3(), looseEvents);
474
+ referenceGrammars = parse2(referenceGrammarsSchema_default, referenceGrammars);
475
+ textNormalization = parse2(textNormalizationSchema_default, textNormalization);
476
+ typeof enableTelemetry !== "undefined" && SpeechRecognizer2.enableTelemetry(enableTelemetry);
417
477
  class SpeechRecognition extends EventTarget {
418
478
  #continuous = false;
419
479
  #eventListenerMap = new EventListenerMap(this);
@@ -783,18 +843,17 @@ function createSpeechRecognitionPonyfillFromRecognizer({
783
843
  }
784
844
 
785
845
  // src/SpeechServices/SpeechToText/createSpeechRecognitionPonyfill.ts
786
- var { AudioConfig: AudioConfig2, OutputFormat: OutputFormat2, SpeechConfig: SpeechConfig2, SpeechRecognizer: SpeechRecognizer3 } = SpeechSDK_default;
846
+ var { OutputFormat: OutputFormat2, SpeechConfig: SpeechConfig2, SpeechRecognizer: SpeechRecognizer3 } = SpeechSDK_default;
787
847
  function createSpeechRecognitionPonyfill(options) {
788
848
  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,
849
+ audioConfig,
850
+ enableTelemetry,
793
851
  fetchCredentials,
852
+ initialSilenceTimeout,
794
853
  looseEvents,
795
854
  referenceGrammars,
796
855
  speechRecognitionEndpointId,
797
- textNormalization = "display"
856
+ textNormalization
798
857
  } = patchOptions(options);
799
858
  if (!audioConfig && (!window.navigator.mediaDevices || !window.navigator.mediaDevices.getUserMedia)) {
800
859
  throw new Error(
@@ -805,7 +864,7 @@ function createSpeechRecognitionPonyfill(options) {
805
864
  const credentials = await fetchCredentials();
806
865
  let speechConfig;
807
866
  if (typeof credentials.speechRecognitionHostname !== "undefined") {
808
- const host = new URL("wss://hostname:443");
867
+ const host = new URL("wss://localhost:443");
809
868
  host.hostname = credentials.speechRecognitionHostname;
810
869
  if (credentials.authorizationToken) {
811
870
  speechConfig = SpeechConfig2.fromHost(host);
@@ -821,6 +880,7 @@ function createSpeechRecognitionPonyfill(options) {
821
880
  }
822
881
  speechConfig.outputFormat = OutputFormat2.Detailed;
823
882
  speechConfig.speechRecognitionLanguage = lang || "en-US";
883
+ typeof initialSilenceTimeout === "number" && speechConfig.setProperty(PropertyId.SpeechServiceConnection_InitialSilenceTimeoutMs, "" + initialSilenceTimeout);
824
884
  return new SpeechRecognizer3(speechConfig, audioConfig);
825
885
  };
826
886
  return createSpeechRecognitionPonyfillFromRecognizer({
@@ -1362,7 +1422,7 @@ function createSpeechServicesPonyfill(options = {}) {
1362
1422
  }
1363
1423
  var meta = document.createElement("meta");
1364
1424
  meta.setAttribute("name", "web-speech-cognitive-services");
1365
- meta.setAttribute("content", `version=${"8.0.0"}`);
1425
+ meta.setAttribute("content", `version=${"8.0.1-main.7b3a347"}`);
1366
1426
  document.head.appendChild(meta);
1367
1427
  export {
1368
1428
  createSpeechRecognitionPonyfill,