@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
130 lines (128 loc) • 5.43 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { CognitiveSubscriptionKeyAuthentication, CognitiveTokenAuthentication, Context, OS, ServiceRecognizerBase, SpeechServiceConfig, } from "../common.speech/Exports";
import { PromiseHelper } from "../common/Exports";
import { Contracts } from "./Contracts";
import { AudioConfig, PropertyId, } from "./Exports";
/**
* Defines the base class Recognizer which mainly contains common event handlers.
* @class Recognizer
*/
export class Recognizer {
/**
* Creates and initializes an instance of a Recognizer
* @constructor
* @param {AudioConfig} audioInput - An optional audio input stream associated with the recognizer
*/
constructor(audioConfig, properties, connectionFactory) {
this.audioConfig = (audioConfig !== undefined) ? audioConfig : AudioConfig.fromDefaultMicrophoneInput();
this.privDisposed = false;
this.privProperties = properties.clone();
this.privConnectionFactory = connectionFactory;
this.implCommonRecognizerSetup();
}
/**
* Dispose of associated resources.
* @member Recognizer.prototype.close
* @function
* @public
*/
close() {
Contracts.throwIfDisposed(this.privDisposed);
this.dispose(true);
}
/**
* @Internal
* Internal data member to support fromRecognizer* pattern methods on other classes.
* Do not use externally, object returned will change without warning or notice.
*/
get internalData() {
return this.privReco;
}
/**
* This method performs cleanup of resources.
* The Boolean parameter disposing indicates whether the method is called
* from Dispose (if disposing is true) or from the finalizer (if disposing is false).
* Derived classes should override this method to dispose resource if needed.
* @member Recognizer.prototype.dispose
* @function
* @public
* @param {boolean} disposing - Flag to request disposal.
*/
dispose(disposing) {
if (this.privDisposed) {
return;
}
if (disposing) {
if (this.privReco) {
this.privReco.audioSource.turnOff();
this.privReco.dispose();
}
}
this.privDisposed = true;
}
/**
* This method returns the current state of the telemetry setting.
* @member Recognizer.prototype.telemetryEnabled
* @function
* @public
* @returns true if the telemetry is enabled, false otherwise.
*/
static get telemetryEnabled() {
return ServiceRecognizerBase.telemetryDataEnabled;
}
/**
* This method globally enables or disables telemetry.
* @member Recognizer.prototype.enableTelemetry
* @function
* @public
* @param enabled - Global setting for telemetry collection.
* If set to true, telemetry information like microphone errors,
* recognition errors are collected and sent to Microsoft.
* If set to false, no telemetry is sent to Microsoft.
*/
/* tslint:disable:member-ordering */
static enableTelemetry(enabled) {
ServiceRecognizerBase.telemetryDataEnabled = enabled;
}
// Does the generic recognizer setup that is common across all recognizer types.
implCommonRecognizerSetup() {
let osPlatform = (typeof window !== "undefined") ? "Browser" : "Node";
let osName = "unknown";
let osVersion = "unknown";
if (typeof navigator !== "undefined") {
osPlatform = osPlatform + "/" + navigator.platform;
osName = navigator.userAgent;
osVersion = navigator.appVersion;
}
const recognizerConfig = this.createRecognizerConfig(new SpeechServiceConfig(new Context(new OS(osPlatform, osName, osVersion))));
const subscriptionKey = this.privProperties.getProperty(PropertyId.SpeechServiceConnection_Key, undefined);
const authentication = (subscriptionKey && subscriptionKey !== "") ?
new CognitiveSubscriptionKeyAuthentication(subscriptionKey) :
new CognitiveTokenAuthentication((authFetchEventId) => {
const authorizationToken = this.privProperties.getProperty(PropertyId.SpeechServiceAuthorization_Token, undefined);
return PromiseHelper.fromResult(authorizationToken);
}, (authFetchEventId) => {
const authorizationToken = this.privProperties.getProperty(PropertyId.SpeechServiceAuthorization_Token, undefined);
return PromiseHelper.fromResult(authorizationToken);
});
this.privReco = this.createServiceRecognizer(authentication, this.privConnectionFactory, this.audioConfig, recognizerConfig);
}
// Start the recognition
implRecognizerStart(recognitionMode, successCallback, errorCallback) {
this.privReco.recognize(recognitionMode, successCallback, errorCallback).on(
/* tslint:disable:no-empty */
(result) => { }, (error) => {
if (!!errorCallback) {
// Internal error with service communication.
errorCallback("Runtime error: " + error);
}
});
}
implRecognizerStop() {
if (this.privReco) {
this.privReco.stopRecognizing();
}
}
}
//# sourceMappingURL=Recognizer.js.map