microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 15.5 kB
Source Map (JSON)
{"version":3,"sources":["src/sdk/SpeechSynthesizer.ts"],"names":[],"mappings":";AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAE9B,OAAO,EACH,eAAe,EACf,2BAA2B,EAE3B,oBAAoB,EAEpB,oBAAoB,EACpB,iBAAiB,EACpB,MAAM,6BAA6B,CAAC;AAarC,OAAO,EACH,WAAW,EACX,iBAAiB,EACjB,8BAA8B,EAG9B,6BAA6B,EAC7B,YAAY,EACZ,gCAAgC,EAChC,wBAAwB,EAExB,qBAAqB,EACrB,8BAA8B,EAC9B,oCAAoC,EACpC,qBAAqB,EACrB,WAAW,EACd,MAAM,cAAc,CAAC;AAItB;;;;GAIG;AACH,qBAAa,iBAAkB,SAAQ,WAAW;IAC9C,SAAS,CAAC,WAAW,EAAE,WAAW,CAAC;IAEnC;;;;;OAKG;IACI,gBAAgB,EAAE,CAAC,MAAM,EAAE,iBAAiB,EAAE,KAAK,EAAE,wBAAwB,KAAK,IAAI,CAAC;IAE9F;;;;;OAKG;IACI,YAAY,EAAE,CAAC,MAAM,EAAE,iBAAiB,EAAE,KAAK,EAAE,wBAAwB,KAAK,IAAI,CAAC;IAE1F;;;;;OAKG;IACI,kBAAkB,EAAE,CAAC,MAAM,EAAE,iBAAiB,EAAE,KAAK,EAAE,wBAAwB,KAAK,IAAI,CAAC;IAEhG;;;;;OAKG;IACI,iBAAiB,EAAE,CAAC,MAAM,EAAE,iBAAiB,EAAE,KAAK,EAAE,wBAAwB,KAAK,IAAI,CAAC;IAE/F;;;;;OAKG;IACI,YAAY,EAAE,CAAC,MAAM,EAAE,iBAAiB,EAAE,KAAK,EAAE,oCAAoC,KAAK,IAAI,CAAC;IAEtG;;;;;;OAMG;IACI,eAAe,EAAE,CAAC,MAAM,EAAE,iBAAiB,EAAE,KAAK,EAAE,gCAAgC,KAAK,IAAI,CA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AG,oBAAoB;IAK/D,SAAS,CAAC,0BAA0B,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.\r\n// Licensed under the MIT license.\r\n\r\n/* eslint-disable @typescript-eslint/no-empty-function */\r\n\r\nimport { PathLike } from \"fs\";\r\nimport { IRestResponse } from \"../common.browser/RestMessageAdapter.js\";\r\nimport {\r\n IAuthentication,\r\n ISynthesisConnectionFactory,\r\n SpeechSynthesisConnectionFactory,\r\n SynthesisAdapterBase,\r\n SpeechSynthesisAdapter,\r\n SynthesisRestAdapter,\r\n SynthesizerConfig,\r\n} from \"../common.speech/Exports.js\";\r\nimport {\r\n createNoDashGuid,\r\n marshalPromiseToCallbacks,\r\n} from \"../common/Exports.js\";\r\nimport { AudioOutputConfigImpl } from \"./Audio/AudioConfig.js\";\r\nimport { AudioFileWriter } from \"./Audio/AudioFileWriter.js\";\r\nimport { AudioOutputFormatImpl } from \"./Audio/AudioOutputFormat.js\";\r\nimport {\r\n PullAudioOutputStreamImpl,\r\n PushAudioOutputStreamImpl\r\n} from \"./Audio/AudioOutputStream.js\";\r\nimport { Contracts } from \"./Contracts.js\";\r\nimport {\r\n AudioConfig,\r\n AudioOutputStream,\r\n AutoDetectSourceLanguageConfig,\r\n PropertyId,\r\n PullAudioOutputStream,\r\n PushAudioOutputStreamCallback,\r\n SpeechConfig,\r\n SpeechSynthesisBookmarkEventArgs,\r\n SpeechSynthesisEventArgs,\r\n SpeechSynthesisOutputFormat,\r\n SpeechSynthesisResult,\r\n SpeechSynthesisVisemeEventArgs,\r\n SpeechSynthesisWordBoundaryEventArgs,\r\n SynthesisVoicesResult,\r\n Synthesizer\r\n} from \"./Exports.js\";\r\nimport { SpeechConfigImpl } from \"./SpeechConfig.js\";\r\nimport { SynthesisRequest } from \"./Synthesizer.js\";\r\n\r\n/**\r\n * Defines the class SpeechSynthesizer for text to speech.\r\n * Updated in version 1.16.0\r\n * @class SpeechSynthesizer\r\n */\r\nexport class SpeechSynthesizer extends Synthesizer {\r\n protected audioConfig: AudioConfig;\r\n\r\n /**\r\n * Defines event handler for synthesis start events.\r\n * @member SpeechSynthesizer.prototype.synthesisStarted\r\n * @function\r\n * @public\r\n */\r\n public synthesisStarted: (sender: SpeechSynthesizer, event: SpeechSynthesisEventArgs) => void;\r\n\r\n /**\r\n * Defines event handler for synthesizing events.\r\n * @member SpeechSynthesizer.prototype.synthesizing\r\n * @function\r\n * @public\r\n */\r\n public synthesizing: (sender: SpeechSynthesizer, event: SpeechSynthesisEventArgs) => void;\r\n\r\n /**\r\n * Defines event handler for synthesis completed events.\r\n * @member SpeechSynthesizer.prototype.synthesisCompleted\r\n * @function\r\n * @public\r\n */\r\n public synthesisCompleted: (sender: SpeechSynthesizer, event: SpeechSynthesisEventArgs) => void;\r\n\r\n /**\r\n * Defines event handler for synthesis cancelled events.\r\n * @member SpeechSynthesizer.prototype.SynthesisCanceled\r\n * @function\r\n * @public\r\n */\r\n public SynthesisCanceled: (sender: SpeechSynthesizer, event: SpeechSynthesisEventArgs) => void;\r\n\r\n /**\r\n * Defines event handler for word boundary events\r\n * @member SpeechSynthesizer.prototype.wordBoundary\r\n * @function\r\n * @public\r\n */\r\n public wordBoundary: (sender: SpeechSynthesizer, event: SpeechSynthesisWordBoundaryEventArgs) => void;\r\n\r\n /**\r\n * Defines event handler for bookmark reached events\r\n * Added in version 1.16.0\r\n * @member SpeechSynthesizer.prototype.bookmarkReached\r\n * @function\r\n * @public\r\n */\r\n public bookmarkReached: (sender: SpeechSynthesizer, event: SpeechSynthesisBookmarkEventArgs) => void;\r\n\r\n /**\r\n * Defines event handler for viseme received event\r\n * Added in version 1.16.0\r\n * @member SpeechSynthesizer.prototype.visemeReceived\r\n * @function\r\n * @public\r\n */\r\n public visemeReceived: (sender: SpeechSynthesizer, event: SpeechSynthesisVisemeEventArgs) => void;\r\n\r\n /**\r\n * SpeechSynthesizer constructor.\r\n * @constructor\r\n * @param {SpeechConfig} speechConfig - An set of initial properties for this synthesizer.\r\n * @param {AudioConfig} audioConfig - An optional audio configuration associated with the synthesizer.\r\n */\r\n public constructor(speechConfig: SpeechConfig, audioConfig?: AudioConfig | null) {\r\n super(speechConfig);\r\n\r\n if (audioConfig !== null) {\r\n if (audioConfig === undefined) {\r\n this.audioConfig = (typeof window === \"undefined\") ? undefined : AudioConfig.fromDefaultSpeakerOutput();\r\n } else {\r\n this.audioConfig = audioConfig;\r\n }\r\n }\r\n\r\n this.privConnectionFactory = new SpeechSynthesisConnectionFactory();\r\n this.implCommonSynthesizeSetup();\r\n }\r\n\r\n /**\r\n * SpeechSynthesizer constructor.\r\n * @constructor\r\n * @param {SpeechConfig} speechConfig - an set of initial properties for this synthesizer\r\n * @param {AutoDetectSourceLanguageConfig} autoDetectSourceLanguageConfig - An source language detection configuration associated with the synthesizer\r\n * @param {AudioConfig} audioConfig - An optional audio configuration associated with the synthesizer\r\n */\r\n public static FromConfig(speechConfig: SpeechConfig, autoDetectSourceLanguageConfig: AutoDetectSourceLanguageConfig, audioConfig?: AudioConfig | null): SpeechSynthesizer {\r\n const speechConfigImpl: SpeechConfigImpl = speechConfig as SpeechConfigImpl;\r\n autoDetectSourceLanguageConfig.properties.mergeTo(speechConfigImpl.properties);\r\n return new SpeechSynthesizer(speechConfig, audioConfig);\r\n }\r\n\r\n /**\r\n * Executes speech synthesis on plain text.\r\n * The task returns the synthesis result.\r\n * @member SpeechSynthesizer.prototype.speakTextAsync\r\n * @function\r\n * @public\r\n * @param text - Text to be synthesized.\r\n * @param cb - Callback that received the SpeechSynthesisResult.\r\n * @param err - Callback invoked in case of an error.\r\n * @param stream - AudioOutputStream to receive the synthesized audio.\r\n */\r\n public speakTextAsync(text: string, cb?: (e: SpeechSynthesisResult) => void, err?: (e: string) => void, stream?: AudioOutputStream | PushAudioOutputStreamCallback | PathLike): void {\r\n this.speakImpl(text, false, cb, err, stream);\r\n }\r\n\r\n /**\r\n * Executes speech synthesis on SSML.\r\n * The task returns the synthesis result.\r\n * @member SpeechSynthesizer.prototype.speakSsmlAsync\r\n * @function\r\n * @public\r\n * @param ssml - SSML to be synthesized.\r\n * @param cb - Callback that received the SpeechSynthesisResult.\r\n * @param err - Callback invoked in case of an error.\r\n * @param stream - AudioOutputStream to receive the synthesized audio.\r\n */\r\n public speakSsmlAsync(ssml: string, cb?: (e: SpeechSynthesisResult) => void, err?: (e: string) => void, stream?: AudioOutputStream | PushAudioOutputStreamCallback | PathLike): void {\r\n this.speakImpl(ssml, true, cb, err, stream);\r\n }\r\n\r\n /**\r\n * Get list of synthesis voices available.\r\n * The task returns the synthesis voice result.\r\n * @member SpeechSynthesizer.prototype.getVoicesAsync\r\n * @function\r\n * @async\r\n * @public\r\n * @param locale - Locale of voices in BCP-47 format; if left empty, get all available voices.\r\n * @return {Promise<SynthesisVoicesResult>} - Promise of a SynthesisVoicesResult.\r\n */\r\n public async getVoicesAsync(locale: string = \"\"): Promise<SynthesisVoicesResult> {\r\n return this.getVoices(locale);\r\n }\r\n\r\n /**\r\n * Dispose of associated resources.\r\n * @member SpeechSynthesizer.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public close(cb?: () => void, err?: (error: string) => void): void {\r\n Contracts.throwIfDisposed(this.privDisposed);\r\n\r\n marshalPromiseToCallbacks(this.dispose(true), cb, err);\r\n }\r\n\r\n /**\r\n * @Internal\r\n * Do not use externally, object returned will change without warning or notice.\r\n */\r\n public get internalData(): object {\r\n return this.privAdapter;\r\n }\r\n\r\n //\r\n // ################################################################################################################\r\n // IMPLEMENTATION.\r\n // ################################################################################################################\r\n //\r\n\r\n // Creates the synthesis adapter\r\n protected createSynthesisAdapter(\r\n authentication: IAuthentication,\r\n connectionFactory: ISynthesisConnectionFactory,\r\n synthesizerConfig: SynthesizerConfig): SynthesisAdapterBase {\r\n return new SpeechSynthesisAdapter(authentication, connectionFactory,\r\n synthesizerConfig, this, this.audioConfig as AudioOutputConfigImpl);\r\n }\r\n\r\n protected createRestSynthesisAdapter(\r\n authentication: IAuthentication,\r\n synthesizerConfig: SynthesizerConfig): SynthesisRestAdapter {\r\n return new SynthesisRestAdapter(synthesizerConfig, authentication);\r\n }\r\n\r\n protected implCommonSynthesizeSetup(): void {\r\n super.implCommonSynthesizeSetup();\r\n\r\n this.privAdapter.audioOutputFormat = AudioOutputFormatImpl.fromSpeechSynthesisOutputFormat(\r\n SpeechSynthesisOutputFormat[this.properties.getProperty(PropertyId.SpeechServiceConnection_SynthOutputFormat, undefined) as keyof typeof SpeechSynthesisOutputFormat]\r\n );\r\n }\r\n\r\n protected speakImpl(text: string, IsSsml: boolean, cb?: (e: SpeechSynthesisResult) => void, err?: (e: string) => void, dataStream?: AudioOutputStream | PushAudioOutputStreamCallback | PathLike): void {\r\n try {\r\n Contracts.throwIfDisposed(this.privDisposed);\r\n const requestId = createNoDashGuid();\r\n let audioDestination;\r\n if (dataStream instanceof PushAudioOutputStreamCallback) {\r\n audioDestination = new PushAudioOutputStreamImpl(dataStream);\r\n } else if (dataStream instanceof PullAudioOutputStream) {\r\n audioDestination = dataStream as PullAudioOutputStreamImpl;\r\n } else if (dataStream !== undefined) {\r\n audioDestination = new AudioFileWriter(dataStream as PathLike);\r\n } else {\r\n audioDestination = undefined;\r\n }\r\n this.synthesisRequestQueue.enqueue(new SynthesisRequest(requestId, text, IsSsml, (e: SpeechSynthesisResult): void => {\r\n this.privSynthesizing = false;\r\n if (!!cb) {\r\n try {\r\n cb(e);\r\n } catch (e) {\r\n if (!!err) {\r\n err(e as string);\r\n }\r\n }\r\n }\r\n cb = undefined;\r\n /* eslint-disable no-empty */\r\n this.adapterSpeak().catch((): void => { });\r\n\r\n }, (e: string): void => {\r\n if (!!err) {\r\n err(e);\r\n }\r\n }, audioDestination));\r\n\r\n /* eslint-disable no-empty-function */\r\n this.adapterSpeak().catch((): void => { });\r\n\r\n } catch (error) {\r\n if (!!err) {\r\n if (error instanceof Error) {\r\n const typedError: Error = error;\r\n err(typedError.name + \": \" + typedError.message);\r\n } else {\r\n err(error as string);\r\n }\r\n }\r\n\r\n // Destroy the synthesizer.\r\n /* eslint-disable no-empty */\r\n this.dispose(true).catch((): void => { });\r\n }\r\n }\r\n\r\n protected async getVoices(locale: string): Promise<SynthesisVoicesResult> {\r\n const requestId = createNoDashGuid();\r\n const response: IRestResponse = await this.privRestAdapter.getVoicesList(requestId);\r\n if (response.ok && Array.isArray(response.json)) {\r\n let json = response.json;\r\n if (!!locale && locale.length > 0) {\r\n json = json.filter((item: { Locale: string }): boolean => !!item.Locale && item.Locale.toLowerCase() === locale.toLowerCase() );\r\n }\r\n return new SynthesisVoicesResult(requestId, json, undefined);\r\n } else {\r\n return new SynthesisVoicesResult(requestId, undefined, `Error: ${response.status}: ${response.statusText}`);\r\n }\r\n }\r\n}\r\n"]}