UNPKG

@euirim/microsoft-cognitiveservices-speech-sdk

Version:
220 lines (218 loc) 8.95 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); var Exports_1 = require("../common/Exports"); var RecognitionEvents_1 = require("./RecognitionEvents"); var ServiceTelemetryListener_Internal_1 = require("./ServiceTelemetryListener.Internal"); var RequestSession = /** @class */ (function () { function RequestSession(audioSourceId) { var _this = this; this.privIsDisposed = false; this.privDetachables = new Array(); this.privIsAudioNodeDetached = false; this.privIsRecognizing = false; this.privIsSpeechEnded = false; this.privTurnStartAudioOffset = 0; this.privLastRecoOffset = 0; this.privHypothesisReceived = false; this.privBytesSent = 0; this.privRecogNumber = 0; this.onAudioSourceAttachCompleted = function (audioNode, isError, error) { _this.privAudioNode = audioNode; _this.privIsAudioNodeDetached = false; if (isError) { _this.onComplete(); } else { _this.onEvent(new RecognitionEvents_1.ListeningStartedEvent(_this.privRequestId, _this.privSessionId, _this.privAudioSourceId, _this.privAudioNodeId)); } }; this.onPreConnectionStart = function (authFetchEventId, connectionId) { _this.privAuthFetchEventId = authFetchEventId; _this.privSessionId = connectionId; _this.onEvent(new RecognitionEvents_1.ConnectingToServiceEvent(_this.privRequestId, _this.privAuthFetchEventId, _this.privSessionId)); }; this.onAuthCompleted = function (isError, error) { if (isError) { _this.onComplete(); } }; this.onConnectionEstablishCompleted = function (statusCode, reason) { if (statusCode === 200) { _this.onEvent(new RecognitionEvents_1.RecognitionStartedEvent(_this.requestId, _this.privAudioSourceId, _this.privAudioNodeId, _this.privAuthFetchEventId, _this.privSessionId)); if (!!_this.privAudioNode) { _this.privAudioNode.replay(); } _this.privTurnStartAudioOffset = _this.privLastRecoOffset; _this.privBytesSent = 0; return; } else if (statusCode === 403) { _this.onComplete(); } }; this.onServiceTurnEndResponse = function (continuousRecognition) { if (!continuousRecognition || _this.isSpeechEnded) { _this.onComplete(); } else { // Start a new request set. _this.privTurnStartAudioOffset = _this.privLastRecoOffset; _this.privRequestId = Exports_1.createNoDashGuid(); _this.privAudioNode.replay(); } }; this.dispose = function (error) { if (!_this.privIsDisposed) { // we should have completed by now. If we did not its an unknown error. _this.privIsDisposed = true; for (var _i = 0, _a = _this.privDetachables; _i < _a.length; _i++) { var detachable = _a[_i]; detachable.detach(); } _this.privServiceTelemetryListener.dispose(); } }; this.getTelemetry = function () { if (_this.privServiceTelemetryListener.hasTelemetry) { return _this.privServiceTelemetryListener.getTelemetry(); } else { return null; } }; this.onEvent = function (event) { if (!!_this.privServiceTelemetryListener) { _this.privServiceTelemetryListener.onEvent(event); } Exports_1.Events.instance.onEvent(event); }; this.onComplete = function () { if (!!_this.privIsRecognizing) { _this.privIsRecognizing = false; _this.detachAudioNode(); } }; this.detachAudioNode = function () { if (!_this.privIsAudioNodeDetached) { _this.privIsAudioNodeDetached = true; if (_this.privAudioNode) { _this.privAudioNode.detach(); } } }; this.privAudioSourceId = audioSourceId; this.privRequestId = Exports_1.createNoDashGuid(); this.privAudioNodeId = Exports_1.createNoDashGuid(); this.privRequestCompletionDeferral = new Exports_1.Deferred(); } Object.defineProperty(RequestSession.prototype, "sessionId", { get: function () { return this.privSessionId; }, enumerable: true, configurable: true }); Object.defineProperty(RequestSession.prototype, "requestId", { get: function () { return this.privRequestId; }, enumerable: true, configurable: true }); Object.defineProperty(RequestSession.prototype, "audioNodeId", { get: function () { return this.privAudioNodeId; }, enumerable: true, configurable: true }); Object.defineProperty(RequestSession.prototype, "completionPromise", { get: function () { return this.privRequestCompletionDeferral.promise(); }, enumerable: true, configurable: true }); Object.defineProperty(RequestSession.prototype, "isSpeechEnded", { get: function () { return this.privIsSpeechEnded; }, enumerable: true, configurable: true }); Object.defineProperty(RequestSession.prototype, "isRecognizing", { get: function () { return this.privIsRecognizing; }, enumerable: true, configurable: true }); Object.defineProperty(RequestSession.prototype, "currentTurnAudioOffset", { get: function () { return this.privTurnStartAudioOffset; }, enumerable: true, configurable: true }); Object.defineProperty(RequestSession.prototype, "recogNumber", { get: function () { return this.privRecogNumber; }, enumerable: true, configurable: true }); Object.defineProperty(RequestSession.prototype, "bytesSent", { // The number of bytes sent for the current connection. // Counter is reset to 0 each time a connection is established. get: function () { return this.privBytesSent; }, enumerable: true, configurable: true }); RequestSession.prototype.listenForServiceTelemetry = function (eventSource) { if (!!this.privServiceTelemetryListener) { this.privDetachables.push(eventSource.attachListener(this.privServiceTelemetryListener)); } }; RequestSession.prototype.startNewRecognition = function () { this.privIsSpeechEnded = false; this.privIsRecognizing = true; this.privTurnStartAudioOffset = 0; this.privLastRecoOffset = 0; this.privRequestId = Exports_1.createNoDashGuid(); this.privRecogNumber++; this.privServiceTelemetryListener = new ServiceTelemetryListener_Internal_1.ServiceTelemetryListener(this.privRequestId, this.privAudioSourceId, this.privAudioNodeId); this.onEvent(new RecognitionEvents_1.RecognitionTriggeredEvent(this.requestId, this.privSessionId, this.privAudioSourceId, this.privAudioNodeId)); }; RequestSession.prototype.onHypothesis = function (offset) { if (!this.privHypothesisReceived) { this.privHypothesisReceived = true; this.privServiceTelemetryListener.hypothesisReceived(this.privAudioNode.findTimeAtOffset(offset)); } }; RequestSession.prototype.onPhraseRecognized = function (offset) { this.privServiceTelemetryListener.phraseReceived(this.privAudioNode.findTimeAtOffset(offset)); this.onServiceRecognized(offset); }; RequestSession.prototype.onServiceRecognized = function (offset) { this.privLastRecoOffset = offset; this.privHypothesisReceived = false; this.privAudioNode.shrinkBuffers(offset); }; RequestSession.prototype.onAudioSent = function (bytesSent) { this.privBytesSent += bytesSent; }; RequestSession.prototype.onStopRecognizing = function () { this.onComplete(); }; // Should be called with the audioNode for this session has indicated that it is out of speech. RequestSession.prototype.onSpeechEnded = function () { this.privIsSpeechEnded = true; }; return RequestSession; }()); exports.RequestSession = RequestSession; //# sourceMappingURL=RequestSession.js.map