@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
183 lines (181 loc) • 9.02 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { ForceDictationPropertyName, OutputFormatPropertyName, ServicePropertiesPropertyName } from "../common.speech/Exports";
import { Contracts } from "./Contracts";
import { OutputFormat, ProfanityOption, PropertyCollection, PropertyId } from "./Exports";
/**
* Speech configuration.
* @class SpeechConfig
*/
export class SpeechConfig {
/**
* Creates and initializes an instance.
* @constructor
*/
constructor() { }
/**
* Static instance of SpeechConfig returned by passing subscriptionKey and service region.
* Note: Please use your LanguageUnderstanding subscription key in case you want to use the Intent recognizer.
* @member SpeechConfig.fromSubscription
* @function
* @public
* @param {string} subscriptionKey - The subscription key.
* @param {string} region - The region name (see the <a href="https://aka.ms/csspeech/region">region page</a>).
* @returns {SpeechConfig} The speech factory
*/
static fromSubscription(subscriptionKey, region) {
Contracts.throwIfNullOrWhitespace(subscriptionKey, "subscriptionKey");
Contracts.throwIfNullOrWhitespace(region, "region");
const speechImpl = new SpeechConfigImpl();
speechImpl.setProperty(PropertyId.SpeechServiceConnection_Region, region);
speechImpl.setProperty(PropertyId.SpeechServiceConnection_IntentRegion, region);
speechImpl.setProperty(PropertyId.SpeechServiceConnection_Key, subscriptionKey);
return speechImpl;
}
/**
* Creates an instance of the speech config with specified endpoint and subscription key.
* This method is intended only for users who use a non-standard service endpoint or parameters.
* Note: Please use your LanguageUnderstanding subscription key in case you want to use the Intent recognizer.
* Note: The query parameters specified in the endpoint URL are not changed, even if they are set by any other APIs.
* For example, if language is defined in the uri as query parameter "language=de-DE", and also set by
* SpeechConfig.speechRecognitionLanguage = "en-US", the language setting in uri takes precedence,
* and the effective language is "de-DE". Only the parameters that are not specified in the
* endpoint URL can be set by other APIs.
* Note: To use authorization token with fromEndpoint, pass an empty string to the subscriptionKey in the
* fromEndpoint method, and then set authorizationToken="token" on the created SpeechConfig instance to
* use the authorization token.
* @member SpeechConfig.fromEndpoint
* @function
* @public
* @param {URL} endpoint - The service endpoint to connect to.
* @param {string} subscriptionKey - The subscription key. If a subscription key is not specified, an authorization token must be set.
* @returns {SpeechConfig} A speech factory instance.
*/
static fromEndpoint(endpoint, subscriptionKey) {
Contracts.throwIfNull(endpoint, "endpoint");
const speechImpl = new SpeechConfigImpl();
speechImpl.setProperty(PropertyId.SpeechServiceConnection_Endpoint, endpoint.href);
if (undefined !== subscriptionKey) {
speechImpl.setProperty(PropertyId.SpeechServiceConnection_Key, subscriptionKey);
}
return speechImpl;
}
/**
* Creates an instance of the speech factory with specified initial authorization token and region.
* Note: The caller needs to ensure that the authorization token is valid. Before the authorization token
* expires, the caller needs to refresh it by calling this setter with a new valid token.
* Note: Please use a token derived from your LanguageUnderstanding subscription key in case you want
* to use the Intent recognizer. As configuration values are copied when creating a new recognizer,
* the new token value will not apply to recognizers that have already been created. For recognizers
* that have been created before, you need to set authorization token of the corresponding recognizer
* to refresh the token. Otherwise, the recognizers will encounter errors during recognition.
* @member SpeechConfig.fromAuthorizationToken
* @function
* @public
* @param {string} authorizationToken - The initial authorization token.
* @param {string} region - The region name (see the <a href="https://aka.ms/csspeech/region">region page</a>).
* @returns {SpeechConfig} A speech factory instance.
*/
static fromAuthorizationToken(authorizationToken, region) {
Contracts.throwIfNull(authorizationToken, "authorizationToken");
Contracts.throwIfNullOrWhitespace(region, "region");
const speechImpl = new SpeechConfigImpl();
speechImpl.setProperty(PropertyId.SpeechServiceConnection_Region, region);
speechImpl.setProperty(PropertyId.SpeechServiceConnection_IntentRegion, region);
speechImpl.authorizationToken = authorizationToken;
return speechImpl;
}
/**
* Closes the configuration.
* @member SpeechConfig.prototype.close
* @function
* @public
*/
/* tslint:disable:no-empty */
close() { }
}
/**
* @public
* @class SpeechConfigImpl
*/
// tslint:disable-next-line:max-classes-per-file
export class SpeechConfigImpl extends SpeechConfig {
constructor() {
super();
this.privProperties = new PropertyCollection();
this.speechRecognitionLanguage = "en-US"; // Should we have a default?
this.outputFormat = OutputFormat.Simple;
}
get properties() {
return this.privProperties;
}
get endPoint() {
return new URL(this.privProperties.getProperty(PropertyId.SpeechServiceConnection_Endpoint));
}
get subscriptionKey() {
return this.privProperties.getProperty(PropertyId.SpeechServiceConnection_Key);
}
get region() {
return this.privProperties.getProperty(PropertyId.SpeechServiceConnection_Region);
}
get authorizationToken() {
return this.privProperties.getProperty(PropertyId.SpeechServiceAuthorization_Token);
}
set authorizationToken(value) {
this.privProperties.setProperty(PropertyId.SpeechServiceAuthorization_Token, value);
}
get speechRecognitionLanguage() {
return this.privProperties.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage);
}
set speechRecognitionLanguage(value) {
this.privProperties.setProperty(PropertyId.SpeechServiceConnection_RecoLanguage, value);
}
get outputFormat() {
return OutputFormat[this.privProperties.getProperty(OutputFormatPropertyName, undefined)];
}
set outputFormat(value) {
this.privProperties.setProperty(OutputFormatPropertyName, OutputFormat[value]);
}
get endpointId() {
return this.privProperties.getProperty(PropertyId.SpeechServiceConnection_EndpointId);
}
set endpointId(value) {
this.privProperties.setProperty(PropertyId.SpeechServiceConnection_EndpointId, value);
}
setProperty(name, value) {
Contracts.throwIfNull(value, "value");
this.privProperties.setProperty(name, value);
}
getProperty(name, def) {
return this.privProperties.getProperty(name, def);
}
setProxy(proxyHostName, proxyPort, proxyUserName, proxyPassword) {
this.setProperty(PropertyId[PropertyId.SpeechServiceConnection_ProxyHostName], proxyHostName);
this.setProperty(PropertyId[PropertyId.SpeechServiceConnection_ProxyPort], proxyPort);
this.setProperty(PropertyId[PropertyId.SpeechServiceConnection_ProxyUserName], proxyUserName);
this.setProperty(PropertyId[PropertyId.SpeechServiceConnection_ProxyPassword], proxyPassword);
}
setServiceProperty(name, value, channel) {
const currentProperties = JSON.parse(this.privProperties.getProperty(ServicePropertiesPropertyName, "{}"));
currentProperties[name] = value;
this.privProperties.setProperty(ServicePropertiesPropertyName, JSON.stringify(currentProperties));
}
setProfanity(profanity) {
this.privProperties.setProperty(PropertyId.SpeechServiceResponse_ProfanityOption, ProfanityOption[profanity]);
}
enableAudioLogging() {
this.privProperties.setProperty(PropertyId.SpeechServiceConnection_EnableAudioLogging, "true");
}
requestWordLevelTimestamps() {
this.privProperties.setProperty(PropertyId.SpeechServiceResponse_RequestWordLevelTimestamps, "true");
}
enableDictation() {
this.privProperties.setProperty(ForceDictationPropertyName, "true");
}
clone() {
const ret = new SpeechConfigImpl();
ret.privProperties = this.privProperties.clone();
return ret;
}
}
//# sourceMappingURL=SpeechConfig.js.map