@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
143 lines (141 loc) • 6.13 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
Object.defineProperty(exports, "__esModule", { value: true });
var Exports_1 = require("../common.speech/Exports");
var Exports_2 = require("../common/Exports");
var Contracts_1 = require("./Contracts");
var Exports_3 = require("./Exports");
/**
* Defines the base class Recognizer which mainly contains common event handlers.
* @class Recognizer
*/
var Recognizer = /** @class */ (function () {
/**
* Creates and initializes an instance of a Recognizer
* @constructor
* @param {AudioConfig} audioInput - An optional audio input stream associated with the recognizer
*/
function Recognizer(audioConfig, properties, connectionFactory) {
this.audioConfig = (audioConfig !== undefined) ? audioConfig : Exports_3.AudioConfig.fromDefaultMicrophoneInput();
this.privDisposed = false;
this.privProperties = properties.clone();
this.privConnectionFactory = connectionFactory;
this.implCommonRecognizerSetup();
}
/**
* Dispose of associated resources.
* @member Recognizer.prototype.close
* @function
* @public
*/
Recognizer.prototype.close = function () {
Contracts_1.Contracts.throwIfDisposed(this.privDisposed);
this.dispose(true);
};
Object.defineProperty(Recognizer.prototype, "internalData", {
/**
* @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: function () {
return this.privReco;
},
enumerable: true,
configurable: true
});
/**
* 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.
*/
Recognizer.prototype.dispose = function (disposing) {
if (this.privDisposed) {
return;
}
if (disposing) {
if (this.privReco) {
this.privReco.audioSource.turnOff();
this.privReco.dispose();
}
}
this.privDisposed = true;
};
Object.defineProperty(Recognizer, "telemetryEnabled", {
/**
* 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.
*/
get: function () {
return Exports_1.ServiceRecognizerBase.telemetryDataEnabled;
},
enumerable: true,
configurable: true
});
/**
* 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 */
Recognizer.enableTelemetry = function (enabled) {
Exports_1.ServiceRecognizerBase.telemetryDataEnabled = enabled;
};
// Does the generic recognizer setup that is common across all recognizer types.
Recognizer.prototype.implCommonRecognizerSetup = function () {
var _this = this;
var osPlatform = (typeof window !== "undefined") ? "Browser" : "Node";
var osName = "unknown";
var osVersion = "unknown";
if (typeof navigator !== "undefined") {
osPlatform = osPlatform + "/" + navigator.platform;
osName = navigator.userAgent;
osVersion = navigator.appVersion;
}
var recognizerConfig = this.createRecognizerConfig(new Exports_1.SpeechServiceConfig(new Exports_1.Context(new Exports_1.OS(osPlatform, osName, osVersion))));
var subscriptionKey = this.privProperties.getProperty(Exports_3.PropertyId.SpeechServiceConnection_Key, undefined);
var authentication = (subscriptionKey && subscriptionKey !== "") ?
new Exports_1.CognitiveSubscriptionKeyAuthentication(subscriptionKey) :
new Exports_1.CognitiveTokenAuthentication(function (authFetchEventId) {
var authorizationToken = _this.privProperties.getProperty(Exports_3.PropertyId.SpeechServiceAuthorization_Token, undefined);
return Exports_2.PromiseHelper.fromResult(authorizationToken);
}, function (authFetchEventId) {
var authorizationToken = _this.privProperties.getProperty(Exports_3.PropertyId.SpeechServiceAuthorization_Token, undefined);
return Exports_2.PromiseHelper.fromResult(authorizationToken);
});
this.privReco = this.createServiceRecognizer(authentication, this.privConnectionFactory, this.audioConfig, recognizerConfig);
};
// Start the recognition
Recognizer.prototype.implRecognizerStart = function (recognitionMode, successCallback, errorCallback) {
this.privReco.recognize(recognitionMode, successCallback, errorCallback).on(
/* tslint:disable:no-empty */
function (result) { }, function (error) {
if (!!errorCallback) {
// Internal error with service communication.
errorCallback("Runtime error: " + error);
}
});
};
Recognizer.prototype.implRecognizerStop = function () {
if (this.privReco) {
this.privReco.stopRecognizing();
}
};
return Recognizer;
}());
exports.Recognizer = Recognizer;
//# sourceMappingURL=Recognizer.js.map