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AW,EAAE,WAAW,EACxB,gBAAgB,EAAE,gBAAgB,GAAG,qBAAqB;IAO9D,OAAO,CAAC,eAAe;CA8B1B","file":"TranslationRecognizer.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport {\r\n    AutoDetectSourceLanguagesOpenRangeOptionName,\r\n    IAuthentication,\r\n    IConnectionFactory,\r\n    RecognizerConfig,\r\n    ServiceRecognizerBase,\r\n    SpeechServiceConfig,\r\n    TranslationConnectionFactory,\r\n    TranslationServiceRecognizer\r\n} from \"../common.speech/Exports.js\";\r\nimport { RecognitionMode } from \"../common.speech/ServiceMessages/PhraseDetection/PhraseDetectionContext.js\";\r\nimport { marshalPromiseToCallbacks } from \"../common/Exports.js\";\r\nimport { AudioConfigImpl } from \"./Audio/AudioConfig.js\";\r\nimport { Connection } from \"./Connection.js\";\r\nimport { Contracts } from \"./Contracts.js\";\r\nimport {\r\n    AudioConfig,\r\n    AutoDetectSourceLanguageConfig,\r\n    PropertyCollection,\r\n    PropertyId,\r\n    Recognizer,\r\n    TranslationRecognitionCanceledEventArgs,\r\n    TranslationRecognitionEventArgs,\r\n    TranslationRecognitionResult,\r\n    TranslationSynthesisEventArgs\r\n} from \"./Exports.js\";\r\nimport { SpeechTranslationConfig, SpeechTranslationConfigImpl } from \"./SpeechTranslationConfig.js\";\r\n\r\n/**\r\n * Translation recognizer\r\n * @class TranslationRecognizer\r\n */\r\nexport class TranslationRecognizer extends Recognizer {\r\n    /**\r\n     * The event recognizing signals that an intermediate recognition result is received.\r\n     * @member TranslationRecognizer.prototype.recognizing\r\n     * @function\r\n     * @public\r\n     */\r\n    public recognizing: (sender: TranslationRecognizer, event: TranslationRecognitionEventArgs) => void;\r\n\r\n    /**\r\n     * The event recognized signals that a final recognition result is received.\r\n     * @member TranslationRecognizer.prototype.recognized\r\n     * @function\r\n     * @public\r\n     */\r\n    public recognized: (sender: TranslationRecognizer, event: TranslationRecognitionEventArgs) => void;\r\n\r\n    /**\r\n     * The event canceled signals that an error occurred during recognition.\r\n     * @member TranslationRecognizer.prototype.canceled\r\n     * @function\r\n     * @public\r\n     */\r\n    public canceled: (sender: TranslationRecognizer, event: TranslationRecognitionCanceledEventArgs) => void;\r\n\r\n    /**\r\n     * The event synthesizing signals that a translation synthesis result is received.\r\n     * @member TranslationRecognizer.prototype.synthesizing\r\n     * @function\r\n     * @public\r\n     */\r\n    public synthesizing: (sender: TranslationRecognizer, event: TranslationSynthesisEventArgs) => void;\r\n\r\n    private privDisposedTranslationRecognizer: boolean;\r\n\r\n    /**\r\n     * Initializes an instance of the TranslationRecognizer.\r\n     * @constructor\r\n     * @param {SpeechTranslationConfig} speechConfig - Set of properties to configure this recognizer.\r\n     * @param {AudioConfig} audioConfig - An optional audio config associated with the recognizer\r\n     * @param {IConnectionFactory} connectionFactory - An optional connection factory to use to generate the endpoint URIs, headers to set, etc...\r\n     */\r\n    public constructor(speechConfig: SpeechTranslationConfig, audioConfig?: AudioConfig, connectionFactory?: IConnectionFactory) {\r\n        const configImpl = speechConfig as SpeechTranslationConfigImpl;\r\n        Contracts.throwIfNull(configImpl, \"speechConfig\");\r\n\r\n        super(audioConfig, configImpl.properties, connectionFactory || new TranslationConnectionFactory(), speechConfig.tokenCredential);\r\n\r\n        this.privDisposedTranslationRecognizer = false;\r\n\r\n        if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined) !== undefined) {\r\n            Contracts.throwIfNullOrWhitespace(\r\n                this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice),\r\n                PropertyId[PropertyId.SpeechServiceConnection_TranslationVoice]);\r\n        }\r\n\r\n        Contracts.throwIfNullOrWhitespace(\r\n            this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages),\r\n            PropertyId[PropertyId.SpeechServiceConnection_TranslationToLanguages]);\r\n\r\n        if (this.properties.getProperty(PropertyId.SpeechServiceConnection_AutoDetectSourceLanguages, undefined) === undefined) {\r\n            Contracts.throwIfNullOrWhitespace(this.properties.getProperty(\r\n                PropertyId.SpeechServiceConnection_RecoLanguage),\r\n                PropertyId[PropertyId.SpeechServiceConnection_RecoLanguage]);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * TranslationRecognizer constructor.\r\n     * @constructor\r\n     * @param {SpeechTranslationConfig} speechTranslationConfig - an set of initial properties for this recognizer\r\n     * @param {AutoDetectSourceLanguageConfig} autoDetectSourceLanguageConfig - An source language detection configuration associated with the recognizer\r\n     * @param {AudioConfig} audioConfig - An optional audio configuration associated with the recognizer\r\n     */\r\n    public static FromConfig(speechTranslationConfig: SpeechTranslationConfig, autoDetectSourceLanguageConfig: AutoDetectSourceLanguageConfig, audioConfig?: AudioConfig): TranslationRecognizer {\r\n        const speechTranslationConfigImpl: SpeechTranslationConfigImpl = speechTranslationConfig as SpeechTranslationConfigImpl;\r\n        autoDetectSourceLanguageConfig.properties.mergeTo(speechTranslationConfigImpl.properties);\r\n\r\n        if (autoDetectSourceLanguageConfig.properties.getProperty(PropertyId.SpeechServiceConnection_AutoDetectSourceLanguages, undefined) === AutoDetectSourceLanguagesOpenRangeOptionName) {\r\n            speechTranslationConfigImpl.properties.setProperty(PropertyId.SpeechServiceConnection_RecoLanguage, \"en-US\");\r\n        }\r\n        return new TranslationRecognizer(speechTranslationConfig, audioConfig);\r\n    }\r\n\r\n    /**\r\n     * Gets the language name that was set when the recognizer was created.\r\n     * @member TranslationRecognizer.prototype.speechRecognitionLanguage\r\n     * @function\r\n     * @public\r\n     * @returns {string} Gets the language name that was set when the recognizer was created.\r\n     */\r\n    public get speechRecognitionLanguage(): string {\r\n        Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\r\n\r\n        return this.properties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage);\r\n    }\r\n\r\n    /**\r\n     * Gets target languages for translation that were set when the recognizer was created.\r\n     * The language is specified in BCP-47 format. The translation will provide translated text for each of language.\r\n     * @member TranslationRecognizer.prototype.targetLanguages\r\n     * @function\r\n     * @public\r\n     * @returns {string[]} Gets target languages for translation that were set when the recognizer was created.\r\n     */\r\n    public get targetLanguages(): string[] {\r\n        Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\r\n\r\n        return this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(\",\");\r\n    }\r\n\r\n    /**\r\n     * Gets the name of output voice.\r\n     * @member TranslationRecognizer.prototype.voiceName\r\n     * @function\r\n     * @public\r\n     * @returns {string} the name of output voice.\r\n     */\r\n    public get voiceName(): string {\r\n        Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\r\n\r\n        return this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined);\r\n    }\r\n\r\n    /**\r\n     * The collection of properties and their values defined for this TranslationRecognizer.\r\n     * @member TranslationRecognizer.prototype.properties\r\n     * @function\r\n     * @public\r\n     * @returns {PropertyCollection} The collection of properties and their values defined for this TranslationRecognizer.\r\n     */\r\n    public get properties(): PropertyCollection {\r\n        return this.privProperties;\r\n    }\r\n\r\n    /**\r\n     * Gets the authorization token used to communicate with the service.\r\n     * @member TranslationRecognizer.prototype.authorizationToken\r\n     * @function\r\n     * @public\r\n     * @returns {string} Authorization token.\r\n     */\r\n    public get authorizationToken(): string {\r\n        return this.properties.getProperty(PropertyId.SpeechServiceAuthorization_Token);\r\n    }\r\n\r\n    /**\r\n     * Gets/Sets the authorization token used to communicate with the service.\r\n     * @member TranslationRecognizer.prototype.authorizationToken\r\n     * @function\r\n     * @public\r\n     * @param {string} value - Authorization token.\r\n     */\r\n    public set authorizationToken(value: string) {\r\n        this.properties.setProperty(PropertyId.SpeechServiceAuthorization_Token, value);\r\n    }\r\n\r\n    /**\r\n     * Starts recognition and translation, and stops after the first utterance is recognized.\r\n     * The task returns the translation text as result.\r\n     * Note: recognizeOnceAsync returns when the first utterance has been recognized, so it is suitable only\r\n     * for single shot recognition like command or query. For long-running recognition,\r\n     * use startContinuousRecognitionAsync() instead.\r\n     * @member TranslationRecognizer.prototype.recognizeOnceAsync\r\n     * @function\r\n     * @public\r\n     * @param cb - Callback that received the result when the translation has completed.\r\n     * @param err - Callback invoked in case of an error.\r\n     */\r\n    public recognizeOnceAsync(cb?: (e: TranslationRecognitionResult) => void, err?: (e: string) => void): void {\r\n        Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\r\n        marshalPromiseToCallbacks(this.recognizeOnceAsyncImpl(RecognitionMode.Interactive), cb, err);\r\n    }\r\n\r\n    /**\r\n     * Starts recognition and translation, until stopContinuousRecognitionAsync() is called.\r\n     * User must subscribe to events to receive translation results.\r\n     * @member TranslationRecognizer.prototype.startContinuousRecognitionAsync\r\n     * @function\r\n     * @public\r\n     * @param cb - Callback that received the translation has started.\r\n     * @param err - Callback invoked in case of an error.\r\n     */\r\n    public startContinuousRecognitionAsync(cb?: () => void, err?: (e: string) => void): void {\r\n        marshalPromiseToCallbacks(this.startContinuousRecognitionAsyncImpl(RecognitionMode.Conversation), cb, err);\r\n    }\r\n\r\n    /**\r\n     * Stops continuous recognition and translation.\r\n     * @member TranslationRecognizer.prototype.stopContinuousRecognitionAsync\r\n     * @function\r\n     * @public\r\n     * @param cb - Callback that received the translation has stopped.\r\n     * @param err - Callback invoked in case of an error.\r\n     */\r\n    public stopContinuousRecognitionAsync(cb?: () => void, err?: (e: string) => void): void {\r\n        marshalPromiseToCallbacks(this.stopContinuousRecognitionAsyncImpl(), cb, err);\r\n    }\r\n\r\n    /**\r\n     * dynamically remove a language from list of target language\r\n     * (can be used while recognition is ongoing)\r\n     * @member TranslationRecognizer.prototype.removeTargetLanguage\r\n     * @function\r\n     * @param lang - language to be removed\r\n     * @public\r\n     */\r\n    public removeTargetLanguage(lang: string): void {\r\n        Contracts.throwIfNullOrUndefined(lang, \"language to be removed\");\r\n        if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, undefined) !== undefined) {\r\n            const languages: string[] = this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(\",\");\r\n            const index: number = languages.indexOf(lang);\r\n            if (index > -1) {\r\n                languages.splice(index, 1);\r\n                this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, languages.join(\",\"));\r\n                this.updateLanguages(languages);\r\n            }\r\n        }\r\n    }\r\n\r\n    /**\r\n     * dynamically add a language to list of target language\r\n     * (can be used while recognition is ongoing)\r\n     * @member TranslationRecognizer.prototype.addTargetLanguage\r\n     * @function\r\n     * @param lang - language to be added\r\n     * @public\r\n     */\r\n    public addTargetLanguage(lang: string): void {\r\n        Contracts.throwIfNullOrUndefined(lang, \"language to be added\");\r\n        let languages: string[] = [];\r\n        if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, undefined) !== undefined) {\r\n            languages = this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(\",\");\r\n            if (!languages.includes(lang)) {\r\n                languages.push(lang);\r\n                this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, languages.join(\",\"));\r\n            }\r\n        } else {\r\n            this.properties.setProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages, lang);\r\n            languages = [lang];\r\n        }\r\n        this.updateLanguages(languages);\r\n    }\r\n\r\n    /**\r\n     * closes all external resources held by an instance of this class.\r\n     * @member TranslationRecognizer.prototype.close\r\n     * @function\r\n     * @public\r\n     */\r\n    public close(cb?: () => void, errorCb?: (error: string) => void): void {\r\n        Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer);\r\n        marshalPromiseToCallbacks(this.dispose(true), cb, errorCb);\r\n    }\r\n\r\n    /**\r\n     * handles ConnectionEstablishedEvent for conversation translation scenarios.\r\n     * @member TranslationRecognizer.prototype.onConnection\r\n     * @function\r\n     * @public\r\n     */\r\n    // eslint-disable-next-line @typescript-eslint/no-empty-function\r\n    public onConnection(): void { }\r\n\r\n    protected async dispose(disposing: boolean): Promise<void> {\r\n        if (this.privDisposedTranslationRecognizer) {\r\n            return;\r\n        }\r\n\r\n        this.privDisposedTranslationRecognizer = true;\r\n\r\n        if (disposing) {\r\n            await this.implRecognizerStop();\r\n            await super.dispose(disposing);\r\n        }\r\n    }\r\n\r\n\r\n    protected createRecognizerConfig(speechConfig: SpeechServiceConfig): RecognizerConfig {\r\n        return new RecognizerConfig(speechConfig, this.privProperties);\r\n    }\r\n\r\n    protected createServiceRecognizer(\r\n        authentication: IAuthentication,\r\n        connectionFactory: IConnectionFactory,\r\n        audioConfig: AudioConfig,\r\n        recognizerConfig: RecognizerConfig): ServiceRecognizerBase {\r\n\r\n        const configImpl: AudioConfigImpl = audioConfig as AudioConfigImpl;\r\n\r\n        return new TranslationServiceRecognizer(authentication, connectionFactory, configImpl, recognizerConfig, this);\r\n    }\r\n\r\n    private updateLanguages(languages: string[]): void {\r\n        const conn: Connection = Connection.fromRecognizer(this);\r\n        if (!!conn) {\r\n            conn.setMessageProperty(\"speech.context\", \"translationcontext\", { to: languages });\r\n\r\n            const voiceName: string = this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined);\r\n            let payload: string;\r\n\r\n            if (!!voiceName) {\r\n                const languageToVoiceMap: { [key: string]: string } = {};\r\n                for (const lang of languages) {\r\n                    languageToVoiceMap[lang] = voiceName;\r\n                }\r\n                payload = JSON.stringify({\r\n                    id: \"translation\",\r\n                    name: \"updateLanguage\",\r\n                    synthesis: { defaultVoices: languageToVoiceMap },\r\n                    to: languages,\r\n                });\r\n            } else {\r\n                payload = JSON.stringify({\r\n                    id: \"translation\",\r\n                    name: \"updateLanguage\",\r\n                    to: languages\r\n                });\r\n            }\r\n\r\n            conn.sendMessageAsync(\"event\", payload);\r\n        }\r\n    }\r\n}\r\n"]}