@hahnpro/ms-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 17.2 kB
Source Map (JSON)
{"version":3,"sources":["src/sdk/TranslationRecognizer.ts"],"names":[],"mappings":"AAGA,OAAO,EAEH,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EAGtB,MAAM,6BAA6B,CAAC;AAMrC,OAAO,EACH,WAAW,EACX,8BAA8B,EAC9B,kBAAkB,EAElB,UAAU,EACV,uCAAuC,EACvC,+BAA+B,EAC/B,4BAA4B,EAC5B,6BAA6B,EAChC,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,uBAAuB,EAA+B,MAAM,8BAA8B,CAAC;AAEpG;;;GAGG;AACH,qBAAa,qBAAsB,SAAQ,UAAU;IACjD;;;;;OAKG;IACI,WAAW,EAAE,CAAC,MAAM,EAAE,qBAAqB,EAAE,KAAK,EAAE,+BAA+B,KAAK,IAAI,CAAC;IAEpG;;;;;OAKG;IACI,UAAU,EAAE,CAAC,MAAM,EAAE,qBAAqB,EAAE,KAAK,EAAE,+BAA+B,KAAK,IAAI,CAAC;IAEnG;;;;;OAKG;IACI,QAAQ,EAAE,CAAC,MAAM,EAAE,qBAAqB,EAAE,KAAK,EAAE,uCAAuC,KAAK,IAAI,CAAC;IAEzG;;;;;OAKG;IACI,YAAY,EAAE,CAAC,MAAM,EAAE,qBAAqB,EAAE,KAAK,EAAE,6BAA6B,KAAK,IAAI,CAAC;IAEnG,OAAO,CAAC,iCAAiC,CAAU;IAEnD;;;;;;OAMG;gBACgB,YAAY,EAAE,uBAAuB,EAAE,WAAW,CAAC,EAAE,WAAW,EAAE,iBAAiB,CAAC,EAAE,kBAAkB;IAwB3H;;;;;;OAMG;WACW,UAAU,CAAC,uBAAuB,EAAE,uBAAuB,EAAE,8BAA8B,EAAE,8BAA8B,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,qBAAqB;IAU5L;;;;;;OAMG;IACH,IAAW,yBAAy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AAS,CAAC,uBAAuB,CAC7B,cAAc,EAAE,eAAe,EAC/B,iBAAiB,EAAE,kBAAkB,EACrC,WAAW,EAAE,WAAW,EACxB,gBAAgB,EAAE,gBAAgB,GAAG,qBAAqB;IAO9D,OAAO,CAAC,eAAe;CAY1B","file":"TranslationRecognizer.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\nimport {\n AutoDetectSourceLanguagesOpenRangeOptionName,\n IAuthentication,\n IConnectionFactory,\n RecognizerConfig,\n ServiceRecognizerBase,\n SpeechServiceConfig,\n TranslationConnectionFactory,\n TranslationServiceRecognizer\n} from \"../common.speech/Exports.js\";\nimport { RecognitionMode } from \"../common.speech/ServiceMessages/PhraseDetection/PhraseDetectionContext.js\";\nimport { marshalPromiseToCallbacks } from \"../common/Exports.js\";\nimport { AudioConfigImpl } from \"./Audio/AudioConfig.js\";\nimport { Connection } from \"./Connection.js\";\nimport { Contracts } from \"./Contracts.js\";\nimport {\n AudioConfig,\n AutoDetectSourceLanguageConfig,\n PropertyCollection,\n PropertyId,\n Recognizer,\n TranslationRecognitionCanceledEventArgs,\n TranslationRecognitionEventArgs,\n TranslationRecognitionResult,\n TranslationSynthesisEventArgs\n} from \"./Exports.js\";\nimport { SpeechTranslationConfig, SpeechTranslationConfigImpl } from \"./SpeechTranslationConfig.js\";\n\n/**\n * Translation recognizer\n * @class TranslationRecognizer\n */\nexport class TranslationRecognizer extends Recognizer {\n /**\n * The event recognizing signals that an intermediate recognition result is received.\n * @member TranslationRecognizer.prototype.recognizing\n * @function\n * @public\n */\n public recognizing: (sender: TranslationRecognizer, event: TranslationRecognitionEventArgs) => void;\n\n /**\n * The event recognized signals that a final recognition result is received.\n * @member TranslationRecognizer.prototype.recognized\n * @function\n * @public\n */\n public recognized: (sender: TranslationRecognizer, event: TranslationRecognitionEventArgs) => void;\n\n /**\n * The event canceled signals that an error occurred during recognition.\n * @member TranslationRecognizer.prototype.canceled\n * @function\n * @public\n */\n public canceled: (sender: TranslationRecognizer, event: TranslationRecognitionCanceledEventArgs) => void;\n\n /**\n * The event synthesizing signals that a translation synthesis result is received.\n * @member TranslationRecognizer.prototype.synthesizing\n * @function\n * @public\n */\n public synthesizing: (sender: TranslationRecognizer, event: TranslationSynthesisEventArgs) => void;\n\n private privDisposedTranslationRecognizer: boolean;\n\n /**\n * Initializes an instance of the TranslationRecognizer.\n * @constructor\n * @param {SpeechTranslationConfig} speechConfig - Set of properties to configure this recognizer.\n * @param {AudioConfig} audioConfig - An optional audio config associated with the recognizer\n * @param {IConnectionFactory} connectionFactory - An optional connection factory to use to generate the endpoint URIs, headers to set, etc...\n */\n public constructor(speechConfig: SpeechTranslationConfig, audioConfig?: AudioConfig, connectionFactory?: IConnectionFactory) {\n const configImpl = speechConfig as SpeechTranslationConfigImpl;\n Contracts.throwIfNull(configImpl, \"speechConfig\");\n\n super(audioConfig, configImpl.properties, connectionFactory || new TranslationConnectionFactory(), speechConfig.tokenCredential);\n\n this.privDisposedTranslationRecognizer = false;\n\n if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined) !== undefined) {\n Contracts.throwIfNullOrWhitespace(\n this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice),\n PropertyId[PropertyId.SpeechServiceConnection_TranslationVoice]);\n }\n\n Contracts.throwIfNullOrWhitespace(\n this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages),\n PropertyId[PropertyId.SpeechServiceConnection_TranslationToLanguages]);\n\n Contracts.throwIfNullOrWhitespace(this.properties.getProperty(\n PropertyId.SpeechServiceConnection_RecoLanguage),\n PropertyId[PropertyId.SpeechServiceConnection_RecoLanguage]);\n\n }\n\n /**\n * TranslationRecognizer constructor.\n * @constructor\n * @param {SpeechTranslationConfig} speechTranslationConfig - an set of initial properties for this recognizer\n * @param {AutoDetectSourceLanguageConfig} autoDetectSourceLanguageConfig - An source language detection configuration associated with the recognizer\n * @param {AudioConfig} audioConfig - An optional audio configuration associated with the recognizer\n */\n public static FromConfig(speechTranslationConfig: SpeechTranslationConfig, autoDetectSourceLanguageConfig: AutoDetectSourceLanguageConfig, audioConfig?: AudioConfig): TranslationRecognizer {\n const speechTranslationConfigImpl: SpeechTranslationConfigImpl = speechTranslationConfig as SpeechTranslationConfigImpl;\n autoDetectSourceLanguageConfig.properties.mergeTo(speechTranslationConfigImpl.properties);\n\n if (autoDetectSourceLanguageConfig.properties.getProperty(PropertyId.SpeechServiceConnection_AutoDetectSourceLanguages, undefined) === AutoDetectSourceLanguagesOpenRangeOptionName) {\n speechTranslationConfigImpl.properties.setProperty(PropertyId.SpeechServiceConnection_RecoLanguage, \"en-US\");\n }\n return new TranslationRecognizer(speechTranslationConfig, audioConfig);\n }\n\n /**\n * Gets the language name that was set when the recognizer was created.\n * @member TranslationRecognizer.prototype.speechRecognitionLanguage\n * @function\n * @public\n * @returns {string} Gets the language name that was set when the recognizer was created.\n */\n public get speechRecognitionLanguage(): string {\n Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\n\n return this.properties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage);\n }\n\n /**\n * Gets target languages for translation that were set when the recognizer was created.\n * The language is specified in BCP-47 format. The translation will provide translated text for each of language.\n * @member TranslationRecognizer.prototype.targetLanguages\n * @function\n * @public\n * @returns {string[]} Gets target languages for translation that were set when the recognizer was created.\n */\n public get targetLanguages(): string[] {\n Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\n\n return this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(\",\");\n }\n\n /**\n * Gets the name of output voice.\n * @member TranslationRecognizer.prototype.voiceName\n * @function\n * @public\n * @returns {string} the name of output voice.\n */\n public get voiceName(): string {\n Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\n\n return this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined);\n }\n\n /**\n * The collection of properties and their values defined for this TranslationRecognizer.\n * @member TranslationRecognizer.prototype.properties\n * @function\n * @public\n * @returns {PropertyCollection} The collection of properties and their values defined for this TranslationRecognizer.\n */\n public get properties(): PropertyCollection {\n return this.privProperties;\n }\n\n /**\n * Gets the authorization token used to communicate with the service.\n * @member TranslationRecognizer.prototype.authorizationToken\n * @function\n * @public\n * @returns {string} Authorization token.\n */\n public get authorizationToken(): string {\n return this.properties.getProperty(PropertyId.SpeechServiceAuthorization_Token);\n }\n\n /**\n * Gets/Sets the authorization token used to communicate with the service.\n * @member TranslationRecognizer.prototype.authorizationToken\n * @function\n * @public\n * @param {string} value - Authorization token.\n */\n public set authorizationToken(value: string) {\n this.properties.setProperty(PropertyId.SpeechServiceAuthorization_Token, value);\n }\n\n /**\n * Starts recognition and translation, and stops after the first utterance is recognized.\n * The task returns the translation text as result.\n * Note: recognizeOnceAsync returns when the first utterance has been recognized, so it is suitable only\n * for single shot recognition like command or query. For long-running recognition,\n * use startContinuousRecognitionAsync() instead.\n * @member TranslationRecognizer.prototype.recognizeOnceAsync\n * @function\n * @public\n * @param cb - Callback that received the result when the translation has completed.\n * @param err - Callback invoked in case of an error.\n */\n public recognizeOnceAsync(cb?: (e: TranslationRecognitionResult) => void, err?: (e: string) => void): void {\n Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\n marshalPromiseToCallbacks(this.recognizeOnceAsyncImpl(RecognitionMode.Interactive), cb, err);\n }\n\n /**\n * Starts recognition and translation, until stopContinuousRecognitionAsync() is called.\n * User must subscribe to events to receive translation results.\n * @member TranslationRecognizer.prototype.startContinuousRecognitionAsync\n * @function\n * @public\n * @param cb - Callback that received the translation has started.\n * @param err - Callback invoked in case of an error.\n */\n public startContinuousRecognitionAsync(cb?: () => void, err?: (e: string) => void): void {\n marshalPromiseToCallbacks(this.startContinuousRecognitionAsyncImpl(RecognitionMode.Conversation), cb, err);\n }\n\n /**\n * Stops continuous recognition and translation.\n * @member TranslationRecognizer.prototype.stopContinuousRecognitionAsync\n * @function\n * @public\n * @param cb - Callback that received the translation has stopped.\n * @param err - Callback invoked in case of an error.\n */\n public stopContinuousRecognitionAsync(cb?: () => void, err?: (e: string) => void): void {\n marshalPromiseToCallbacks(this.stopContinuousRecognitionAsyncImpl(), cb, err);\n }\n\n /**\n * dynamically remove a language from list of target language\n * (can be used while recognition is ongoing)\n * @member TranslationRecognizer.prototype.removeTargetLanguage\n * @function\n * @param lang - language to be removed\n * @public\n */\n public removeTargetLanguage(lang: string): void {\n Contracts.throwIfNullOrUndefined(lang, \"language to be removed\");\n if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, undefined) !== undefined) {\n const languages: string[] = this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(\",\");\n const index: number = languages.indexOf(lang);\n if (index > -1) {\n languages.splice(index, 1);\n this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, languages.join(\",\"));\n this.updateLanguages(languages);\n }\n }\n }\n\n /**\n * dynamically add a language to list of target language\n * (can be used while recognition is ongoing)\n * @member TranslationRecognizer.prototype.addTargetLanguage\n * @function\n * @param lang - language to be added\n * @public\n */\n public addTargetLanguage(lang: string): void {\n Contracts.throwIfNullOrUndefined(lang, \"language to be added\");\n let languages: string[] = [];\n if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, undefined) !== undefined) {\n languages = this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(\",\");\n if (!languages.includes(lang)) {\n languages.push(lang);\n this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, languages.join(\",\"));\n }\n } else {\n this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, lang);\n languages = [lang];\n }\n this.updateLanguages(languages);\n }\n\n /**\n * closes all external resources held by an instance of this class.\n * @member TranslationRecognizer.prototype.close\n * @function\n * @public\n */\n public close(cb?: () => void, errorCb?: (error: string) => void): void {\n Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\n marshalPromiseToCallbacks(this.dispose(true), cb, errorCb);\n }\n\n /**\n * handles ConnectionEstablishedEvent for conversation translation scenarios.\n * @member TranslationRecognizer.prototype.onConnection\n * @function\n * @public\n */\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n public onConnection(): void { }\n\n protected async dispose(disposing: boolean): Promise<void> {\n if (this.privDisposedTranslationRecognizer) {\n return;\n }\n\n this.privDisposedTranslationRecognizer = true;\n\n if (disposing) {\n await this.implRecognizerStop();\n await super.dispose(disposing);\n }\n }\n\n\n protected createRecognizerConfig(speechConfig: SpeechServiceConfig): RecognizerConfig {\n return new RecognizerConfig(speechConfig, this.privProperties);\n }\n\n protected createServiceRecognizer(\n authentication: IAuthentication,\n connectionFactory: IConnectionFactory,\n audioConfig: AudioConfig,\n recognizerConfig: RecognizerConfig): ServiceRecognizerBase {\n\n const configImpl: AudioConfigImpl = audioConfig as AudioConfigImpl;\n\n return new TranslationServiceRecognizer(authentication, connectionFactory, configImpl, recognizerConfig, this);\n }\n\n private updateLanguages(languages: string[]): void {\n const conn: Connection = Connection.fromRecognizer(this);\n if (!!conn) {\n conn.setMessageProperty(\"speech.context\", \"translationcontext\", { to: languages });\n conn.sendMessageAsync(\"event\", JSON.stringify({\n id: \"translation\",\n name: \"updateLanguage\",\n to: languages\n }));\n }\n }\n\n}\n"]}