@creativeorange/azure-text-to-speech 2.2.1 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8455,6 +8455,7 @@ class TextToSpeech {
8455
8455
  __publicField(this, "currentWord", "");
8456
8456
  __publicField(this, "currentOffset", 0);
8457
8457
  __publicField(this, "wordBoundaryOffset", 0);
8458
+ __publicField(this, "playbackTextOffsetBase");
8458
8459
  __publicField(this, "prevTextOffset", 0);
8459
8460
  __publicField(this, "url", "");
8460
8461
  __publicField(this, "prefetchedAudio", /* @__PURE__ */ new Map());
@@ -8643,6 +8644,7 @@ class TextToSpeech {
8643
8644
  this.player = void 0;
8644
8645
  this.highlightDiv = void 0;
8645
8646
  this.prevTextOffset = 0;
8647
+ this.playbackTextOffsetBase = void 0;
8646
8648
  }
8647
8649
  async startSynthesizer(node, attr) {
8648
8650
  this.speechConfig = SpeechConfig.fromSubscription(this.key, this.region);
@@ -8722,7 +8724,7 @@ class TextToSpeech {
8722
8724
  highlightDiv,
8723
8725
  originalHighlightDivInnerHTML: (_b = highlightDiv == null ? void 0 : highlightDiv.innerHTML) != null ? _b : ""
8724
8726
  });
8725
- offset += text.length + 2;
8727
+ offset += text.length + 1;
8726
8728
  if (!currentNode.hasAttribute("co-tts.next")) {
8727
8729
  break;
8728
8730
  }
@@ -8732,12 +8734,13 @@ class TextToSpeech {
8732
8734
  }
8733
8735
  preparePlaybackChain(chain) {
8734
8736
  this.playbackSegments = chain;
8735
- this.textToRead = chain.map((segment) => segment.text).join(". ");
8737
+ this.textToRead = chain.map((segment) => segment.text).join(" ");
8736
8738
  this.highlightDiv = void 0;
8737
8739
  this.originalHighlightDivInnerHTML = "";
8738
8740
  this.wordEncounters = [];
8739
8741
  this.previousWordBoundary = void 0;
8740
8742
  this.prevTextOffset = 0;
8743
+ this.playbackTextOffsetBase = void 0;
8741
8744
  this.currentWord = "";
8742
8745
  this.currentOffset = 0;
8743
8746
  this.wordBoundaryOffset = 0;
@@ -8768,13 +8771,17 @@ class TextToSpeech {
8768
8771
  this.resetPlaybackSegments();
8769
8772
  return;
8770
8773
  }
8771
- const segment = this.playbackSegments.find((candidate) => wordBoundary.textOffset >= candidate.start && wordBoundary.textOffset < candidate.end);
8774
+ if (this.playbackTextOffsetBase === void 0) {
8775
+ this.playbackTextOffsetBase = wordBoundary.textOffset;
8776
+ }
8777
+ const normalizedTextOffset = Math.max(0, wordBoundary.textOffset - this.playbackTextOffsetBase);
8778
+ const segment = this.playbackSegments.find((candidate) => normalizedTextOffset >= candidate.start && normalizedTextOffset < candidate.end);
8772
8779
  this.resetPlaybackSegments();
8773
8780
  if (!(segment == null ? void 0 : segment.highlightDiv)) {
8774
8781
  this.previousWordBoundary = wordBoundary;
8775
8782
  return;
8776
8783
  }
8777
- const relativeTextOffset = wordBoundary.textOffset - segment.start;
8784
+ const relativeTextOffset = normalizedTextOffset - segment.start;
8778
8785
  const currentOffset = this.getPosition(segment.originalHighlightDivInnerHTML, wordBoundary.text, relativeTextOffset);
8779
8786
  if (currentOffset === Number.MAX_SAFE_INTEGER) {
8780
8787
  this.previousWordBoundary = wordBoundary;
@@ -8938,7 +8945,7 @@ class TextToSpeech {
8938
8945
  async createInterval() {
8939
8946
  this.interval = setInterval(() => {
8940
8947
  var _a;
8941
- if (this.player !== void 0 && this.highlightDiv) {
8948
+ if (this.player !== void 0 && (this.highlightDiv || this.playbackSegments.length > 0)) {
8942
8949
  const currentTime = this.player.currentTime;
8943
8950
  let wordBoundary;
8944
8951
  for (const e of this.wordBoundryList) {
@@ -8985,6 +8992,8 @@ class TextToSpeech {
8985
8992
  `;
8986
8993
  }
8987
8994
  }
8995
+ } else if (this.playbackSegments.length > 0) {
8996
+ this.resetPlaybackSegments();
8988
8997
  } else {
8989
8998
  this.highlightDiv.innerHTML = this.originalHighlightDivInnerHTML;
8990
8999
  }
@@ -17,11 +17,11 @@ File System access not available, please use Push or PullAudioOutputStream`),thi
17
17
  return true;
18
18
  }
19
19
  }
20
- registerProcessor('speech-processor', SP);`,h=new Blob([u],{type:"application/javascript; charset=utf-8"});this.privSpeechProcessorScript=URL.createObjectURL(h)}const a=()=>{const u=(()=>{let h=0;try{return e.createScriptProcessor(h,1,1)}catch{h=2048;let d=e.sampleRate;for(;h<16384&&d>=32e3;)h<<=1,d>>=1;return e.createScriptProcessor(h,1,1)}})();u.onaudioprocess=h=>{const c=h.inputBuffer.getChannelData(0);if(i&&!i.isClosed){const d=s.encode(c);d&&i.writeStreamChunk({buffer:d,isEnd:!1,timeReceived:Date.now()})}},o.connect(u),u.connect(e.destination),this.privMediaResources={scriptProcessorNode:u,source:o,stream:t}};if(!!this.privSpeechProcessorScript&&!!e.audioWorklet)e.audioWorklet.addModule(this.privSpeechProcessorScript).then(()=>{const u=new AudioWorkletNode(e,"speech-processor");u.port.onmessage=h=>{const c=h.data;if(i&&!i.isClosed){const d=s.encode(c);d&&i.writeStreamChunk({buffer:d,isEnd:!1,timeReceived:Date.now()})}},o.connect(u),u.connect(e.destination),this.privMediaResources={scriptProcessorNode:u,source:o,stream:t}}).catch(()=>{a()});else try{a()}catch(u){throw new Error(`Unable to start audio worklet node for PCMRecorder: ${u}`)}}releaseMediaResources(e){this.privMediaResources&&(this.privMediaResources.scriptProcessorNode&&(this.privMediaResources.scriptProcessorNode.disconnect(e.destination),this.privMediaResources.scriptProcessorNode=null),this.privMediaResources.source&&(this.privMediaResources.source.disconnect(),this.privStopInputOnRelease&&this.privMediaResources.stream.getTracks().forEach(t=>t.stop()),this.privMediaResources.source=null))}setWorkletUrl(e){this.privSpeechProcessorScript=e}}var De=globalThis&&globalThis.__awaiter||function(r,e,t,i){function n(s){return s instanceof t?s:new t(function(o){o(s)})}return new(t||(t=Promise))(function(s,o){function a(c){try{h(i.next(c))}catch(d){o(d)}}function u(c){try{h(i.throw(c))}catch(d){o(d)}}function h(c){c.done?s(c.value):n(c.value).then(a,u)}h((i=i.apply(r,e||[])).next())})};class A{constructor(e){e&&(this.privProxyInfo=e),A.privDiskCache||(A.privDiskCache=new L("microsoft-cognitiveservices-speech-sdk-cache",{supportBuffer:!0,location:typeof process!="undefined"&&!!{}.SPEECH_OCSP_CACHE_ROOT?{}.SPEECH_OCSP_CACHE_ROOT:void 0}))}static forceReinitDiskCache(){A.privDiskCache=void 0,A.privMemCache={}}GetAgent(e){const t=new L.Agent(this.CreateConnection);if(this.privProxyInfo!==void 0&&this.privProxyInfo.HostName!==void 0&&this.privProxyInfo.Port>0){const i="privProxyInfo";t[i]=this.privProxyInfo}return t}static GetProxyAgent(e){const t={host:e.HostName,port:e.Port};return e.UserName?t.headers={"Proxy-Authentication":"Basic "+new Buffer(`${e.UserName}:${e.Password===void 0?"":e.Password}`).toString("base64")}:t.headers={},t.headers.requestOCSP="true",new L(t)}static OCSPCheck(e,t){return De(this,void 0,void 0,function*(){let i,n,s=!1;const o=yield e;o.cork();const a=o;return new Promise((u,h)=>{o.on("OCSPResponse",c=>{c&&(this.onEvent(new Oi),n=c)}),o.on("error",c=>{s||(s=!0,o.destroy(),h(c))}),a.on("secure",()=>De(this,void 0,void 0,function*(){const c=a.getPeerCertificate(!0);try{const d=yield this.GetIssuer(c);i=(void 0)(c.raw,d.raw);const S=i.id.toString("hex");n||(n=yield A.GetResponseFromCache(S,i,t)),yield this.VerifyOCSPResponse(n,i,t),o.uncork(),s=!0,u(o)}catch(d){o.destroy(),s=!0,h(d)}}))})})}static GetIssuer(e){return e.issuerCertificate?Promise.resolve(e.issuerCertificate):new Promise((t,i)=>{new(void 0)({}).fetchIssuer(e,null,(s,o)=>{if(s){i(s);return}t(o)})})}static GetResponseFromCache(e,t,i){return De(this,void 0,void 0,function*(){let n=A.privMemCache[e];if(n&&this.onEvent(new Di(e)),!n)try{const s=yield A.privDiskCache.get(e);s.isCached&&(A.onEvent(new xi(e)),A.StoreMemoryCacheEntry(e,s.value),n=s.value)}catch{n=null}if(!n)return n;try{const a=(void 0)(n).value.tbsResponseData;if(a.responses.length<1){this.onEvent(new Bt(e,"Not enough data in cached response"));return}const u=a.responses[0].thisUpdate,h=a.responses[0].nextUpdate;if(h<Date.now()+this.testTimeOffset-6e4)this.onEvent(new _i(e,h)),n=null;else{const c=Math.min(864e5,(h-u)/2);h-(Date.now()+this.testTimeOffset)<c?(this.onEvent(new Hi(e,u,h)),this.UpdateCache(t,i).catch(d=>{this.onEvent(new Wi(e,d.toString()))})):this.onEvent(new Ki(e,u,h))}}catch(s){this.onEvent(new Bt(e,s)),n=null}return n||this.onEvent(new ki(e)),n})}static VerifyOCSPResponse(e,t,i){return De(this,void 0,void 0,function*(){let n=e;return n||(n=yield A.GetOCSPResponse(t,i)),new Promise((s,o)=>{(void 0)({request:t,response:n},a=>{a?(A.onEvent(new qi(t.id.toString("hex"),a)),e?this.VerifyOCSPResponse(null,t,i).then(()=>{s()},u=>{o(u)}):o(a)):(e||A.StoreCacheEntry(t.id.toString("hex"),n),s())})})})}static UpdateCache(e,t){return De(this,void 0,void 0,function*(){const i=e.id.toString("hex");this.onEvent(new Ni(i));const n=yield this.GetOCSPResponse(e,t);this.StoreCacheEntry(i,n),this.onEvent(new Bi(e.id.toString("hex")))})}static StoreCacheEntry(e,t){this.StoreMemoryCacheEntry(e,t),this.StoreDiskCacheEntry(e,t)}static StoreMemoryCacheEntry(e,t){this.privMemCache[e]=t,this.onEvent(new zi(e))}static StoreDiskCacheEntry(e,t){this.privDiskCache.set(e,t).then(()=>{this.onEvent(new Li(e))})}static GetOCSPResponse(e,t){const i="1.3.6.1.5.5.7.48.1";let n={};if(t){const s=A.GetProxyAgent(t);n.agent=s}return new Promise((s,o)=>{(void 0)(e.cert,i,(a,u)=>{if(a){o(a);return}const h=new URL(u);n=Object.assign(Object.assign({},n),{host:h.host,protocol:h.protocol,port:h.port,path:h.pathname,hostname:h.host}),(void 0)(n,e.data,(c,d)=>{if(c){o(c);return}const S=e.certID;this.onEvent(new Ui(S.toString("hex"))),s(d)})})})}static onEvent(e){N.instance.onEvent(e)}CreateConnection(e,t){const i=typeof process!="undefined"&&{}.NODE_TLS_REJECT_UNAUTHORIZED!=="0"&&{}.SPEECH_CONDUCT_OCSP_CHECK!=="0"&&t.secureEndpoint;let n;if(t=Object.assign(Object.assign({},t),{requestOCSP:!A.forceDisableOCSPStapling,servername:t.host}),this.privProxyInfo){const o=A.GetProxyAgent(this.privProxyInfo);n=new Promise((a,u)=>{o.callback(e,t,(h,c)=>{h?u(h):a(c)})})}else t.secureEndpoint,n=Promise.resolve((void 0)(t));return i?A.OCSPCheck(n,this.privProxyInfo):n}}A.testTimeOffset=0,A.forceDisableOCSPStapling=!1,A.privMemCache={};var fi=globalThis&&globalThis.__awaiter||function(r,e,t,i){function n(s){return s instanceof t?s:new t(function(o){o(s)})}return new(t||(t=Promise))(function(s,o){function a(c){try{h(i.next(c))}catch(d){o(d)}}function u(c){try{h(i.throw(c))}catch(d){o(d)}}function h(c){c.done?s(c.value):n(c.value).then(a,u)}h((i=i.apply(r,e||[])).next())})};class tt{constructor(e,t,i,n,s,o){if(!e)throw new I("uri");if(!i)throw new I("messageFormatter");this.proxyInfo=n,this.privConnectionEvents=new j,this.privConnectionId=t,this.privMessageFormatter=i,this.privConnectionState=M.None,this.privUri=e,this.privHeaders=s,this.privEnableCompression=o,this.privHeaders[T.ConnectionId]=this.privConnectionId,this.privLastErrorReceived=""}get state(){return this.privConnectionState}open(){if(this.privConnectionState===M.Disconnected)return Promise.reject(`Cannot open a connection that is in ${this.privConnectionState} state`);if(this.privConnectionEstablishDeferral)return this.privConnectionEstablishDeferral.promise;this.privConnectionEstablishDeferral=new D,this.privCertificateValidatedDeferral=new D,this.privConnectionState=M.Connecting;try{if(typeof WebSocket!="undefined"&&!tt.forceNpmWebSocket)this.privCertificateValidatedDeferral.resolve(),this.privWebsocketClient=new WebSocket(this.privUri);else{const e={headers:this.privHeaders,perMessageDeflate:this.privEnableCompression};this.privCertificateValidatedDeferral.resolve();const t=new A(this.proxyInfo);e.agent=t.GetAgent();let n=new URL(this.privUri).protocol;(n==null?void 0:n.toLocaleLowerCase())==="wss:"?n="https:":(n==null?void 0:n.toLocaleLowerCase())==="ws:"&&(n="http:"),e.agent.protocol=n,this.privWebsocketClient=new L(this.privUri,e)}this.privWebsocketClient.binaryType="arraybuffer",this.privReceivingMessageQueue=new be,this.privDisconnectDeferral=new D,this.privSendMessageQueue=new be,this.processSendQueue().catch(e=>{N.instance.onEvent(new Ee(e))})}catch(e){return this.privConnectionEstablishDeferral.resolve(new ct(500,e)),this.privConnectionEstablishDeferral.promise}return this.onEvent(new kt(this.privConnectionId,this.privUri)),this.privWebsocketClient.onopen=()=>{this.privCertificateValidatedDeferral.promise.then(()=>{this.privConnectionState=M.Connected,this.onEvent(new xt(this.privConnectionId)),this.privConnectionEstablishDeferral.resolve(new ct(200,""))},e=>{this.privConnectionEstablishDeferral.reject(e)})},this.privWebsocketClient.onerror=e=>{this.onEvent(new Ri(this.privConnectionId,e.message,e.type)),this.privLastErrorReceived=e.message},this.privWebsocketClient.onclose=e=>{this.privConnectionState===M.Connecting?(this.privConnectionState=M.Disconnected,this.privConnectionEstablishDeferral.resolve(new ct(e.code,e.reason+" "+this.privLastErrorReceived))):(this.privConnectionState=M.Disconnected,this.privWebsocketClient=null,this.onEvent(new Ei(this.privConnectionId,e.code,e.reason))),this.onClose(e.code,e.reason).catch(t=>{N.instance.onEvent(new Ee(t))})},this.privWebsocketClient.onmessage=e=>{const t=new Date().toISOString();if(this.privConnectionState===M.Connected){const i=new D;if(this.privReceivingMessageQueue.enqueueFromPromise(i.promise),e.data instanceof ArrayBuffer){const n=new Le(m.Binary,e.data);this.privMessageFormatter.toConnectionMessage(n).then(s=>{this.onEvent(new st(this.privConnectionId,t,s)),i.resolve(s)},s=>{i.reject(`Invalid binary message format. Error: ${s}`)})}else{const n=new Le(m.Text,e.data);this.privMessageFormatter.toConnectionMessage(n).then(s=>{this.onEvent(new st(this.privConnectionId,t,s)),i.resolve(s)},s=>{i.reject(`Invalid text message format. Error: ${s}`)})}}},this.privConnectionEstablishDeferral.promise}send(e){if(this.privConnectionState!==M.Connected)return Promise.reject(`Cannot send on connection that is in ${M[this.privConnectionState]} state`);const t=new D,i=new D;return this.privSendMessageQueue.enqueueFromPromise(i.promise),this.privMessageFormatter.fromConnectionMessage(e).then(n=>{i.resolve({Message:e,RawWebsocketMessage:n,sendStatusDeferral:t})},n=>{i.reject(`Error formatting the message. ${n}`)}),t.promise}read(){return this.privConnectionState!==M.Connected?Promise.reject(`Cannot read on connection that is in ${this.privConnectionState} state`):this.privReceivingMessageQueue.dequeue()}close(e){if(this.privWebsocketClient)this.privConnectionState!==M.Disconnected&&this.privWebsocketClient.close(1e3,e||"Normal closure by client");else return Promise.resolve();return this.privDisconnectDeferral.promise}get events(){return this.privConnectionEvents}sendRawMessage(e){try{if(!e)return Promise.resolve();if(this.onEvent(new Ai(this.privConnectionId,new Date().toISOString(),e.Message)),this.isWebsocketOpen)this.privWebsocketClient.send(e.RawWebsocketMessage.payload);else return Promise.reject("websocket send error: Websocket not ready "+this.privConnectionId+" "+e.Message.id+" "+new Error().stack);return Promise.resolve()}catch(t){return Promise.reject(`websocket send error: ${t}`)}}onClose(e,t){return fi(this,void 0,void 0,function*(){const i=`Connection closed. ${e}: ${t}`;this.privConnectionState=M.Disconnected,this.privDisconnectDeferral.resolve(),yield this.privReceivingMessageQueue.drainAndDispose(()=>{},i),yield this.privSendMessageQueue.drainAndDispose(n=>{n.sendStatusDeferral.reject(i)},i)})}processSendQueue(){return fi(this,void 0,void 0,function*(){for(;;){const t=yield this.privSendMessageQueue.dequeue();if(!t)return;try{yield this.sendRawMessage(t),t.sendStatusDeferral.resolve()}catch(i){t.sendStatusDeferral.reject(i)}}})}onEvent(e){this.privConnectionEvents.onEvent(e),N.instance.onEvent(e)}get isWebsocketOpen(){return this.privWebsocketClient&&this.privWebsocketClient.readyState===this.privWebsocketClient.OPEN}}tt.forceNpmWebSocket=!1;var Er=globalThis&&globalThis.__awaiter||function(r,e,t,i){function n(s){return s instanceof t?s:new t(function(o){o(s)})}return new(t||(t=Promise))(function(s,o){function a(c){try{h(i.next(c))}catch(d){o(d)}}function u(c){try{h(i.throw(c))}catch(d){o(d)}}function h(c){c.done?s(c.value):n(c.value).then(a,u)}h((i=i.apply(r,e||[])).next())})};class gi{constructor(e,t,i,n,s,o=!1,a){if(this.privIsDisposed=!1,!e)throw new I("uri");if(!n)throw new I("messageFormatter");this.privMessageFormatter=n;let u="",h=0;if(t){for(const c in t)if(c){u+=h===0&&e.indexOf("?")===-1?"?":"&";const d=encodeURIComponent(t[c]);u+=`${c}=${d}`,h++}}if(i){for(const c in i)if(c){u+=h===0&&e.indexOf("?")===-1?"?":"&";const d=encodeURIComponent(i[c]);u+=`${c}=${d}`,h++}}this.privUri=e+u,this.privId=a||E(),this.privConnectionMessageAdapter=new tt(this.privUri,this.id,this.privMessageFormatter,s,i,o)}dispose(){return Er(this,void 0,void 0,function*(){this.privIsDisposed=!0,this.privConnectionMessageAdapter&&(yield this.privConnectionMessageAdapter.close())})}isDisposed(){return this.privIsDisposed}get id(){return this.privId}state(){return this.privConnectionMessageAdapter.state}open(){return this.privConnectionMessageAdapter.open()}send(e){return this.privConnectionMessageAdapter.send(e)}read(){return this.privConnectionMessageAdapter.read()}get events(){return this.privConnectionMessageAdapter.events}}class Rr{constructor(e,t){this.privBuffers=[],this.privReplayOffset=0,this.privLastShrinkOffset=0,this.privBufferStartOffset=0,this.privBufferSerial=0,this.privBufferedBytes=0,this.privReplay=!1,this.privLastChunkAcquiredTime=0,this.privAudioNode=e,this.privBytesPerSecond=t}id(){return this.privAudioNode.id()}read(){if(!!this.privReplay&&this.privBuffers.length!==0){const e=this.privReplayOffset-this.privBufferStartOffset;let t=Math.round(e*this.privBytesPerSecond*1e-7);t%2!==0&&t++;let i=0;for(;i<this.privBuffers.length&&t>=this.privBuffers[i].chunk.buffer.byteLength;)t-=this.privBuffers[i++].chunk.buffer.byteLength;if(i<this.privBuffers.length){const n=this.privBuffers[i].chunk.buffer.slice(t);return this.privReplayOffset+=n.byteLength/this.privBytesPerSecond*1e7,i===this.privBuffers.length-1&&(this.privReplay=!1),Promise.resolve({buffer:n,isEnd:!1,timeReceived:this.privBuffers[i].chunk.timeReceived})}}return this.privAudioNode.read().then(e=>(e&&e.buffer&&(this.privBuffers.push(new Pr(e,this.privBufferSerial++,this.privBufferedBytes)),this.privBufferedBytes+=e.buffer.byteLength),e))}detach(){return this.privBuffers=void 0,this.privAudioNode.detach()}replay(){this.privBuffers&&this.privBuffers.length!==0&&(this.privReplay=!0,this.privReplayOffset=this.privLastShrinkOffset)}shrinkBuffers(e){if(this.privBuffers===void 0||this.privBuffers.length===0)return;this.privLastShrinkOffset=e;const t=e-this.privBufferStartOffset;let i=Math.round(t*this.privBytesPerSecond*1e-7),n=0;for(;n<this.privBuffers.length&&i>=this.privBuffers[n].chunk.buffer.byteLength;)i-=this.privBuffers[n++].chunk.buffer.byteLength;this.privBufferStartOffset=Math.round(e-i/this.privBytesPerSecond*1e7),this.privBuffers=this.privBuffers.slice(n)}findTimeAtOffset(e){if(e<this.privBufferStartOffset||this.privBuffers===void 0)return 0;for(const t of this.privBuffers){const i=t.byteOffset/this.privBytesPerSecond*1e7,n=i+t.chunk.buffer.byteLength/this.privBytesPerSecond*1e7;if(e>=i&&e<=n)return t.chunk.timeReceived}return 0}}class Pr{constructor(e,t,i){this.chunk=e,this.serial=t,this.byteOffset=i}}class it{constructor(e,t,i,n){this.privProxyHostName=e,this.privProxyPort=t,this.privProxyUserName=i,this.privProxyPassword=n}static fromParameters(e){return new it(e.getProperty(p.SpeechServiceConnection_ProxyHostName),parseInt(e.getProperty(p.SpeechServiceConnection_ProxyPort),10),e.getProperty(p.SpeechServiceConnection_ProxyUserName),e.getProperty(p.SpeechServiceConnection_ProxyPassword))}static fromRecognizerConfig(e){return this.fromParameters(e.parameters)}get HostName(){return this.privProxyHostName}get Port(){return this.privProxyPort}get UserName(){return this.privProxyUserName}get Password(){return this.privProxyPassword}}const Ar=new Set(["json","buffer","string"]);var Ir=r=>(...e)=>{const t=new Set;let i,n,s,o="";return e.forEach(a=>{if(typeof a=="string")if(a.toUpperCase()===a)if(i){const u=`Can't set method to ${a}, already set to ${i}.`;throw new Error(u)}else i=a;else if(a.startsWith("http:")||a.startsWith("https:"))o=a;else if(Ar.has(a))n=a;else throw new Error(`Unknown encoding, ${a}`);else if(typeof a=="number")t.add(a);else if(typeof a=="object")if(Array.isArray(a)||a instanceof Set)a.forEach(u=>t.add(u));else{if(s)throw new Error("Cannot set headers twice.");s=a}else throw new Error(`Unknown type: ${typeof a}`)}),i||(i="GET"),t.size===0&&t.add(200),r(t,i,n,s,o)};const Mr=Ir;class It extends Error{constructor(e,...t){super(...t),Error.captureStackTrace&&Error.captureStackTrace(this,It),this.name="StatusError",this.message=e.statusMessage,this.statusCode=e.status,this.res=e,this.json=e.json.bind(e),this.text=e.text.bind(e),this.arrayBuffer=e.arrayBuffer.bind(e);let i;Object.defineProperty(this,"responseBody",{get:()=>(i||(i=this.arrayBuffer()),i)}),this.headers={};for(const[s,o]of e.headers.entries())this.headers[s.toLowerCase()]=o}}var Dr=Mr((r,e,t,i,n)=>async(s,o,a={})=>{s=n+(s||"");let u=new URL(s);if(i||(i={}),u.username&&(i.Authorization="Basic "+btoa(u.username+":"+u.password),u=new URL(u.protocol+"//"+u.host+u.pathname+u.search)),u.protocol!=="https:"&&u.protocol!=="http:")throw new Error(`Unknown protocol, ${u.protocol}`);if(o&&!(o instanceof ArrayBuffer||ArrayBuffer.isView(o)||typeof o=="string"))if(typeof o=="object")o=JSON.stringify(o),i["Content-Type"]="application/json";else throw new Error("Unknown body type.");a=new Headers({...i||{},...a});const h=await fetch(u,{method:e,headers:a,body:o});if(h.statusCode=h.status,!r.has(h.status))throw new It(h);return t==="json"?h.json():t==="buffer"?h.arrayBuffer():t==="string"?h.text():h}),kr=globalThis&&globalThis.__awaiter||function(r,e,t,i){function n(s){return s instanceof t?s:new t(function(o){o(s)})}return new(t||(t=Promise))(function(s,o){function a(c){try{h(i.next(c))}catch(d){o(d)}}function u(c){try{h(i.throw(c))}catch(d){o(d)}}function h(c){c.done?s(c.value):n(c.value).then(a,u)}h((i=i.apply(r,e||[])).next())})},he;(function(r){r.Get="GET",r.Post="POST",r.Delete="DELETE",r.File="file"})(he||(he={}));class xr{constructor(e){if(!e)throw new I("configParams");this.privHeaders=e.headers,this.privIgnoreCache=e.ignoreCache}static extractHeaderValue(e,t){let i="";try{const n=t.trim().split(/[\r\n]+/),s={};n.forEach(o=>{const a=o.split(": "),u=a.shift().toLowerCase(),h=a.join(": ");s[u]=h}),i=s[e.toLowerCase()]}catch{}return i}set options(e){this.privHeaders=e.headers,this.privIgnoreCache=e.ignoreCache}setHeaders(e,t){this.privHeaders[e]=t}request(e,t,i={},n=null,s=null){const o=new D,a=e===he.File?"POST":e,u=(d,S={})=>{const _=d;return{data:JSON.stringify(S),headers:JSON.stringify(d.headers),json:S,ok:d.statusCode>=200&&d.statusCode<300,status:d.statusCode,statusText:S.error?S.error.message:_.statusText?_.statusText:_.statusMessage}},h=d=>{const S=new FileReader;return S.readAsArrayBuffer(d),new Promise(_=>{S.onloadend=()=>{_(S.result)}})},c=d=>{const S=Dr(t,a,this.privHeaders,200,201,202,204,400,401,402,403,404),_=this.queryParams(i)===""?"":`?${this.queryParams(i)}`;S(_,d).then(Z=>kr(this,void 0,void 0,function*(){if(e===he.Delete||Z.statusCode===204)o.resolve(u(Z));else try{const ke=yield Z.json();o.resolve(u(Z,ke))}catch{o.resolve(u(Z))}})).catch(Z=>{o.reject(Z)})};if(this.privIgnoreCache&&(this.privHeaders["Cache-Control"]="no-cache"),e===he.File&&s){const d="multipart/form-data";this.privHeaders["content-type"]=d,this.privHeaders["Content-Type"]=d,typeof Blob!="undefined"&&s instanceof Blob?h(s).then(S=>{c(S)}).catch(S=>{o.reject(S)}):c(s)}else e===he.Post&&n&&(this.privHeaders["content-type"]="application/json",this.privHeaders["Content-Type"]="application/json"),c(n);return o.promise}withQuery(e,t={}){const i=this.queryParams(t);return i?e+(e.indexOf("?")===-1?"?":"&")+i:e}queryParams(e={}){return Object.keys(e).map(t=>encodeURIComponent(t)+"="+encodeURIComponent(e[t])).join("&")}}class Nr{constructor(e,t,i,n=null){y(this,"key");y(this,"region");y(this,"sourceLanguage");y(this,"targetLanguage");y(this,"recognizer");this.key=e,this.region=t,this.sourceLanguage=i,this.targetLanguage=n!==null?n:i}async start(){await this.registerBindings(document)}async registerBindings(e){const t=e.childNodes;for(let i=0;i<t.length;i++){if(!t[i])continue;const n=t[i];n.attributes&&(n.attributes.getNamedItem("co-stt.start")?await this.handleStartModifier(n,n.attributes.getNamedItem("co-stt.start")):n.attributes.getNamedItem("co-stt.stop")&&await this.handleStopModifier(n,n.attributes.getNamedItem("co-stt.stop"))),n.childNodes.length>0&&await this.registerBindings(n)}}async handleStartModifier(e,t){e.addEventListener("click",async i=>{const n=Jt.fromSubscription(this.key,this.region);n.speechRecognitionLanguage=this.sourceLanguage,n.addTargetLanguage(this.targetLanguage);const s=ee.fromDefaultMicrophoneInput();this.recognizer=new Ji(n,s),document.dispatchEvent(new CustomEvent("COAzureSTTStartedRecording",{}));const o=[];this.recognizer.recognizing=(a,u)=>{const h=u.result;if(h&&h.reason===P.TranslatingSpeech){const c=h.translations.get(this.targetLanguage);o["result_"+h.privOffset.toString()]=c;const d=Object.values(o).join(". "),S=document.getElementById(t.value);S!==null&&(S instanceof HTMLInputElement?S.value=`${d} `:S.innerHTML=`${d} `)}},this.recognizer.startContinuousRecognitionAsync(a=>{},a=>{console.log(a),this.stop()})})}async handleStopModifier(e,t){e.addEventListener("click",async i=>{await this.stop()})}async stop(){this.recognizer!==void 0&&(this.recognizer.stopContinuousRecognitionAsync(),this.recognizer.close(),this.recognizer=void 0),document.dispatchEvent(new CustomEvent("COAzureSTTStoppedRecording",{}))}}class zr{constructor(e,t,i,n=0,s=0,o=""){y(this,"key");y(this,"region");y(this,"voice");y(this,"rate");y(this,"pitch");y(this,"textToRead","");y(this,"wordBoundryList",[]);y(this,"clickedNode");y(this,"highlightDiv");y(this,"speechConfig");y(this,"audioConfig");y(this,"player");y(this,"synthesizer");y(this,"previousWordBoundary");y(this,"interval");y(this,"wordEncounters",[]);y(this,"originalHighlightDivInnerHTML","");y(this,"currentWord","");y(this,"currentOffset",0);y(this,"wordBoundaryOffset",0);y(this,"prevTextOffset",0);y(this,"url","");y(this,"prefetchedAudio",new Map);y(this,"prefetchPromises",new Map);y(this,"activePrefetchedAudioUrl","");y(this,"playbackSegments",[]);this.key=e,this.region=t,this.voice=i,this.rate=n,this.pitch=s,this.url=o}async start(){await this.registerBindings(document)}setVoice(e){return this.voice=e,this.clearPrefetchedAudio(),this}setRate(e){return this.rate=e,this.clearPrefetchedAudio(),this}setPitch(e){return this.pitch=e,this.clearPrefetchedAudio(),this}async registerBindings(e){const t=e.childNodes;for(let i=0;i<t.length;i++){if(!t[i])continue;const n=t[i];n.attributes&&(n.attributes.getNamedItem("co-tts.id")?await this.handleIdModifier(n,n.attributes.getNamedItem("co-tts.id")):n.attributes.getNamedItem("co-tts.ajax")?await this.handleAjaxModifier(n,n.attributes.getNamedItem("co-tts.ajax")):n.attributes.getNamedItem("co-tts")?await this.handleDefault(n,n.attributes.getNamedItem("co-tts")):n.attributes.getNamedItem("co-tts.stop")?await this.handleStopModifier(n,n.attributes.getNamedItem("co-tts.stop")):n.attributes.getNamedItem("co-tts.resume")?await this.handleResumeModifier(n,n.attributes.getNamedItem("co-tts.resume")):n.attributes.getNamedItem("co-tts.pause")&&await this.handlePauseModifier(n,n.attributes.getNamedItem("co-tts.pause"))),n.childNodes.length>0&&await this.registerBindings(n)}}async handleIdModifier(e,t){e.addEventListener("click",async i=>{var s,o;this.stopPlayer(),await this.createInterval();const n=document.getElementById(t.value);if(this.clickedNode=n,!!n){if(n.hasAttribute("co-tts.text")&&n.getAttribute("co-tts.text")!==""?this.textToRead=(s=n.getAttribute("co-tts.text"))!=null?s:"":this.textToRead=n.innerText,n.hasAttribute("co-tts.highlight"))if(((o=n.attributes.getNamedItem("co-tts.highlight"))==null?void 0:o.value)!==""){const a=document.getElementById(n.attributes.getNamedItem("co-tts.highlight").value);this.highlightDiv=a,this.originalHighlightDivInnerHTML=a.innerHTML}else this.highlightDiv=n,this.originalHighlightDivInnerHTML=n.innerHTML;this.startSynthesizer(e,t)}})}async handleAjaxModifier(e,t){e.addEventListener("click",async i=>{this.stopPlayer(),await this.createInterval(),this.clickedNode=e;const n=await fetch(t.value,{method:"GET"});this.textToRead=await n.text(),this.startSynthesizer(e,t)})}async handleDefault(e,t){e.addEventListener("click",async i=>{var n;if(this.stopPlayer(),await this.createInterval(),this.clickedNode=e,e.hasAttribute("co-tts.highlight"))if(((n=e.attributes.getNamedItem("co-tts.highlight"))==null?void 0:n.value)!==""){const s=document.getElementById(e.attributes.getNamedItem("co-tts.highlight").value);this.highlightDiv=s,this.originalHighlightDivInnerHTML=s.innerHTML}else this.highlightDiv=e,this.originalHighlightDivInnerHTML=e.innerHTML;t.value===""?this.textToRead=e.innerText:this.textToRead=t.value,this.startSynthesizer(e,t)})}async handleWithoutClick(e,t){var i;if(this.stopPlayer(),await this.createInterval(),this.clickedNode=e,e.hasAttribute("co-tts.highlight"))if(((i=e.attributes.getNamedItem("co-tts.highlight"))==null?void 0:i.value)!==""){const n=document.getElementById(e.attributes.getNamedItem("co-tts.highlight").value);this.highlightDiv=n,n!==null&&(this.originalHighlightDivInnerHTML=n.innerHTML)}else this.highlightDiv=e,this.originalHighlightDivInnerHTML=e.innerHTML;t.value===""?this.textToRead=e.innerText:this.textToRead=t.value,this.startSynthesizer(e,t)}async handleStopModifier(e,t){e.addEventListener("click",async i=>{await this.stopPlayer(),document.dispatchEvent(new CustomEvent("COAzureTTSStoppedPlaying",{}))})}async handlePauseModifier(e,t){e.addEventListener("click",async i=>{await this.clearInterval(),await this.player.pause(),document.dispatchEvent(new CustomEvent("COAzureTTSPausedPlaying",{}))})}async handleResumeModifier(e,t){e.addEventListener("click",async i=>{await this.createInterval(),await this.player.resume(),document.dispatchEvent(new CustomEvent("COAzureTTSResumedPlaying",{}))})}async stopPlayer(){await this.clearInterval(),this.highlightDiv!==void 0&&(this.highlightDiv.innerHTML=this.originalHighlightDivInnerHTML),this.textToRead="",this.currentWord="",this.originalHighlightDivInnerHTML="",this.wordBoundryList=[],this.wordEncounters=[],this.resetPlaybackSegments(),this.playbackSegments=[],this.player!==void 0&&this.player.pause(),this.activePrefetchedAudioUrl!==""&&(URL.revokeObjectURL(this.activePrefetchedAudioUrl),this.activePrefetchedAudioUrl=""),this.player=void 0,this.highlightDiv=void 0,this.prevTextOffset=0}async startSynthesizer(e,t){this.speechConfig=_e.fromSubscription(this.key,this.region),this.speechConfig.speechSynthesisVoiceName=`Microsoft Server Speech Text to Speech Voice (${this.voice})`,this.speechConfig.speechSynthesisOutputFormat=g.Audio24Khz160KBitRateMonoMp3,this.player=new yt,this.audioConfig=ee.fromSpeakerOutput(this.player),this.synthesizer=new Pe(this.speechConfig,this.audioConfig),this.synthesizer.wordBoundary=(s,o)=>{this.wordBoundryList.push(o)};const i=this.collectPlaybackChain(this.clickedNode),n=i.length>1;n?this.preparePlaybackChain(i):this.playbackSegments=[],this.player.onAudioEnd=async()=>{const s=this.playbackSegments.length>0;if(this.stopPlayer(),s){document.dispatchEvent(new CustomEvent("COAzureTTSFinishedPlaying",{}));return}if(this.clickedNode.hasAttribute("co-tts.next")){const o=document.getElementById(this.clickedNode.getAttribute("co-tts.next"));if(o&&await this.playPrefetchedNode(o))return;o&&o.attributes.getNamedItem("co-tts.text")?this.handleWithoutClick(o,o.attributes.getNamedItem("co-tts.text")):o&&o.dispatchEvent(new Event("click"))}else document.dispatchEvent(new CustomEvent("COAzureTTSFinishedPlaying",{}))},this.player.onAudioStart=async()=>{document.dispatchEvent(new CustomEvent("COAzureTTSStartedPlaying",{}))},n||this.prefetchNextNode(this.clickedNode),this.synthesizer.speakSsmlAsync(this.buildSSML(this.textToRead),()=>{this.synthesizer.close(),this.synthesizer=void 0},()=>{this.synthesizer.close(),this.synthesizer=void 0})}collectPlaybackChain(e){var o,a;const t=[],i=new Set;let n=e,s=0;for(;n&&!i.has(n.id);){i.add(n.id);const u=(o=n.attributes.getNamedItem("co-tts.text"))!=null?o:n.attributes.getNamedItem("co-tts"),h=this.getNodeText(n,u);if(h==="")break;const c=this.getHighlightDivForNode(n);if(t.push({node:n,text:h,start:s,end:s+h.length,highlightDiv:c,originalHighlightDivInnerHTML:(a=c==null?void 0:c.innerHTML)!=null?a:""}),s+=h.length+2,!n.hasAttribute("co-tts.next"))break;n=document.getElementById(n.getAttribute("co-tts.next"))}return t}preparePlaybackChain(e){this.playbackSegments=e,this.textToRead=e.map(t=>t.text).join(". "),this.highlightDiv=void 0,this.originalHighlightDivInnerHTML="",this.wordEncounters=[],this.previousWordBoundary=void 0,this.prevTextOffset=0,this.currentWord="",this.currentOffset=0,this.wordBoundaryOffset=0}getHighlightDivForNode(e){var t;if(!!e.hasAttribute("co-tts.highlight"))return((t=e.attributes.getNamedItem("co-tts.highlight"))==null?void 0:t.value)!==""?document.getElementById(e.attributes.getNamedItem("co-tts.highlight").value):e}resetPlaybackSegments(){this.playbackSegments.forEach(e=>{e.highlightDiv&&(e.highlightDiv.innerHTML=e.originalHighlightDivInnerHTML)})}updateChainedHighlight(e){var u;if(~[".",",","!","?","*","(",")","&","\\","/","^","[","]","<",">",":"].indexOf(e.text)&&(e=(u=this.previousWordBoundary)!=null?u:void 0),!e){this.resetPlaybackSegments();return}const t=this.playbackSegments.find(h=>e.textOffset>=h.start&&e.textOffset<h.end);if(this.resetPlaybackSegments(),!(t!=null&&t.highlightDiv)){this.previousWordBoundary=e;return}const i=e.textOffset-t.start,n=this.getPosition(t.originalHighlightDivInnerHTML,e.text,i);if(n===Number.MAX_SAFE_INTEGER){this.previousWordBoundary=e;return}const s=t.originalHighlightDivInnerHTML.substring(0,n),o=n+e.wordLength,a=t.originalHighlightDivInnerHTML.substring(o);t.highlightDiv.innerHTML=`
21
- ${s}<mark class='co-tts-highlight'>${e.text}</mark>${a}
22
- `,this.previousWordBoundary=e}getNodeText(e,t){var i;return t&&t.value!==""?t.value:e.hasAttribute("co-tts.text")&&e.getAttribute("co-tts.text")!==""?(i=e.getAttribute("co-tts.text"))!=null?i:"":e.innerText}getPrefetchKey(e,t){return[e.id,t,this.voice,this.rate,this.pitch,this.url].join("|")}clearPrefetchedAudio(){this.prefetchedAudio.forEach(e=>{e.url&&URL.revokeObjectURL(e.url)}),this.prefetchedAudio.clear(),this.prefetchPromises.clear()}async prefetchNextNode(e){var c;if(!e||!e.hasAttribute("co-tts.next"))return;const t=document.getElementById(e.getAttribute("co-tts.next"));if(!t)return;const i=(c=t.attributes.getNamedItem("co-tts.text"))!=null?c:t.attributes.getNamedItem("co-tts"),n=this.getNodeText(t,i);if(n==="")return;const s=this.getPrefetchKey(t,n);if(this.prefetchedAudio.has(s)||this.prefetchPromises.has(s))return;const o=_e.fromSubscription(this.key,this.region);o.speechSynthesisVoiceName=`Microsoft Server Speech Text to Speech Voice (${this.voice})`,o.speechSynthesisOutputFormat=g.Audio24Khz160KBitRateMonoMp3;const a=new Pe(o,null),u=[];a.wordBoundary=(d,S)=>{u.push(S)};const h=new Promise(d=>{a.speakSsmlAsync(this.buildSSML(n),S=>{if(a.close(),!(S!=null&&S.audioData)){d(null);return}const _=new Blob([S.audioData],{type:"audio/mpeg"}),Z=URL.createObjectURL(_),ke={key:s,nodeId:t.id,text:n,url:Z,wordBoundryList:u};this.prefetchedAudio.set(s,ke),d(ke)},()=>{a.close(),d(null)})}).finally(()=>{this.prefetchPromises.delete(s)});this.prefetchPromises.set(s,h)}async playPrefetchedNode(e){var a,u,h;const t=(a=e.attributes.getNamedItem("co-tts.text"))!=null?a:e.attributes.getNamedItem("co-tts"),i=this.getNodeText(e,t),n=this.getPrefetchKey(e,i),s=(u=this.prefetchedAudio.get(n))!=null?u:await this.prefetchPromises.get(n);if(!s)return!1;if(this.prefetchedAudio.delete(n),this.clickedNode=e,this.textToRead=s.text,this.wordBoundryList=s.wordBoundryList,this.wordEncounters=[],this.previousWordBoundary=void 0,this.prevTextOffset=0,this.currentWord="",this.currentOffset=0,this.wordBoundaryOffset=0,e.hasAttribute("co-tts.highlight"))if(((h=e.attributes.getNamedItem("co-tts.highlight"))==null?void 0:h.value)!==""){const c=document.getElementById(e.attributes.getNamedItem("co-tts.highlight").value);this.highlightDiv=c,c!==null&&(this.originalHighlightDivInnerHTML=c.innerHTML)}else this.highlightDiv=e,this.originalHighlightDivInnerHTML=e.innerHTML;await this.createInterval();const o=new Audio(s.url);return this.activePrefetchedAudioUrl=s.url,this.player=o,o.addEventListener("play",()=>{document.dispatchEvent(new CustomEvent("COAzureTTSStartedPlaying",{}))},{once:!0}),o.addEventListener("ended",async()=>{if(this.stopPlayer(),this.clickedNode.hasAttribute("co-tts.next")){const c=document.getElementById(this.clickedNode.getAttribute("co-tts.next"));if(c&&await this.playPrefetchedNode(c))return;c&&c.attributes.getNamedItem("co-tts.text")?this.handleWithoutClick(c,c.attributes.getNamedItem("co-tts.text")):c&&c.dispatchEvent(new Event("click"))}else document.dispatchEvent(new CustomEvent("COAzureTTSFinishedPlaying",{}))},{once:!0}),this.prefetchNextNode(e),await o.play(),!0}async clearInterval(){clearInterval(this.interval)}async createInterval(){this.interval=setInterval(()=>{var e;if(this.player!==void 0&&this.highlightDiv){const t=this.player.currentTime;let i;for(const n of this.wordBoundryList)if(t*1e3>n.audioOffset/1e4)i=n;else break;if(i!==void 0){if(this.playbackSegments.length>0){this.updateChainedHighlight(i);return}if(~[".",",","!","?","*","(",")","&","\\","/","^","[","]","<",">",":"].indexOf(i.text)&&(i=(e=this.previousWordBoundary)!=null?e:void 0),i===void 0||this.prevTextOffset>i.prevTextOffset)this.highlightDiv.innerHTML=this.originalHighlightDivInnerHTML;else if(this.wordEncounters[i.text]||(this.wordEncounters[i.text]=0),this.prevTextOffset=i.prevTextOffset,(this.currentWord!==i.text||this.wordBoundaryOffset!==i.textOffset)&&(this.currentOffset=this.getPosition(this.originalHighlightDivInnerHTML,i.text,i.textOffset),this.wordEncounters[i.text]=this.currentOffset+i.wordLength,this.currentWord=i.text,this.wordBoundaryOffset=i.textOffset),this.currentOffset===Number.MAX_SAFE_INTEGER)this.highlightDiv.innerHTML=this.originalHighlightDivInnerHTML;else{this.previousWordBoundary=i;const n=this.originalHighlightDivInnerHTML.substring(0,this.currentOffset),s=this.currentOffset+i.wordLength,o=this.originalHighlightDivInnerHTML.substring(s);this.highlightDiv.innerHTML=`
20
+ registerProcessor('speech-processor', SP);`,h=new Blob([u],{type:"application/javascript; charset=utf-8"});this.privSpeechProcessorScript=URL.createObjectURL(h)}const a=()=>{const u=(()=>{let h=0;try{return e.createScriptProcessor(h,1,1)}catch{h=2048;let d=e.sampleRate;for(;h<16384&&d>=32e3;)h<<=1,d>>=1;return e.createScriptProcessor(h,1,1)}})();u.onaudioprocess=h=>{const c=h.inputBuffer.getChannelData(0);if(i&&!i.isClosed){const d=s.encode(c);d&&i.writeStreamChunk({buffer:d,isEnd:!1,timeReceived:Date.now()})}},o.connect(u),u.connect(e.destination),this.privMediaResources={scriptProcessorNode:u,source:o,stream:t}};if(!!this.privSpeechProcessorScript&&!!e.audioWorklet)e.audioWorklet.addModule(this.privSpeechProcessorScript).then(()=>{const u=new AudioWorkletNode(e,"speech-processor");u.port.onmessage=h=>{const c=h.data;if(i&&!i.isClosed){const d=s.encode(c);d&&i.writeStreamChunk({buffer:d,isEnd:!1,timeReceived:Date.now()})}},o.connect(u),u.connect(e.destination),this.privMediaResources={scriptProcessorNode:u,source:o,stream:t}}).catch(()=>{a()});else try{a()}catch(u){throw new Error(`Unable to start audio worklet node for PCMRecorder: ${u}`)}}releaseMediaResources(e){this.privMediaResources&&(this.privMediaResources.scriptProcessorNode&&(this.privMediaResources.scriptProcessorNode.disconnect(e.destination),this.privMediaResources.scriptProcessorNode=null),this.privMediaResources.source&&(this.privMediaResources.source.disconnect(),this.privStopInputOnRelease&&this.privMediaResources.stream.getTracks().forEach(t=>t.stop()),this.privMediaResources.source=null))}setWorkletUrl(e){this.privSpeechProcessorScript=e}}var De=globalThis&&globalThis.__awaiter||function(r,e,t,i){function n(s){return s instanceof t?s:new t(function(o){o(s)})}return new(t||(t=Promise))(function(s,o){function a(c){try{h(i.next(c))}catch(d){o(d)}}function u(c){try{h(i.throw(c))}catch(d){o(d)}}function h(c){c.done?s(c.value):n(c.value).then(a,u)}h((i=i.apply(r,e||[])).next())})};class A{constructor(e){e&&(this.privProxyInfo=e),A.privDiskCache||(A.privDiskCache=new L("microsoft-cognitiveservices-speech-sdk-cache",{supportBuffer:!0,location:typeof process!="undefined"&&!!{}.SPEECH_OCSP_CACHE_ROOT?{}.SPEECH_OCSP_CACHE_ROOT:void 0}))}static forceReinitDiskCache(){A.privDiskCache=void 0,A.privMemCache={}}GetAgent(e){const t=new L.Agent(this.CreateConnection);if(this.privProxyInfo!==void 0&&this.privProxyInfo.HostName!==void 0&&this.privProxyInfo.Port>0){const i="privProxyInfo";t[i]=this.privProxyInfo}return t}static GetProxyAgent(e){const t={host:e.HostName,port:e.Port};return e.UserName?t.headers={"Proxy-Authentication":"Basic "+new Buffer(`${e.UserName}:${e.Password===void 0?"":e.Password}`).toString("base64")}:t.headers={},t.headers.requestOCSP="true",new L(t)}static OCSPCheck(e,t){return De(this,void 0,void 0,function*(){let i,n,s=!1;const o=yield e;o.cork();const a=o;return new Promise((u,h)=>{o.on("OCSPResponse",c=>{c&&(this.onEvent(new Oi),n=c)}),o.on("error",c=>{s||(s=!0,o.destroy(),h(c))}),a.on("secure",()=>De(this,void 0,void 0,function*(){const c=a.getPeerCertificate(!0);try{const d=yield this.GetIssuer(c);i=(void 0)(c.raw,d.raw);const S=i.id.toString("hex");n||(n=yield A.GetResponseFromCache(S,i,t)),yield this.VerifyOCSPResponse(n,i,t),o.uncork(),s=!0,u(o)}catch(d){o.destroy(),s=!0,h(d)}}))})})}static GetIssuer(e){return e.issuerCertificate?Promise.resolve(e.issuerCertificate):new Promise((t,i)=>{new(void 0)({}).fetchIssuer(e,null,(s,o)=>{if(s){i(s);return}t(o)})})}static GetResponseFromCache(e,t,i){return De(this,void 0,void 0,function*(){let n=A.privMemCache[e];if(n&&this.onEvent(new Di(e)),!n)try{const s=yield A.privDiskCache.get(e);s.isCached&&(A.onEvent(new xi(e)),A.StoreMemoryCacheEntry(e,s.value),n=s.value)}catch{n=null}if(!n)return n;try{const a=(void 0)(n).value.tbsResponseData;if(a.responses.length<1){this.onEvent(new Bt(e,"Not enough data in cached response"));return}const u=a.responses[0].thisUpdate,h=a.responses[0].nextUpdate;if(h<Date.now()+this.testTimeOffset-6e4)this.onEvent(new _i(e,h)),n=null;else{const c=Math.min(864e5,(h-u)/2);h-(Date.now()+this.testTimeOffset)<c?(this.onEvent(new Hi(e,u,h)),this.UpdateCache(t,i).catch(d=>{this.onEvent(new Wi(e,d.toString()))})):this.onEvent(new Ki(e,u,h))}}catch(s){this.onEvent(new Bt(e,s)),n=null}return n||this.onEvent(new ki(e)),n})}static VerifyOCSPResponse(e,t,i){return De(this,void 0,void 0,function*(){let n=e;return n||(n=yield A.GetOCSPResponse(t,i)),new Promise((s,o)=>{(void 0)({request:t,response:n},a=>{a?(A.onEvent(new qi(t.id.toString("hex"),a)),e?this.VerifyOCSPResponse(null,t,i).then(()=>{s()},u=>{o(u)}):o(a)):(e||A.StoreCacheEntry(t.id.toString("hex"),n),s())})})})}static UpdateCache(e,t){return De(this,void 0,void 0,function*(){const i=e.id.toString("hex");this.onEvent(new Ni(i));const n=yield this.GetOCSPResponse(e,t);this.StoreCacheEntry(i,n),this.onEvent(new Bi(e.id.toString("hex")))})}static StoreCacheEntry(e,t){this.StoreMemoryCacheEntry(e,t),this.StoreDiskCacheEntry(e,t)}static StoreMemoryCacheEntry(e,t){this.privMemCache[e]=t,this.onEvent(new zi(e))}static StoreDiskCacheEntry(e,t){this.privDiskCache.set(e,t).then(()=>{this.onEvent(new Li(e))})}static GetOCSPResponse(e,t){const i="1.3.6.1.5.5.7.48.1";let n={};if(t){const s=A.GetProxyAgent(t);n.agent=s}return new Promise((s,o)=>{(void 0)(e.cert,i,(a,u)=>{if(a){o(a);return}const h=new URL(u);n=Object.assign(Object.assign({},n),{host:h.host,protocol:h.protocol,port:h.port,path:h.pathname,hostname:h.host}),(void 0)(n,e.data,(c,d)=>{if(c){o(c);return}const S=e.certID;this.onEvent(new Ui(S.toString("hex"))),s(d)})})})}static onEvent(e){N.instance.onEvent(e)}CreateConnection(e,t){const i=typeof process!="undefined"&&{}.NODE_TLS_REJECT_UNAUTHORIZED!=="0"&&{}.SPEECH_CONDUCT_OCSP_CHECK!=="0"&&t.secureEndpoint;let n;if(t=Object.assign(Object.assign({},t),{requestOCSP:!A.forceDisableOCSPStapling,servername:t.host}),this.privProxyInfo){const o=A.GetProxyAgent(this.privProxyInfo);n=new Promise((a,u)=>{o.callback(e,t,(h,c)=>{h?u(h):a(c)})})}else t.secureEndpoint,n=Promise.resolve((void 0)(t));return i?A.OCSPCheck(n,this.privProxyInfo):n}}A.testTimeOffset=0,A.forceDisableOCSPStapling=!1,A.privMemCache={};var fi=globalThis&&globalThis.__awaiter||function(r,e,t,i){function n(s){return s instanceof t?s:new t(function(o){o(s)})}return new(t||(t=Promise))(function(s,o){function a(c){try{h(i.next(c))}catch(d){o(d)}}function u(c){try{h(i.throw(c))}catch(d){o(d)}}function h(c){c.done?s(c.value):n(c.value).then(a,u)}h((i=i.apply(r,e||[])).next())})};class tt{constructor(e,t,i,n,s,o){if(!e)throw new I("uri");if(!i)throw new I("messageFormatter");this.proxyInfo=n,this.privConnectionEvents=new j,this.privConnectionId=t,this.privMessageFormatter=i,this.privConnectionState=M.None,this.privUri=e,this.privHeaders=s,this.privEnableCompression=o,this.privHeaders[T.ConnectionId]=this.privConnectionId,this.privLastErrorReceived=""}get state(){return this.privConnectionState}open(){if(this.privConnectionState===M.Disconnected)return Promise.reject(`Cannot open a connection that is in ${this.privConnectionState} state`);if(this.privConnectionEstablishDeferral)return this.privConnectionEstablishDeferral.promise;this.privConnectionEstablishDeferral=new D,this.privCertificateValidatedDeferral=new D,this.privConnectionState=M.Connecting;try{if(typeof WebSocket!="undefined"&&!tt.forceNpmWebSocket)this.privCertificateValidatedDeferral.resolve(),this.privWebsocketClient=new WebSocket(this.privUri);else{const e={headers:this.privHeaders,perMessageDeflate:this.privEnableCompression};this.privCertificateValidatedDeferral.resolve();const t=new A(this.proxyInfo);e.agent=t.GetAgent();let n=new URL(this.privUri).protocol;(n==null?void 0:n.toLocaleLowerCase())==="wss:"?n="https:":(n==null?void 0:n.toLocaleLowerCase())==="ws:"&&(n="http:"),e.agent.protocol=n,this.privWebsocketClient=new L(this.privUri,e)}this.privWebsocketClient.binaryType="arraybuffer",this.privReceivingMessageQueue=new be,this.privDisconnectDeferral=new D,this.privSendMessageQueue=new be,this.processSendQueue().catch(e=>{N.instance.onEvent(new Ee(e))})}catch(e){return this.privConnectionEstablishDeferral.resolve(new ct(500,e)),this.privConnectionEstablishDeferral.promise}return this.onEvent(new kt(this.privConnectionId,this.privUri)),this.privWebsocketClient.onopen=()=>{this.privCertificateValidatedDeferral.promise.then(()=>{this.privConnectionState=M.Connected,this.onEvent(new xt(this.privConnectionId)),this.privConnectionEstablishDeferral.resolve(new ct(200,""))},e=>{this.privConnectionEstablishDeferral.reject(e)})},this.privWebsocketClient.onerror=e=>{this.onEvent(new Ri(this.privConnectionId,e.message,e.type)),this.privLastErrorReceived=e.message},this.privWebsocketClient.onclose=e=>{this.privConnectionState===M.Connecting?(this.privConnectionState=M.Disconnected,this.privConnectionEstablishDeferral.resolve(new ct(e.code,e.reason+" "+this.privLastErrorReceived))):(this.privConnectionState=M.Disconnected,this.privWebsocketClient=null,this.onEvent(new Ei(this.privConnectionId,e.code,e.reason))),this.onClose(e.code,e.reason).catch(t=>{N.instance.onEvent(new Ee(t))})},this.privWebsocketClient.onmessage=e=>{const t=new Date().toISOString();if(this.privConnectionState===M.Connected){const i=new D;if(this.privReceivingMessageQueue.enqueueFromPromise(i.promise),e.data instanceof ArrayBuffer){const n=new Le(m.Binary,e.data);this.privMessageFormatter.toConnectionMessage(n).then(s=>{this.onEvent(new st(this.privConnectionId,t,s)),i.resolve(s)},s=>{i.reject(`Invalid binary message format. Error: ${s}`)})}else{const n=new Le(m.Text,e.data);this.privMessageFormatter.toConnectionMessage(n).then(s=>{this.onEvent(new st(this.privConnectionId,t,s)),i.resolve(s)},s=>{i.reject(`Invalid text message format. Error: ${s}`)})}}},this.privConnectionEstablishDeferral.promise}send(e){if(this.privConnectionState!==M.Connected)return Promise.reject(`Cannot send on connection that is in ${M[this.privConnectionState]} state`);const t=new D,i=new D;return this.privSendMessageQueue.enqueueFromPromise(i.promise),this.privMessageFormatter.fromConnectionMessage(e).then(n=>{i.resolve({Message:e,RawWebsocketMessage:n,sendStatusDeferral:t})},n=>{i.reject(`Error formatting the message. ${n}`)}),t.promise}read(){return this.privConnectionState!==M.Connected?Promise.reject(`Cannot read on connection that is in ${this.privConnectionState} state`):this.privReceivingMessageQueue.dequeue()}close(e){if(this.privWebsocketClient)this.privConnectionState!==M.Disconnected&&this.privWebsocketClient.close(1e3,e||"Normal closure by client");else return Promise.resolve();return this.privDisconnectDeferral.promise}get events(){return this.privConnectionEvents}sendRawMessage(e){try{if(!e)return Promise.resolve();if(this.onEvent(new Ai(this.privConnectionId,new Date().toISOString(),e.Message)),this.isWebsocketOpen)this.privWebsocketClient.send(e.RawWebsocketMessage.payload);else return Promise.reject("websocket send error: Websocket not ready "+this.privConnectionId+" "+e.Message.id+" "+new Error().stack);return Promise.resolve()}catch(t){return Promise.reject(`websocket send error: ${t}`)}}onClose(e,t){return fi(this,void 0,void 0,function*(){const i=`Connection closed. ${e}: ${t}`;this.privConnectionState=M.Disconnected,this.privDisconnectDeferral.resolve(),yield this.privReceivingMessageQueue.drainAndDispose(()=>{},i),yield this.privSendMessageQueue.drainAndDispose(n=>{n.sendStatusDeferral.reject(i)},i)})}processSendQueue(){return fi(this,void 0,void 0,function*(){for(;;){const t=yield this.privSendMessageQueue.dequeue();if(!t)return;try{yield this.sendRawMessage(t),t.sendStatusDeferral.resolve()}catch(i){t.sendStatusDeferral.reject(i)}}})}onEvent(e){this.privConnectionEvents.onEvent(e),N.instance.onEvent(e)}get isWebsocketOpen(){return this.privWebsocketClient&&this.privWebsocketClient.readyState===this.privWebsocketClient.OPEN}}tt.forceNpmWebSocket=!1;var Er=globalThis&&globalThis.__awaiter||function(r,e,t,i){function n(s){return s instanceof t?s:new t(function(o){o(s)})}return new(t||(t=Promise))(function(s,o){function a(c){try{h(i.next(c))}catch(d){o(d)}}function u(c){try{h(i.throw(c))}catch(d){o(d)}}function h(c){c.done?s(c.value):n(c.value).then(a,u)}h((i=i.apply(r,e||[])).next())})};class gi{constructor(e,t,i,n,s,o=!1,a){if(this.privIsDisposed=!1,!e)throw new I("uri");if(!n)throw new I("messageFormatter");this.privMessageFormatter=n;let u="",h=0;if(t){for(const c in t)if(c){u+=h===0&&e.indexOf("?")===-1?"?":"&";const d=encodeURIComponent(t[c]);u+=`${c}=${d}`,h++}}if(i){for(const c in i)if(c){u+=h===0&&e.indexOf("?")===-1?"?":"&";const d=encodeURIComponent(i[c]);u+=`${c}=${d}`,h++}}this.privUri=e+u,this.privId=a||E(),this.privConnectionMessageAdapter=new tt(this.privUri,this.id,this.privMessageFormatter,s,i,o)}dispose(){return Er(this,void 0,void 0,function*(){this.privIsDisposed=!0,this.privConnectionMessageAdapter&&(yield this.privConnectionMessageAdapter.close())})}isDisposed(){return this.privIsDisposed}get id(){return this.privId}state(){return this.privConnectionMessageAdapter.state}open(){return this.privConnectionMessageAdapter.open()}send(e){return this.privConnectionMessageAdapter.send(e)}read(){return this.privConnectionMessageAdapter.read()}get events(){return this.privConnectionMessageAdapter.events}}class Rr{constructor(e,t){this.privBuffers=[],this.privReplayOffset=0,this.privLastShrinkOffset=0,this.privBufferStartOffset=0,this.privBufferSerial=0,this.privBufferedBytes=0,this.privReplay=!1,this.privLastChunkAcquiredTime=0,this.privAudioNode=e,this.privBytesPerSecond=t}id(){return this.privAudioNode.id()}read(){if(!!this.privReplay&&this.privBuffers.length!==0){const e=this.privReplayOffset-this.privBufferStartOffset;let t=Math.round(e*this.privBytesPerSecond*1e-7);t%2!==0&&t++;let i=0;for(;i<this.privBuffers.length&&t>=this.privBuffers[i].chunk.buffer.byteLength;)t-=this.privBuffers[i++].chunk.buffer.byteLength;if(i<this.privBuffers.length){const n=this.privBuffers[i].chunk.buffer.slice(t);return this.privReplayOffset+=n.byteLength/this.privBytesPerSecond*1e7,i===this.privBuffers.length-1&&(this.privReplay=!1),Promise.resolve({buffer:n,isEnd:!1,timeReceived:this.privBuffers[i].chunk.timeReceived})}}return this.privAudioNode.read().then(e=>(e&&e.buffer&&(this.privBuffers.push(new Pr(e,this.privBufferSerial++,this.privBufferedBytes)),this.privBufferedBytes+=e.buffer.byteLength),e))}detach(){return this.privBuffers=void 0,this.privAudioNode.detach()}replay(){this.privBuffers&&this.privBuffers.length!==0&&(this.privReplay=!0,this.privReplayOffset=this.privLastShrinkOffset)}shrinkBuffers(e){if(this.privBuffers===void 0||this.privBuffers.length===0)return;this.privLastShrinkOffset=e;const t=e-this.privBufferStartOffset;let i=Math.round(t*this.privBytesPerSecond*1e-7),n=0;for(;n<this.privBuffers.length&&i>=this.privBuffers[n].chunk.buffer.byteLength;)i-=this.privBuffers[n++].chunk.buffer.byteLength;this.privBufferStartOffset=Math.round(e-i/this.privBytesPerSecond*1e7),this.privBuffers=this.privBuffers.slice(n)}findTimeAtOffset(e){if(e<this.privBufferStartOffset||this.privBuffers===void 0)return 0;for(const t of this.privBuffers){const i=t.byteOffset/this.privBytesPerSecond*1e7,n=i+t.chunk.buffer.byteLength/this.privBytesPerSecond*1e7;if(e>=i&&e<=n)return t.chunk.timeReceived}return 0}}class Pr{constructor(e,t,i){this.chunk=e,this.serial=t,this.byteOffset=i}}class it{constructor(e,t,i,n){this.privProxyHostName=e,this.privProxyPort=t,this.privProxyUserName=i,this.privProxyPassword=n}static fromParameters(e){return new it(e.getProperty(p.SpeechServiceConnection_ProxyHostName),parseInt(e.getProperty(p.SpeechServiceConnection_ProxyPort),10),e.getProperty(p.SpeechServiceConnection_ProxyUserName),e.getProperty(p.SpeechServiceConnection_ProxyPassword))}static fromRecognizerConfig(e){return this.fromParameters(e.parameters)}get HostName(){return this.privProxyHostName}get Port(){return this.privProxyPort}get UserName(){return this.privProxyUserName}get Password(){return this.privProxyPassword}}const Ar=new Set(["json","buffer","string"]);var Ir=r=>(...e)=>{const t=new Set;let i,n,s,o="";return e.forEach(a=>{if(typeof a=="string")if(a.toUpperCase()===a)if(i){const u=`Can't set method to ${a}, already set to ${i}.`;throw new Error(u)}else i=a;else if(a.startsWith("http:")||a.startsWith("https:"))o=a;else if(Ar.has(a))n=a;else throw new Error(`Unknown encoding, ${a}`);else if(typeof a=="number")t.add(a);else if(typeof a=="object")if(Array.isArray(a)||a instanceof Set)a.forEach(u=>t.add(u));else{if(s)throw new Error("Cannot set headers twice.");s=a}else throw new Error(`Unknown type: ${typeof a}`)}),i||(i="GET"),t.size===0&&t.add(200),r(t,i,n,s,o)};const Mr=Ir;class It extends Error{constructor(e,...t){super(...t),Error.captureStackTrace&&Error.captureStackTrace(this,It),this.name="StatusError",this.message=e.statusMessage,this.statusCode=e.status,this.res=e,this.json=e.json.bind(e),this.text=e.text.bind(e),this.arrayBuffer=e.arrayBuffer.bind(e);let i;Object.defineProperty(this,"responseBody",{get:()=>(i||(i=this.arrayBuffer()),i)}),this.headers={};for(const[s,o]of e.headers.entries())this.headers[s.toLowerCase()]=o}}var Dr=Mr((r,e,t,i,n)=>async(s,o,a={})=>{s=n+(s||"");let u=new URL(s);if(i||(i={}),u.username&&(i.Authorization="Basic "+btoa(u.username+":"+u.password),u=new URL(u.protocol+"//"+u.host+u.pathname+u.search)),u.protocol!=="https:"&&u.protocol!=="http:")throw new Error(`Unknown protocol, ${u.protocol}`);if(o&&!(o instanceof ArrayBuffer||ArrayBuffer.isView(o)||typeof o=="string"))if(typeof o=="object")o=JSON.stringify(o),i["Content-Type"]="application/json";else throw new Error("Unknown body type.");a=new Headers({...i||{},...a});const h=await fetch(u,{method:e,headers:a,body:o});if(h.statusCode=h.status,!r.has(h.status))throw new It(h);return t==="json"?h.json():t==="buffer"?h.arrayBuffer():t==="string"?h.text():h}),kr=globalThis&&globalThis.__awaiter||function(r,e,t,i){function n(s){return s instanceof t?s:new t(function(o){o(s)})}return new(t||(t=Promise))(function(s,o){function a(c){try{h(i.next(c))}catch(d){o(d)}}function u(c){try{h(i.throw(c))}catch(d){o(d)}}function h(c){c.done?s(c.value):n(c.value).then(a,u)}h((i=i.apply(r,e||[])).next())})},he;(function(r){r.Get="GET",r.Post="POST",r.Delete="DELETE",r.File="file"})(he||(he={}));class xr{constructor(e){if(!e)throw new I("configParams");this.privHeaders=e.headers,this.privIgnoreCache=e.ignoreCache}static extractHeaderValue(e,t){let i="";try{const n=t.trim().split(/[\r\n]+/),s={};n.forEach(o=>{const a=o.split(": "),u=a.shift().toLowerCase(),h=a.join(": ");s[u]=h}),i=s[e.toLowerCase()]}catch{}return i}set options(e){this.privHeaders=e.headers,this.privIgnoreCache=e.ignoreCache}setHeaders(e,t){this.privHeaders[e]=t}request(e,t,i={},n=null,s=null){const o=new D,a=e===he.File?"POST":e,u=(d,S={})=>{const _=d;return{data:JSON.stringify(S),headers:JSON.stringify(d.headers),json:S,ok:d.statusCode>=200&&d.statusCode<300,status:d.statusCode,statusText:S.error?S.error.message:_.statusText?_.statusText:_.statusMessage}},h=d=>{const S=new FileReader;return S.readAsArrayBuffer(d),new Promise(_=>{S.onloadend=()=>{_(S.result)}})},c=d=>{const S=Dr(t,a,this.privHeaders,200,201,202,204,400,401,402,403,404),_=this.queryParams(i)===""?"":`?${this.queryParams(i)}`;S(_,d).then(Z=>kr(this,void 0,void 0,function*(){if(e===he.Delete||Z.statusCode===204)o.resolve(u(Z));else try{const ke=yield Z.json();o.resolve(u(Z,ke))}catch{o.resolve(u(Z))}})).catch(Z=>{o.reject(Z)})};if(this.privIgnoreCache&&(this.privHeaders["Cache-Control"]="no-cache"),e===he.File&&s){const d="multipart/form-data";this.privHeaders["content-type"]=d,this.privHeaders["Content-Type"]=d,typeof Blob!="undefined"&&s instanceof Blob?h(s).then(S=>{c(S)}).catch(S=>{o.reject(S)}):c(s)}else e===he.Post&&n&&(this.privHeaders["content-type"]="application/json",this.privHeaders["Content-Type"]="application/json"),c(n);return o.promise}withQuery(e,t={}){const i=this.queryParams(t);return i?e+(e.indexOf("?")===-1?"?":"&")+i:e}queryParams(e={}){return Object.keys(e).map(t=>encodeURIComponent(t)+"="+encodeURIComponent(e[t])).join("&")}}class Nr{constructor(e,t,i,n=null){y(this,"key");y(this,"region");y(this,"sourceLanguage");y(this,"targetLanguage");y(this,"recognizer");this.key=e,this.region=t,this.sourceLanguage=i,this.targetLanguage=n!==null?n:i}async start(){await this.registerBindings(document)}async registerBindings(e){const t=e.childNodes;for(let i=0;i<t.length;i++){if(!t[i])continue;const n=t[i];n.attributes&&(n.attributes.getNamedItem("co-stt.start")?await this.handleStartModifier(n,n.attributes.getNamedItem("co-stt.start")):n.attributes.getNamedItem("co-stt.stop")&&await this.handleStopModifier(n,n.attributes.getNamedItem("co-stt.stop"))),n.childNodes.length>0&&await this.registerBindings(n)}}async handleStartModifier(e,t){e.addEventListener("click",async i=>{const n=Jt.fromSubscription(this.key,this.region);n.speechRecognitionLanguage=this.sourceLanguage,n.addTargetLanguage(this.targetLanguage);const s=ee.fromDefaultMicrophoneInput();this.recognizer=new Ji(n,s),document.dispatchEvent(new CustomEvent("COAzureSTTStartedRecording",{}));const o=[];this.recognizer.recognizing=(a,u)=>{const h=u.result;if(h&&h.reason===P.TranslatingSpeech){const c=h.translations.get(this.targetLanguage);o["result_"+h.privOffset.toString()]=c;const d=Object.values(o).join(". "),S=document.getElementById(t.value);S!==null&&(S instanceof HTMLInputElement?S.value=`${d} `:S.innerHTML=`${d} `)}},this.recognizer.startContinuousRecognitionAsync(a=>{},a=>{console.log(a),this.stop()})})}async handleStopModifier(e,t){e.addEventListener("click",async i=>{await this.stop()})}async stop(){this.recognizer!==void 0&&(this.recognizer.stopContinuousRecognitionAsync(),this.recognizer.close(),this.recognizer=void 0),document.dispatchEvent(new CustomEvent("COAzureSTTStoppedRecording",{}))}}class zr{constructor(e,t,i,n=0,s=0,o=""){y(this,"key");y(this,"region");y(this,"voice");y(this,"rate");y(this,"pitch");y(this,"textToRead","");y(this,"wordBoundryList",[]);y(this,"clickedNode");y(this,"highlightDiv");y(this,"speechConfig");y(this,"audioConfig");y(this,"player");y(this,"synthesizer");y(this,"previousWordBoundary");y(this,"interval");y(this,"wordEncounters",[]);y(this,"originalHighlightDivInnerHTML","");y(this,"currentWord","");y(this,"currentOffset",0);y(this,"wordBoundaryOffset",0);y(this,"playbackTextOffsetBase");y(this,"prevTextOffset",0);y(this,"url","");y(this,"prefetchedAudio",new Map);y(this,"prefetchPromises",new Map);y(this,"activePrefetchedAudioUrl","");y(this,"playbackSegments",[]);this.key=e,this.region=t,this.voice=i,this.rate=n,this.pitch=s,this.url=o}async start(){await this.registerBindings(document)}setVoice(e){return this.voice=e,this.clearPrefetchedAudio(),this}setRate(e){return this.rate=e,this.clearPrefetchedAudio(),this}setPitch(e){return this.pitch=e,this.clearPrefetchedAudio(),this}async registerBindings(e){const t=e.childNodes;for(let i=0;i<t.length;i++){if(!t[i])continue;const n=t[i];n.attributes&&(n.attributes.getNamedItem("co-tts.id")?await this.handleIdModifier(n,n.attributes.getNamedItem("co-tts.id")):n.attributes.getNamedItem("co-tts.ajax")?await this.handleAjaxModifier(n,n.attributes.getNamedItem("co-tts.ajax")):n.attributes.getNamedItem("co-tts")?await this.handleDefault(n,n.attributes.getNamedItem("co-tts")):n.attributes.getNamedItem("co-tts.stop")?await this.handleStopModifier(n,n.attributes.getNamedItem("co-tts.stop")):n.attributes.getNamedItem("co-tts.resume")?await this.handleResumeModifier(n,n.attributes.getNamedItem("co-tts.resume")):n.attributes.getNamedItem("co-tts.pause")&&await this.handlePauseModifier(n,n.attributes.getNamedItem("co-tts.pause"))),n.childNodes.length>0&&await this.registerBindings(n)}}async handleIdModifier(e,t){e.addEventListener("click",async i=>{var s,o;this.stopPlayer(),await this.createInterval();const n=document.getElementById(t.value);if(this.clickedNode=n,!!n){if(n.hasAttribute("co-tts.text")&&n.getAttribute("co-tts.text")!==""?this.textToRead=(s=n.getAttribute("co-tts.text"))!=null?s:"":this.textToRead=n.innerText,n.hasAttribute("co-tts.highlight"))if(((o=n.attributes.getNamedItem("co-tts.highlight"))==null?void 0:o.value)!==""){const a=document.getElementById(n.attributes.getNamedItem("co-tts.highlight").value);this.highlightDiv=a,this.originalHighlightDivInnerHTML=a.innerHTML}else this.highlightDiv=n,this.originalHighlightDivInnerHTML=n.innerHTML;this.startSynthesizer(e,t)}})}async handleAjaxModifier(e,t){e.addEventListener("click",async i=>{this.stopPlayer(),await this.createInterval(),this.clickedNode=e;const n=await fetch(t.value,{method:"GET"});this.textToRead=await n.text(),this.startSynthesizer(e,t)})}async handleDefault(e,t){e.addEventListener("click",async i=>{var n;if(this.stopPlayer(),await this.createInterval(),this.clickedNode=e,e.hasAttribute("co-tts.highlight"))if(((n=e.attributes.getNamedItem("co-tts.highlight"))==null?void 0:n.value)!==""){const s=document.getElementById(e.attributes.getNamedItem("co-tts.highlight").value);this.highlightDiv=s,this.originalHighlightDivInnerHTML=s.innerHTML}else this.highlightDiv=e,this.originalHighlightDivInnerHTML=e.innerHTML;t.value===""?this.textToRead=e.innerText:this.textToRead=t.value,this.startSynthesizer(e,t)})}async handleWithoutClick(e,t){var i;if(this.stopPlayer(),await this.createInterval(),this.clickedNode=e,e.hasAttribute("co-tts.highlight"))if(((i=e.attributes.getNamedItem("co-tts.highlight"))==null?void 0:i.value)!==""){const n=document.getElementById(e.attributes.getNamedItem("co-tts.highlight").value);this.highlightDiv=n,n!==null&&(this.originalHighlightDivInnerHTML=n.innerHTML)}else this.highlightDiv=e,this.originalHighlightDivInnerHTML=e.innerHTML;t.value===""?this.textToRead=e.innerText:this.textToRead=t.value,this.startSynthesizer(e,t)}async handleStopModifier(e,t){e.addEventListener("click",async i=>{await this.stopPlayer(),document.dispatchEvent(new CustomEvent("COAzureTTSStoppedPlaying",{}))})}async handlePauseModifier(e,t){e.addEventListener("click",async i=>{await this.clearInterval(),await this.player.pause(),document.dispatchEvent(new CustomEvent("COAzureTTSPausedPlaying",{}))})}async handleResumeModifier(e,t){e.addEventListener("click",async i=>{await this.createInterval(),await this.player.resume(),document.dispatchEvent(new CustomEvent("COAzureTTSResumedPlaying",{}))})}async stopPlayer(){await this.clearInterval(),this.highlightDiv!==void 0&&(this.highlightDiv.innerHTML=this.originalHighlightDivInnerHTML),this.textToRead="",this.currentWord="",this.originalHighlightDivInnerHTML="",this.wordBoundryList=[],this.wordEncounters=[],this.resetPlaybackSegments(),this.playbackSegments=[],this.player!==void 0&&this.player.pause(),this.activePrefetchedAudioUrl!==""&&(URL.revokeObjectURL(this.activePrefetchedAudioUrl),this.activePrefetchedAudioUrl=""),this.player=void 0,this.highlightDiv=void 0,this.prevTextOffset=0,this.playbackTextOffsetBase=void 0}async startSynthesizer(e,t){this.speechConfig=_e.fromSubscription(this.key,this.region),this.speechConfig.speechSynthesisVoiceName=`Microsoft Server Speech Text to Speech Voice (${this.voice})`,this.speechConfig.speechSynthesisOutputFormat=g.Audio24Khz160KBitRateMonoMp3,this.player=new yt,this.audioConfig=ee.fromSpeakerOutput(this.player),this.synthesizer=new Pe(this.speechConfig,this.audioConfig),this.synthesizer.wordBoundary=(s,o)=>{this.wordBoundryList.push(o)};const i=this.collectPlaybackChain(this.clickedNode),n=i.length>1;n?this.preparePlaybackChain(i):this.playbackSegments=[],this.player.onAudioEnd=async()=>{const s=this.playbackSegments.length>0;if(this.stopPlayer(),s){document.dispatchEvent(new CustomEvent("COAzureTTSFinishedPlaying",{}));return}if(this.clickedNode.hasAttribute("co-tts.next")){const o=document.getElementById(this.clickedNode.getAttribute("co-tts.next"));if(o&&await this.playPrefetchedNode(o))return;o&&o.attributes.getNamedItem("co-tts.text")?this.handleWithoutClick(o,o.attributes.getNamedItem("co-tts.text")):o&&o.dispatchEvent(new Event("click"))}else document.dispatchEvent(new CustomEvent("COAzureTTSFinishedPlaying",{}))},this.player.onAudioStart=async()=>{document.dispatchEvent(new CustomEvent("COAzureTTSStartedPlaying",{}))},n||this.prefetchNextNode(this.clickedNode),this.synthesizer.speakSsmlAsync(this.buildSSML(this.textToRead),()=>{this.synthesizer.close(),this.synthesizer=void 0},()=>{this.synthesizer.close(),this.synthesizer=void 0})}collectPlaybackChain(e){var o,a;const t=[],i=new Set;let n=e,s=0;for(;n&&!i.has(n.id);){i.add(n.id);const u=(o=n.attributes.getNamedItem("co-tts.text"))!=null?o:n.attributes.getNamedItem("co-tts"),h=this.getNodeText(n,u);if(h==="")break;const c=this.getHighlightDivForNode(n);if(t.push({node:n,text:h,start:s,end:s+h.length,highlightDiv:c,originalHighlightDivInnerHTML:(a=c==null?void 0:c.innerHTML)!=null?a:""}),s+=h.length+1,!n.hasAttribute("co-tts.next"))break;n=document.getElementById(n.getAttribute("co-tts.next"))}return t}preparePlaybackChain(e){this.playbackSegments=e,this.textToRead=e.map(t=>t.text).join(" "),this.highlightDiv=void 0,this.originalHighlightDivInnerHTML="",this.wordEncounters=[],this.previousWordBoundary=void 0,this.prevTextOffset=0,this.playbackTextOffsetBase=void 0,this.currentWord="",this.currentOffset=0,this.wordBoundaryOffset=0}getHighlightDivForNode(e){var t;if(!!e.hasAttribute("co-tts.highlight"))return((t=e.attributes.getNamedItem("co-tts.highlight"))==null?void 0:t.value)!==""?document.getElementById(e.attributes.getNamedItem("co-tts.highlight").value):e}resetPlaybackSegments(){this.playbackSegments.forEach(e=>{e.highlightDiv&&(e.highlightDiv.innerHTML=e.originalHighlightDivInnerHTML)})}updateChainedHighlight(e){var h;if(~[".",",","!","?","*","(",")","&","\\","/","^","[","]","<",">",":"].indexOf(e.text)&&(e=(h=this.previousWordBoundary)!=null?h:void 0),!e){this.resetPlaybackSegments();return}this.playbackTextOffsetBase===void 0&&(this.playbackTextOffsetBase=e.textOffset);const t=Math.max(0,e.textOffset-this.playbackTextOffsetBase),i=this.playbackSegments.find(c=>t>=c.start&&t<c.end);if(this.resetPlaybackSegments(),!(i!=null&&i.highlightDiv)){this.previousWordBoundary=e;return}const n=t-i.start,s=this.getPosition(i.originalHighlightDivInnerHTML,e.text,n);if(s===Number.MAX_SAFE_INTEGER){this.previousWordBoundary=e;return}const o=i.originalHighlightDivInnerHTML.substring(0,s),a=s+e.wordLength,u=i.originalHighlightDivInnerHTML.substring(a);i.highlightDiv.innerHTML=`
21
+ ${o}<mark class='co-tts-highlight'>${e.text}</mark>${u}
22
+ `,this.previousWordBoundary=e}getNodeText(e,t){var i;return t&&t.value!==""?t.value:e.hasAttribute("co-tts.text")&&e.getAttribute("co-tts.text")!==""?(i=e.getAttribute("co-tts.text"))!=null?i:"":e.innerText}getPrefetchKey(e,t){return[e.id,t,this.voice,this.rate,this.pitch,this.url].join("|")}clearPrefetchedAudio(){this.prefetchedAudio.forEach(e=>{e.url&&URL.revokeObjectURL(e.url)}),this.prefetchedAudio.clear(),this.prefetchPromises.clear()}async prefetchNextNode(e){var c;if(!e||!e.hasAttribute("co-tts.next"))return;const t=document.getElementById(e.getAttribute("co-tts.next"));if(!t)return;const i=(c=t.attributes.getNamedItem("co-tts.text"))!=null?c:t.attributes.getNamedItem("co-tts"),n=this.getNodeText(t,i);if(n==="")return;const s=this.getPrefetchKey(t,n);if(this.prefetchedAudio.has(s)||this.prefetchPromises.has(s))return;const o=_e.fromSubscription(this.key,this.region);o.speechSynthesisVoiceName=`Microsoft Server Speech Text to Speech Voice (${this.voice})`,o.speechSynthesisOutputFormat=g.Audio24Khz160KBitRateMonoMp3;const a=new Pe(o,null),u=[];a.wordBoundary=(d,S)=>{u.push(S)};const h=new Promise(d=>{a.speakSsmlAsync(this.buildSSML(n),S=>{if(a.close(),!(S!=null&&S.audioData)){d(null);return}const _=new Blob([S.audioData],{type:"audio/mpeg"}),Z=URL.createObjectURL(_),ke={key:s,nodeId:t.id,text:n,url:Z,wordBoundryList:u};this.prefetchedAudio.set(s,ke),d(ke)},()=>{a.close(),d(null)})}).finally(()=>{this.prefetchPromises.delete(s)});this.prefetchPromises.set(s,h)}async playPrefetchedNode(e){var a,u,h;const t=(a=e.attributes.getNamedItem("co-tts.text"))!=null?a:e.attributes.getNamedItem("co-tts"),i=this.getNodeText(e,t),n=this.getPrefetchKey(e,i),s=(u=this.prefetchedAudio.get(n))!=null?u:await this.prefetchPromises.get(n);if(!s)return!1;if(this.prefetchedAudio.delete(n),this.clickedNode=e,this.textToRead=s.text,this.wordBoundryList=s.wordBoundryList,this.wordEncounters=[],this.previousWordBoundary=void 0,this.prevTextOffset=0,this.currentWord="",this.currentOffset=0,this.wordBoundaryOffset=0,e.hasAttribute("co-tts.highlight"))if(((h=e.attributes.getNamedItem("co-tts.highlight"))==null?void 0:h.value)!==""){const c=document.getElementById(e.attributes.getNamedItem("co-tts.highlight").value);this.highlightDiv=c,c!==null&&(this.originalHighlightDivInnerHTML=c.innerHTML)}else this.highlightDiv=e,this.originalHighlightDivInnerHTML=e.innerHTML;await this.createInterval();const o=new Audio(s.url);return this.activePrefetchedAudioUrl=s.url,this.player=o,o.addEventListener("play",()=>{document.dispatchEvent(new CustomEvent("COAzureTTSStartedPlaying",{}))},{once:!0}),o.addEventListener("ended",async()=>{if(this.stopPlayer(),this.clickedNode.hasAttribute("co-tts.next")){const c=document.getElementById(this.clickedNode.getAttribute("co-tts.next"));if(c&&await this.playPrefetchedNode(c))return;c&&c.attributes.getNamedItem("co-tts.text")?this.handleWithoutClick(c,c.attributes.getNamedItem("co-tts.text")):c&&c.dispatchEvent(new Event("click"))}else document.dispatchEvent(new CustomEvent("COAzureTTSFinishedPlaying",{}))},{once:!0}),this.prefetchNextNode(e),await o.play(),!0}async clearInterval(){clearInterval(this.interval)}async createInterval(){this.interval=setInterval(()=>{var e;if(this.player!==void 0&&(this.highlightDiv||this.playbackSegments.length>0)){const t=this.player.currentTime;let i;for(const n of this.wordBoundryList)if(t*1e3>n.audioOffset/1e4)i=n;else break;if(i!==void 0){if(this.playbackSegments.length>0){this.updateChainedHighlight(i);return}if(~[".",",","!","?","*","(",")","&","\\","/","^","[","]","<",">",":"].indexOf(i.text)&&(i=(e=this.previousWordBoundary)!=null?e:void 0),i===void 0||this.prevTextOffset>i.prevTextOffset)this.highlightDiv.innerHTML=this.originalHighlightDivInnerHTML;else if(this.wordEncounters[i.text]||(this.wordEncounters[i.text]=0),this.prevTextOffset=i.prevTextOffset,(this.currentWord!==i.text||this.wordBoundaryOffset!==i.textOffset)&&(this.currentOffset=this.getPosition(this.originalHighlightDivInnerHTML,i.text,i.textOffset),this.wordEncounters[i.text]=this.currentOffset+i.wordLength,this.currentWord=i.text,this.wordBoundaryOffset=i.textOffset),this.currentOffset===Number.MAX_SAFE_INTEGER)this.highlightDiv.innerHTML=this.originalHighlightDivInnerHTML;else{this.previousWordBoundary=i;const n=this.originalHighlightDivInnerHTML.substring(0,this.currentOffset),s=this.currentOffset+i.wordLength,o=this.originalHighlightDivInnerHTML.substring(s);this.highlightDiv.innerHTML=`
23
23
  ${n}<mark class='co-tts-highlight'>${i.text}</mark>${o}
24
- `}}else this.highlightDiv.innerHTML=this.originalHighlightDivInnerHTML}},50)}getPosition(e,t,i){let n=0,s=!1;for(let o=0;o<e.length;o++){const a=e[o];if(a==="<"&&(s=!0),!s){if(n===i)return o;n++}a===">"&&(s=!1)}return e.indexOf(t)}buildSSML(e){let t=`<speak xmlns="http://www.w3.org/2001/10/synthesis"
24
+ `}}else this.playbackSegments.length>0?this.resetPlaybackSegments():this.highlightDiv.innerHTML=this.originalHighlightDivInnerHTML}},50)}getPosition(e,t,i){let n=0,s=!1;for(let o=0;o<e.length;o++){const a=e[o];if(a==="<"&&(s=!0),!s){if(n===i)return o;n++}a===">"&&(s=!1)}return e.indexOf(t)}buildSSML(e){let t=`<speak xmlns="http://www.w3.org/2001/10/synthesis"
25
25
  xmlns:mstts="http://www.w3.org/2001/mstts"
26
26
  xmlns:emo="http://www.w3.org/2009/10/emotionml"
27
27
  version="1.0"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creativeorange/azure-text-to-speech",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "main": "dist/co-azure-tts.umd.js",
5
5
  "browser": "dist/co-azure-tts.es.js",
6
6
  "scripts": {
@@ -34,6 +34,7 @@ export class TextToSpeech {
34
34
  currentWord: string = '';
35
35
  currentOffset: number = 0;
36
36
  wordBoundaryOffset: number = 0;
37
+ playbackTextOffsetBase: number | undefined;
37
38
  prevTextOffset: number = 0;
38
39
  url: string = '';
39
40
  prefetchedAudio: Map<string, any> = new Map();
@@ -253,6 +254,7 @@ export class TextToSpeech {
253
254
  this.player = undefined;
254
255
  this.highlightDiv = undefined;
255
256
  this.prevTextOffset = 0;
257
+ this.playbackTextOffsetBase = undefined;
256
258
  }
257
259
 
258
260
  async startSynthesizer(node: any, attr: Attr) {
@@ -352,7 +354,7 @@ export class TextToSpeech {
352
354
  originalHighlightDivInnerHTML: highlightDiv?.innerHTML ?? '',
353
355
  });
354
356
 
355
- offset += text.length + 2;
357
+ offset += text.length + 1;
356
358
 
357
359
  if (!currentNode.hasAttribute('co-tts.next')) {
358
360
  break;
@@ -366,12 +368,13 @@ export class TextToSpeech {
366
368
 
367
369
  preparePlaybackChain(chain: any[]) {
368
370
  this.playbackSegments = chain;
369
- this.textToRead = chain.map((segment) => segment.text).join('. ');
371
+ this.textToRead = chain.map((segment) => segment.text).join(' ');
370
372
  this.highlightDiv = undefined;
371
373
  this.originalHighlightDivInnerHTML = '';
372
374
  this.wordEncounters = [];
373
375
  this.previousWordBoundary = undefined;
374
376
  this.prevTextOffset = 0;
377
+ this.playbackTextOffsetBase = undefined;
375
378
  this.currentWord = '';
376
379
  this.currentOffset = 0;
377
380
  this.wordBoundaryOffset = 0;
@@ -408,8 +411,13 @@ export class TextToSpeech {
408
411
  return;
409
412
  }
410
413
 
414
+ if (this.playbackTextOffsetBase === undefined) {
415
+ this.playbackTextOffsetBase = wordBoundary.textOffset;
416
+ }
417
+
418
+ const normalizedTextOffset = Math.max(0, wordBoundary.textOffset - this.playbackTextOffsetBase);
411
419
  const segment = this.playbackSegments.find((candidate) => (
412
- wordBoundary.textOffset >= candidate.start && wordBoundary.textOffset < candidate.end
420
+ normalizedTextOffset >= candidate.start && normalizedTextOffset < candidate.end
413
421
  ));
414
422
 
415
423
  this.resetPlaybackSegments();
@@ -420,7 +428,7 @@ export class TextToSpeech {
420
428
  return;
421
429
  }
422
430
 
423
- const relativeTextOffset = wordBoundary.textOffset - segment.start;
431
+ const relativeTextOffset = normalizedTextOffset - segment.start;
424
432
  const currentOffset = this.getPosition(segment.originalHighlightDivInnerHTML, wordBoundary.text, relativeTextOffset);
425
433
 
426
434
  if (currentOffset === Number.MAX_SAFE_INTEGER) {
@@ -620,7 +628,7 @@ export class TextToSpeech {
620
628
 
621
629
  async createInterval() {
622
630
  this.interval = setInterval(() => {
623
- if (this.player !== undefined && this.highlightDiv) {
631
+ if (this.player !== undefined && (this.highlightDiv || this.playbackSegments.length > 0)) {
624
632
  const currentTime = this.player.currentTime;
625
633
  let wordBoundary;
626
634
  for (const e of this.wordBoundryList) {
@@ -674,6 +682,8 @@ export class TextToSpeech {
674
682
  `;
675
683
  }
676
684
  }
685
+ } else if (this.playbackSegments.length > 0) {
686
+ this.resetPlaybackSegments();
677
687
  } else {
678
688
  this.highlightDiv.innerHTML = this.originalHighlightDivInnerHTML;
679
689
  }