microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
66 lines (64 loc) • 4.19 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
Object.defineProperty(exports, "__esModule", { value: true });
exports.TranslationConnectionFactory = void 0;
const Exports_js_1 = require("../common.browser/Exports.js");
const StringUtils_js_1 = require("../common/StringUtils.js");
const Exports_js_2 = require("../sdk/Exports.js");
const ConnectionFactoryBase_js_1 = require("./ConnectionFactoryBase.js");
const Exports_js_3 = require("./Exports.js");
const HeaderNames_js_1 = require("./HeaderNames.js");
const QueryParameterNames_js_1 = require("./QueryParameterNames.js");
class TranslationConnectionFactory extends ConnectionFactoryBase_js_1.ConnectionFactoryBase {
create(config, authInfo, connectionId) {
const endpoint = this.getEndpointUrl(config);
const queryParams = {};
if (config.autoDetectSourceLanguages !== undefined) {
queryParams[QueryParameterNames_js_1.QueryParameterNames.EnableLanguageId] = "true";
}
this.setQueryParams(queryParams, config, endpoint);
const headers = {};
if (authInfo.token !== undefined && authInfo.token !== "") {
headers[authInfo.headerName] = authInfo.token;
}
headers[HeaderNames_js_1.HeaderNames.ConnectionId] = connectionId;
config.parameters.setProperty(Exports_js_2.PropertyId.SpeechServiceConnection_Url, endpoint);
const enableCompression = config.parameters.getProperty("SPEECH-EnableWebsocketCompression", "false") === "true";
return new Exports_js_1.WebsocketConnection(endpoint, queryParams, headers, new Exports_js_3.WebsocketMessageFormatter(), Exports_js_1.ProxyInfo.fromRecognizerConfig(config), enableCompression, connectionId);
}
getEndpointUrl(config, returnRegionPlaceholder) {
const region = config.parameters.getProperty(Exports_js_2.PropertyId.SpeechServiceConnection_Region);
const hostSuffix = ConnectionFactoryBase_js_1.ConnectionFactoryBase.getHostSuffix(region);
let endpointUrl = config.parameters.getProperty(Exports_js_2.PropertyId.SpeechServiceConnection_Endpoint, undefined);
if (!endpointUrl) {
if (config.autoDetectSourceLanguages !== undefined) {
const host = config.parameters.getProperty(Exports_js_2.PropertyId.SpeechServiceConnection_Host, "wss://{region}.stt.speech" + hostSuffix);
endpointUrl = host + "/speech/universal/v2";
}
else {
const host = config.parameters.getProperty(Exports_js_2.PropertyId.SpeechServiceConnection_Host, "wss://{region}.s2s.speech" + hostSuffix);
endpointUrl = host + "/speech/translation/cognitiveservices/v1";
}
}
if (returnRegionPlaceholder === true) {
return endpointUrl;
}
return StringUtils_js_1.StringUtils.formatString(endpointUrl, { region });
}
setQueryParams(queryParams, config, endpointUrl) {
queryParams.from = config.parameters.getProperty(Exports_js_2.PropertyId.SpeechServiceConnection_RecoLanguage);
queryParams.to = config.parameters.getProperty(Exports_js_2.PropertyId.SpeechServiceConnection_TranslationToLanguages);
queryParams.scenario = config.recognitionMode === Exports_js_3.RecognitionMode.Interactive ? "interactive" :
config.recognitionMode === Exports_js_3.RecognitionMode.Conversation ? "conversation" : "";
this.setCommonUrlParams(config, queryParams, endpointUrl);
this.setUrlParameter(Exports_js_2.PropertyId.SpeechServiceResponse_TranslationRequestStablePartialResult, QueryParameterNames_js_1.QueryParameterNames.StableTranslation, config, queryParams, endpointUrl);
const translationVoice = config.parameters.getProperty(Exports_js_2.PropertyId.SpeechServiceConnection_TranslationVoice, undefined);
if (translationVoice !== undefined) {
queryParams.voice = translationVoice;
queryParams.features = "texttospeech";
}
}
}
exports.TranslationConnectionFactory = TranslationConnectionFactory;
//# sourceMappingURL=TranslationConnectionFactory.js.map