@creativeorange/azure-text-to-speech 2.2.1 → 2.2.2
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.
package/dist/co-azure-tts.es.js
CHANGED
|
@@ -8722,7 +8722,7 @@ class TextToSpeech {
|
|
|
8722
8722
|
highlightDiv,
|
|
8723
8723
|
originalHighlightDivInnerHTML: (_b = highlightDiv == null ? void 0 : highlightDiv.innerHTML) != null ? _b : ""
|
|
8724
8724
|
});
|
|
8725
|
-
offset += text.length +
|
|
8725
|
+
offset += text.length + 1;
|
|
8726
8726
|
if (!currentNode.hasAttribute("co-tts.next")) {
|
|
8727
8727
|
break;
|
|
8728
8728
|
}
|
|
@@ -8732,7 +8732,7 @@ class TextToSpeech {
|
|
|
8732
8732
|
}
|
|
8733
8733
|
preparePlaybackChain(chain) {
|
|
8734
8734
|
this.playbackSegments = chain;
|
|
8735
|
-
this.textToRead = chain.map((segment) => segment.text).join("
|
|
8735
|
+
this.textToRead = chain.map((segment) => segment.text).join(" ");
|
|
8736
8736
|
this.highlightDiv = void 0;
|
|
8737
8737
|
this.originalHighlightDivInnerHTML = "";
|
|
8738
8738
|
this.wordEncounters = [];
|
package/dist/co-azure-tts.umd.js
CHANGED
|
@@ -17,7 +17,7 @@ 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=`
|
|
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+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.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
21
|
${s}<mark class='co-tts-highlight'>${e.text}</mark>${a}
|
|
22
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=`
|
|
23
23
|
${n}<mark class='co-tts-highlight'>${i.text}</mark>${o}
|
package/package.json
CHANGED
package/src/TextToSpeech.ts
CHANGED
|
@@ -352,7 +352,7 @@ export class TextToSpeech {
|
|
|
352
352
|
originalHighlightDivInnerHTML: highlightDiv?.innerHTML ?? '',
|
|
353
353
|
});
|
|
354
354
|
|
|
355
|
-
offset += text.length +
|
|
355
|
+
offset += text.length + 1;
|
|
356
356
|
|
|
357
357
|
if (!currentNode.hasAttribute('co-tts.next')) {
|
|
358
358
|
break;
|
|
@@ -366,7 +366,7 @@ export class TextToSpeech {
|
|
|
366
366
|
|
|
367
367
|
preparePlaybackChain(chain: any[]) {
|
|
368
368
|
this.playbackSegments = chain;
|
|
369
|
-
this.textToRead = chain.map((segment) => segment.text).join('
|
|
369
|
+
this.textToRead = chain.map((segment) => segment.text).join(' ');
|
|
370
370
|
this.highlightDiv = undefined;
|
|
371
371
|
this.originalHighlightDivInnerHTML = '';
|
|
372
372
|
this.wordEncounters = [];
|