@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
63 lines (61 loc) • 3.67 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { ProxyInfo, WebsocketConnection, } from "../common.browser/Exports";
import { ForceDictationPropertyName, OutputFormatPropertyName, } from "../common.speech/Exports";
import { OutputFormat, PropertyId } from "../sdk/Exports";
import { ConnectionFactoryBase } from "./ConnectionFactoryBase";
import { RecognitionMode, WebsocketMessageFormatter } from "./Exports";
import { QueryParameterNames } from "./QueryParameterNames";
export class SpeechConnectionFactory extends ConnectionFactoryBase {
constructor() {
super(...arguments);
this.interactiveRelativeUri = "/speech/recognition/interactive/cognitiveservices/v1";
this.conversationRelativeUri = "/speech/recognition/conversation/cognitiveservices/v1";
this.dictationRelativeUri = "/speech/recognition/dictation/cognitiveservices/v1";
this.create = (config, authInfo, connectionId) => {
let endpoint = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, undefined);
const queryParams = {};
const endpointId = config.parameters.getProperty(PropertyId.SpeechServiceConnection_EndpointId, undefined);
const language = config.parameters.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage, undefined);
if (endpointId) {
if (!endpoint || endpoint.search(QueryParameterNames.DeploymentIdParamName) === -1) {
queryParams[QueryParameterNames.DeploymentIdParamName] = endpointId;
}
}
else if (language) {
if (!endpoint || endpoint.search(QueryParameterNames.LanguageParamName) === -1) {
queryParams[QueryParameterNames.LanguageParamName] = language;
}
}
if (!endpoint || endpoint.search(QueryParameterNames.FormatParamName) === -1) {
queryParams[QueryParameterNames.FormatParamName] = config.parameters.getProperty(OutputFormatPropertyName, OutputFormat[OutputFormat.Simple]).toLowerCase();
}
this.setCommonUrlParams(config, queryParams, endpoint);
if (!endpoint) {
const region = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region, undefined);
const host = "wss://" + region + ".stt.speech.microsoft.com";
switch (config.recognitionMode) {
case RecognitionMode.Conversation:
if (config.parameters.getProperty(ForceDictationPropertyName, "false") === "true") {
endpoint = host + this.dictationRelativeUri;
}
else {
endpoint = host + this.conversationRelativeUri;
}
break;
case RecognitionMode.Dictation:
endpoint = host + this.dictationRelativeUri;
break;
default:
endpoint = host + this.interactiveRelativeUri; // default is interactive
break;
}
}
const headers = {};
headers[authInfo.headerName] = authInfo.token;
headers[QueryParameterNames.ConnectionIdHeader] = connectionId;
return new WebsocketConnection(endpoint, queryParams, headers, new WebsocketMessageFormatter(), ProxyInfo.fromRecognizerConfig(config), connectionId);
};
}
}
//# sourceMappingURL=SpeechConnectionFactory.js.map