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CAChC,cAAc,EAAE,eAAe,EAC/B,iBAAiB,EAAE,iBAAiB,GAAG,oBAAoB;IAI/D,SAAS,CAAC,yBAAyB,IAAI,IAAI;IAQ3C,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,qBAAqB,KAAK,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,IAAI,EAAE,UAAU,CAAC,EAAE,iBAAiB,GAAG,6BAA6B,GAAG,QAAQ,GAAG,IAAI;cAsDvL,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC;CAa5E","file":"SpeechSynthesizer.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\n/* eslint-disable @typescript-eslint/no-empty-function */\n\nimport { PathLike } from \"fs\";\nimport { IRestResponse } from \"../common.browser/RestMessageAdapter.js\";\nimport {\n IAuthentication,\n ISynthesisConnectionFactory,\n SpeechSynthesisConnectionFactory,\n SynthesisAdapterBase,\n SpeechSynthesisAdapter,\n SynthesisRestAdapter,\n SynthesizerConfig,\n} from \"../common.speech/Exports.js\";\nimport {\n createNoDashGuid,\n marshalPromiseToCallbacks,\n} from \"../common/Exports.js\";\nimport { AudioOutputConfigImpl } from \"./Audio/AudioConfig.js\";\nimport { AudioFileWriter } from \"./Audio/AudioFileWriter.js\";\nimport { AudioOutputFormatImpl } from \"./Audio/AudioOutputFormat.js\";\nimport {\n PullAudioOutputStreamImpl,\n PushAudioOutputStreamImpl\n} from \"./Audio/AudioOutputStream.js\";\nimport { Contracts } from \"./Contracts.js\";\nimport {\n AudioConfig,\n AudioOutputStream,\n AutoDetectSourceLanguageConfig,\n PropertyId,\n PullAudioOutputStream,\n PushAudioOutputStreamCallback,\n SpeechConfig,\n SpeechSynthesisBookmarkEventArgs,\n SpeechSynthesisEventArgs,\n SpeechSynthesisOutputFormat,\n SpeechSynthesisResult,\n SpeechSynthesisVisemeEventArgs,\n SpeechSynthesisWordBoundaryEventArgs,\n SynthesisVoicesResult,\n Synthesizer\n} from \"./Exports.js\";\nimport { SpeechConfigImpl } from \"./SpeechConfig.js\";\nimport { SynthesisRequest } from \"./Synthesizer.js\";\n\n/**\n * Defines the class SpeechSynthesizer for text to speech.\n * Updated in version 1.16.0\n * @class SpeechSynthesizer\n */\nexport class SpeechSynthesizer extends Synthesizer {\n protected audioConfig: AudioConfig;\n\n /**\n * Defines event handler for synthesis start events.\n * @member SpeechSynthesizer.prototype.synthesisStarted\n * @function\n * @public\n */\n public synthesisStarted: (sender: SpeechSynthesizer, event: SpeechSynthesisEventArgs) => void;\n\n /**\n * Defines event handler for synthesizing events.\n * @member SpeechSynthesizer.prototype.synthesizing\n * @function\n * @public\n */\n public synthesizing: (sender: SpeechSynthesizer, event: SpeechSynthesisEventArgs) => void;\n\n /**\n * Defines event handler for synthesis completed events.\n * @member SpeechSynthesizer.prototype.synthesisCompleted\n * @function\n * @public\n */\n public synthesisCompleted: (sender: SpeechSynthesizer, event: SpeechSynthesisEventArgs) => void;\n\n /**\n * Defines event handler for synthesis cancelled events.\n * @member SpeechSynthesizer.prototype.SynthesisCanceled\n * @function\n * @public\n */\n public SynthesisCanceled: (sender: SpeechSynthesizer, event: SpeechSynthesisEventArgs) => void;\n\n /**\n * Defines event handler for word boundary events\n * @member SpeechSynthesizer.prototype.wordBoundary\n * @function\n * @public\n */\n public wordBoundary: (sender: SpeechSynthesizer, event: SpeechSynthesisWordBoundaryEventArgs) => void;\n\n /**\n * Defines event handler for bookmark reached events\n * Added in version 1.16.0\n * @member SpeechSynthesizer.prototype.bookmarkReached\n * @function\n * @public\n */\n public bookmarkReached: (sender: SpeechSynthesizer, event: SpeechSynthesisBookmarkEventArgs) => void;\n\n /**\n * Defines event handler for viseme received event\n * Added in version 1.16.0\n * @member SpeechSynthesizer.prototype.visemeReceived\n * @function\n * @public\n */\n public visemeReceived: (sender: SpeechSynthesizer, event: SpeechSynthesisVisemeEventArgs) => void;\n\n /**\n * SpeechSynthesizer constructor.\n * @constructor\n * @param {SpeechConfig} speechConfig - An set of initial properties for this synthesizer.\n * @param {AudioConfig} audioConfig - An optional audio configuration associated with the synthesizer.\n */\n public constructor(speechConfig: SpeechConfig, audioConfig?: AudioConfig | null) {\n super(speechConfig);\n\n if (audioConfig !== null) {\n if (audioConfig === undefined) {\n this.audioConfig = (typeof window === \"undefined\") ? undefined : AudioConfig.fromDefaultSpeakerOutput();\n } else {\n this.audioConfig = audioConfig;\n }\n }\n\n this.privConnectionFactory = new SpeechSynthesisConnectionFactory();\n this.implCommonSynthesizeSetup();\n }\n\n /**\n * SpeechSynthesizer constructor.\n * @constructor\n * @param {SpeechConfig} speechConfig - an set of initial properties for this synthesizer\n * @param {AutoDetectSourceLanguageConfig} autoDetectSourceLanguageConfig - An source language detection configuration associated with the synthesizer\n * @param {AudioConfig} audioConfig - An optional audio configuration associated with the synthesizer\n */\n public static FromConfig(speechConfig: SpeechConfig, autoDetectSourceLanguageConfig: AutoDetectSourceLanguageConfig, audioConfig?: AudioConfig | null): SpeechSynthesizer {\n const speechConfigImpl: SpeechConfigImpl = speechConfig as SpeechConfigImpl;\n autoDetectSourceLanguageConfig.properties.mergeTo(speechConfigImpl.properties);\n return new SpeechSynthesizer(speechConfig, audioConfig);\n }\n\n /**\n * Executes speech synthesis on plain text.\n * The task returns the synthesis result.\n * @member SpeechSynthesizer.prototype.speakTextAsync\n * @function\n * @public\n * @param text - Text to be synthesized.\n * @param cb - Callback that received the SpeechSynthesisResult.\n * @param err - Callback invoked in case of an error.\n * @param stream - AudioOutputStream to receive the synthesized audio.\n */\n public speakTextAsync(text: string, cb?: (e: SpeechSynthesisResult) => void, err?: (e: string) => void, stream?: AudioOutputStream | PushAudioOutputStreamCallback | PathLike): void {\n this.speakImpl(text, false, cb, err, stream);\n }\n\n /**\n * Executes speech synthesis on SSML.\n * The task returns the synthesis result.\n * @member SpeechSynthesizer.prototype.speakSsmlAsync\n * @function\n * @public\n * @param ssml - SSML to be synthesized.\n * @param cb - Callback that received the SpeechSynthesisResult.\n * @param err - Callback invoked in case of an error.\n * @param stream - AudioOutputStream to receive the synthesized audio.\n */\n public speakSsmlAsync(ssml: string, cb?: (e: SpeechSynthesisResult) => void, err?: (e: string) => void, stream?: AudioOutputStream | PushAudioOutputStreamCallback | PathLike): void {\n this.speakImpl(ssml, true, cb, err, stream);\n }\n\n /**\n * Get list of synthesis voices available.\n * The task returns the synthesis voice result.\n * @member SpeechSynthesizer.prototype.getVoicesAsync\n * @function\n * @async\n * @public\n * @param locale - Locale of voices in BCP-47 format; if left empty, get all available voices.\n * @return {Promise<SynthesisVoicesResult>} - Promise of a SynthesisVoicesResult.\n */\n public async getVoicesAsync(locale: string = \"\"): Promise<SynthesisVoicesResult> {\n return this.getVoices(locale);\n }\n\n /**\n * Dispose of associated resources.\n * @member SpeechSynthesizer.prototype.close\n * @function\n * @public\n */\n public close(cb?: () => void, err?: (error: string) => void): void {\n Contracts.throwIfDisposed(this.privDisposed);\n\n marshalPromiseToCallbacks(this.dispose(true), cb, err);\n }\n\n /**\n * @Internal\n * Do not use externally, object returned will change without warning or notice.\n */\n public get internalData(): object {\n return this.privAdapter;\n }\n\n //\n // ################################################################################################################\n // IMPLEMENTATION.\n // ################################################################################################################\n //\n\n // Creates the synthesis adapter\n protected createSynthesisAdapter(\n authentication: IAuthentication,\n connectionFactory: ISynthesisConnectionFactory,\n synthesizerConfig: SynthesizerConfig): SynthesisAdapterBase {\n return new SpeechSynthesisAdapter(authentication, connectionFactory,\n synthesizerConfig, this, this.audioConfig as AudioOutputConfigImpl);\n }\n\n protected createRestSynthesisAdapter(\n authentication: IAuthentication,\n synthesizerConfig: SynthesizerConfig): SynthesisRestAdapter {\n return new SynthesisRestAdapter(synthesizerConfig, authentication);\n }\n\n protected implCommonSynthesizeSetup(): void {\n super.implCommonSynthesizeSetup();\n\n this.privAdapter.audioOutputFormat = AudioOutputFormatImpl.fromSpeechSynthesisOutputFormat(\n SpeechSynthesisOutputFormat[this.properties.getProperty(PropertyId.SpeechServiceConnection_SynthOutputFormat, undefined) as keyof typeof SpeechSynthesisOutputFormat]\n );\n }\n\n protected speakImpl(text: string, IsSsml: boolean, cb?: (e: SpeechSynthesisResult) => void, err?: (e: string) => void, dataStream?: AudioOutputStream | PushAudioOutputStreamCallback | PathLike): void {\n try {\n Contracts.throwIfDisposed(this.privDisposed);\n const requestId = createNoDashGuid();\n let audioDestination;\n if (dataStream instanceof PushAudioOutputStreamCallback) {\n audioDestination = new PushAudioOutputStreamImpl(dataStream);\n } else if (dataStream instanceof PullAudioOutputStream) {\n audioDestination = dataStream as PullAudioOutputStreamImpl;\n } else if (dataStream !== undefined) {\n audioDestination = new AudioFileWriter(dataStream as PathLike);\n } else {\n audioDestination = undefined;\n }\n this.synthesisRequestQueue.enqueue(new SynthesisRequest(requestId, text, IsSsml, (e: SpeechSynthesisResult): void => {\n this.privSynthesizing = false;\n if (!!cb) {\n try {\n cb(e);\n } catch (e) {\n if (!!err) {\n err(e as string);\n }\n }\n }\n cb = undefined;\n /* eslint-disable no-empty */\n this.adapterSpeak().catch((): void => { });\n\n }, (e: string): void => {\n if (!!err) {\n err(e);\n }\n }, audioDestination));\n\n /* eslint-disable no-empty-function */\n this.adapterSpeak().catch((): void => { });\n\n } catch (error) {\n if (!!err) {\n if (error instanceof Error) {\n const typedError: Error = error;\n err(typedError.name + \": \" + typedError.message);\n } else {\n err(error as string);\n }\n }\n\n // Destroy the synthesizer.\n /* eslint-disable no-empty */\n this.dispose(true).catch((): void => { });\n }\n }\n\n protected async getVoices(locale: string): Promise<SynthesisVoicesResult> {\n const requestId = createNoDashGuid();\n const response: IRestResponse = await this.privRestAdapter.getVoicesList(requestId);\n if (response.ok && Array.isArray(response.json)) {\n let json = response.json;\n if (!!locale && locale.length > 0) {\n json = json.filter((item: { Locale: string }): boolean => !!item.Locale && item.Locale.toLowerCase() === locale.toLowerCase() );\n }\n return new SynthesisVoicesResult(requestId, json, undefined);\n } else {\n return new SynthesisVoicesResult(requestId, undefined, `Error: ${response.status}: ${response.statusText}`);\n }\n }\n}\n"]}