UNPKG

@euirim/microsoft-cognitiveservices-speech-sdk

Version:
243 lines (241 loc) 9.66 kB
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. import { RecognitionMode, RecognizerConfig, TranslationConnectionFactory, TranslationServiceRecognizer, } from "../common.speech/Exports"; import { Contracts } from "./Contracts"; import { PropertyId, Recognizer, } from "./Exports"; /** * Translation recognizer * @class TranslationRecognizer */ export class TranslationRecognizer extends Recognizer { /** * Initializes an instance of the TranslationRecognizer. * @constructor * @param {SpeechTranslationConfig} speechConfig - Set of properties to configure this recognizer. * @param {AudioConfig} audioConfig - An optional audio config associated with the recognizer */ constructor(speechConfig, audioConfig) { const configImpl = speechConfig; Contracts.throwIfNull(configImpl, "speechConfig"); super(audioConfig, configImpl.properties, new TranslationConnectionFactory()); this.privDisposedTranslationRecognizer = false; this.privProperties = configImpl.properties.clone(); if (this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined) !== undefined) { Contracts.throwIfNullOrWhitespace(this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice), PropertyId[PropertyId.SpeechServiceConnection_TranslationVoice]); } Contracts.throwIfNullOrWhitespace(this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages), PropertyId[PropertyId.SpeechServiceConnection_TranslationToLanguages]); Contracts.throwIfNullOrWhitespace(this.properties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage), PropertyId[PropertyId.SpeechServiceConnection_RecoLanguage]); } /** * Gets the language name that was set when the recognizer was created. * @member TranslationRecognizer.prototype.speechRecognitionLanguage * @function * @public * @returns {string} Gets the language name that was set when the recognizer was created. */ get speechRecognitionLanguage() { Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer); return this.properties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage); } /** * Gets target languages for translation that were set when the recognizer was created. * The language is specified in BCP-47 format. The translation will provide translated text for each of language. * @member TranslationRecognizer.prototype.targetLanguages * @function * @public * @returns {string[]} Gets target languages for translation that were set when the recognizer was created. */ get targetLanguages() { Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer); return this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationToLanguages).split(","); } /** * Gets the name of output voice. * @member TranslationRecognizer.prototype.voiceName * @function * @public * @returns {string} the name of output voice. */ get voiceName() { Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer); return this.properties.getProperty(PropertyId.SpeechServiceConnection_TranslationVoice, undefined); } /** * Gets the authorization token used to communicate with the service. * @member TranslationRecognizer.prototype.authorizationToken * @function * @public * @returns {string} Authorization token. */ get authorizationToken() { return this.properties.getProperty(PropertyId.SpeechServiceAuthorization_Token); } /** * Gets/Sets the authorization token used to communicate with the service. * @member TranslationRecognizer.prototype.authorizationToken * @function * @public * @param {string} value - Authorization token. */ set authorizationToken(value) { this.properties.setProperty(PropertyId.SpeechServiceAuthorization_Token, value); } /** * The collection of properties and their values defined for this TranslationRecognizer. * @member TranslationRecognizer.prototype.properties * @function * @public * @returns {PropertyCollection} The collection of properties and their values defined for this TranslationRecognizer. */ get properties() { return this.privProperties; } /** * Starts recognition and translation, and stops after the first utterance is recognized. * The task returns the translation text as result. * Note: recognizeOnceAsync returns when the first utterance has been recognized, so it is suitableonly * for single shot recognition like command or query. For long-running recognition, * use startContinuousRecognitionAsync() instead. * @member TranslationRecognizer.prototype.recognizeOnceAsync * @function * @public * @param cb - Callback that received the result when the translation has completed. * @param err - Callback invoked in case of an error. */ recognizeOnceAsync(cb, err) { try { Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer); this.implRecognizerStop(); this.implRecognizerStart(RecognitionMode.Conversation, (e) => { this.implRecognizerStop(); if (!!cb) { cb(e); } }, (e) => { this.implRecognizerStop(); if (!!err) { err(e); } }); } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Starts recognition and translation, until stopContinuousRecognitionAsync() is called. * User must subscribe to events to receive translation results. * @member TranslationRecognizer.prototype.startContinuousRecognitionAsync * @function * @public * @param cb - Callback that received the translation has started. * @param err - Callback invoked in case of an error. */ startContinuousRecognitionAsync(cb, err) { try { Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer); this.implRecognizerStop(); this.implRecognizerStart(RecognitionMode.Conversation, undefined, undefined); // report result to promise. if (!!cb) { try { cb(); } catch (e) { if (!!err) { err(e); } } cb = undefined; } } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Stops continuous recognition and translation. * @member TranslationRecognizer.prototype.stopContinuousRecognitionAsync * @function * @public * @param cb - Callback that received the translation has stopped. * @param err - Callback invoked in case of an error. */ stopContinuousRecognitionAsync(cb, err) { try { Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer); this.implRecognizerStop(); if (!!cb) { try { cb(); } catch (e) { if (!!err) { err(e); } } } } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * closes all external resources held by an instance of this class. * @member TranslationRecognizer.prototype.close * @function * @public */ close() { Contracts.throwIfDisposed(this.privDisposedTranslationRecognizer); this.dispose(true); } dispose(disposing) { if (this.privDisposedTranslationRecognizer) { return; } if (disposing) { this.implRecognizerStop(); this.privDisposedTranslationRecognizer = true; super.dispose(disposing); } } createRecognizerConfig(speechConfig) { return new RecognizerConfig(speechConfig, this.properties); } createServiceRecognizer(authentication, connectionFactory, audioConfig, recognizerConfig) { const configImpl = audioConfig; return new TranslationServiceRecognizer(authentication, connectionFactory, configImpl, recognizerConfig, this); } } //# sourceMappingURL=TranslationRecognizer.js.map