@creativeorange/azure-text-to-speech 1.1.7 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -208,6 +208,19 @@ class ConnectionErrorEvent extends ConnectionEvent {
208
208
  return this.privType;
209
209
  }
210
210
  }
211
+ class ConnectionEstablishErrorEvent extends ConnectionEvent {
212
+ constructor(connectionId, statuscode, reason) {
213
+ super("ConnectionEstablishErrorEvent", connectionId, EventType.Error);
214
+ this.privStatusCode = statuscode;
215
+ this.privReason = reason;
216
+ }
217
+ get reason() {
218
+ return this.privReason;
219
+ }
220
+ get statusCode() {
221
+ return this.privStatusCode;
222
+ }
223
+ }
211
224
  class ConnectionMessageReceivedEvent extends ConnectionEvent {
212
225
  constructor(connectionId, networkReceivedTimeISO, message) {
213
226
  super("ConnectionMessageReceivedEvent", connectionId);
@@ -648,7 +661,7 @@ function marshalPromiseToCallbacks(promise, cb, err) {
648
661
  }
649
662
  });
650
663
  }
651
- var __awaiter$d = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
664
+ var __awaiter$i = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
652
665
  function adopt(value) {
653
666
  return value instanceof P ? value : new P(function(resolve) {
654
667
  resolve(value);
@@ -729,7 +742,7 @@ class Queue {
729
742
  return this.privSubscribers == null;
730
743
  }
731
744
  drainAndDispose(pendingItemProcessor, reason) {
732
- return __awaiter$d(this, void 0, void 0, function* () {
745
+ return __awaiter$i(this, void 0, void 0, function* () {
733
746
  if (!this.isDisposed() && !this.privIsDisposing) {
734
747
  this.privDisposeReason = reason;
735
748
  this.privIsDisposing = true;
@@ -767,7 +780,7 @@ class Queue {
767
780
  });
768
781
  }
769
782
  dispose(reason) {
770
- return __awaiter$d(this, void 0, void 0, function* () {
783
+ return __awaiter$i(this, void 0, void 0, function* () {
771
784
  yield this.drainAndDispose(null, reason);
772
785
  });
773
786
  }
@@ -897,7 +910,7 @@ class RiffPcmEncoder {
897
910
  return dstFrame;
898
911
  }
899
912
  }
900
- var __awaiter$c = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
913
+ var __awaiter$h = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
901
914
  function adopt(value) {
902
915
  return value instanceof P ? value : new P(function(resolve) {
903
916
  resolve(value);
@@ -963,7 +976,7 @@ class Stream {
963
976
  if (this.privIsReadEnded) {
964
977
  throw new InvalidOperationError("Stream read has already finished");
965
978
  }
966
- return this.privReaderQueue.dequeue().then((streamChunk) => __awaiter$c(this, void 0, void 0, function* () {
979
+ return this.privReaderQueue.dequeue().then((streamChunk) => __awaiter$h(this, void 0, void 0, function* () {
967
980
  if (streamChunk === void 0 || streamChunk.isEnd) {
968
981
  yield this.privReaderQueue.dispose("End of stream reached");
969
982
  }
@@ -982,6 +995,11 @@ class Stream {
982
995
  }
983
996
  }
984
997
  }
998
+ var TranslationStatus;
999
+ (function(TranslationStatus2) {
1000
+ TranslationStatus2[TranslationStatus2["Success"] = 0] = "Success";
1001
+ TranslationStatus2[TranslationStatus2["Error"] = 1] = "Error";
1002
+ })(TranslationStatus || (TranslationStatus = {}));
985
1003
  class ChunkedArrayBufferStream extends Stream {
986
1004
  constructor(targetChunkSize, streamId) {
987
1005
  super(streamId);
@@ -1027,6 +1045,93 @@ class ChunkedArrayBufferStream extends Stream {
1027
1045
  super.close();
1028
1046
  }
1029
1047
  }
1048
+ class Timeout {
1049
+ static load(url) {
1050
+ const scheduledTimeoutFunctions = /* @__PURE__ */ new Map([[0, () => {
1051
+ }]]);
1052
+ const unhandledRequests = /* @__PURE__ */ new Map();
1053
+ const worker = new Worker(url);
1054
+ worker.addEventListener("message", ({ data }) => {
1055
+ if (Timeout.isCallNotification(data)) {
1056
+ const { params: { timerId } } = data;
1057
+ const idOrFunc = scheduledTimeoutFunctions.get(timerId);
1058
+ if (typeof idOrFunc === "number") {
1059
+ const unhandledTimerId = unhandledRequests.get(idOrFunc);
1060
+ if (unhandledTimerId === void 0 || unhandledTimerId !== timerId) {
1061
+ throw new Error("The timer is in an undefined state.");
1062
+ }
1063
+ } else if (typeof idOrFunc !== "undefined") {
1064
+ idOrFunc();
1065
+ scheduledTimeoutFunctions.delete(timerId);
1066
+ } else {
1067
+ throw new Error("The timer is in an undefined state.");
1068
+ }
1069
+ } else if (Timeout.isClearResponse(data)) {
1070
+ const { id } = data;
1071
+ const unhandledTimerId = unhandledRequests.get(id);
1072
+ if (unhandledTimerId === void 0) {
1073
+ throw new Error("The timer is in an undefined state.");
1074
+ }
1075
+ unhandledRequests.delete(id);
1076
+ scheduledTimeoutFunctions.delete(unhandledTimerId);
1077
+ } else {
1078
+ const { error: { message } } = data;
1079
+ throw new Error(message);
1080
+ }
1081
+ });
1082
+ const clearTimeout = (timerId) => {
1083
+ const id = Math.random();
1084
+ unhandledRequests.set(id, timerId);
1085
+ scheduledTimeoutFunctions.set(timerId, id);
1086
+ worker.postMessage({
1087
+ id,
1088
+ method: "clear",
1089
+ params: { timerId }
1090
+ });
1091
+ };
1092
+ const setTimeout2 = (func, delay) => {
1093
+ const timerId = Math.random();
1094
+ scheduledTimeoutFunctions.set(timerId, func);
1095
+ worker.postMessage({
1096
+ id: null,
1097
+ method: "set",
1098
+ params: {
1099
+ delay,
1100
+ now: performance.now(),
1101
+ timerId
1102
+ }
1103
+ });
1104
+ return timerId;
1105
+ };
1106
+ return {
1107
+ clearTimeout,
1108
+ setTimeout: setTimeout2
1109
+ };
1110
+ }
1111
+ static loadWorkerTimers() {
1112
+ const worker = `!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=14)}([function(e,t,n){"use strict";n.d(t,"a",(function(){return i})),n.d(t,"b",(function(){return u})),n.d(t,"c",(function(){return a})),n.d(t,"d",(function(){return d}));const r=new Map,o=new Map,i=e=>{const t=r.get(e);if(void 0===t)throw new Error('There is no interval scheduled with the given id "'.concat(e,'".'));clearTimeout(t),r.delete(e)},u=e=>{const t=o.get(e);if(void 0===t)throw new Error('There is no timeout scheduled with the given id "'.concat(e,'".'));clearTimeout(t),o.delete(e)},f=(e,t)=>{let n,r;if("performance"in self){const o=performance.now();n=o,r=e-Math.max(0,o-t)}else n=Date.now(),r=e;return{expected:n+r,remainingDelay:r}},c=(e,t,n,r)=>{const o="performance"in self?performance.now():Date.now();o>n?postMessage({id:null,method:"call",params:{timerId:t}}):e.set(t,setTimeout(c,n-o,e,t,n))},a=(e,t,n)=>{const{expected:o,remainingDelay:i}=f(e,n);r.set(t,setTimeout(c,i,r,t,o))},d=(e,t,n)=>{const{expected:r,remainingDelay:i}=f(e,n);o.set(t,setTimeout(c,i,o,t,r))}},function(e,t,n){"use strict";n.r(t);var r=n(2);for(var o in r)"default"!==o&&function(e){n.d(t,e,(function(){return r[e]}))}(o);var i=n(3);for(var o in i)"default"!==o&&function(e){n.d(t,e,(function(){return i[e]}))}(o);var u=n(4);for(var o in u)"default"!==o&&function(e){n.d(t,e,(function(){return u[e]}))}(o);var f=n(5);for(var o in f)"default"!==o&&function(e){n.d(t,e,(function(){return f[e]}))}(o);var c=n(6);for(var o in c)"default"!==o&&function(e){n.d(t,e,(function(){return c[e]}))}(o);var a=n(7);for(var o in a)"default"!==o&&function(e){n.d(t,e,(function(){return a[e]}))}(o);var d=n(8);for(var o in d)"default"!==o&&function(e){n.d(t,e,(function(){return d[e]}))}(o);var s=n(9);for(var o in s)"default"!==o&&function(e){n.d(t,e,(function(){return s[e]}))}(o)},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t){},function(e,t,n){"use strict";n.r(t);var r=n(11);for(var o in r)"default"!==o&&function(e){n.d(t,e,(function(){return r[e]}))}(o);var i=n(12);for(var o in i)"default"!==o&&function(e){n.d(t,e,(function(){return i[e]}))}(o);var u=n(13);for(var o in u)"default"!==o&&function(e){n.d(t,e,(function(){return u[e]}))}(o)},function(e,t){},function(e,t){},function(e,t){},function(e,t,n){"use strict";n.r(t);var r=n(0),o=n(1);for(var i in o)"default"!==i&&function(e){n.d(t,e,(function(){return o[e]}))}(i);var u=n(10);for(var i in u)"default"!==i&&function(e){n.d(t,e,(function(){return u[e]}))}(i);addEventListener("message",({data:e})=>{try{if("clear"===e.method){const{id:t,params:{timerId:n}}=e;Object(r.b)(n),postMessage({error:null,id:t})}else{if("set"!==e.method)throw new Error('The given method "'.concat(e.method,'" is not supported'));{const{params:{delay:t,now:n,timerId:o}}=e;Object(r.d)(t,o,n)}}}catch(t){postMessage({error:{message:t.message},id:e.id,result:null})}})}]);`;
1113
+ return () => {
1114
+ if (Timeout.workerTimers !== null) {
1115
+ return Timeout.workerTimers;
1116
+ }
1117
+ const blob = new Blob([worker], { type: "application/javascript; charset=utf-8" });
1118
+ const url = URL.createObjectURL(blob);
1119
+ Timeout.workerTimers = Timeout.load(url);
1120
+ Timeout.workerTimers.setTimeout(() => URL.revokeObjectURL(url), 0);
1121
+ return Timeout.workerTimers;
1122
+ };
1123
+ }
1124
+ static isCallNotification(message) {
1125
+ return message.method !== void 0 && message.method === "call";
1126
+ }
1127
+ static isClearResponse(message) {
1128
+ return message.error === null && typeof message.id === "number";
1129
+ }
1130
+ }
1131
+ Timeout.workerTimers = null;
1132
+ Timeout.clearTimeout = (timerId) => Timeout.timers().clearTimeout(timerId);
1133
+ Timeout.setTimeout = (func, delay) => Timeout.timers().setTimeout(func, delay);
1134
+ Timeout.timers = Timeout.loadWorkerTimers();
1030
1135
  class OCSPEvent extends PlatformEvent {
1031
1136
  constructor(eventName, eventType, signature) {
1032
1137
  super(eventName, eventType);
@@ -1352,7 +1457,7 @@ class AudioStreamFormatImpl extends AudioStreamFormat {
1352
1457
  }
1353
1458
  }
1354
1459
  }
1355
- var __awaiter$b = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
1460
+ var __awaiter$g = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
1356
1461
  function adopt(value) {
1357
1462
  return value instanceof P ? value : new P(function(resolve) {
1358
1463
  resolve(value);
@@ -1452,13 +1557,13 @@ class PushAudioInputStreamImpl extends PushAudioInputStream {
1452
1557
  return;
1453
1558
  }
1454
1559
  attach(audioNodeId) {
1455
- return __awaiter$b(this, void 0, void 0, function* () {
1560
+ return __awaiter$g(this, void 0, void 0, function* () {
1456
1561
  this.onEvent(new AudioStreamNodeAttachingEvent(this.privId, audioNodeId));
1457
1562
  yield this.turnOn();
1458
1563
  const stream = this.privStream;
1459
1564
  this.onEvent(new AudioStreamNodeAttachedEvent(this.privId, audioNodeId));
1460
1565
  return {
1461
- detach: () => __awaiter$b(this, void 0, void 0, function* () {
1566
+ detach: () => __awaiter$g(this, void 0, void 0, function* () {
1462
1567
  this.onEvent(new AudioStreamNodeDetachedEvent(this.privId, audioNodeId));
1463
1568
  return this.turnOff();
1464
1569
  }),
@@ -1541,7 +1646,7 @@ class PullAudioInputStreamImpl extends PullAudioInputStream {
1541
1646
  return;
1542
1647
  }
1543
1648
  attach(audioNodeId) {
1544
- return __awaiter$b(this, void 0, void 0, function* () {
1649
+ return __awaiter$g(this, void 0, void 0, function* () {
1545
1650
  this.onEvent(new AudioStreamNodeAttachingEvent(this.privId, audioNodeId));
1546
1651
  yield this.turnOn();
1547
1652
  this.onEvent(new AudioStreamNodeAttachedEvent(this.privId, audioNodeId));
@@ -1804,7 +1909,7 @@ AudioOutputFormatImpl.SpeechSynthesisOutputFormatToString = {
1804
1909
  [SpeechSynthesisOutputFormat.Raw44100Hz16BitMonoPcm]: "raw-44100hz-16bit-mono-pcm",
1805
1910
  [SpeechSynthesisOutputFormat.Riff44100Hz16BitMonoPcm]: "riff-44100hz-16bit-mono-pcm"
1806
1911
  };
1807
- var __awaiter$a = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
1912
+ var __awaiter$f = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
1808
1913
  function adopt(value) {
1809
1914
  return value instanceof P ? value : new P(function(resolve) {
1810
1915
  resolve(value);
@@ -1866,7 +1971,7 @@ class PullAudioOutputStreamImpl extends PullAudioOutputStream {
1866
1971
  return this.privId;
1867
1972
  }
1868
1973
  read(dataBuffer) {
1869
- return __awaiter$a(this, void 0, void 0, function* () {
1974
+ return __awaiter$f(this, void 0, void 0, function* () {
1870
1975
  const intView = new Int8Array(dataBuffer);
1871
1976
  let totalBytes = 0;
1872
1977
  if (this.privLastChunkView !== void 0) {
@@ -2085,11 +2190,120 @@ class PullAudioInputStreamCallback {
2085
2190
  }
2086
2191
  class PushAudioOutputStreamCallback {
2087
2192
  }
2193
+ class SessionEventArgs {
2194
+ constructor(sessionId) {
2195
+ this.privSessionId = sessionId;
2196
+ }
2197
+ get sessionId() {
2198
+ return this.privSessionId;
2199
+ }
2200
+ }
2201
+ class RecognitionEventArgs extends SessionEventArgs {
2202
+ constructor(offset, sessionId) {
2203
+ super(sessionId);
2204
+ this.privOffset = offset;
2205
+ }
2206
+ get offset() {
2207
+ return this.privOffset;
2208
+ }
2209
+ }
2088
2210
  var OutputFormat;
2089
2211
  (function(OutputFormat2) {
2090
2212
  OutputFormat2[OutputFormat2["Simple"] = 0] = "Simple";
2091
2213
  OutputFormat2[OutputFormat2["Detailed"] = 1] = "Detailed";
2092
2214
  })(OutputFormat || (OutputFormat = {}));
2215
+ class RecognitionResult {
2216
+ constructor(resultId, reason, text, duration, offset, language, languageDetectionConfidence, errorDetails, json, properties) {
2217
+ this.privResultId = resultId;
2218
+ this.privReason = reason;
2219
+ this.privText = text;
2220
+ this.privDuration = duration;
2221
+ this.privOffset = offset;
2222
+ this.privLanguage = language;
2223
+ this.privLanguageDetectionConfidence = languageDetectionConfidence;
2224
+ this.privErrorDetails = errorDetails;
2225
+ this.privJson = json;
2226
+ this.privProperties = properties;
2227
+ }
2228
+ get resultId() {
2229
+ return this.privResultId;
2230
+ }
2231
+ get reason() {
2232
+ return this.privReason;
2233
+ }
2234
+ get text() {
2235
+ return this.privText;
2236
+ }
2237
+ get duration() {
2238
+ return this.privDuration;
2239
+ }
2240
+ get offset() {
2241
+ return this.privOffset;
2242
+ }
2243
+ get language() {
2244
+ return this.privLanguage;
2245
+ }
2246
+ get languageDetectionConfidence() {
2247
+ return this.privLanguageDetectionConfidence;
2248
+ }
2249
+ get errorDetails() {
2250
+ return this.privErrorDetails;
2251
+ }
2252
+ get json() {
2253
+ return this.privJson;
2254
+ }
2255
+ get properties() {
2256
+ return this.privProperties;
2257
+ }
2258
+ }
2259
+ class SpeechRecognitionResult extends RecognitionResult {
2260
+ constructor(resultId, reason, text, duration, offset, language, languageDetectionConfidence, speakerId, errorDetails, json, properties) {
2261
+ super(resultId, reason, text, duration, offset, language, languageDetectionConfidence, errorDetails, json, properties);
2262
+ this.privSpeakerId = speakerId;
2263
+ }
2264
+ get speakerId() {
2265
+ return this.privSpeakerId;
2266
+ }
2267
+ }
2268
+ class TranslationRecognitionEventArgs extends RecognitionEventArgs {
2269
+ constructor(result, offset, sessionId) {
2270
+ super(offset, sessionId);
2271
+ this.privResult = result;
2272
+ }
2273
+ get result() {
2274
+ return this.privResult;
2275
+ }
2276
+ }
2277
+ class TranslationSynthesisEventArgs extends SessionEventArgs {
2278
+ constructor(result, sessionId) {
2279
+ super(sessionId);
2280
+ this.privResult = result;
2281
+ }
2282
+ get result() {
2283
+ return this.privResult;
2284
+ }
2285
+ }
2286
+ class TranslationRecognitionResult extends SpeechRecognitionResult {
2287
+ constructor(translations, resultId, reason, text, duration, offset, errorDetails, json, properties) {
2288
+ super(resultId, reason, text, duration, offset, void 0, void 0, void 0, errorDetails, json, properties);
2289
+ this.privTranslations = translations;
2290
+ }
2291
+ get translations() {
2292
+ return this.privTranslations;
2293
+ }
2294
+ }
2295
+ class TranslationSynthesisResult {
2296
+ constructor(reason, audio) {
2297
+ this.privReason = reason;
2298
+ this.privAudio = audio;
2299
+ }
2300
+ get audio() {
2301
+ return this.privAudio;
2302
+ }
2303
+ get reason() {
2304
+ return this.privReason;
2305
+ }
2306
+ }
2093
2307
  var ResultReason;
2094
2308
  (function(ResultReason2) {
2095
2309
  ResultReason2[ResultReason2["NoMatch"] = 0] = "NoMatch";
@@ -2258,6 +2472,153 @@ class SpeechConfigImpl extends SpeechConfig {
2258
2472
  this.privProperties.setProperty(PropertyId.SpeechServiceConnection_SynthOutputFormat, SpeechSynthesisOutputFormat[format]);
2259
2473
  }
2260
2474
  }
2475
+ class SpeechTranslationConfig extends SpeechConfig {
2476
+ constructor() {
2477
+ super();
2478
+ }
2479
+ static fromSubscription(subscriptionKey, region) {
2480
+ Contracts.throwIfNullOrWhitespace(subscriptionKey, "subscriptionKey");
2481
+ Contracts.throwIfNullOrWhitespace(region, "region");
2482
+ const ret = new SpeechTranslationConfigImpl();
2483
+ ret.properties.setProperty(PropertyId.SpeechServiceConnection_Key, subscriptionKey);
2484
+ ret.properties.setProperty(PropertyId.SpeechServiceConnection_Region, region);
2485
+ return ret;
2486
+ }
2487
+ static fromAuthorizationToken(authorizationToken, region) {
2488
+ Contracts.throwIfNullOrWhitespace(authorizationToken, "authorizationToken");
2489
+ Contracts.throwIfNullOrWhitespace(region, "region");
2490
+ const ret = new SpeechTranslationConfigImpl();
2491
+ ret.properties.setProperty(PropertyId.SpeechServiceAuthorization_Token, authorizationToken);
2492
+ ret.properties.setProperty(PropertyId.SpeechServiceConnection_Region, region);
2493
+ return ret;
2494
+ }
2495
+ static fromHost(hostName, subscriptionKey) {
2496
+ Contracts.throwIfNull(hostName, "hostName");
2497
+ const speechImpl = new SpeechTranslationConfigImpl();
2498
+ speechImpl.setProperty(PropertyId.SpeechServiceConnection_Host, hostName.protocol + "//" + hostName.hostname + (hostName.port === "" ? "" : ":" + hostName.port));
2499
+ if (void 0 !== subscriptionKey) {
2500
+ speechImpl.setProperty(PropertyId.SpeechServiceConnection_Key, subscriptionKey);
2501
+ }
2502
+ return speechImpl;
2503
+ }
2504
+ static fromEndpoint(endpoint, subscriptionKey) {
2505
+ Contracts.throwIfNull(endpoint, "endpoint");
2506
+ Contracts.throwIfNull(subscriptionKey, "subscriptionKey");
2507
+ const ret = new SpeechTranslationConfigImpl();
2508
+ ret.properties.setProperty(PropertyId.SpeechServiceConnection_Endpoint, endpoint.href);
2509
+ ret.properties.setProperty(PropertyId.SpeechServiceConnection_Key, subscriptionKey);
2510
+ return ret;
2511
+ }
2512
+ }
2513
+ class SpeechTranslationConfigImpl extends SpeechTranslationConfig {
2514
+ constructor() {
2515
+ super();
2516
+ this.privSpeechProperties = new PropertyCollection();
2517
+ this.outputFormat = OutputFormat.Simple;
2518
+ }
2519
+ set authorizationToken(value) {
2520
+ Contracts.throwIfNullOrWhitespace(value, "value");
2521
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceAuthorization_Token, value);
2522
+ }
2523
+ set speechRecognitionLanguage(value) {
2524
+ Contracts.throwIfNullOrWhitespace(value, "value");
2525
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceConnection_RecoLanguage, value);
2526
+ }
2527
+ get speechRecognitionLanguage() {
2528
+ return this.privSpeechProperties.getProperty(PropertyId[PropertyId.SpeechServiceConnection_RecoLanguage]);
2529
+ }
2530
+ get subscriptionKey() {
2531
+ return this.privSpeechProperties.getProperty(PropertyId[PropertyId.SpeechServiceConnection_Key]);
2532
+ }
2533
+ get outputFormat() {
2534
+ return OutputFormat[this.privSpeechProperties.getProperty(OutputFormatPropertyName, void 0)];
2535
+ }
2536
+ set outputFormat(value) {
2537
+ this.privSpeechProperties.setProperty(OutputFormatPropertyName, OutputFormat[value]);
2538
+ }
2539
+ get endpointId() {
2540
+ return this.privSpeechProperties.getProperty(PropertyId.SpeechServiceConnection_EndpointId);
2541
+ }
2542
+ set endpointId(value) {
2543
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceConnection_EndpointId, value);
2544
+ }
2545
+ addTargetLanguage(value) {
2546
+ Contracts.throwIfNullOrWhitespace(value, "value");
2547
+ const languages = this.targetLanguages;
2548
+ languages.push(value);
2549
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, languages.join(","));
2550
+ }
2551
+ get targetLanguages() {
2552
+ if (this.privSpeechProperties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, void 0) !== void 0) {
2553
+ return this.privSpeechProperties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(",");
2554
+ } else {
2555
+ return [];
2556
+ }
2557
+ }
2558
+ get voiceName() {
2559
+ return this.getProperty(PropertyId[PropertyId.SpeechServiceConnection_TranslationVoice]);
2560
+ }
2561
+ set voiceName(value) {
2562
+ Contracts.throwIfNullOrWhitespace(value, "value");
2563
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceConnection_TranslationVoice, value);
2564
+ }
2565
+ get region() {
2566
+ return this.privSpeechProperties.getProperty(PropertyId.SpeechServiceConnection_Region);
2567
+ }
2568
+ setProxy(proxyHostName, proxyPort, proxyUserName, proxyPassword) {
2569
+ this.setProperty(PropertyId[PropertyId.SpeechServiceConnection_ProxyHostName], proxyHostName);
2570
+ this.setProperty(PropertyId[PropertyId.SpeechServiceConnection_ProxyPort], proxyPort);
2571
+ this.setProperty(PropertyId[PropertyId.SpeechServiceConnection_ProxyUserName], proxyUserName);
2572
+ this.setProperty(PropertyId[PropertyId.SpeechServiceConnection_ProxyPassword], proxyPassword);
2573
+ }
2574
+ getProperty(name, def) {
2575
+ return this.privSpeechProperties.getProperty(name, def);
2576
+ }
2577
+ setProperty(name, value) {
2578
+ this.privSpeechProperties.setProperty(name, value);
2579
+ }
2580
+ get properties() {
2581
+ return this.privSpeechProperties;
2582
+ }
2583
+ close() {
2584
+ return;
2585
+ }
2586
+ setServiceProperty(name, value) {
2587
+ const currentProperties = JSON.parse(this.privSpeechProperties.getProperty(ServicePropertiesPropertyName, "{}"));
2588
+ currentProperties[name] = value;
2589
+ this.privSpeechProperties.setProperty(ServicePropertiesPropertyName, JSON.stringify(currentProperties));
2590
+ }
2591
+ setProfanity(profanity) {
2592
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceResponse_ProfanityOption, ProfanityOption[profanity]);
2593
+ }
2594
+ enableAudioLogging() {
2595
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceConnection_EnableAudioLogging, "true");
2596
+ }
2597
+ requestWordLevelTimestamps() {
2598
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceResponse_RequestWordLevelTimestamps, "true");
2599
+ }
2600
+ enableDictation() {
2601
+ this.privSpeechProperties.setProperty(ForceDictationPropertyName, "true");
2602
+ }
2603
+ get speechSynthesisLanguage() {
2604
+ return this.privSpeechProperties.getProperty(PropertyId.SpeechServiceConnection_SynthLanguage);
2605
+ }
2606
+ set speechSynthesisLanguage(language) {
2607
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceConnection_SynthLanguage, language);
2608
+ }
2609
+ get speechSynthesisVoiceName() {
2610
+ return this.privSpeechProperties.getProperty(PropertyId.SpeechServiceConnection_SynthVoice);
2611
+ }
2612
+ set speechSynthesisVoiceName(voice) {
2613
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceConnection_SynthVoice, voice);
2614
+ }
2615
+ get speechSynthesisOutputFormat() {
2616
+ return SpeechSynthesisOutputFormat[this.privSpeechProperties.getProperty(PropertyId.SpeechServiceConnection_SynthOutputFormat, void 0)];
2617
+ }
2618
+ set speechSynthesisOutputFormat(format) {
2619
+ this.privSpeechProperties.setProperty(PropertyId.SpeechServiceConnection_SynthOutputFormat, SpeechSynthesisOutputFormat[format]);
2620
+ }
2621
+ }
2261
2622
  class PropertyCollection {
2262
2623
  constructor() {
2263
2624
  this.privKeys = [];
@@ -2386,48 +2747,466 @@ var PropertyId;
2386
2747
  PropertyId2[PropertyId2["PronunciationAssessment_Params"] = 65] = "PronunciationAssessment_Params";
2387
2748
  PropertyId2[PropertyId2["SpeakerRecognition_Api_Version"] = 66] = "SpeakerRecognition_Api_Version";
2388
2749
  })(PropertyId || (PropertyId = {}));
2389
- var CancellationErrorCode;
2390
- (function(CancellationErrorCode2) {
2391
- CancellationErrorCode2[CancellationErrorCode2["NoError"] = 0] = "NoError";
2392
- CancellationErrorCode2[CancellationErrorCode2["AuthenticationFailure"] = 1] = "AuthenticationFailure";
2393
- CancellationErrorCode2[CancellationErrorCode2["BadRequestParameters"] = 2] = "BadRequestParameters";
2394
- CancellationErrorCode2[CancellationErrorCode2["TooManyRequests"] = 3] = "TooManyRequests";
2395
- CancellationErrorCode2[CancellationErrorCode2["ConnectionFailure"] = 4] = "ConnectionFailure";
2396
- CancellationErrorCode2[CancellationErrorCode2["ServiceTimeout"] = 5] = "ServiceTimeout";
2397
- CancellationErrorCode2[CancellationErrorCode2["ServiceError"] = 6] = "ServiceError";
2398
- CancellationErrorCode2[CancellationErrorCode2["RuntimeError"] = 7] = "RuntimeError";
2399
- CancellationErrorCode2[CancellationErrorCode2["Forbidden"] = 8] = "Forbidden";
2400
- })(CancellationErrorCode || (CancellationErrorCode = {}));
2401
- class QueryParameterNames {
2402
- }
2403
- QueryParameterNames.BotId = "botid";
2404
- QueryParameterNames.CustomSpeechDeploymentId = "cid";
2405
- QueryParameterNames.CustomVoiceDeploymentId = "deploymentId";
2406
- QueryParameterNames.EnableAudioLogging = "storeAudio";
2407
- QueryParameterNames.EnableLanguageId = "lidEnabled";
2408
- QueryParameterNames.EnableWordLevelTimestamps = "wordLevelTimestamps";
2409
- QueryParameterNames.EndSilenceTimeoutMs = "endSilenceTimeoutMs";
2410
- QueryParameterNames.SegmentationSilenceTimeoutMs = "segmentationSilenceTimeoutMs";
2411
- QueryParameterNames.Format = "format";
2412
- QueryParameterNames.InitialSilenceTimeoutMs = "initialSilenceTimeoutMs";
2413
- QueryParameterNames.Language = "language";
2414
- QueryParameterNames.Profanity = "profanity";
2415
- QueryParameterNames.RequestBotStatusMessages = "enableBotMessageStatus";
2416
- QueryParameterNames.StableIntermediateThreshold = "stableIntermediateThreshold";
2417
- QueryParameterNames.StableTranslation = "stableTranslation";
2418
- QueryParameterNames.TestHooks = "testhooks";
2419
- QueryParameterNames.Postprocessing = "postprocessing";
2420
- class ConnectionFactoryBase {
2421
- static getHostSuffix(region) {
2422
- if (!!region) {
2423
- if (region.toLowerCase().startsWith("china")) {
2424
- return ".azure.cn";
2750
+ var __awaiter$e = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
2751
+ function adopt(value) {
2752
+ return value instanceof P ? value : new P(function(resolve) {
2753
+ resolve(value);
2754
+ });
2755
+ }
2756
+ return new (P || (P = Promise))(function(resolve, reject) {
2757
+ function fulfilled(value) {
2758
+ try {
2759
+ step(generator.next(value));
2760
+ } catch (e) {
2761
+ reject(e);
2425
2762
  }
2426
- if (region.toLowerCase().startsWith("usgov")) {
2427
- return ".azure.us";
2763
+ }
2764
+ function rejected(value) {
2765
+ try {
2766
+ step(generator["throw"](value));
2767
+ } catch (e) {
2768
+ reject(e);
2428
2769
  }
2429
2770
  }
2430
- return ".microsoft.com";
2771
+ function step(result) {
2772
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
2773
+ }
2774
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
2775
+ });
2776
+ };
2777
+ class Recognizer {
2778
+ constructor(audioConfig, properties, connectionFactory) {
2779
+ this.audioConfig = audioConfig !== void 0 ? audioConfig : AudioConfig.fromDefaultMicrophoneInput();
2780
+ this.privDisposed = false;
2781
+ this.privProperties = properties.clone();
2782
+ this.privConnectionFactory = connectionFactory;
2783
+ this.implCommonRecognizerSetup();
2784
+ }
2785
+ close(cb, errorCb) {
2786
+ Contracts.throwIfDisposed(this.privDisposed);
2787
+ marshalPromiseToCallbacks(this.dispose(true), cb, errorCb);
2788
+ }
2789
+ get internalData() {
2790
+ return this.privReco;
2791
+ }
2792
+ dispose(disposing) {
2793
+ return __awaiter$e(this, void 0, void 0, function* () {
2794
+ if (this.privDisposed) {
2795
+ return;
2796
+ }
2797
+ this.privDisposed = true;
2798
+ if (disposing) {
2799
+ if (this.privReco) {
2800
+ yield this.privReco.audioSource.turnOff();
2801
+ yield this.privReco.dispose();
2802
+ }
2803
+ }
2804
+ });
2805
+ }
2806
+ static get telemetryEnabled() {
2807
+ return ServiceRecognizerBase.telemetryDataEnabled;
2808
+ }
2809
+ static enableTelemetry(enabled) {
2810
+ ServiceRecognizerBase.telemetryDataEnabled = enabled;
2811
+ }
2812
+ implCommonRecognizerSetup() {
2813
+ let osPlatform = typeof window !== "undefined" ? "Browser" : "Node";
2814
+ let osName = "unknown";
2815
+ let osVersion = "unknown";
2816
+ if (typeof navigator !== "undefined") {
2817
+ osPlatform = osPlatform + "/" + navigator.platform;
2818
+ osName = navigator.userAgent;
2819
+ osVersion = navigator.appVersion;
2820
+ }
2821
+ const recognizerConfig = this.createRecognizerConfig(new SpeechServiceConfig(new Context(new OS(osPlatform, osName, osVersion))));
2822
+ this.privReco = this.createServiceRecognizer(Recognizer.getAuthFromProperties(this.privProperties), this.privConnectionFactory, this.audioConfig, recognizerConfig);
2823
+ }
2824
+ recognizeOnceAsyncImpl(recognitionMode) {
2825
+ return __awaiter$e(this, void 0, void 0, function* () {
2826
+ Contracts.throwIfDisposed(this.privDisposed);
2827
+ const ret = new Deferred();
2828
+ yield this.implRecognizerStop();
2829
+ yield this.privReco.recognize(recognitionMode, ret.resolve, ret.reject);
2830
+ const result = yield ret.promise;
2831
+ yield this.implRecognizerStop();
2832
+ return result;
2833
+ });
2834
+ }
2835
+ startContinuousRecognitionAsyncImpl(recognitionMode) {
2836
+ return __awaiter$e(this, void 0, void 0, function* () {
2837
+ Contracts.throwIfDisposed(this.privDisposed);
2838
+ yield this.implRecognizerStop();
2839
+ yield this.privReco.recognize(recognitionMode, void 0, void 0);
2840
+ });
2841
+ }
2842
+ stopContinuousRecognitionAsyncImpl() {
2843
+ return __awaiter$e(this, void 0, void 0, function* () {
2844
+ Contracts.throwIfDisposed(this.privDisposed);
2845
+ yield this.implRecognizerStop();
2846
+ });
2847
+ }
2848
+ implRecognizerStop() {
2849
+ return __awaiter$e(this, void 0, void 0, function* () {
2850
+ if (this.privReco) {
2851
+ yield this.privReco.stopRecognizing();
2852
+ }
2853
+ return;
2854
+ });
2855
+ }
2856
+ static getAuthFromProperties(properties) {
2857
+ const subscriptionKey = properties.getProperty(PropertyId.SpeechServiceConnection_Key, void 0);
2858
+ const authentication = subscriptionKey && subscriptionKey !== "" ? new CognitiveSubscriptionKeyAuthentication(subscriptionKey) : new CognitiveTokenAuthentication(() => {
2859
+ const authorizationToken = properties.getProperty(PropertyId.SpeechServiceAuthorization_Token, void 0);
2860
+ return Promise.resolve(authorizationToken);
2861
+ }, () => {
2862
+ const authorizationToken = properties.getProperty(PropertyId.SpeechServiceAuthorization_Token, void 0);
2863
+ return Promise.resolve(authorizationToken);
2864
+ });
2865
+ return authentication;
2866
+ }
2867
+ }
2868
+ class ConnectionMessageImpl {
2869
+ constructor(message) {
2870
+ this.privConnectionMessage = message;
2871
+ this.privProperties = new PropertyCollection();
2872
+ if (!!this.privConnectionMessage.headers[HeaderNames.ConnectionId]) {
2873
+ this.privProperties.setProperty(PropertyId.Speech_SessionId, this.privConnectionMessage.headers[HeaderNames.ConnectionId]);
2874
+ }
2875
+ Object.keys(this.privConnectionMessage.headers).forEach((header) => {
2876
+ this.privProperties.setProperty(header, this.privConnectionMessage.headers[header]);
2877
+ });
2878
+ }
2879
+ get path() {
2880
+ return this.privConnectionMessage.headers[Object.keys(this.privConnectionMessage.headers).find((key) => key.toLowerCase() === "path".toLowerCase())];
2881
+ }
2882
+ get isTextMessage() {
2883
+ return this.privConnectionMessage.messageType === MessageType.Text;
2884
+ }
2885
+ get isBinaryMessage() {
2886
+ return this.privConnectionMessage.messageType === MessageType.Binary;
2887
+ }
2888
+ get TextMessage() {
2889
+ return this.privConnectionMessage.textBody;
2890
+ }
2891
+ get binaryMessage() {
2892
+ return this.privConnectionMessage.binaryBody;
2893
+ }
2894
+ get properties() {
2895
+ return this.privProperties;
2896
+ }
2897
+ toString() {
2898
+ return "";
2899
+ }
2900
+ }
2901
+ class Connection {
2902
+ static fromRecognizer(recognizer) {
2903
+ const recoBase = recognizer.internalData;
2904
+ const ret = new Connection();
2905
+ ret.privInternalData = recoBase;
2906
+ ret.setupEvents();
2907
+ return ret;
2908
+ }
2909
+ static fromSynthesizer(synthesizer) {
2910
+ const synthBase = synthesizer.internalData;
2911
+ const ret = new Connection();
2912
+ ret.privInternalData = synthBase;
2913
+ ret.setupEvents();
2914
+ return ret;
2915
+ }
2916
+ openConnection(cb, err) {
2917
+ marshalPromiseToCallbacks(this.privInternalData.connect(), cb, err);
2918
+ }
2919
+ closeConnection(cb, err) {
2920
+ if (this.privInternalData instanceof SynthesisAdapterBase) {
2921
+ throw new Error("Disconnecting a synthesizer's connection is currently not supported");
2922
+ } else {
2923
+ marshalPromiseToCallbacks(this.privInternalData.disconnect(), cb, err);
2924
+ }
2925
+ }
2926
+ setMessageProperty(path, propertyName, propertyValue) {
2927
+ Contracts.throwIfNullOrWhitespace(propertyName, "propertyName");
2928
+ if (this.privInternalData instanceof ServiceRecognizerBase) {
2929
+ if (path.toLowerCase() !== "speech.context") {
2930
+ throw new Error("Only speech.context message property sets are currently supported for recognizer");
2931
+ } else {
2932
+ this.privInternalData.speechContext.setSection(propertyName, propertyValue);
2933
+ }
2934
+ } else if (this.privInternalData instanceof SynthesisAdapterBase) {
2935
+ if (path.toLowerCase() !== "synthesis.context") {
2936
+ throw new Error("Only synthesis.context message property sets are currently supported for synthesizer");
2937
+ } else {
2938
+ this.privInternalData.synthesisContext.setSection(propertyName, propertyValue);
2939
+ }
2940
+ }
2941
+ }
2942
+ sendMessageAsync(path, payload, success, error) {
2943
+ marshalPromiseToCallbacks(this.privInternalData.sendNetworkMessage(path, payload), success, error);
2944
+ }
2945
+ close() {
2946
+ }
2947
+ setupEvents() {
2948
+ this.privEventListener = this.privInternalData.connectionEvents.attach((connectionEvent) => {
2949
+ if (connectionEvent.name === "ConnectionEstablishedEvent") {
2950
+ if (!!this.connected) {
2951
+ this.connected(new ConnectionEventArgs(connectionEvent.connectionId));
2952
+ }
2953
+ } else if (connectionEvent.name === "ConnectionClosedEvent") {
2954
+ if (!!this.disconnected) {
2955
+ this.disconnected(new ConnectionEventArgs(connectionEvent.connectionId));
2956
+ }
2957
+ } else if (connectionEvent.name === "ConnectionMessageSentEvent") {
2958
+ if (!!this.messageSent) {
2959
+ this.messageSent(new ConnectionMessageEventArgs(new ConnectionMessageImpl(connectionEvent.message)));
2960
+ }
2961
+ } else if (connectionEvent.name === "ConnectionMessageReceivedEvent") {
2962
+ if (!!this.messageReceived) {
2963
+ this.messageReceived(new ConnectionMessageEventArgs(new ConnectionMessageImpl(connectionEvent.message)));
2964
+ }
2965
+ }
2966
+ });
2967
+ this.privServiceEventListener = this.privInternalData.serviceEvents.attach((e) => {
2968
+ if (!!this.receivedServiceMessage) {
2969
+ this.receivedServiceMessage(new ServiceEventArgs(e.jsonString, e.name));
2970
+ }
2971
+ });
2972
+ }
2973
+ }
2974
+ var __awaiter$d = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
2975
+ function adopt(value) {
2976
+ return value instanceof P ? value : new P(function(resolve) {
2977
+ resolve(value);
2978
+ });
2979
+ }
2980
+ return new (P || (P = Promise))(function(resolve, reject) {
2981
+ function fulfilled(value) {
2982
+ try {
2983
+ step(generator.next(value));
2984
+ } catch (e) {
2985
+ reject(e);
2986
+ }
2987
+ }
2988
+ function rejected(value) {
2989
+ try {
2990
+ step(generator["throw"](value));
2991
+ } catch (e) {
2992
+ reject(e);
2993
+ }
2994
+ }
2995
+ function step(result) {
2996
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
2997
+ }
2998
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
2999
+ });
3000
+ };
3001
+ class TranslationRecognizer extends Recognizer {
3002
+ constructor(speechConfig, audioConfig) {
3003
+ const configImpl = speechConfig;
3004
+ Contracts.throwIfNull(configImpl, "speechConfig");
3005
+ super(audioConfig, configImpl.properties, new TranslationConnectionFactory());
3006
+ this.privDisposedTranslationRecognizer = false;
3007
+ if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, void 0) !== void 0) {
3008
+ Contracts.throwIfNullOrWhitespace(this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice), PropertyId[PropertyId.SpeechServiceConnection_TranslationVoice]);
3009
+ }
3010
+ Contracts.throwIfNullOrWhitespace(this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages), PropertyId[PropertyId.SpeechServiceConnection_TranslationToLanguages]);
3011
+ Contracts.throwIfNullOrWhitespace(this.properties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage), PropertyId[PropertyId.SpeechServiceConnection_RecoLanguage]);
3012
+ }
3013
+ get speechRecognitionLanguage() {
3014
+ Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);
3015
+ return this.properties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage);
3016
+ }
3017
+ get targetLanguages() {
3018
+ Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);
3019
+ return this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(",");
3020
+ }
3021
+ get voiceName() {
3022
+ Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);
3023
+ return this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, void 0);
3024
+ }
3025
+ get properties() {
3026
+ return this.privProperties;
3027
+ }
3028
+ get authorizationToken() {
3029
+ return this.properties.getProperty(PropertyId.SpeechServiceAuthorization_Token);
3030
+ }
3031
+ set authorizationToken(value) {
3032
+ this.properties.setProperty(PropertyId.SpeechServiceAuthorization_Token, value);
3033
+ }
3034
+ recognizeOnceAsync(cb, err) {
3035
+ Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);
3036
+ marshalPromiseToCallbacks(this.recognizeOnceAsyncImpl(RecognitionMode.Conversation), cb, err);
3037
+ }
3038
+ startContinuousRecognitionAsync(cb, err) {
3039
+ marshalPromiseToCallbacks(this.startContinuousRecognitionAsyncImpl(RecognitionMode.Conversation), cb, err);
3040
+ }
3041
+ stopContinuousRecognitionAsync(cb, err) {
3042
+ marshalPromiseToCallbacks(this.stopContinuousRecognitionAsyncImpl(), cb, err);
3043
+ }
3044
+ removeTargetLanguage(lang) {
3045
+ Contracts.throwIfNullOrUndefined(lang, "language to be removed");
3046
+ if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, void 0) !== void 0) {
3047
+ const languages = this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(",");
3048
+ const index = languages.indexOf(lang);
3049
+ if (index > -1) {
3050
+ languages.splice(index, 1);
3051
+ this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, languages.join(","));
3052
+ this.updateLanguages(languages);
3053
+ }
3054
+ }
3055
+ }
3056
+ addTargetLanguage(lang) {
3057
+ Contracts.throwIfNullOrUndefined(lang, "language to be added");
3058
+ let languages = [];
3059
+ if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, void 0) !== void 0) {
3060
+ languages = this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(",");
3061
+ if (!languages.includes(lang)) {
3062
+ languages.push(lang);
3063
+ this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, languages.join(","));
3064
+ }
3065
+ } else {
3066
+ this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, lang);
3067
+ languages = [lang];
3068
+ }
3069
+ this.updateLanguages(languages);
3070
+ }
3071
+ close(cb, errorCb) {
3072
+ Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);
3073
+ marshalPromiseToCallbacks(this.dispose(true), cb, errorCb);
3074
+ }
3075
+ onConnection() {
3076
+ }
3077
+ onDisconnection() {
3078
+ return __awaiter$d(this, void 0, void 0, function* () {
3079
+ });
3080
+ }
3081
+ dispose(disposing) {
3082
+ const _super = Object.create(null, {
3083
+ dispose: { get: () => super.dispose }
3084
+ });
3085
+ return __awaiter$d(this, void 0, void 0, function* () {
3086
+ if (this.privDisposedTranslationRecognizer) {
3087
+ return;
3088
+ }
3089
+ this.privDisposedTranslationRecognizer = true;
3090
+ if (disposing) {
3091
+ yield this.implRecognizerStop();
3092
+ yield _super.dispose.call(this, disposing);
3093
+ }
3094
+ });
3095
+ }
3096
+ createRecognizerConfig(speechConfig) {
3097
+ return new RecognizerConfig(speechConfig, this.properties);
3098
+ }
3099
+ createServiceRecognizer(authentication, connectionFactory, audioConfig, recognizerConfig) {
3100
+ const configImpl = audioConfig;
3101
+ return new TranslationServiceRecognizer(authentication, connectionFactory, configImpl, recognizerConfig, this);
3102
+ }
3103
+ updateLanguages(languages) {
3104
+ const conn = Connection.fromRecognizer(this);
3105
+ if (!!conn) {
3106
+ conn.setMessageProperty("speech.context", "translationcontext", { to: languages });
3107
+ conn.sendMessageAsync("event", JSON.stringify({
3108
+ id: "translation",
3109
+ name: "updateLanguage",
3110
+ to: languages
3111
+ }));
3112
+ }
3113
+ }
3114
+ }
3115
+ class Translations {
3116
+ constructor() {
3117
+ this.privMap = new PropertyCollection();
3118
+ }
3119
+ get languages() {
3120
+ return this.privMap.keys;
3121
+ }
3122
+ get(key, def) {
3123
+ return this.privMap.getProperty(key, def);
3124
+ }
3125
+ set(key, value) {
3126
+ this.privMap.setProperty(key, value);
3127
+ }
3128
+ }
3129
+ class TranslationRecognitionCanceledEventArgs {
3130
+ constructor(sessionid, cancellationReason, errorDetails, errorCode, result) {
3131
+ this.privCancelReason = cancellationReason;
3132
+ this.privErrorDetails = errorDetails;
3133
+ this.privResult = result;
3134
+ this.privSessionId = sessionid;
3135
+ this.privErrorCode = errorCode;
3136
+ }
3137
+ get result() {
3138
+ return this.privResult;
3139
+ }
3140
+ get sessionId() {
3141
+ return this.privSessionId;
3142
+ }
3143
+ get reason() {
3144
+ return this.privCancelReason;
3145
+ }
3146
+ get errorCode() {
3147
+ return this.privErrorCode;
3148
+ }
3149
+ get errorDetails() {
3150
+ return this.privErrorDetails;
3151
+ }
3152
+ }
3153
+ var CancellationErrorCode;
3154
+ (function(CancellationErrorCode2) {
3155
+ CancellationErrorCode2[CancellationErrorCode2["NoError"] = 0] = "NoError";
3156
+ CancellationErrorCode2[CancellationErrorCode2["AuthenticationFailure"] = 1] = "AuthenticationFailure";
3157
+ CancellationErrorCode2[CancellationErrorCode2["BadRequestParameters"] = 2] = "BadRequestParameters";
3158
+ CancellationErrorCode2[CancellationErrorCode2["TooManyRequests"] = 3] = "TooManyRequests";
3159
+ CancellationErrorCode2[CancellationErrorCode2["ConnectionFailure"] = 4] = "ConnectionFailure";
3160
+ CancellationErrorCode2[CancellationErrorCode2["ServiceTimeout"] = 5] = "ServiceTimeout";
3161
+ CancellationErrorCode2[CancellationErrorCode2["ServiceError"] = 6] = "ServiceError";
3162
+ CancellationErrorCode2[CancellationErrorCode2["RuntimeError"] = 7] = "RuntimeError";
3163
+ CancellationErrorCode2[CancellationErrorCode2["Forbidden"] = 8] = "Forbidden";
3164
+ })(CancellationErrorCode || (CancellationErrorCode = {}));
3165
+ class ConnectionEventArgs extends SessionEventArgs {
3166
+ }
3167
+ class ServiceEventArgs extends SessionEventArgs {
3168
+ constructor(json, name, sessionId) {
3169
+ super(sessionId);
3170
+ this.privJsonResult = json;
3171
+ this.privEventName = name;
3172
+ }
3173
+ get jsonString() {
3174
+ return this.privJsonResult;
3175
+ }
3176
+ get eventName() {
3177
+ return this.privEventName;
3178
+ }
3179
+ }
3180
+ class QueryParameterNames {
3181
+ }
3182
+ QueryParameterNames.BotId = "botid";
3183
+ QueryParameterNames.CustomSpeechDeploymentId = "cid";
3184
+ QueryParameterNames.CustomVoiceDeploymentId = "deploymentId";
3185
+ QueryParameterNames.EnableAudioLogging = "storeAudio";
3186
+ QueryParameterNames.EnableLanguageId = "lidEnabled";
3187
+ QueryParameterNames.EnableWordLevelTimestamps = "wordLevelTimestamps";
3188
+ QueryParameterNames.EndSilenceTimeoutMs = "endSilenceTimeoutMs";
3189
+ QueryParameterNames.SegmentationSilenceTimeoutMs = "segmentationSilenceTimeoutMs";
3190
+ QueryParameterNames.Format = "format";
3191
+ QueryParameterNames.InitialSilenceTimeoutMs = "initialSilenceTimeoutMs";
3192
+ QueryParameterNames.Language = "language";
3193
+ QueryParameterNames.Profanity = "profanity";
3194
+ QueryParameterNames.RequestBotStatusMessages = "enableBotMessageStatus";
3195
+ QueryParameterNames.StableIntermediateThreshold = "stableIntermediateThreshold";
3196
+ QueryParameterNames.StableTranslation = "stableTranslation";
3197
+ QueryParameterNames.TestHooks = "testhooks";
3198
+ QueryParameterNames.Postprocessing = "postprocessing";
3199
+ class ConnectionFactoryBase {
3200
+ static getHostSuffix(region) {
3201
+ if (!!region) {
3202
+ if (region.toLowerCase().startsWith("china")) {
3203
+ return ".azure.cn";
3204
+ }
3205
+ if (region.toLowerCase().startsWith("usgov")) {
3206
+ return ".azure.us";
3207
+ }
3208
+ }
3209
+ return ".microsoft.com";
2431
3210
  }
2432
3211
  setCommonUrlParams(config, queryParams, endpoint) {
2433
3212
  const propertyIdToParameterMap = /* @__PURE__ */ new Map([
@@ -2461,7 +3240,18 @@ var ProfanityOption;
2461
3240
  ProfanityOption2[ProfanityOption2["Removed"] = 1] = "Removed";
2462
3241
  ProfanityOption2[ProfanityOption2["Raw"] = 2] = "Raw";
2463
3242
  })(ProfanityOption || (ProfanityOption = {}));
2464
- var __awaiter$9 = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
3243
+ class ConnectionMessageEventArgs {
3244
+ constructor(message) {
3245
+ this.privConnectionMessage = message;
3246
+ }
3247
+ get message() {
3248
+ return this.privConnectionMessage;
3249
+ }
3250
+ toString() {
3251
+ return "Message: " + this.privConnectionMessage.toString();
3252
+ }
3253
+ }
3254
+ var __awaiter$c = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
2465
3255
  function adopt(value) {
2466
3256
  return value instanceof P ? value : new P(function(resolve) {
2467
3257
  resolve(value);
@@ -2677,7 +3467,7 @@ class SpeechSynthesizer {
2677
3467
  this.speakImpl(ssml, true, cb, err, stream);
2678
3468
  }
2679
3469
  getVoicesAsync(locale = "") {
2680
- return __awaiter$9(this, void 0, void 0, function* () {
3470
+ return __awaiter$c(this, void 0, void 0, function* () {
2681
3471
  return this.getVoices(locale);
2682
3472
  });
2683
3473
  }
@@ -2689,7 +3479,7 @@ class SpeechSynthesizer {
2689
3479
  return this.privAdapter;
2690
3480
  }
2691
3481
  dispose(disposing) {
2692
- return __awaiter$9(this, void 0, void 0, function* () {
3482
+ return __awaiter$c(this, void 0, void 0, function* () {
2693
3483
  if (this.privDisposed) {
2694
3484
  return;
2695
3485
  }
@@ -2778,7 +3568,7 @@ class SpeechSynthesizer {
2778
3568
  }
2779
3569
  }
2780
3570
  getVoices(locale) {
2781
- return __awaiter$9(this, void 0, void 0, function* () {
3571
+ return __awaiter$c(this, void 0, void 0, function* () {
2782
3572
  const requestId = createNoDashGuid();
2783
3573
  const response = yield this.privRestAdapter.getVoicesList(requestId);
2784
3574
  if (response.ok && Array.isArray(response.json)) {
@@ -2793,7 +3583,7 @@ class SpeechSynthesizer {
2793
3583
  });
2794
3584
  }
2795
3585
  adapterSpeak() {
2796
- return __awaiter$9(this, void 0, void 0, function* () {
3586
+ return __awaiter$c(this, void 0, void 0, function* () {
2797
3587
  if (!this.privDisposed && !this.privSynthesizing) {
2798
3588
  this.privSynthesizing = true;
2799
3589
  const request = yield this.synthesisRequestQueue.dequeue();
@@ -2984,7 +3774,7 @@ class VoiceInfo {
2984
3774
  return this.privVoicePath;
2985
3775
  }
2986
3776
  }
2987
- var __awaiter$8 = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
3777
+ var __awaiter$b = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
2988
3778
  function adopt(value) {
2989
3779
  return value instanceof P ? value : new P(function(resolve) {
2990
3780
  resolve(value);
@@ -3189,7 +3979,7 @@ class SpeakerAudioDestination {
3189
3979
  return this.privAudio;
3190
3980
  }
3191
3981
  updateSourceBuffer() {
3192
- return __awaiter$8(this, void 0, void 0, function* () {
3982
+ return __awaiter$b(this, void 0, void 0, function* () {
3193
3983
  if (this.privAudioBuffer !== void 0 && this.privAudioBuffer.length > 0 && this.sourceBufferAvailable()) {
3194
3984
  this.privAppendingToBuffer = true;
3195
3985
  const binary = this.privAudioBuffer.shift();
@@ -3207,7 +3997,7 @@ class SpeakerAudioDestination {
3207
3997
  });
3208
3998
  }
3209
3999
  handleSourceBufferUpdateEnd() {
3210
- return __awaiter$8(this, void 0, void 0, function* () {
4000
+ return __awaiter$b(this, void 0, void 0, function* () {
3211
4001
  if (this.canEndStream() && this.sourceBufferAvailable()) {
3212
4002
  this.privMediaSource.endOfStream();
3213
4003
  yield this.notifyPlayback();
@@ -3215,7 +4005,7 @@ class SpeakerAudioDestination {
3215
4005
  });
3216
4006
  }
3217
4007
  notifyPlayback() {
3218
- return __awaiter$8(this, void 0, void 0, function* () {
4008
+ return __awaiter$b(this, void 0, void 0, function* () {
3219
4009
  if (!this.privPlaybackStarted && this.privAudio !== void 0) {
3220
4010
  this.privPlaybackStarted = true;
3221
4011
  if (!!this.onAudioStart) {
@@ -3239,14 +4029,92 @@ class SpeakerAudioDestination {
3239
4029
  return this.privSourceBuffer !== void 0 && !this.privSourceBuffer.updating;
3240
4030
  }
3241
4031
  }
3242
- class SpeechConnectionMessage extends ConnectionMessage {
3243
- constructor(messageType, path, requestId, contentType, body, streamId, additionalHeaders, id) {
3244
- if (!path) {
3245
- throw new ArgumentNullError("path");
3246
- }
3247
- if (!requestId) {
3248
- throw new ArgumentNullError("requestId");
3249
- }
4032
+ class SpeechRecognitionEvent extends PlatformEvent {
4033
+ constructor(eventName, requestId, sessionId, eventType = EventType.Info) {
4034
+ super(eventName, eventType);
4035
+ this.privRequestId = requestId;
4036
+ this.privSessionId = sessionId;
4037
+ }
4038
+ get requestId() {
4039
+ return this.privRequestId;
4040
+ }
4041
+ get sessionId() {
4042
+ return this.privSessionId;
4043
+ }
4044
+ }
4045
+ class RecognitionTriggeredEvent extends SpeechRecognitionEvent {
4046
+ constructor(requestId, sessionId, audioSourceId, audioNodeId) {
4047
+ super("RecognitionTriggeredEvent", requestId, sessionId);
4048
+ this.privAudioSourceId = audioSourceId;
4049
+ this.privAudioNodeId = audioNodeId;
4050
+ }
4051
+ get audioSourceId() {
4052
+ return this.privAudioSourceId;
4053
+ }
4054
+ get audioNodeId() {
4055
+ return this.privAudioNodeId;
4056
+ }
4057
+ }
4058
+ class ListeningStartedEvent extends SpeechRecognitionEvent {
4059
+ constructor(requestId, sessionId, audioSourceId, audioNodeId) {
4060
+ super("ListeningStartedEvent", requestId, sessionId);
4061
+ this.privAudioSourceId = audioSourceId;
4062
+ this.privAudioNodeId = audioNodeId;
4063
+ }
4064
+ get audioSourceId() {
4065
+ return this.privAudioSourceId;
4066
+ }
4067
+ get audioNodeId() {
4068
+ return this.privAudioNodeId;
4069
+ }
4070
+ }
4071
+ class ConnectingToServiceEvent extends SpeechRecognitionEvent {
4072
+ constructor(requestId, authFetchEventid, sessionId) {
4073
+ super("ConnectingToServiceEvent", requestId, sessionId);
4074
+ this.privAuthFetchEventid = authFetchEventid;
4075
+ }
4076
+ get authFetchEventid() {
4077
+ return this.privAuthFetchEventid;
4078
+ }
4079
+ }
4080
+ class RecognitionStartedEvent extends SpeechRecognitionEvent {
4081
+ constructor(requestId, audioSourceId, audioNodeId, authFetchEventId, sessionId) {
4082
+ super("RecognitionStartedEvent", requestId, sessionId);
4083
+ this.privAudioSourceId = audioSourceId;
4084
+ this.privAudioNodeId = audioNodeId;
4085
+ this.privAuthFetchEventId = authFetchEventId;
4086
+ }
4087
+ get audioSourceId() {
4088
+ return this.privAudioSourceId;
4089
+ }
4090
+ get audioNodeId() {
4091
+ return this.privAudioNodeId;
4092
+ }
4093
+ get authFetchEventId() {
4094
+ return this.privAuthFetchEventId;
4095
+ }
4096
+ }
4097
+ var RecognitionCompletionStatus;
4098
+ (function(RecognitionCompletionStatus2) {
4099
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["Success"] = 0] = "Success";
4100
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["AudioSourceError"] = 1] = "AudioSourceError";
4101
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["AudioSourceTimeout"] = 2] = "AudioSourceTimeout";
4102
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["AuthTokenFetchError"] = 3] = "AuthTokenFetchError";
4103
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["AuthTokenFetchTimeout"] = 4] = "AuthTokenFetchTimeout";
4104
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["UnAuthorized"] = 5] = "UnAuthorized";
4105
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["ConnectTimeout"] = 6] = "ConnectTimeout";
4106
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["ConnectError"] = 7] = "ConnectError";
4107
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["ClientRecognitionActivityTimeout"] = 8] = "ClientRecognitionActivityTimeout";
4108
+ RecognitionCompletionStatus2[RecognitionCompletionStatus2["UnknownError"] = 9] = "UnknownError";
4109
+ })(RecognitionCompletionStatus || (RecognitionCompletionStatus = {}));
4110
+ class SpeechConnectionMessage extends ConnectionMessage {
4111
+ constructor(messageType, path, requestId, contentType, body, streamId, additionalHeaders, id) {
4112
+ if (!path) {
4113
+ throw new ArgumentNullError("path");
4114
+ }
4115
+ if (!requestId) {
4116
+ throw new ArgumentNullError("requestId");
4117
+ }
3250
4118
  const headers = {};
3251
4119
  headers[HeaderNames.Path] = path;
3252
4120
  headers[HeaderNames.RequestId] = requestId;
@@ -3316,227 +4184,1775 @@ class SpeechConnectionMessage extends ConnectionMessage {
3316
4184
  return new SpeechConnectionMessage(message.messageType, path, requestId, contentType, message.body, streamId, additionalHeaders, message.id);
3317
4185
  }
3318
4186
  }
3319
- var RecognitionMode;
3320
- (function(RecognitionMode2) {
3321
- RecognitionMode2[RecognitionMode2["Interactive"] = 0] = "Interactive";
3322
- RecognitionMode2[RecognitionMode2["Conversation"] = 1] = "Conversation";
3323
- RecognitionMode2[RecognitionMode2["Dictation"] = 2] = "Dictation";
3324
- })(RecognitionMode || (RecognitionMode = {}));
3325
- var SpeechResultFormat;
3326
- (function(SpeechResultFormat2) {
3327
- SpeechResultFormat2[SpeechResultFormat2["Simple"] = 0] = "Simple";
3328
- SpeechResultFormat2[SpeechResultFormat2["Detailed"] = 1] = "Detailed";
3329
- })(SpeechResultFormat || (SpeechResultFormat = {}));
3330
- class SpeechServiceConfig {
3331
- constructor(context) {
3332
- this.context = context;
4187
+ var __awaiter$a = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
4188
+ function adopt(value) {
4189
+ return value instanceof P ? value : new P(function(resolve) {
4190
+ resolve(value);
4191
+ });
3333
4192
  }
3334
- serialize() {
3335
- return JSON.stringify(this, (key, value) => {
3336
- if (value && typeof value === "object") {
3337
- const replacement = {};
3338
- for (const k in value) {
3339
- if (Object.hasOwnProperty.call(value, k)) {
3340
- replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
3341
- }
4193
+ return new (P || (P = Promise))(function(resolve, reject) {
4194
+ function fulfilled(value) {
4195
+ try {
4196
+ step(generator.next(value));
4197
+ } catch (e) {
4198
+ reject(e);
4199
+ }
4200
+ }
4201
+ function rejected(value) {
4202
+ try {
4203
+ step(generator["throw"](value));
4204
+ } catch (e) {
4205
+ reject(e);
4206
+ }
4207
+ }
4208
+ function step(result) {
4209
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
4210
+ }
4211
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
4212
+ });
4213
+ };
4214
+ class ServiceRecognizerBase {
4215
+ constructor(authentication, connectionFactory, audioSource, recognizerConfig, recognizer) {
4216
+ this.privConnectionConfigurationPromise = void 0;
4217
+ this.privConnectionPromise = void 0;
4218
+ this.privSetTimeout = setTimeout;
4219
+ this.privIsLiveAudio = false;
4220
+ this.recognizeOverride = void 0;
4221
+ this.disconnectOverride = void 0;
4222
+ this.receiveMessageOverride = void 0;
4223
+ this.sendPrePayloadJSONOverride = void 0;
4224
+ this.postConnectImplOverride = void 0;
4225
+ this.configConnectionOverride = void 0;
4226
+ if (!authentication) {
4227
+ throw new ArgumentNullError("authentication");
4228
+ }
4229
+ if (!connectionFactory) {
4230
+ throw new ArgumentNullError("connectionFactory");
4231
+ }
4232
+ if (!audioSource) {
4233
+ throw new ArgumentNullError("audioSource");
4234
+ }
4235
+ if (!recognizerConfig) {
4236
+ throw new ArgumentNullError("recognizerConfig");
4237
+ }
4238
+ this.privMustReportEndOfStream = false;
4239
+ this.privAuthentication = authentication;
4240
+ this.privConnectionFactory = connectionFactory;
4241
+ this.privAudioSource = audioSource;
4242
+ this.privRecognizerConfig = recognizerConfig;
4243
+ this.privIsDisposed = false;
4244
+ this.privRecognizer = recognizer;
4245
+ this.privRequestSession = new RequestSession(this.privAudioSource.id());
4246
+ this.privConnectionEvents = new EventSource();
4247
+ this.privServiceEvents = new EventSource();
4248
+ this.privDynamicGrammar = new DynamicGrammarBuilder();
4249
+ this.privSpeechContext = new SpeechContext(this.privDynamicGrammar);
4250
+ this.privAgentConfig = new AgentConfig();
4251
+ if (typeof Blob !== "undefined" && typeof Worker !== "undefined") {
4252
+ this.privSetTimeout = Timeout.setTimeout;
4253
+ }
4254
+ this.connectionEvents.attach((connectionEvent) => {
4255
+ if (connectionEvent.name === "ConnectionClosedEvent") {
4256
+ const connectionClosedEvent = connectionEvent;
4257
+ if (connectionClosedEvent.statusCode === 1003 || connectionClosedEvent.statusCode === 1007 || connectionClosedEvent.statusCode === 1002 || connectionClosedEvent.statusCode === 4e3 || this.privRequestSession.numConnectionAttempts > this.privRecognizerConfig.maxRetryCount) {
4258
+ void this.cancelRecognitionLocal(CancellationReason.Error, connectionClosedEvent.statusCode === 1007 ? CancellationErrorCode.BadRequestParameters : CancellationErrorCode.ConnectionFailure, `${connectionClosedEvent.reason} websocket error code: ${connectionClosedEvent.statusCode}`);
3342
4259
  }
3343
- return replacement;
3344
4260
  }
3345
- return value;
3346
4261
  });
3347
4262
  }
3348
- get Context() {
3349
- return this.context;
4263
+ get audioSource() {
4264
+ return this.privAudioSource;
3350
4265
  }
3351
- get Recognition() {
3352
- return this.recognition;
4266
+ get speechContext() {
4267
+ return this.privSpeechContext;
3353
4268
  }
3354
- set Recognition(value) {
3355
- this.recognition = value.toLowerCase();
4269
+ get dynamicGrammar() {
4270
+ return this.privDynamicGrammar;
3356
4271
  }
3357
- }
3358
- class Context {
3359
- constructor(os) {
3360
- this.system = new System();
3361
- this.os = os;
4272
+ get agentConfig() {
4273
+ return this.privAgentConfig;
3362
4274
  }
3363
- }
3364
- class System {
3365
- constructor() {
3366
- const SPEECHSDK_CLIENTSDK_VERSION = "1.22.0";
3367
- this.name = "SpeechSDK";
3368
- this.version = SPEECHSDK_CLIENTSDK_VERSION;
3369
- this.build = "JavaScript";
3370
- this.lang = "JavaScript";
4275
+ set conversationTranslatorToken(token) {
4276
+ this.privRecognizerConfig.parameters.setProperty(PropertyId.ConversationTranslator_Token, token);
3371
4277
  }
3372
- }
3373
- class OS {
3374
- constructor(platform, name, version) {
3375
- this.platform = platform;
3376
- this.name = name;
3377
- this.version = version;
4278
+ set authentication(auth) {
4279
+ this.privAuthentication = this.authentication;
3378
4280
  }
3379
- }
3380
- var connectivity;
3381
- (function(connectivity2) {
3382
- connectivity2["Bluetooth"] = "Bluetooth";
3383
- connectivity2["Wired"] = "Wired";
3384
- connectivity2["WiFi"] = "WiFi";
3385
- connectivity2["Cellular"] = "Cellular";
3386
- connectivity2["InBuilt"] = "InBuilt";
3387
- connectivity2["Unknown"] = "Unknown";
3388
- })(connectivity || (connectivity = {}));
3389
- var type;
3390
- (function(type2) {
3391
- type2["Phone"] = "Phone";
3392
- type2["Speaker"] = "Speaker";
3393
- type2["Car"] = "Car";
3394
- type2["Headset"] = "Headset";
3395
- type2["Thermostat"] = "Thermostat";
3396
- type2["Microphones"] = "Microphones";
3397
- type2["Deskphone"] = "Deskphone";
3398
- type2["RemoteControl"] = "RemoteControl";
3399
- type2["Unknown"] = "Unknown";
3400
- type2["File"] = "File";
3401
- type2["Stream"] = "Stream";
3402
- })(type || (type = {}));
3403
- const CRLF = "\r\n";
3404
- class WebsocketMessageFormatter {
3405
- toConnectionMessage(message) {
3406
- const deferral = new Deferred();
3407
- try {
3408
- if (message.messageType === MessageType.Text) {
3409
- const textMessage = message.textContent;
3410
- let headers = {};
3411
- let body = null;
3412
- if (textMessage) {
3413
- const headerBodySplit = textMessage.split("\r\n\r\n");
3414
- if (headerBodySplit && headerBodySplit.length > 0) {
3415
- headers = this.parseHeaders(headerBodySplit[0]);
3416
- if (headerBodySplit.length > 1) {
3417
- body = headerBodySplit[1];
3418
- }
3419
- }
3420
- }
3421
- deferral.resolve(new ConnectionMessage(message.messageType, body, headers, message.id));
3422
- } else if (message.messageType === MessageType.Binary) {
3423
- const binaryMessage = message.binaryContent;
3424
- let headers = {};
3425
- let body = null;
3426
- if (!binaryMessage || binaryMessage.byteLength < 2) {
3427
- throw new Error("Invalid binary message format. Header length missing.");
3428
- }
3429
- const dataView = new DataView(binaryMessage);
3430
- const headerLength = dataView.getInt16(0);
3431
- if (binaryMessage.byteLength < headerLength + 2) {
3432
- throw new Error("Invalid binary message format. Header content missing.");
3433
- }
3434
- let headersString = "";
3435
- for (let i = 0; i < headerLength; i++) {
3436
- headersString += String.fromCharCode(dataView.getInt8(i + 2));
4281
+ isDisposed() {
4282
+ return this.privIsDisposed;
4283
+ }
4284
+ dispose(reason) {
4285
+ return __awaiter$a(this, void 0, void 0, function* () {
4286
+ this.privIsDisposed = true;
4287
+ if (this.privConnectionConfigurationPromise !== void 0) {
4288
+ try {
4289
+ const connection = yield this.privConnectionConfigurationPromise;
4290
+ yield connection.dispose(reason);
4291
+ } catch (error) {
4292
+ return;
3437
4293
  }
3438
- headers = this.parseHeaders(headersString);
3439
- if (binaryMessage.byteLength > headerLength + 2) {
3440
- body = binaryMessage.slice(2 + headerLength);
4294
+ }
4295
+ });
4296
+ }
4297
+ get connectionEvents() {
4298
+ return this.privConnectionEvents;
4299
+ }
4300
+ get serviceEvents() {
4301
+ return this.privServiceEvents;
4302
+ }
4303
+ get recognitionMode() {
4304
+ return this.privRecognizerConfig.recognitionMode;
4305
+ }
4306
+ recognize(recoMode, successCallback, errorCallBack) {
4307
+ return __awaiter$a(this, void 0, void 0, function* () {
4308
+ if (this.recognizeOverride !== void 0) {
4309
+ yield this.recognizeOverride(recoMode, successCallback, errorCallBack);
4310
+ return;
4311
+ }
4312
+ this.privConnectionConfigurationPromise = void 0;
4313
+ this.privRecognizerConfig.recognitionMode = recoMode;
4314
+ this.privSuccessCallback = successCallback;
4315
+ this.privErrorCallback = errorCallBack;
4316
+ this.privRequestSession.startNewRecognition();
4317
+ this.privRequestSession.listenForServiceTelemetry(this.privAudioSource.events);
4318
+ const conPromise = this.connectImpl();
4319
+ let audioNode;
4320
+ try {
4321
+ const audioStreamNode = yield this.audioSource.attach(this.privRequestSession.audioNodeId);
4322
+ const format = yield this.audioSource.format;
4323
+ const deviceInfo = yield this.audioSource.deviceInfo;
4324
+ this.privIsLiveAudio = deviceInfo.type && deviceInfo.type === type.Microphones;
4325
+ audioNode = new ReplayableAudioNode(audioStreamNode, format.avgBytesPerSec);
4326
+ yield this.privRequestSession.onAudioSourceAttachCompleted(audioNode, false);
4327
+ this.privRecognizerConfig.SpeechServiceConfig.Context.audio = { source: deviceInfo };
4328
+ } catch (error) {
4329
+ yield this.privRequestSession.onStopRecognizing();
4330
+ throw error;
4331
+ }
4332
+ try {
4333
+ yield conPromise;
4334
+ } catch (error) {
4335
+ yield this.cancelRecognitionLocal(CancellationReason.Error, CancellationErrorCode.ConnectionFailure, error);
4336
+ return;
4337
+ }
4338
+ const sessionStartEventArgs = new SessionEventArgs(this.privRequestSession.sessionId);
4339
+ if (!!this.privRecognizer.sessionStarted) {
4340
+ this.privRecognizer.sessionStarted(this.privRecognizer, sessionStartEventArgs);
4341
+ }
4342
+ void this.receiveMessage();
4343
+ const audioSendPromise = this.sendAudio(audioNode);
4344
+ audioSendPromise.catch((error) => __awaiter$a(this, void 0, void 0, function* () {
4345
+ yield this.cancelRecognitionLocal(CancellationReason.Error, CancellationErrorCode.RuntimeError, error);
4346
+ }));
4347
+ return;
4348
+ });
4349
+ }
4350
+ stopRecognizing() {
4351
+ return __awaiter$a(this, void 0, void 0, function* () {
4352
+ if (this.privRequestSession.isRecognizing) {
4353
+ try {
4354
+ yield this.audioSource.turnOff();
4355
+ yield this.sendFinalAudio();
4356
+ yield this.privRequestSession.onStopRecognizing();
4357
+ yield this.privRequestSession.turnCompletionPromise;
4358
+ } finally {
4359
+ yield this.privRequestSession.dispose();
3441
4360
  }
3442
- deferral.resolve(new ConnectionMessage(message.messageType, body, headers, message.id));
3443
4361
  }
3444
- } catch (e) {
3445
- deferral.reject(`Error formatting the message. Error: ${e}`);
4362
+ return;
4363
+ });
4364
+ }
4365
+ connect() {
4366
+ return __awaiter$a(this, void 0, void 0, function* () {
4367
+ yield this.connectImpl();
4368
+ return Promise.resolve();
4369
+ });
4370
+ }
4371
+ connectAsync(cb, err) {
4372
+ this.connectImpl().then(() => {
4373
+ try {
4374
+ if (!!cb) {
4375
+ cb();
4376
+ }
4377
+ } catch (e) {
4378
+ if (!!err) {
4379
+ err(e);
4380
+ }
4381
+ }
4382
+ }, (reason) => {
4383
+ try {
4384
+ if (!!err) {
4385
+ err(reason);
4386
+ }
4387
+ } catch (error) {
4388
+ }
4389
+ });
4390
+ }
4391
+ disconnect() {
4392
+ return __awaiter$a(this, void 0, void 0, function* () {
4393
+ yield this.cancelRecognitionLocal(CancellationReason.Error, CancellationErrorCode.NoError, "Disconnecting");
4394
+ if (this.disconnectOverride !== void 0) {
4395
+ yield this.disconnectOverride();
4396
+ }
4397
+ if (this.privConnectionPromise !== void 0) {
4398
+ try {
4399
+ yield (yield this.privConnectionPromise).dispose();
4400
+ } catch (error) {
4401
+ }
4402
+ }
4403
+ this.privConnectionPromise = void 0;
4404
+ });
4405
+ }
4406
+ sendMessage(message) {
4407
+ return;
4408
+ }
4409
+ sendNetworkMessage(path, payload) {
4410
+ return __awaiter$a(this, void 0, void 0, function* () {
4411
+ const type2 = typeof payload === "string" ? MessageType.Text : MessageType.Binary;
4412
+ const contentType = typeof payload === "string" ? "application/json" : "";
4413
+ const connection = yield this.fetchConnection();
4414
+ return connection.send(new SpeechConnectionMessage(type2, path, this.privRequestSession.requestId, contentType, payload));
4415
+ });
4416
+ }
4417
+ set activityTemplate(messagePayload) {
4418
+ this.privActivityTemplate = messagePayload;
4419
+ }
4420
+ get activityTemplate() {
4421
+ return this.privActivityTemplate;
4422
+ }
4423
+ sendTelemetryData() {
4424
+ return __awaiter$a(this, void 0, void 0, function* () {
4425
+ const telemetryData = this.privRequestSession.getTelemetry();
4426
+ if (ServiceRecognizerBase.telemetryDataEnabled !== true || this.privIsDisposed || null === telemetryData) {
4427
+ return;
4428
+ }
4429
+ if (!!ServiceRecognizerBase.telemetryData) {
4430
+ try {
4431
+ ServiceRecognizerBase.telemetryData(telemetryData);
4432
+ } catch (_a) {
4433
+ }
4434
+ }
4435
+ const connection = yield this.fetchConnection();
4436
+ yield connection.send(new SpeechConnectionMessage(MessageType.Text, "telemetry", this.privRequestSession.requestId, "application/json", telemetryData));
4437
+ });
4438
+ }
4439
+ cancelRecognitionLocal(cancellationReason, errorCode, error) {
4440
+ return __awaiter$a(this, void 0, void 0, function* () {
4441
+ if (!!this.privRequestSession.isRecognizing) {
4442
+ yield this.privRequestSession.onStopRecognizing();
4443
+ this.cancelRecognition(this.privRequestSession.sessionId, this.privRequestSession.requestId, cancellationReason, errorCode, error);
4444
+ }
4445
+ });
4446
+ }
4447
+ receiveMessage() {
4448
+ return __awaiter$a(this, void 0, void 0, function* () {
4449
+ try {
4450
+ if (this.privIsDisposed) {
4451
+ return;
4452
+ }
4453
+ let connection = yield this.fetchConnection();
4454
+ const message = yield connection.read();
4455
+ if (this.receiveMessageOverride !== void 0) {
4456
+ return this.receiveMessageOverride();
4457
+ }
4458
+ if (!message) {
4459
+ if (!this.privRequestSession.isRecognizing) {
4460
+ return;
4461
+ } else {
4462
+ return this.receiveMessage();
4463
+ }
4464
+ }
4465
+ this.privServiceHasSentMessage = true;
4466
+ const connectionMessage = SpeechConnectionMessage.fromConnectionMessage(message);
4467
+ if (connectionMessage.requestId.toLowerCase() === this.privRequestSession.requestId.toLowerCase()) {
4468
+ switch (connectionMessage.path.toLowerCase()) {
4469
+ case "turn.start":
4470
+ this.privMustReportEndOfStream = true;
4471
+ this.privRequestSession.onServiceTurnStartResponse();
4472
+ break;
4473
+ case "speech.startdetected":
4474
+ const speechStartDetected = SpeechDetected.fromJSON(connectionMessage.textBody);
4475
+ const speechStartEventArgs = new RecognitionEventArgs(speechStartDetected.Offset, this.privRequestSession.sessionId);
4476
+ if (!!this.privRecognizer.speechStartDetected) {
4477
+ this.privRecognizer.speechStartDetected(this.privRecognizer, speechStartEventArgs);
4478
+ }
4479
+ break;
4480
+ case "speech.enddetected":
4481
+ let json;
4482
+ if (connectionMessage.textBody.length > 0) {
4483
+ json = connectionMessage.textBody;
4484
+ } else {
4485
+ json = "{ Offset: 0 }";
4486
+ }
4487
+ const speechStopDetected = SpeechDetected.fromJSON(json);
4488
+ if (this.privRecognizerConfig.isContinuousRecognition) {
4489
+ this.privRequestSession.onServiceRecognized(speechStopDetected.Offset + this.privRequestSession.currentTurnAudioOffset);
4490
+ }
4491
+ const speechStopEventArgs = new RecognitionEventArgs(speechStopDetected.Offset + this.privRequestSession.currentTurnAudioOffset, this.privRequestSession.sessionId);
4492
+ if (!!this.privRecognizer.speechEndDetected) {
4493
+ this.privRecognizer.speechEndDetected(this.privRecognizer, speechStopEventArgs);
4494
+ }
4495
+ break;
4496
+ case "turn.end":
4497
+ yield this.sendTelemetryData();
4498
+ if (this.privRequestSession.isSpeechEnded && this.privMustReportEndOfStream) {
4499
+ this.privMustReportEndOfStream = false;
4500
+ yield this.cancelRecognitionLocal(CancellationReason.EndOfStream, CancellationErrorCode.NoError, void 0);
4501
+ }
4502
+ const sessionStopEventArgs = new SessionEventArgs(this.privRequestSession.sessionId);
4503
+ yield this.privRequestSession.onServiceTurnEndResponse(this.privRecognizerConfig.isContinuousRecognition);
4504
+ if (!this.privRecognizerConfig.isContinuousRecognition || this.privRequestSession.isSpeechEnded || !this.privRequestSession.isRecognizing) {
4505
+ if (!!this.privRecognizer.sessionStopped) {
4506
+ this.privRecognizer.sessionStopped(this.privRecognizer, sessionStopEventArgs);
4507
+ }
4508
+ return;
4509
+ } else {
4510
+ connection = yield this.fetchConnection();
4511
+ yield this.sendPrePayloadJSON(connection);
4512
+ }
4513
+ break;
4514
+ default:
4515
+ if (!(yield this.processTypeSpecificMessages(connectionMessage))) {
4516
+ if (!!this.privServiceEvents) {
4517
+ this.serviceEvents.onEvent(new ServiceEvent(connectionMessage.path.toLowerCase(), connectionMessage.textBody));
4518
+ }
4519
+ }
4520
+ }
4521
+ }
4522
+ return this.receiveMessage();
4523
+ } catch (error) {
4524
+ return null;
4525
+ }
4526
+ });
4527
+ }
4528
+ sendSpeechContext(connection, generateNewRequestId) {
4529
+ const speechContextJson = this.speechContext.toJSON();
4530
+ if (generateNewRequestId) {
4531
+ this.privRequestSession.onSpeechContext();
4532
+ }
4533
+ if (speechContextJson) {
4534
+ return connection.send(new SpeechConnectionMessage(MessageType.Text, "speech.context", this.privRequestSession.requestId, "application/json", speechContextJson));
4535
+ }
4536
+ return;
4537
+ }
4538
+ sendPrePayloadJSON(connection, generateNewRequestId = true) {
4539
+ return __awaiter$a(this, void 0, void 0, function* () {
4540
+ if (this.sendPrePayloadJSONOverride !== void 0) {
4541
+ return this.sendPrePayloadJSONOverride(connection);
4542
+ }
4543
+ yield this.sendSpeechContext(connection, generateNewRequestId);
4544
+ yield this.sendWaveHeader(connection);
4545
+ return;
4546
+ });
4547
+ }
4548
+ sendWaveHeader(connection) {
4549
+ return __awaiter$a(this, void 0, void 0, function* () {
4550
+ const format = yield this.audioSource.format;
4551
+ return connection.send(new SpeechConnectionMessage(MessageType.Binary, "audio", this.privRequestSession.requestId, "audio/x-wav", format.header));
4552
+ });
4553
+ }
4554
+ connectImpl() {
4555
+ if (this.privConnectionPromise !== void 0) {
4556
+ return this.privConnectionPromise.then((connection) => {
4557
+ if (connection.state() === ConnectionState.Disconnected) {
4558
+ this.privConnectionId = null;
4559
+ this.privConnectionPromise = void 0;
4560
+ this.privServiceHasSentMessage = false;
4561
+ return this.connectImpl();
4562
+ }
4563
+ return this.privConnectionPromise;
4564
+ }, () => {
4565
+ this.privConnectionId = null;
4566
+ this.privConnectionPromise = void 0;
4567
+ this.privServiceHasSentMessage = false;
4568
+ return this.connectImpl();
4569
+ });
4570
+ }
4571
+ this.privConnectionPromise = this.retryableConnect();
4572
+ this.privConnectionPromise.catch(() => {
4573
+ });
4574
+ if (this.postConnectImplOverride !== void 0) {
4575
+ return this.postConnectImplOverride(this.privConnectionPromise);
4576
+ }
4577
+ return this.privConnectionPromise;
4578
+ }
4579
+ sendSpeechServiceConfig(connection, requestSession, SpeechServiceConfigJson) {
4580
+ requestSession.onSpeechContext();
4581
+ if (ServiceRecognizerBase.telemetryDataEnabled !== true) {
4582
+ const withTelemetry = JSON.parse(SpeechServiceConfigJson);
4583
+ const replacement = {
4584
+ context: {
4585
+ system: withTelemetry.context.system
4586
+ }
4587
+ };
4588
+ SpeechServiceConfigJson = JSON.stringify(replacement);
4589
+ }
4590
+ if (this.privRecognizerConfig.parameters.getProperty("TranscriptionService_SingleChannel", "false").toLowerCase() === "true") {
4591
+ const json = JSON.parse(SpeechServiceConfigJson);
4592
+ json.context.DisableReferenceChannel = "True";
4593
+ json.context.MicSpec = "1_0_0";
4594
+ SpeechServiceConfigJson = JSON.stringify(json);
4595
+ }
4596
+ if (SpeechServiceConfigJson) {
4597
+ return connection.send(new SpeechConnectionMessage(MessageType.Text, "speech.config", requestSession.requestId, "application/json", SpeechServiceConfigJson));
4598
+ }
4599
+ return;
4600
+ }
4601
+ fetchConnection() {
4602
+ return __awaiter$a(this, void 0, void 0, function* () {
4603
+ if (this.privConnectionConfigurationPromise !== void 0) {
4604
+ return this.privConnectionConfigurationPromise.then((connection) => {
4605
+ if (connection.state() === ConnectionState.Disconnected) {
4606
+ this.privConnectionId = null;
4607
+ this.privConnectionConfigurationPromise = void 0;
4608
+ this.privServiceHasSentMessage = false;
4609
+ return this.fetchConnection();
4610
+ }
4611
+ return this.privConnectionConfigurationPromise;
4612
+ }, () => {
4613
+ this.privConnectionId = null;
4614
+ this.privConnectionConfigurationPromise = void 0;
4615
+ this.privServiceHasSentMessage = false;
4616
+ return this.fetchConnection();
4617
+ });
4618
+ }
4619
+ this.privConnectionConfigurationPromise = this.configureConnection();
4620
+ return yield this.privConnectionConfigurationPromise;
4621
+ });
4622
+ }
4623
+ sendAudio(audioStreamNode) {
4624
+ return __awaiter$a(this, void 0, void 0, function* () {
4625
+ const audioFormat = yield this.audioSource.format;
4626
+ let nextSendTime = Date.now();
4627
+ const fastLaneSizeMs = this.privRecognizerConfig.parameters.getProperty("SPEECH-TransmitLengthBeforThrottleMs", "5000");
4628
+ const maxSendUnthrottledBytes = audioFormat.avgBytesPerSec / 1e3 * parseInt(fastLaneSizeMs, 10);
4629
+ const startRecogNumber = this.privRequestSession.recogNumber;
4630
+ const readAndUploadCycle = () => __awaiter$a(this, void 0, void 0, function* () {
4631
+ if (!this.privIsDisposed && !this.privRequestSession.isSpeechEnded && this.privRequestSession.isRecognizing && this.privRequestSession.recogNumber === startRecogNumber) {
4632
+ const connection = yield this.fetchConnection();
4633
+ const audioStreamChunk = yield audioStreamNode.read();
4634
+ if (this.privRequestSession.isSpeechEnded) {
4635
+ return;
4636
+ }
4637
+ let payload;
4638
+ let sendDelay;
4639
+ if (!audioStreamChunk || audioStreamChunk.isEnd) {
4640
+ payload = null;
4641
+ sendDelay = 0;
4642
+ } else {
4643
+ payload = audioStreamChunk.buffer;
4644
+ this.privRequestSession.onAudioSent(payload.byteLength);
4645
+ if (maxSendUnthrottledBytes >= this.privRequestSession.bytesSent) {
4646
+ sendDelay = 0;
4647
+ } else {
4648
+ sendDelay = Math.max(0, nextSendTime - Date.now());
4649
+ }
4650
+ }
4651
+ if (0 !== sendDelay) {
4652
+ yield this.delay(sendDelay);
4653
+ }
4654
+ if (payload !== null) {
4655
+ nextSendTime = Date.now() + payload.byteLength * 1e3 / (audioFormat.avgBytesPerSec * 2);
4656
+ }
4657
+ if (!this.privIsDisposed && !this.privRequestSession.isSpeechEnded && this.privRequestSession.isRecognizing && this.privRequestSession.recogNumber === startRecogNumber) {
4658
+ connection.send(new SpeechConnectionMessage(MessageType.Binary, "audio", this.privRequestSession.requestId, null, payload)).catch(() => {
4659
+ this.privRequestSession.onServiceTurnEndResponse(this.privRecognizerConfig.isContinuousRecognition).catch(() => {
4660
+ });
4661
+ });
4662
+ if (!(audioStreamChunk === null || audioStreamChunk === void 0 ? void 0 : audioStreamChunk.isEnd)) {
4663
+ return readAndUploadCycle();
4664
+ } else {
4665
+ if (!this.privIsLiveAudio) {
4666
+ this.privRequestSession.onSpeechEnded();
4667
+ }
4668
+ }
4669
+ }
4670
+ }
4671
+ });
4672
+ return readAndUploadCycle();
4673
+ });
4674
+ }
4675
+ retryableConnect() {
4676
+ return __awaiter$a(this, void 0, void 0, function* () {
4677
+ let isUnAuthorized = false;
4678
+ this.privAuthFetchEventId = createNoDashGuid();
4679
+ const sessionId = this.privRequestSession.sessionId;
4680
+ this.privConnectionId = sessionId !== void 0 ? sessionId : createNoDashGuid();
4681
+ this.privRequestSession.onPreConnectionStart(this.privAuthFetchEventId, this.privConnectionId);
4682
+ let lastStatusCode = 0;
4683
+ let lastReason = "";
4684
+ while (this.privRequestSession.numConnectionAttempts <= this.privRecognizerConfig.maxRetryCount) {
4685
+ const authPromise = isUnAuthorized ? this.privAuthentication.fetchOnExpiry(this.privAuthFetchEventId) : this.privAuthentication.fetch(this.privAuthFetchEventId);
4686
+ const auth = yield authPromise;
4687
+ yield this.privRequestSession.onAuthCompleted(false);
4688
+ const connection = this.privConnectionFactory.create(this.privRecognizerConfig, auth, this.privConnectionId);
4689
+ this.privRequestSession.listenForServiceTelemetry(connection.events);
4690
+ connection.events.attach((event) => {
4691
+ this.connectionEvents.onEvent(event);
4692
+ });
4693
+ const response = yield connection.open();
4694
+ if (response.statusCode === 200) {
4695
+ yield this.privRequestSession.onConnectionEstablishCompleted(response.statusCode);
4696
+ return Promise.resolve(connection);
4697
+ } else if (response.statusCode === 1006) {
4698
+ isUnAuthorized = true;
4699
+ }
4700
+ lastStatusCode = response.statusCode;
4701
+ lastReason = response.reason;
4702
+ this.privRequestSession.onRetryConnection();
4703
+ }
4704
+ yield this.privRequestSession.onConnectionEstablishCompleted(lastStatusCode, lastReason);
4705
+ return Promise.reject(`Unable to contact server. StatusCode: ${lastStatusCode}, ${this.privRecognizerConfig.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint)} Reason: ${lastReason}`);
4706
+ });
4707
+ }
4708
+ delay(delayMs) {
4709
+ return new Promise((resolve) => this.privSetTimeout(resolve, delayMs));
4710
+ }
4711
+ writeBufferToConsole(buffer) {
4712
+ let out = "Buffer Size: ";
4713
+ if (null === buffer) {
4714
+ out += "null";
4715
+ } else {
4716
+ const readView = new Uint8Array(buffer);
4717
+ out += `${buffer.byteLength}\r
4718
+ `;
4719
+ for (let i = 0; i < buffer.byteLength; i++) {
4720
+ out += readView[i].toString(16).padStart(2, "0") + " ";
4721
+ }
4722
+ }
4723
+ console.info(out);
4724
+ }
4725
+ sendFinalAudio() {
4726
+ return __awaiter$a(this, void 0, void 0, function* () {
4727
+ const connection = yield this.fetchConnection();
4728
+ yield connection.send(new SpeechConnectionMessage(MessageType.Binary, "audio", this.privRequestSession.requestId, null, null));
4729
+ return;
4730
+ });
4731
+ }
4732
+ configureConnection() {
4733
+ return __awaiter$a(this, void 0, void 0, function* () {
4734
+ const connection = yield this.connectImpl();
4735
+ if (this.configConnectionOverride !== void 0) {
4736
+ return this.configConnectionOverride(connection);
4737
+ }
4738
+ yield this.sendSpeechServiceConfig(connection, this.privRequestSession, this.privRecognizerConfig.SpeechServiceConfig.serialize());
4739
+ yield this.sendPrePayloadJSON(connection, false);
4740
+ return connection;
4741
+ });
4742
+ }
4743
+ }
4744
+ ServiceRecognizerBase.telemetryDataEnabled = true;
4745
+ var RecognitionMode;
4746
+ (function(RecognitionMode2) {
4747
+ RecognitionMode2[RecognitionMode2["Interactive"] = 0] = "Interactive";
4748
+ RecognitionMode2[RecognitionMode2["Conversation"] = 1] = "Conversation";
4749
+ RecognitionMode2[RecognitionMode2["Dictation"] = 2] = "Dictation";
4750
+ })(RecognitionMode || (RecognitionMode = {}));
4751
+ var SpeechResultFormat;
4752
+ (function(SpeechResultFormat2) {
4753
+ SpeechResultFormat2[SpeechResultFormat2["Simple"] = 0] = "Simple";
4754
+ SpeechResultFormat2[SpeechResultFormat2["Detailed"] = 1] = "Detailed";
4755
+ })(SpeechResultFormat || (SpeechResultFormat = {}));
4756
+ class RecognizerConfig {
4757
+ constructor(speechServiceConfig, parameters) {
4758
+ this.privSpeechServiceConfig = speechServiceConfig ? speechServiceConfig : new SpeechServiceConfig(new Context(null));
4759
+ this.privParameters = parameters;
4760
+ this.privMaxRetryCount = parseInt(parameters.getProperty("SPEECH-Error-MaxRetryCount", "4"), 10);
4761
+ this.privLanguageIdPriority = parameters.getProperty(PropertyId.SpeechServiceConnection_ContinuousLanguageIdPriority, void 0);
4762
+ this.privLanguageIdMode = this.privLanguageIdPriority === "Latency" ? "DetectContinuous" : "DetectAtAudioStart";
4763
+ if (this.privLanguageIdMode === "DetectAtAudioStart") {
4764
+ this.privLanguageIdPriority = parameters.getProperty(PropertyId.SpeechServiceConnection_AtStartLanguageIdPriority, void 0);
4765
+ }
4766
+ }
4767
+ get parameters() {
4768
+ return this.privParameters;
4769
+ }
4770
+ get recognitionMode() {
4771
+ return this.privRecognitionMode;
4772
+ }
4773
+ set recognitionMode(value) {
4774
+ this.privRecognitionMode = value;
4775
+ this.privRecognitionActivityTimeout = value === RecognitionMode.Interactive ? 8e3 : 25e3;
4776
+ this.privSpeechServiceConfig.Recognition = RecognitionMode[value];
4777
+ }
4778
+ get SpeechServiceConfig() {
4779
+ return this.privSpeechServiceConfig;
4780
+ }
4781
+ get recognitionActivityTimeout() {
4782
+ return this.privRecognitionActivityTimeout;
4783
+ }
4784
+ get isContinuousRecognition() {
4785
+ return this.privRecognitionMode !== RecognitionMode.Interactive;
4786
+ }
4787
+ get languageIdPriority() {
4788
+ return !!this.privLanguageIdPriority ? `Prioritize${this.privLanguageIdPriority}` : "";
4789
+ }
4790
+ get languageIdMode() {
4791
+ return this.privLanguageIdMode;
4792
+ }
4793
+ get autoDetectSourceLanguages() {
4794
+ return this.parameters.getProperty(PropertyId.SpeechServiceConnection_AutoDetectSourceLanguages, void 0);
4795
+ }
4796
+ get recognitionEndpointVersion() {
4797
+ return this.parameters.getProperty(PropertyId.SpeechServiceConnection_RecognitionEndpointVersion, void 0);
4798
+ }
4799
+ get sourceLanguageModels() {
4800
+ const models = [];
4801
+ let modelsExist = false;
4802
+ if (this.autoDetectSourceLanguages !== void 0) {
4803
+ for (const language of this.autoDetectSourceLanguages.split(",")) {
4804
+ const customProperty = language + PropertyId.SpeechServiceConnection_EndpointId.toString();
4805
+ const modelId = this.parameters.getProperty(customProperty, void 0);
4806
+ if (modelId !== void 0) {
4807
+ models.push({ language, endpoint: modelId });
4808
+ modelsExist = true;
4809
+ } else {
4810
+ models.push({ language, endpoint: "" });
4811
+ }
4812
+ }
4813
+ }
4814
+ return modelsExist ? models : void 0;
4815
+ }
4816
+ get maxRetryCount() {
4817
+ return this.privMaxRetryCount;
4818
+ }
4819
+ }
4820
+ class SpeechServiceConfig {
4821
+ constructor(context) {
4822
+ this.context = context;
4823
+ }
4824
+ serialize() {
4825
+ return JSON.stringify(this, (key, value) => {
4826
+ if (value && typeof value === "object") {
4827
+ const replacement = {};
4828
+ for (const k in value) {
4829
+ if (Object.hasOwnProperty.call(value, k)) {
4830
+ replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
4831
+ }
4832
+ }
4833
+ return replacement;
4834
+ }
4835
+ return value;
4836
+ });
4837
+ }
4838
+ get Context() {
4839
+ return this.context;
4840
+ }
4841
+ get Recognition() {
4842
+ return this.recognition;
4843
+ }
4844
+ set Recognition(value) {
4845
+ this.recognition = value.toLowerCase();
4846
+ }
4847
+ }
4848
+ class Context {
4849
+ constructor(os) {
4850
+ this.system = new System();
4851
+ this.os = os;
4852
+ }
4853
+ }
4854
+ class System {
4855
+ constructor() {
4856
+ const SPEECHSDK_CLIENTSDK_VERSION = "1.22.0";
4857
+ this.name = "SpeechSDK";
4858
+ this.version = SPEECHSDK_CLIENTSDK_VERSION;
4859
+ this.build = "JavaScript";
4860
+ this.lang = "JavaScript";
4861
+ }
4862
+ }
4863
+ class OS {
4864
+ constructor(platform, name, version) {
4865
+ this.platform = platform;
4866
+ this.name = name;
4867
+ this.version = version;
4868
+ }
4869
+ }
4870
+ var connectivity;
4871
+ (function(connectivity2) {
4872
+ connectivity2["Bluetooth"] = "Bluetooth";
4873
+ connectivity2["Wired"] = "Wired";
4874
+ connectivity2["WiFi"] = "WiFi";
4875
+ connectivity2["Cellular"] = "Cellular";
4876
+ connectivity2["InBuilt"] = "InBuilt";
4877
+ connectivity2["Unknown"] = "Unknown";
4878
+ })(connectivity || (connectivity = {}));
4879
+ var type;
4880
+ (function(type2) {
4881
+ type2["Phone"] = "Phone";
4882
+ type2["Speaker"] = "Speaker";
4883
+ type2["Car"] = "Car";
4884
+ type2["Headset"] = "Headset";
4885
+ type2["Thermostat"] = "Thermostat";
4886
+ type2["Microphones"] = "Microphones";
4887
+ type2["Deskphone"] = "Deskphone";
4888
+ type2["RemoteControl"] = "RemoteControl";
4889
+ type2["Unknown"] = "Unknown";
4890
+ type2["File"] = "File";
4891
+ type2["Stream"] = "Stream";
4892
+ })(type || (type = {}));
4893
+ const CRLF = "\r\n";
4894
+ class WebsocketMessageFormatter {
4895
+ toConnectionMessage(message) {
4896
+ const deferral = new Deferred();
4897
+ try {
4898
+ if (message.messageType === MessageType.Text) {
4899
+ const textMessage = message.textContent;
4900
+ let headers = {};
4901
+ let body = null;
4902
+ if (textMessage) {
4903
+ const headerBodySplit = textMessage.split("\r\n\r\n");
4904
+ if (headerBodySplit && headerBodySplit.length > 0) {
4905
+ headers = this.parseHeaders(headerBodySplit[0]);
4906
+ if (headerBodySplit.length > 1) {
4907
+ body = headerBodySplit[1];
4908
+ }
4909
+ }
4910
+ }
4911
+ deferral.resolve(new ConnectionMessage(message.messageType, body, headers, message.id));
4912
+ } else if (message.messageType === MessageType.Binary) {
4913
+ const binaryMessage = message.binaryContent;
4914
+ let headers = {};
4915
+ let body = null;
4916
+ if (!binaryMessage || binaryMessage.byteLength < 2) {
4917
+ throw new Error("Invalid binary message format. Header length missing.");
4918
+ }
4919
+ const dataView = new DataView(binaryMessage);
4920
+ const headerLength = dataView.getInt16(0);
4921
+ if (binaryMessage.byteLength < headerLength + 2) {
4922
+ throw new Error("Invalid binary message format. Header content missing.");
4923
+ }
4924
+ let headersString = "";
4925
+ for (let i = 0; i < headerLength; i++) {
4926
+ headersString += String.fromCharCode(dataView.getInt8(i + 2));
4927
+ }
4928
+ headers = this.parseHeaders(headersString);
4929
+ if (binaryMessage.byteLength > headerLength + 2) {
4930
+ body = binaryMessage.slice(2 + headerLength);
4931
+ }
4932
+ deferral.resolve(new ConnectionMessage(message.messageType, body, headers, message.id));
4933
+ }
4934
+ } catch (e) {
4935
+ deferral.reject(`Error formatting the message. Error: ${e}`);
4936
+ }
4937
+ return deferral.promise;
4938
+ }
4939
+ fromConnectionMessage(message) {
4940
+ const deferral = new Deferred();
4941
+ try {
4942
+ if (message.messageType === MessageType.Text) {
4943
+ const payload = `${this.makeHeaders(message)}${CRLF}${message.textBody ? message.textBody : ""}`;
4944
+ deferral.resolve(new RawWebsocketMessage(MessageType.Text, payload, message.id));
4945
+ } else if (message.messageType === MessageType.Binary) {
4946
+ const headersString = this.makeHeaders(message);
4947
+ const content = message.binaryBody;
4948
+ const headerBuffer = this.stringToArrayBuffer(headersString);
4949
+ const headerInt8Array = new Int8Array(headerBuffer);
4950
+ const headerLength = headerInt8Array.byteLength;
4951
+ const payloadInt8Array = new Int8Array(2 + headerLength + (content ? content.byteLength : 0));
4952
+ payloadInt8Array[0] = headerLength >> 8 & 255;
4953
+ payloadInt8Array[1] = headerLength & 255;
4954
+ payloadInt8Array.set(headerInt8Array, 2);
4955
+ if (content) {
4956
+ const bodyInt8Array = new Int8Array(content);
4957
+ payloadInt8Array.set(bodyInt8Array, 2 + headerLength);
4958
+ }
4959
+ const payload = payloadInt8Array.buffer;
4960
+ deferral.resolve(new RawWebsocketMessage(MessageType.Binary, payload, message.id));
4961
+ }
4962
+ } catch (e) {
4963
+ deferral.reject(`Error formatting the message. ${e}`);
4964
+ }
4965
+ return deferral.promise;
4966
+ }
4967
+ makeHeaders(message) {
4968
+ let headersString = "";
4969
+ if (message.headers) {
4970
+ for (const header in message.headers) {
4971
+ if (header) {
4972
+ headersString += `${header}: ${message.headers[header]}${CRLF}`;
4973
+ }
4974
+ }
4975
+ }
4976
+ return headersString;
4977
+ }
4978
+ parseHeaders(headersString) {
4979
+ const headers = {};
4980
+ if (headersString) {
4981
+ const headerMatches = headersString.match(/[^\r\n]+/g);
4982
+ if (headers) {
4983
+ for (const header of headerMatches) {
4984
+ if (header) {
4985
+ const separatorIndex = header.indexOf(":");
4986
+ const headerName = separatorIndex > 0 ? header.substr(0, separatorIndex).trim().toLowerCase() : header;
4987
+ const headerValue = separatorIndex > 0 && header.length > separatorIndex + 1 ? header.substr(separatorIndex + 1).trim() : "";
4988
+ headers[headerName] = headerValue;
4989
+ }
4990
+ }
4991
+ }
4992
+ }
4993
+ return headers;
4994
+ }
4995
+ stringToArrayBuffer(str) {
4996
+ const buffer = new ArrayBuffer(str.length);
4997
+ const view = new DataView(buffer);
4998
+ for (let i = 0; i < str.length; i++) {
4999
+ view.setUint8(i, str.charCodeAt(i));
5000
+ }
5001
+ return buffer;
5002
+ }
5003
+ }
5004
+ class TranslationConnectionFactory extends ConnectionFactoryBase {
5005
+ create(config, authInfo, connectionId) {
5006
+ let endpoint = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, void 0);
5007
+ if (!endpoint) {
5008
+ const region = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region, void 0);
5009
+ const hostSuffix = ConnectionFactoryBase.getHostSuffix(region);
5010
+ const host = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, "wss://" + region + ".s2s.speech" + hostSuffix);
5011
+ endpoint = host + "/speech/translation/cognitiveservices/v1";
5012
+ }
5013
+ const queryParams = {
5014
+ from: config.parameters.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage),
5015
+ to: config.parameters.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages)
5016
+ };
5017
+ this.setCommonUrlParams(config, queryParams, endpoint);
5018
+ this.setUrlParameter(PropertyId.SpeechServiceResponse_TranslationRequestStablePartialResult, QueryParameterNames.StableTranslation, config, queryParams, endpoint);
5019
+ const voiceName = "voice";
5020
+ const featureName = "features";
5021
+ if (config.parameters.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, void 0) !== void 0) {
5022
+ queryParams[voiceName] = config.parameters.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice);
5023
+ queryParams[featureName] = "texttospeech";
5024
+ }
5025
+ const headers = {};
5026
+ if (authInfo.token !== void 0 && authInfo.token !== "") {
5027
+ headers[authInfo.headerName] = authInfo.token;
5028
+ }
5029
+ headers[HeaderNames.ConnectionId] = connectionId;
5030
+ config.parameters.setProperty(PropertyId.SpeechServiceConnection_Url, endpoint);
5031
+ const enableCompression = config.parameters.getProperty("SPEECH-EnableWebsocketCompression", "false") === "true";
5032
+ return new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromRecognizerConfig(config), enableCompression, connectionId);
5033
+ }
5034
+ }
5035
+ class SpeechSynthesisConnectionFactory {
5036
+ constructor() {
5037
+ this.synthesisUri = "/cognitiveservices/websocket/v1";
5038
+ }
5039
+ create(config, authInfo, connectionId) {
5040
+ let endpoint = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, void 0);
5041
+ const region = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region, void 0);
5042
+ const hostSuffix = ConnectionFactoryBase.getHostSuffix(region);
5043
+ const endpointId = config.parameters.getProperty(PropertyId.SpeechServiceConnection_EndpointId, void 0);
5044
+ const hostPrefix = endpointId === void 0 ? "tts" : "voice";
5045
+ const host = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, "wss://" + region + "." + hostPrefix + ".speech" + hostSuffix);
5046
+ const queryParams = {};
5047
+ if (!endpoint) {
5048
+ endpoint = host + this.synthesisUri;
5049
+ }
5050
+ const headers = {};
5051
+ if (authInfo.token !== void 0 && authInfo.token !== "") {
5052
+ headers[authInfo.headerName] = authInfo.token;
5053
+ }
5054
+ headers[HeaderNames.ConnectionId] = connectionId;
5055
+ if (endpointId !== void 0) {
5056
+ headers[QueryParameterNames.CustomVoiceDeploymentId] = endpointId;
5057
+ }
5058
+ config.parameters.setProperty(PropertyId.SpeechServiceConnection_Url, endpoint);
5059
+ const enableCompression = config.parameters.getProperty("SPEECH-EnableWebsocketCompression", "false") === "true";
5060
+ return new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromParameters(config.parameters), enableCompression, connectionId);
5061
+ }
5062
+ }
5063
+ class EnumTranslation {
5064
+ static implTranslateRecognitionResult(recognitionStatus) {
5065
+ let reason = ResultReason.Canceled;
5066
+ switch (recognitionStatus) {
5067
+ case RecognitionStatus.Success:
5068
+ reason = ResultReason.RecognizedSpeech;
5069
+ break;
5070
+ case RecognitionStatus.NoMatch:
5071
+ case RecognitionStatus.InitialSilenceTimeout:
5072
+ case RecognitionStatus.BabbleTimeout:
5073
+ case RecognitionStatus.EndOfDictation:
5074
+ reason = ResultReason.NoMatch;
5075
+ break;
5076
+ case RecognitionStatus.Error:
5077
+ case RecognitionStatus.BadRequest:
5078
+ case RecognitionStatus.Forbidden:
5079
+ default:
5080
+ reason = ResultReason.Canceled;
5081
+ break;
5082
+ }
5083
+ return reason;
5084
+ }
5085
+ static implTranslateCancelResult(recognitionStatus) {
5086
+ let reason = CancellationReason.EndOfStream;
5087
+ switch (recognitionStatus) {
5088
+ case RecognitionStatus.Success:
5089
+ case RecognitionStatus.EndOfDictation:
5090
+ case RecognitionStatus.NoMatch:
5091
+ reason = CancellationReason.EndOfStream;
5092
+ break;
5093
+ case RecognitionStatus.InitialSilenceTimeout:
5094
+ case RecognitionStatus.BabbleTimeout:
5095
+ case RecognitionStatus.Error:
5096
+ case RecognitionStatus.BadRequest:
5097
+ case RecognitionStatus.Forbidden:
5098
+ default:
5099
+ reason = CancellationReason.Error;
5100
+ break;
5101
+ }
5102
+ return reason;
5103
+ }
5104
+ static implTranslateCancelErrorCode(recognitionStatus) {
5105
+ let reason = CancellationErrorCode.NoError;
5106
+ switch (recognitionStatus) {
5107
+ case RecognitionStatus.Error:
5108
+ reason = CancellationErrorCode.ServiceError;
5109
+ break;
5110
+ case RecognitionStatus.TooManyRequests:
5111
+ reason = CancellationErrorCode.TooManyRequests;
5112
+ break;
5113
+ case RecognitionStatus.BadRequest:
5114
+ reason = CancellationErrorCode.BadRequestParameters;
5115
+ break;
5116
+ case RecognitionStatus.Forbidden:
5117
+ reason = CancellationErrorCode.Forbidden;
5118
+ break;
5119
+ default:
5120
+ reason = CancellationErrorCode.NoError;
5121
+ break;
5122
+ }
5123
+ return reason;
5124
+ }
5125
+ static implTranslateErrorDetails(cancellationErrorCode) {
5126
+ let errorDetails = "The speech service encountered an internal error and could not continue.";
5127
+ switch (cancellationErrorCode) {
5128
+ case CancellationErrorCode.Forbidden:
5129
+ errorDetails = "The recognizer is using a free subscription that ran out of quota.";
5130
+ break;
5131
+ case CancellationErrorCode.BadRequestParameters:
5132
+ errorDetails = "Invalid parameter or unsupported audio format in the request.";
5133
+ break;
5134
+ case CancellationErrorCode.TooManyRequests:
5135
+ errorDetails = "The number of parallel requests exceeded the number of allowed concurrent transcriptions.";
5136
+ break;
5137
+ }
5138
+ return errorDetails;
5139
+ }
5140
+ }
5141
+ var SynthesisStatus;
5142
+ (function(SynthesisStatus2) {
5143
+ SynthesisStatus2[SynthesisStatus2["Success"] = 0] = "Success";
5144
+ SynthesisStatus2[SynthesisStatus2["SynthesisEnd"] = 1] = "SynthesisEnd";
5145
+ SynthesisStatus2[SynthesisStatus2["Error"] = 2] = "Error";
5146
+ })(SynthesisStatus || (SynthesisStatus = {}));
5147
+ var RecognitionStatus;
5148
+ (function(RecognitionStatus2) {
5149
+ RecognitionStatus2[RecognitionStatus2["Success"] = 0] = "Success";
5150
+ RecognitionStatus2[RecognitionStatus2["NoMatch"] = 1] = "NoMatch";
5151
+ RecognitionStatus2[RecognitionStatus2["InitialSilenceTimeout"] = 2] = "InitialSilenceTimeout";
5152
+ RecognitionStatus2[RecognitionStatus2["BabbleTimeout"] = 3] = "BabbleTimeout";
5153
+ RecognitionStatus2[RecognitionStatus2["Error"] = 4] = "Error";
5154
+ RecognitionStatus2[RecognitionStatus2["EndOfDictation"] = 5] = "EndOfDictation";
5155
+ RecognitionStatus2[RecognitionStatus2["TooManyRequests"] = 6] = "TooManyRequests";
5156
+ RecognitionStatus2[RecognitionStatus2["BadRequest"] = 7] = "BadRequest";
5157
+ RecognitionStatus2[RecognitionStatus2["Forbidden"] = 8] = "Forbidden";
5158
+ })(RecognitionStatus || (RecognitionStatus = {}));
5159
+ class TranslationSynthesisEnd {
5160
+ constructor(json) {
5161
+ this.privSynthesisEnd = JSON.parse(json);
5162
+ this.privSynthesisEnd.SynthesisStatus = SynthesisStatus[this.privSynthesisEnd.SynthesisStatus];
5163
+ }
5164
+ static fromJSON(json) {
5165
+ return new TranslationSynthesisEnd(json);
5166
+ }
5167
+ get SynthesisStatus() {
5168
+ return this.privSynthesisEnd.SynthesisStatus;
5169
+ }
5170
+ get FailureReason() {
5171
+ return this.privSynthesisEnd.FailureReason;
5172
+ }
5173
+ }
5174
+ class TranslationHypothesis {
5175
+ constructor(json) {
5176
+ this.privTranslationHypothesis = JSON.parse(json);
5177
+ this.privTranslationHypothesis.Translation.TranslationStatus = TranslationStatus[this.privTranslationHypothesis.Translation.TranslationStatus];
5178
+ }
5179
+ static fromJSON(json) {
5180
+ return new TranslationHypothesis(json);
5181
+ }
5182
+ get Duration() {
5183
+ return this.privTranslationHypothesis.Duration;
5184
+ }
5185
+ get Offset() {
5186
+ return this.privTranslationHypothesis.Offset;
5187
+ }
5188
+ get Text() {
5189
+ return this.privTranslationHypothesis.Text;
5190
+ }
5191
+ get Translation() {
5192
+ return this.privTranslationHypothesis.Translation;
5193
+ }
5194
+ }
5195
+ class TranslationPhrase {
5196
+ constructor(phrase) {
5197
+ this.privTranslationPhrase = phrase;
5198
+ this.privTranslationPhrase.RecognitionStatus = RecognitionStatus[this.privTranslationPhrase.RecognitionStatus];
5199
+ if (this.privTranslationPhrase.Translation !== void 0) {
5200
+ this.privTranslationPhrase.Translation.TranslationStatus = TranslationStatus[this.privTranslationPhrase.Translation.TranslationStatus];
5201
+ }
5202
+ }
5203
+ static fromJSON(json) {
5204
+ return new TranslationPhrase(JSON.parse(json));
5205
+ }
5206
+ static fromTranslationResponse(translationResponse) {
5207
+ Contracts.throwIfNullOrUndefined(translationResponse, "translationResponse");
5208
+ const phrase = translationResponse.SpeechPhrase;
5209
+ translationResponse.SpeechPhrase = void 0;
5210
+ phrase.Translation = translationResponse;
5211
+ phrase.Text = phrase.DisplayText;
5212
+ return new TranslationPhrase(phrase);
5213
+ }
5214
+ get RecognitionStatus() {
5215
+ return this.privTranslationPhrase.RecognitionStatus;
5216
+ }
5217
+ get Offset() {
5218
+ return this.privTranslationPhrase.Offset;
5219
+ }
5220
+ get Duration() {
5221
+ return this.privTranslationPhrase.Duration;
5222
+ }
5223
+ get Text() {
5224
+ return this.privTranslationPhrase.Text;
5225
+ }
5226
+ get Translation() {
5227
+ return this.privTranslationPhrase.Translation;
5228
+ }
5229
+ }
5230
+ var __awaiter$9 = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
5231
+ function adopt(value) {
5232
+ return value instanceof P ? value : new P(function(resolve) {
5233
+ resolve(value);
5234
+ });
5235
+ }
5236
+ return new (P || (P = Promise))(function(resolve, reject) {
5237
+ function fulfilled(value) {
5238
+ try {
5239
+ step(generator.next(value));
5240
+ } catch (e) {
5241
+ reject(e);
5242
+ }
5243
+ }
5244
+ function rejected(value) {
5245
+ try {
5246
+ step(generator["throw"](value));
5247
+ } catch (e) {
5248
+ reject(e);
5249
+ }
5250
+ }
5251
+ function step(result) {
5252
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
5253
+ }
5254
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
5255
+ });
5256
+ };
5257
+ class TranslationServiceRecognizer extends ServiceRecognizerBase {
5258
+ constructor(authentication, connectionFactory, audioSource, recognizerConfig, translationRecognizer) {
5259
+ super(authentication, connectionFactory, audioSource, recognizerConfig, translationRecognizer);
5260
+ this.privTranslationRecognizer = translationRecognizer;
5261
+ this.connectionEvents.attach((connectionEvent) => {
5262
+ if (connectionEvent.name === "ConnectionEstablishedEvent") {
5263
+ this.privTranslationRecognizer.onConnection();
5264
+ } else if (connectionEvent.name === "ConnectionClosedEvent") {
5265
+ void this.privTranslationRecognizer.onDisconnection();
5266
+ }
5267
+ });
5268
+ }
5269
+ processTypeSpecificMessages(connectionMessage) {
5270
+ return __awaiter$9(this, void 0, void 0, function* () {
5271
+ const resultProps = new PropertyCollection();
5272
+ let processed = false;
5273
+ const handleTranslationPhrase = (translatedPhrase) => __awaiter$9(this, void 0, void 0, function* () {
5274
+ this.privRequestSession.onPhraseRecognized(this.privRequestSession.currentTurnAudioOffset + translatedPhrase.Offset + translatedPhrase.Duration);
5275
+ if (translatedPhrase.RecognitionStatus === RecognitionStatus.Success) {
5276
+ const result = this.fireEventForResult(translatedPhrase, resultProps);
5277
+ if (!!this.privTranslationRecognizer.recognized) {
5278
+ try {
5279
+ this.privTranslationRecognizer.recognized(this.privTranslationRecognizer, result);
5280
+ } catch (error) {
5281
+ }
5282
+ }
5283
+ if (!!this.privSuccessCallback) {
5284
+ try {
5285
+ this.privSuccessCallback(result.result);
5286
+ } catch (e) {
5287
+ if (!!this.privErrorCallback) {
5288
+ this.privErrorCallback(e);
5289
+ }
5290
+ }
5291
+ this.privSuccessCallback = void 0;
5292
+ this.privErrorCallback = void 0;
5293
+ }
5294
+ } else {
5295
+ const reason = EnumTranslation.implTranslateRecognitionResult(translatedPhrase.RecognitionStatus);
5296
+ const result = new TranslationRecognitionResult(void 0, this.privRequestSession.requestId, reason, translatedPhrase.Text, translatedPhrase.Duration, this.privRequestSession.currentTurnAudioOffset + translatedPhrase.Offset, void 0, connectionMessage.textBody, resultProps);
5297
+ if (reason === ResultReason.Canceled) {
5298
+ const cancelReason = EnumTranslation.implTranslateCancelResult(translatedPhrase.RecognitionStatus);
5299
+ const cancellationErrorCode = EnumTranslation.implTranslateCancelErrorCode(translatedPhrase.RecognitionStatus);
5300
+ yield this.cancelRecognitionLocal(cancelReason, cancellationErrorCode, EnumTranslation.implTranslateErrorDetails(cancellationErrorCode));
5301
+ } else {
5302
+ if (!(this.privRequestSession.isSpeechEnded && reason === ResultReason.NoMatch && translatedPhrase.RecognitionStatus !== RecognitionStatus.InitialSilenceTimeout)) {
5303
+ const ev = new TranslationRecognitionEventArgs(result, result.offset, this.privRequestSession.sessionId);
5304
+ if (!!this.privTranslationRecognizer.recognized) {
5305
+ try {
5306
+ this.privTranslationRecognizer.recognized(this.privTranslationRecognizer, ev);
5307
+ } catch (error) {
5308
+ }
5309
+ }
5310
+ }
5311
+ if (!!this.privSuccessCallback) {
5312
+ try {
5313
+ this.privSuccessCallback(result);
5314
+ } catch (e) {
5315
+ if (!!this.privErrorCallback) {
5316
+ this.privErrorCallback(e);
5317
+ }
5318
+ }
5319
+ this.privSuccessCallback = void 0;
5320
+ this.privErrorCallback = void 0;
5321
+ }
5322
+ }
5323
+ processed = true;
5324
+ }
5325
+ });
5326
+ if (connectionMessage.messageType === MessageType.Text) {
5327
+ resultProps.setProperty(PropertyId.SpeechServiceResponse_JsonResult, connectionMessage.textBody);
5328
+ }
5329
+ switch (connectionMessage.path.toLowerCase()) {
5330
+ case "translation.hypothesis":
5331
+ const result = this.fireEventForResult(TranslationHypothesis.fromJSON(connectionMessage.textBody), resultProps);
5332
+ this.privRequestSession.onHypothesis(this.privRequestSession.currentTurnAudioOffset + result.offset);
5333
+ if (!!this.privTranslationRecognizer.recognizing) {
5334
+ try {
5335
+ this.privTranslationRecognizer.recognizing(this.privTranslationRecognizer, result);
5336
+ } catch (error) {
5337
+ }
5338
+ }
5339
+ processed = true;
5340
+ break;
5341
+ case "translation.response":
5342
+ const phrase = JSON.parse(connectionMessage.textBody);
5343
+ if (!!phrase.SpeechPhrase) {
5344
+ yield handleTranslationPhrase(TranslationPhrase.fromTranslationResponse(phrase));
5345
+ }
5346
+ break;
5347
+ case "translation.phrase":
5348
+ yield handleTranslationPhrase(TranslationPhrase.fromJSON(connectionMessage.textBody));
5349
+ break;
5350
+ case "translation.synthesis":
5351
+ this.sendSynthesisAudio(connectionMessage.binaryBody, this.privRequestSession.sessionId);
5352
+ processed = true;
5353
+ break;
5354
+ case "translation.synthesis.end":
5355
+ const synthEnd = TranslationSynthesisEnd.fromJSON(connectionMessage.textBody);
5356
+ switch (synthEnd.SynthesisStatus) {
5357
+ case SynthesisStatus.Error:
5358
+ if (!!this.privTranslationRecognizer.synthesizing) {
5359
+ const result2 = new TranslationSynthesisResult(ResultReason.Canceled, void 0);
5360
+ const retEvent = new TranslationSynthesisEventArgs(result2, this.privRequestSession.sessionId);
5361
+ try {
5362
+ this.privTranslationRecognizer.synthesizing(this.privTranslationRecognizer, retEvent);
5363
+ } catch (error) {
5364
+ }
5365
+ }
5366
+ if (!!this.privTranslationRecognizer.canceled) {
5367
+ const canceledResult = new TranslationRecognitionCanceledEventArgs(this.privRequestSession.sessionId, CancellationReason.Error, synthEnd.FailureReason, CancellationErrorCode.ServiceError, null);
5368
+ try {
5369
+ this.privTranslationRecognizer.canceled(this.privTranslationRecognizer, canceledResult);
5370
+ } catch (error) {
5371
+ }
5372
+ }
5373
+ break;
5374
+ case SynthesisStatus.Success:
5375
+ this.sendSynthesisAudio(void 0, this.privRequestSession.sessionId);
5376
+ break;
5377
+ }
5378
+ processed = true;
5379
+ break;
5380
+ }
5381
+ return processed;
5382
+ });
5383
+ }
5384
+ cancelRecognition(sessionId, requestId, cancellationReason, errorCode, error) {
5385
+ const properties = new PropertyCollection();
5386
+ properties.setProperty(CancellationErrorCodePropertyName, CancellationErrorCode[errorCode]);
5387
+ if (!!this.privTranslationRecognizer.canceled) {
5388
+ const cancelEvent = new TranslationRecognitionCanceledEventArgs(sessionId, cancellationReason, error, errorCode, void 0);
5389
+ try {
5390
+ this.privTranslationRecognizer.canceled(this.privTranslationRecognizer, cancelEvent);
5391
+ } catch (_a) {
5392
+ }
5393
+ }
5394
+ if (!!this.privSuccessCallback) {
5395
+ const result = new TranslationRecognitionResult(
5396
+ void 0,
5397
+ requestId,
5398
+ ResultReason.Canceled,
5399
+ void 0,
5400
+ void 0,
5401
+ void 0,
5402
+ error,
5403
+ void 0,
5404
+ properties
5405
+ );
5406
+ try {
5407
+ this.privSuccessCallback(result);
5408
+ this.privSuccessCallback = void 0;
5409
+ } catch (_b) {
5410
+ }
5411
+ }
5412
+ }
5413
+ fireEventForResult(serviceResult, properties) {
5414
+ let translations;
5415
+ if (void 0 !== serviceResult.Translation.Translations) {
5416
+ translations = new Translations();
5417
+ for (const translation of serviceResult.Translation.Translations) {
5418
+ translations.set(translation.Language, translation.Text || translation.DisplayText);
5419
+ }
5420
+ }
5421
+ let resultReason;
5422
+ if (serviceResult instanceof TranslationPhrase) {
5423
+ if (serviceResult.Translation.TranslationStatus === TranslationStatus.Success) {
5424
+ resultReason = ResultReason.TranslatedSpeech;
5425
+ } else {
5426
+ resultReason = ResultReason.RecognizedSpeech;
5427
+ }
5428
+ } else {
5429
+ resultReason = ResultReason.TranslatingSpeech;
5430
+ }
5431
+ const offset = serviceResult.Offset + this.privRequestSession.currentTurnAudioOffset;
5432
+ const result = new TranslationRecognitionResult(translations, this.privRequestSession.requestId, resultReason, serviceResult.Text, serviceResult.Duration, offset, serviceResult.Translation.FailureReason, JSON.stringify(serviceResult), properties);
5433
+ const ev = new TranslationRecognitionEventArgs(result, offset, this.privRequestSession.sessionId);
5434
+ return ev;
5435
+ }
5436
+ sendSynthesisAudio(audio, sessionId) {
5437
+ const reason = void 0 === audio ? ResultReason.SynthesizingAudioCompleted : ResultReason.SynthesizingAudio;
5438
+ const result = new TranslationSynthesisResult(reason, audio);
5439
+ const retEvent = new TranslationSynthesisEventArgs(result, sessionId);
5440
+ if (!!this.privTranslationRecognizer.synthesizing) {
5441
+ try {
5442
+ this.privTranslationRecognizer.synthesizing(this.privTranslationRecognizer, retEvent);
5443
+ } catch (error) {
5444
+ }
5445
+ }
5446
+ }
5447
+ }
5448
+ class SpeechDetected {
5449
+ constructor(json) {
5450
+ this.privSpeechStartDetected = JSON.parse(json);
5451
+ }
5452
+ static fromJSON(json) {
5453
+ return new SpeechDetected(json);
5454
+ }
5455
+ get Offset() {
5456
+ return this.privSpeechStartDetected.Offset;
5457
+ }
5458
+ }
5459
+ class ServiceTelemetryListener {
5460
+ constructor(requestId, audioSourceId, audioNodeId) {
5461
+ this.privIsDisposed = false;
5462
+ this.privListeningTriggerMetric = null;
5463
+ this.privMicMetric = null;
5464
+ this.privConnectionEstablishMetric = null;
5465
+ this.privRequestId = requestId;
5466
+ this.privAudioSourceId = audioSourceId;
5467
+ this.privAudioNodeId = audioNodeId;
5468
+ this.privReceivedMessages = {};
5469
+ this.privPhraseLatencies = [];
5470
+ this.privHypothesisLatencies = [];
5471
+ }
5472
+ phraseReceived(audioReceivedTime) {
5473
+ if (audioReceivedTime > 0) {
5474
+ this.privPhraseLatencies.push(Date.now() - audioReceivedTime);
5475
+ }
5476
+ }
5477
+ hypothesisReceived(audioReceivedTime) {
5478
+ if (audioReceivedTime > 0) {
5479
+ this.privHypothesisLatencies.push(Date.now() - audioReceivedTime);
5480
+ }
5481
+ }
5482
+ onEvent(e) {
5483
+ if (this.privIsDisposed) {
5484
+ return;
5485
+ }
5486
+ if (e instanceof RecognitionTriggeredEvent && e.requestId === this.privRequestId) {
5487
+ this.privListeningTriggerMetric = {
5488
+ End: e.eventTime,
5489
+ Name: "ListeningTrigger",
5490
+ Start: e.eventTime
5491
+ };
5492
+ }
5493
+ if (e instanceof AudioStreamNodeAttachingEvent && e.audioSourceId === this.privAudioSourceId && e.audioNodeId === this.privAudioNodeId) {
5494
+ this.privMicStartTime = e.eventTime;
5495
+ }
5496
+ if (e instanceof AudioStreamNodeAttachedEvent && e.audioSourceId === this.privAudioSourceId && e.audioNodeId === this.privAudioNodeId) {
5497
+ this.privMicStartTime = e.eventTime;
5498
+ }
5499
+ if (e instanceof AudioSourceErrorEvent && e.audioSourceId === this.privAudioSourceId) {
5500
+ if (!this.privMicMetric) {
5501
+ this.privMicMetric = {
5502
+ End: e.eventTime,
5503
+ Error: e.error,
5504
+ Name: "Microphone",
5505
+ Start: this.privMicStartTime
5506
+ };
5507
+ }
5508
+ }
5509
+ if (e instanceof AudioStreamNodeErrorEvent && e.audioSourceId === this.privAudioSourceId && e.audioNodeId === this.privAudioNodeId) {
5510
+ if (!this.privMicMetric) {
5511
+ this.privMicMetric = {
5512
+ End: e.eventTime,
5513
+ Error: e.error,
5514
+ Name: "Microphone",
5515
+ Start: this.privMicStartTime
5516
+ };
5517
+ }
5518
+ }
5519
+ if (e instanceof AudioStreamNodeDetachedEvent && e.audioSourceId === this.privAudioSourceId && e.audioNodeId === this.privAudioNodeId) {
5520
+ if (!this.privMicMetric) {
5521
+ this.privMicMetric = {
5522
+ End: e.eventTime,
5523
+ Name: "Microphone",
5524
+ Start: this.privMicStartTime
5525
+ };
5526
+ }
5527
+ }
5528
+ if (e instanceof ConnectingToServiceEvent && e.requestId === this.privRequestId) {
5529
+ this.privConnectionId = e.sessionId;
5530
+ }
5531
+ if (e instanceof ConnectionStartEvent && e.connectionId === this.privConnectionId) {
5532
+ this.privConnectionStartTime = e.eventTime;
5533
+ }
5534
+ if (e instanceof ConnectionEstablishedEvent && e.connectionId === this.privConnectionId) {
5535
+ if (!this.privConnectionEstablishMetric) {
5536
+ this.privConnectionEstablishMetric = {
5537
+ End: e.eventTime,
5538
+ Id: this.privConnectionId,
5539
+ Name: "Connection",
5540
+ Start: this.privConnectionStartTime
5541
+ };
5542
+ }
5543
+ }
5544
+ if (e instanceof ConnectionEstablishErrorEvent && e.connectionId === this.privConnectionId) {
5545
+ if (!this.privConnectionEstablishMetric) {
5546
+ this.privConnectionEstablishMetric = {
5547
+ End: e.eventTime,
5548
+ Error: this.getConnectionError(e.statusCode),
5549
+ Id: this.privConnectionId,
5550
+ Name: "Connection",
5551
+ Start: this.privConnectionStartTime
5552
+ };
5553
+ }
5554
+ }
5555
+ if (e instanceof ConnectionMessageReceivedEvent && e.connectionId === this.privConnectionId) {
5556
+ if (e.message && e.message.headers && e.message.headers.path) {
5557
+ if (!this.privReceivedMessages[e.message.headers.path]) {
5558
+ this.privReceivedMessages[e.message.headers.path] = new Array();
5559
+ }
5560
+ const maxMessagesToSend = 50;
5561
+ if (this.privReceivedMessages[e.message.headers.path].length < maxMessagesToSend) {
5562
+ this.privReceivedMessages[e.message.headers.path].push(e.networkReceivedTime);
5563
+ }
5564
+ }
5565
+ }
5566
+ }
5567
+ getTelemetry() {
5568
+ const metrics = new Array();
5569
+ if (this.privListeningTriggerMetric) {
5570
+ metrics.push(this.privListeningTriggerMetric);
5571
+ }
5572
+ if (this.privMicMetric) {
5573
+ metrics.push(this.privMicMetric);
5574
+ }
5575
+ if (this.privConnectionEstablishMetric) {
5576
+ metrics.push(this.privConnectionEstablishMetric);
5577
+ }
5578
+ if (this.privPhraseLatencies.length > 0) {
5579
+ metrics.push({
5580
+ PhraseLatencyMs: this.privPhraseLatencies
5581
+ });
5582
+ }
5583
+ if (this.privHypothesisLatencies.length > 0) {
5584
+ metrics.push({
5585
+ FirstHypothesisLatencyMs: this.privHypothesisLatencies
5586
+ });
5587
+ }
5588
+ const telemetry = {
5589
+ Metrics: metrics,
5590
+ ReceivedMessages: this.privReceivedMessages
5591
+ };
5592
+ const json = JSON.stringify(telemetry);
5593
+ this.privReceivedMessages = {};
5594
+ this.privListeningTriggerMetric = null;
5595
+ this.privMicMetric = null;
5596
+ this.privConnectionEstablishMetric = null;
5597
+ this.privPhraseLatencies = [];
5598
+ this.privHypothesisLatencies = [];
5599
+ return json;
5600
+ }
5601
+ get hasTelemetry() {
5602
+ return Object.keys(this.privReceivedMessages).length !== 0 || this.privListeningTriggerMetric !== null || this.privMicMetric !== null || this.privConnectionEstablishMetric !== null || this.privPhraseLatencies.length !== 0 || this.privHypothesisLatencies.length !== 0;
5603
+ }
5604
+ dispose() {
5605
+ this.privIsDisposed = true;
5606
+ }
5607
+ getConnectionError(statusCode) {
5608
+ switch (statusCode) {
5609
+ case 400:
5610
+ case 1002:
5611
+ case 1003:
5612
+ case 1005:
5613
+ case 1007:
5614
+ case 1008:
5615
+ case 1009:
5616
+ return "BadRequest";
5617
+ case 401:
5618
+ return "Unauthorized";
5619
+ case 403:
5620
+ return "Forbidden";
5621
+ case 503:
5622
+ case 1001:
5623
+ return "ServerUnavailable";
5624
+ case 500:
5625
+ case 1011:
5626
+ return "ServerError";
5627
+ case 408:
5628
+ case 504:
5629
+ return "Timeout";
5630
+ default:
5631
+ return "statuscode:" + statusCode.toString();
5632
+ }
5633
+ }
5634
+ }
5635
+ var __awaiter$8 = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
5636
+ function adopt(value) {
5637
+ return value instanceof P ? value : new P(function(resolve) {
5638
+ resolve(value);
5639
+ });
5640
+ }
5641
+ return new (P || (P = Promise))(function(resolve, reject) {
5642
+ function fulfilled(value) {
5643
+ try {
5644
+ step(generator.next(value));
5645
+ } catch (e) {
5646
+ reject(e);
5647
+ }
5648
+ }
5649
+ function rejected(value) {
5650
+ try {
5651
+ step(generator["throw"](value));
5652
+ } catch (e) {
5653
+ reject(e);
5654
+ }
5655
+ }
5656
+ function step(result) {
5657
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
5658
+ }
5659
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
5660
+ });
5661
+ };
5662
+ class RequestSession {
5663
+ constructor(audioSourceId) {
5664
+ this.privIsDisposed = false;
5665
+ this.privDetachables = new Array();
5666
+ this.privIsAudioNodeDetached = false;
5667
+ this.privIsRecognizing = false;
5668
+ this.privIsSpeechEnded = false;
5669
+ this.privTurnStartAudioOffset = 0;
5670
+ this.privLastRecoOffset = 0;
5671
+ this.privHypothesisReceived = false;
5672
+ this.privBytesSent = 0;
5673
+ this.privRecogNumber = 0;
5674
+ this.privInTurn = false;
5675
+ this.privConnectionAttempts = 0;
5676
+ this.privAudioSourceId = audioSourceId;
5677
+ this.privRequestId = createNoDashGuid();
5678
+ this.privAudioNodeId = createNoDashGuid();
5679
+ this.privTurnDeferral = new Deferred();
5680
+ this.privTurnDeferral.resolve();
5681
+ }
5682
+ get sessionId() {
5683
+ return this.privSessionId;
5684
+ }
5685
+ get requestId() {
5686
+ return this.privRequestId;
5687
+ }
5688
+ get audioNodeId() {
5689
+ return this.privAudioNodeId;
5690
+ }
5691
+ get turnCompletionPromise() {
5692
+ return this.privTurnDeferral.promise;
5693
+ }
5694
+ get isSpeechEnded() {
5695
+ return this.privIsSpeechEnded;
5696
+ }
5697
+ get isRecognizing() {
5698
+ return this.privIsRecognizing;
5699
+ }
5700
+ get currentTurnAudioOffset() {
5701
+ return this.privTurnStartAudioOffset;
5702
+ }
5703
+ get recogNumber() {
5704
+ return this.privRecogNumber;
5705
+ }
5706
+ get numConnectionAttempts() {
5707
+ return this.privConnectionAttempts;
5708
+ }
5709
+ get bytesSent() {
5710
+ return this.privBytesSent;
5711
+ }
5712
+ listenForServiceTelemetry(eventSource) {
5713
+ if (!!this.privServiceTelemetryListener) {
5714
+ this.privDetachables.push(eventSource.attachListener(this.privServiceTelemetryListener));
3446
5715
  }
3447
- return deferral.promise;
3448
5716
  }
3449
- fromConnectionMessage(message) {
3450
- const deferral = new Deferred();
3451
- try {
3452
- if (message.messageType === MessageType.Text) {
3453
- const payload = `${this.makeHeaders(message)}${CRLF}${message.textBody ? message.textBody : ""}`;
3454
- deferral.resolve(new RawWebsocketMessage(MessageType.Text, payload, message.id));
3455
- } else if (message.messageType === MessageType.Binary) {
3456
- const headersString = this.makeHeaders(message);
3457
- const content = message.binaryBody;
3458
- const headerBuffer = this.stringToArrayBuffer(headersString);
3459
- const headerInt8Array = new Int8Array(headerBuffer);
3460
- const headerLength = headerInt8Array.byteLength;
3461
- const payloadInt8Array = new Int8Array(2 + headerLength + (content ? content.byteLength : 0));
3462
- payloadInt8Array[0] = headerLength >> 8 & 255;
3463
- payloadInt8Array[1] = headerLength & 255;
3464
- payloadInt8Array.set(headerInt8Array, 2);
3465
- if (content) {
3466
- const bodyInt8Array = new Int8Array(content);
3467
- payloadInt8Array.set(bodyInt8Array, 2 + headerLength);
5717
+ startNewRecognition() {
5718
+ this.privIsSpeechEnded = false;
5719
+ this.privIsRecognizing = true;
5720
+ this.privTurnStartAudioOffset = 0;
5721
+ this.privLastRecoOffset = 0;
5722
+ this.privRecogNumber++;
5723
+ this.privServiceTelemetryListener = new ServiceTelemetryListener(this.privRequestId, this.privAudioSourceId, this.privAudioNodeId);
5724
+ this.onEvent(new RecognitionTriggeredEvent(this.requestId, this.privSessionId, this.privAudioSourceId, this.privAudioNodeId));
5725
+ }
5726
+ onAudioSourceAttachCompleted(audioNode, isError) {
5727
+ return __awaiter$8(this, void 0, void 0, function* () {
5728
+ this.privAudioNode = audioNode;
5729
+ this.privIsAudioNodeDetached = false;
5730
+ if (isError) {
5731
+ yield this.onComplete();
5732
+ } else {
5733
+ this.onEvent(new ListeningStartedEvent(this.privRequestId, this.privSessionId, this.privAudioSourceId, this.privAudioNodeId));
5734
+ }
5735
+ });
5736
+ }
5737
+ onPreConnectionStart(authFetchEventId, connectionId) {
5738
+ this.privAuthFetchEventId = authFetchEventId;
5739
+ this.privSessionId = connectionId;
5740
+ this.onEvent(new ConnectingToServiceEvent(this.privRequestId, this.privAuthFetchEventId, this.privSessionId));
5741
+ }
5742
+ onAuthCompleted(isError) {
5743
+ return __awaiter$8(this, void 0, void 0, function* () {
5744
+ if (isError) {
5745
+ yield this.onComplete();
5746
+ }
5747
+ });
5748
+ }
5749
+ onConnectionEstablishCompleted(statusCode, reason) {
5750
+ return __awaiter$8(this, void 0, void 0, function* () {
5751
+ if (statusCode === 200) {
5752
+ this.onEvent(new RecognitionStartedEvent(this.requestId, this.privAudioSourceId, this.privAudioNodeId, this.privAuthFetchEventId, this.privSessionId));
5753
+ if (!!this.privAudioNode) {
5754
+ this.privAudioNode.replay();
3468
5755
  }
3469
- const payload = payloadInt8Array.buffer;
3470
- deferral.resolve(new RawWebsocketMessage(MessageType.Binary, payload, message.id));
5756
+ this.privTurnStartAudioOffset = this.privLastRecoOffset;
5757
+ this.privBytesSent = 0;
5758
+ return;
5759
+ } else if (statusCode === 403) {
5760
+ yield this.onComplete();
3471
5761
  }
3472
- } catch (e) {
3473
- deferral.reject(`Error formatting the message. ${e}`);
5762
+ });
5763
+ }
5764
+ onServiceTurnEndResponse(continuousRecognition) {
5765
+ return __awaiter$8(this, void 0, void 0, function* () {
5766
+ this.privTurnDeferral.resolve();
5767
+ if (!continuousRecognition || this.isSpeechEnded) {
5768
+ yield this.onComplete();
5769
+ this.privInTurn = false;
5770
+ } else {
5771
+ this.privTurnStartAudioOffset = this.privLastRecoOffset;
5772
+ this.privAudioNode.replay();
5773
+ }
5774
+ });
5775
+ }
5776
+ onSpeechContext() {
5777
+ this.privRequestId = createNoDashGuid();
5778
+ }
5779
+ onServiceTurnStartResponse() {
5780
+ if (!!this.privTurnDeferral && !!this.privInTurn) {
5781
+ this.privTurnDeferral.reject("Another turn started before current completed.");
5782
+ this.privTurnDeferral.promise.then().catch(() => {
5783
+ });
3474
5784
  }
3475
- return deferral.promise;
5785
+ this.privInTurn = true;
5786
+ this.privTurnDeferral = new Deferred();
3476
5787
  }
3477
- makeHeaders(message) {
3478
- let headersString = "";
3479
- if (message.headers) {
3480
- for (const header in message.headers) {
3481
- if (header) {
3482
- headersString += `${header}: ${message.headers[header]}${CRLF}`;
5788
+ onHypothesis(offset) {
5789
+ if (!this.privHypothesisReceived) {
5790
+ this.privHypothesisReceived = true;
5791
+ this.privServiceTelemetryListener.hypothesisReceived(this.privAudioNode.findTimeAtOffset(offset));
5792
+ }
5793
+ }
5794
+ onPhraseRecognized(offset) {
5795
+ this.privServiceTelemetryListener.phraseReceived(this.privAudioNode.findTimeAtOffset(offset));
5796
+ this.onServiceRecognized(offset);
5797
+ }
5798
+ onServiceRecognized(offset) {
5799
+ this.privLastRecoOffset = offset;
5800
+ this.privHypothesisReceived = false;
5801
+ this.privAudioNode.shrinkBuffers(offset);
5802
+ this.privConnectionAttempts = 0;
5803
+ }
5804
+ onAudioSent(bytesSent) {
5805
+ this.privBytesSent += bytesSent;
5806
+ }
5807
+ onRetryConnection() {
5808
+ this.privConnectionAttempts++;
5809
+ }
5810
+ dispose() {
5811
+ return __awaiter$8(this, void 0, void 0, function* () {
5812
+ if (!this.privIsDisposed) {
5813
+ this.privIsDisposed = true;
5814
+ for (const detachable of this.privDetachables) {
5815
+ yield detachable.detach();
5816
+ }
5817
+ if (!!this.privServiceTelemetryListener) {
5818
+ this.privServiceTelemetryListener.dispose();
3483
5819
  }
5820
+ this.privIsRecognizing = false;
3484
5821
  }
5822
+ });
5823
+ }
5824
+ getTelemetry() {
5825
+ if (this.privServiceTelemetryListener.hasTelemetry) {
5826
+ return this.privServiceTelemetryListener.getTelemetry();
5827
+ } else {
5828
+ return null;
3485
5829
  }
3486
- return headersString;
3487
5830
  }
3488
- parseHeaders(headersString) {
3489
- const headers = {};
3490
- if (headersString) {
3491
- const headerMatches = headersString.match(/[^\r\n]+/g);
3492
- if (headers) {
3493
- for (const header of headerMatches) {
3494
- if (header) {
3495
- const separatorIndex = header.indexOf(":");
3496
- const headerName = separatorIndex > 0 ? header.substr(0, separatorIndex).trim().toLowerCase() : header;
3497
- const headerValue = separatorIndex > 0 && header.length > separatorIndex + 1 ? header.substr(separatorIndex + 1).trim() : "";
3498
- headers[headerName] = headerValue;
3499
- }
5831
+ onStopRecognizing() {
5832
+ return __awaiter$8(this, void 0, void 0, function* () {
5833
+ yield this.onComplete();
5834
+ });
5835
+ }
5836
+ onSpeechEnded() {
5837
+ this.privIsSpeechEnded = true;
5838
+ }
5839
+ onEvent(event) {
5840
+ if (!!this.privServiceTelemetryListener) {
5841
+ this.privServiceTelemetryListener.onEvent(event);
5842
+ }
5843
+ Events.instance.onEvent(event);
5844
+ }
5845
+ onComplete() {
5846
+ return __awaiter$8(this, void 0, void 0, function* () {
5847
+ if (!!this.privIsRecognizing) {
5848
+ this.privIsRecognizing = false;
5849
+ yield this.detachAudioNode();
5850
+ }
5851
+ });
5852
+ }
5853
+ detachAudioNode() {
5854
+ return __awaiter$8(this, void 0, void 0, function* () {
5855
+ if (!this.privIsAudioNodeDetached) {
5856
+ this.privIsAudioNodeDetached = true;
5857
+ if (this.privAudioNode) {
5858
+ yield this.privAudioNode.detach();
3500
5859
  }
3501
5860
  }
5861
+ });
5862
+ }
5863
+ }
5864
+ class SpeechContext {
5865
+ constructor(dynamicGrammar) {
5866
+ this.privContext = {};
5867
+ this.privDynamicGrammar = dynamicGrammar;
5868
+ }
5869
+ setSection(sectionName, value) {
5870
+ this.privContext[sectionName] = value;
5871
+ }
5872
+ setPronunciationAssessmentParams(params) {
5873
+ if (this.privContext.phraseDetection === void 0) {
5874
+ this.privContext.phraseDetection = {
5875
+ enrichment: {
5876
+ pronunciationAssessment: {}
5877
+ }
5878
+ };
5879
+ }
5880
+ this.privContext.phraseDetection.enrichment.pronunciationAssessment = JSON.parse(params);
5881
+ this.setWordLevelTimings();
5882
+ this.privContext.phraseOutput.detailed.options.push("PronunciationAssessment");
5883
+ if (this.privContext.phraseOutput.detailed.options.indexOf("SNR") === -1) {
5884
+ this.privContext.phraseOutput.detailed.options.push("SNR");
3502
5885
  }
3503
- return headers;
3504
5886
  }
3505
- stringToArrayBuffer(str) {
3506
- const buffer = new ArrayBuffer(str.length);
3507
- const view = new DataView(buffer);
3508
- for (let i = 0; i < str.length; i++) {
3509
- view.setUint8(i, str.charCodeAt(i));
5887
+ setWordLevelTimings() {
5888
+ if (this.privContext.phraseOutput === void 0) {
5889
+ this.privContext.phraseOutput = {
5890
+ detailed: {
5891
+ options: []
5892
+ },
5893
+ format: {}
5894
+ };
3510
5895
  }
3511
- return buffer;
5896
+ if (this.privContext.phraseOutput.detailed === void 0) {
5897
+ this.privContext.phraseOutput.detailed = {
5898
+ options: []
5899
+ };
5900
+ }
5901
+ this.privContext.phraseOutput.format = "Detailed";
5902
+ if (this.privContext.phraseOutput.detailed.options.indexOf("WordTimings") === -1) {
5903
+ this.privContext.phraseOutput.detailed.options.push("WordTimings");
5904
+ }
5905
+ }
5906
+ toJSON() {
5907
+ const dgi = this.privDynamicGrammar.generateGrammarObject();
5908
+ this.setSection("dgi", dgi);
5909
+ const ret = JSON.stringify(this.privContext);
5910
+ return ret;
3512
5911
  }
3513
5912
  }
3514
- class SpeechSynthesisConnectionFactory {
3515
- constructor() {
3516
- this.synthesisUri = "/cognitiveservices/websocket/v1";
5913
+ class DynamicGrammarBuilder {
5914
+ addPhrase(phrase) {
5915
+ if (!this.privPhrases) {
5916
+ this.privPhrases = [];
5917
+ }
5918
+ if (phrase instanceof Array) {
5919
+ this.privPhrases = this.privPhrases.concat(phrase);
5920
+ } else {
5921
+ this.privPhrases.push(phrase);
5922
+ }
3517
5923
  }
3518
- create(config, authInfo, connectionId) {
3519
- let endpoint = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, void 0);
3520
- const region = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region, void 0);
3521
- const hostSuffix = ConnectionFactoryBase.getHostSuffix(region);
3522
- const endpointId = config.parameters.getProperty(PropertyId.SpeechServiceConnection_EndpointId, void 0);
3523
- const hostPrefix = endpointId === void 0 ? "tts" : "voice";
3524
- const host = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, "wss://" + region + "." + hostPrefix + ".speech" + hostSuffix);
3525
- const queryParams = {};
3526
- if (!endpoint) {
3527
- endpoint = host + this.synthesisUri;
5924
+ clearPhrases() {
5925
+ this.privPhrases = void 0;
5926
+ }
5927
+ addReferenceGrammar(grammar) {
5928
+ if (!this.privGrammars) {
5929
+ this.privGrammars = [];
3528
5930
  }
3529
- const headers = {};
3530
- if (authInfo.token !== void 0 && authInfo.token !== "") {
3531
- headers[authInfo.headerName] = authInfo.token;
5931
+ if (grammar instanceof Array) {
5932
+ this.privGrammars = this.privGrammars.concat(grammar);
5933
+ } else {
5934
+ this.privGrammars.push(grammar);
3532
5935
  }
3533
- headers[HeaderNames.ConnectionId] = connectionId;
3534
- if (endpointId !== void 0) {
3535
- headers[QueryParameterNames.CustomVoiceDeploymentId] = endpointId;
5936
+ }
5937
+ clearGrammars() {
5938
+ this.privGrammars = void 0;
5939
+ }
5940
+ generateGrammarObject() {
5941
+ if (this.privGrammars === void 0 && this.privPhrases === void 0) {
5942
+ return void 0;
3536
5943
  }
3537
- config.parameters.setProperty(PropertyId.SpeechServiceConnection_Url, endpoint);
3538
- const enableCompression = config.parameters.getProperty("SPEECH-EnableWebsocketCompression", "false") === "true";
3539
- return new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromParameters(config.parameters), enableCompression, connectionId);
5944
+ const retObj = {};
5945
+ retObj.ReferenceGrammars = this.privGrammars;
5946
+ if (void 0 !== this.privPhrases && 0 !== this.privPhrases.length) {
5947
+ const retPhrases = [];
5948
+ this.privPhrases.forEach((value) => {
5949
+ retPhrases.push({
5950
+ Text: value
5951
+ });
5952
+ });
5953
+ retObj.Groups = [{ Type: "Generic", Items: retPhrases }];
5954
+ }
5955
+ return retObj;
3540
5956
  }
3541
5957
  }
3542
5958
  class AgentConfig {
@@ -5564,6 +7980,99 @@ class WebsocketConnection {
5564
7980
  return this.privConnectionMessageAdapter.events;
5565
7981
  }
5566
7982
  }
7983
+ class ReplayableAudioNode {
7984
+ constructor(audioSource, bytesPerSecond) {
7985
+ this.privBuffers = [];
7986
+ this.privReplayOffset = 0;
7987
+ this.privLastShrinkOffset = 0;
7988
+ this.privBufferStartOffset = 0;
7989
+ this.privBufferSerial = 0;
7990
+ this.privBufferedBytes = 0;
7991
+ this.privReplay = false;
7992
+ this.privLastChunkAcquiredTime = 0;
7993
+ this.privAudioNode = audioSource;
7994
+ this.privBytesPerSecond = bytesPerSecond;
7995
+ }
7996
+ id() {
7997
+ return this.privAudioNode.id();
7998
+ }
7999
+ read() {
8000
+ if (!!this.privReplay && this.privBuffers.length !== 0) {
8001
+ const offsetToSeek = this.privReplayOffset - this.privBufferStartOffset;
8002
+ let bytesToSeek = Math.round(offsetToSeek * this.privBytesPerSecond * 1e-7);
8003
+ if (0 !== bytesToSeek % 2) {
8004
+ bytesToSeek++;
8005
+ }
8006
+ let i = 0;
8007
+ while (i < this.privBuffers.length && bytesToSeek >= this.privBuffers[i].chunk.buffer.byteLength) {
8008
+ bytesToSeek -= this.privBuffers[i++].chunk.buffer.byteLength;
8009
+ }
8010
+ if (i < this.privBuffers.length) {
8011
+ const retVal = this.privBuffers[i].chunk.buffer.slice(bytesToSeek);
8012
+ this.privReplayOffset += retVal.byteLength / this.privBytesPerSecond * 1e7;
8013
+ if (i === this.privBuffers.length - 1) {
8014
+ this.privReplay = false;
8015
+ }
8016
+ return Promise.resolve({
8017
+ buffer: retVal,
8018
+ isEnd: false,
8019
+ timeReceived: this.privBuffers[i].chunk.timeReceived
8020
+ });
8021
+ }
8022
+ }
8023
+ return this.privAudioNode.read().then((result) => {
8024
+ if (result && result.buffer) {
8025
+ this.privBuffers.push(new BufferEntry(result, this.privBufferSerial++, this.privBufferedBytes));
8026
+ this.privBufferedBytes += result.buffer.byteLength;
8027
+ }
8028
+ return result;
8029
+ });
8030
+ }
8031
+ detach() {
8032
+ this.privBuffers = void 0;
8033
+ return this.privAudioNode.detach();
8034
+ }
8035
+ replay() {
8036
+ if (this.privBuffers && 0 !== this.privBuffers.length) {
8037
+ this.privReplay = true;
8038
+ this.privReplayOffset = this.privLastShrinkOffset;
8039
+ }
8040
+ }
8041
+ shrinkBuffers(offset) {
8042
+ if (this.privBuffers === void 0 || this.privBuffers.length === 0) {
8043
+ return;
8044
+ }
8045
+ this.privLastShrinkOffset = offset;
8046
+ const offsetToSeek = offset - this.privBufferStartOffset;
8047
+ let bytesToSeek = Math.round(offsetToSeek * this.privBytesPerSecond * 1e-7);
8048
+ let i = 0;
8049
+ while (i < this.privBuffers.length && bytesToSeek >= this.privBuffers[i].chunk.buffer.byteLength) {
8050
+ bytesToSeek -= this.privBuffers[i++].chunk.buffer.byteLength;
8051
+ }
8052
+ this.privBufferStartOffset = Math.round(offset - bytesToSeek / this.privBytesPerSecond * 1e7);
8053
+ this.privBuffers = this.privBuffers.slice(i);
8054
+ }
8055
+ findTimeAtOffset(offset) {
8056
+ if (offset < this.privBufferStartOffset || this.privBuffers === void 0) {
8057
+ return 0;
8058
+ }
8059
+ for (const value of this.privBuffers) {
8060
+ const startOffset = value.byteOffset / this.privBytesPerSecond * 1e7;
8061
+ const endOffset = startOffset + value.chunk.buffer.byteLength / this.privBytesPerSecond * 1e7;
8062
+ if (offset >= startOffset && offset <= endOffset) {
8063
+ return value.chunk.timeReceived;
8064
+ }
8065
+ }
8066
+ return 0;
8067
+ }
8068
+ }
8069
+ class BufferEntry {
8070
+ constructor(chunk, serial, byteOffset) {
8071
+ this.chunk = chunk;
8072
+ this.serial = serial;
8073
+ this.byteOffset = byteOffset;
8074
+ }
8075
+ }
5567
8076
  class ProxyInfo {
5568
8077
  constructor(proxyHostName, proxyPort, proxyUserName, proxyPassword) {
5569
8078
  this.privProxyHostName = proxyHostName;
@@ -5841,6 +8350,83 @@ class RestMessageAdapter {
5841
8350
  return Object.keys(params).map((k) => encodeURIComponent(k) + "=" + encodeURIComponent(params[k])).join("&");
5842
8351
  }
5843
8352
  }
8353
+ class SpeechToText {
8354
+ constructor(key, region, sourceLanguage, targetLanguage = null) {
8355
+ __publicField(this, "key");
8356
+ __publicField(this, "region");
8357
+ __publicField(this, "sourceLanguage");
8358
+ __publicField(this, "targetLanguage");
8359
+ __publicField(this, "recognizer");
8360
+ this.key = key;
8361
+ this.region = region;
8362
+ this.sourceLanguage = sourceLanguage;
8363
+ this.targetLanguage = targetLanguage !== null ? targetLanguage : sourceLanguage;
8364
+ }
8365
+ async start() {
8366
+ await this.registerBindings(document);
8367
+ }
8368
+ async registerBindings(node) {
8369
+ const nodes = node.childNodes;
8370
+ for (let i = 0; i < nodes.length; i++) {
8371
+ if (!nodes[i]) {
8372
+ continue;
8373
+ }
8374
+ const currentNode = nodes[i];
8375
+ if (currentNode.attributes) {
8376
+ if (currentNode.attributes.getNamedItem("co-stt.start")) {
8377
+ await this.handleStartModifier(currentNode, currentNode.attributes.getNamedItem("co-stt.start"));
8378
+ } else if (currentNode.attributes.getNamedItem("co-stt.stop")) {
8379
+ await this.handleStopModifier(currentNode, currentNode.attributes.getNamedItem("co-stt.stop"));
8380
+ }
8381
+ }
8382
+ if (currentNode.childNodes.length > 0) {
8383
+ await this.registerBindings(currentNode);
8384
+ }
8385
+ }
8386
+ }
8387
+ async handleStartModifier(node, attr) {
8388
+ node.addEventListener("click", async (_) => {
8389
+ const speechConfig = SpeechTranslationConfig.fromSubscription(this.key, this.region);
8390
+ speechConfig.speechRecognitionLanguage = this.sourceLanguage;
8391
+ speechConfig.addTargetLanguage(this.targetLanguage);
8392
+ const audioConfig = AudioConfig.fromDefaultMicrophoneInput();
8393
+ this.recognizer = new TranslationRecognizer(speechConfig, audioConfig);
8394
+ document.dispatchEvent(new CustomEvent("COAzureSTTStartedRecording", {}));
8395
+ this.recognizer.recognizeOnceAsync(
8396
+ (result) => {
8397
+ if (result.reason === ResultReason.TranslatedSpeech) {
8398
+ const translation = result.translations.get(this.targetLanguage);
8399
+ const inputElement = document.getElementById(attr.value);
8400
+ if (inputElement !== null) {
8401
+ if (inputElement instanceof HTMLInputElement) {
8402
+ inputElement.value += `${translation} `;
8403
+ } else {
8404
+ inputElement.innerHTML += `${translation} `;
8405
+ }
8406
+ }
8407
+ }
8408
+ this.stop();
8409
+ },
8410
+ (err) => {
8411
+ console.log(err);
8412
+ this.stop();
8413
+ }
8414
+ );
8415
+ });
8416
+ }
8417
+ async handleStopModifier(node, attr) {
8418
+ node.addEventListener("click", async (_) => {
8419
+ await this.stop();
8420
+ });
8421
+ }
8422
+ async stop() {
8423
+ if (this.recognizer !== void 0) {
8424
+ this.recognizer.close();
8425
+ this.recognizer = void 0;
8426
+ }
8427
+ document.dispatchEvent(new CustomEvent("COAzureSTTStoppedRecording", {}));
8428
+ }
8429
+ }
5844
8430
  class TextToSpeech {
5845
8431
  constructor(key, region, voice, rate = 0, pitch = 0) {
5846
8432
  __publicField(this, "key");
@@ -5872,6 +8458,18 @@ class TextToSpeech {
5872
8458
  async start() {
5873
8459
  await this.registerBindings(document);
5874
8460
  }
8461
+ setVoice(voice) {
8462
+ this.voice = voice;
8463
+ return this;
8464
+ }
8465
+ setRate(rate) {
8466
+ this.rate = rate;
8467
+ return this;
8468
+ }
8469
+ setPitch(pitch) {
8470
+ this.pitch = pitch;
8471
+ return this;
8472
+ }
5875
8473
  async registerBindings(node) {
5876
8474
  const nodes = node.childNodes;
5877
8475
  for (let i = 0; i < nodes.length; i++) {
@@ -5972,7 +8570,9 @@ class TextToSpeech {
5972
8570
  if (((_a = node.attributes.getNamedItem("co-tts.highlight")) == null ? void 0 : _a.value) !== "") {
5973
8571
  const newReferenceDiv = document.getElementById(node.attributes.getNamedItem("co-tts.highlight").value);
5974
8572
  this.highlightDiv = newReferenceDiv;
5975
- this.originalHighlightDivInnerHTML = newReferenceDiv.innerHTML;
8573
+ if (newReferenceDiv !== null) {
8574
+ this.originalHighlightDivInnerHTML = newReferenceDiv.innerHTML;
8575
+ }
5976
8576
  } else {
5977
8577
  this.highlightDiv = node;
5978
8578
  this.originalHighlightDivInnerHTML = node.innerHTML;
@@ -6132,4 +8732,4 @@ class TextToSpeech {
6132
8732
  </speak>`;
6133
8733
  }
6134
8734
  }
6135
- export { TextToSpeech as default };
8735
+ export { SpeechToText, TextToSpeech };