@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
178 lines (176 loc) • 6.91 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { createNoDashGuid, Deferred, Events, } from "../common/Exports";
import { ConnectingToServiceEvent, ListeningStartedEvent, RecognitionStartedEvent, RecognitionTriggeredEvent, } from "./RecognitionEvents";
import { ServiceTelemetryListener } from "./ServiceTelemetryListener.Internal";
export class RequestSession {
constructor(audioSourceId) {
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 = (audioNode, isError, error) => {
this.privAudioNode = audioNode;
this.privIsAudioNodeDetached = false;
if (isError) {
this.onComplete();
}
else {
this.onEvent(new ListeningStartedEvent(this.privRequestId, this.privSessionId, this.privAudioSourceId, this.privAudioNodeId));
}
};
this.onPreConnectionStart = (authFetchEventId, connectionId) => {
this.privAuthFetchEventId = authFetchEventId;
this.privSessionId = connectionId;
this.onEvent(new ConnectingToServiceEvent(this.privRequestId, this.privAuthFetchEventId, this.privSessionId));
};
this.onAuthCompleted = (isError, error) => {
if (isError) {
this.onComplete();
}
};
this.onConnectionEstablishCompleted = (statusCode, reason) => {
if (statusCode === 200) {
this.onEvent(new 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 = (continuousRecognition) => {
if (!continuousRecognition || this.isSpeechEnded) {
this.onComplete();
}
else {
// Start a new request set.
this.privTurnStartAudioOffset = this.privLastRecoOffset;
this.privRequestId = createNoDashGuid();
this.privAudioNode.replay();
}
};
this.dispose = (error) => {
if (!this.privIsDisposed) {
// we should have completed by now. If we did not its an unknown error.
this.privIsDisposed = true;
for (const detachable of this.privDetachables) {
detachable.detach();
}
this.privServiceTelemetryListener.dispose();
}
};
this.getTelemetry = () => {
if (this.privServiceTelemetryListener.hasTelemetry) {
return this.privServiceTelemetryListener.getTelemetry();
}
else {
return null;
}
};
this.onEvent = (event) => {
if (!!this.privServiceTelemetryListener) {
this.privServiceTelemetryListener.onEvent(event);
}
Events.instance.onEvent(event);
};
this.onComplete = () => {
if (!!this.privIsRecognizing) {
this.privIsRecognizing = false;
this.detachAudioNode();
}
};
this.detachAudioNode = () => {
if (!this.privIsAudioNodeDetached) {
this.privIsAudioNodeDetached = true;
if (this.privAudioNode) {
this.privAudioNode.detach();
}
}
};
this.privAudioSourceId = audioSourceId;
this.privRequestId = createNoDashGuid();
this.privAudioNodeId = createNoDashGuid();
this.privRequestCompletionDeferral = new Deferred();
}
get sessionId() {
return this.privSessionId;
}
get requestId() {
return this.privRequestId;
}
get audioNodeId() {
return this.privAudioNodeId;
}
get completionPromise() {
return this.privRequestCompletionDeferral.promise();
}
get isSpeechEnded() {
return this.privIsSpeechEnded;
}
get isRecognizing() {
return this.privIsRecognizing;
}
get currentTurnAudioOffset() {
return this.privTurnStartAudioOffset;
}
get recogNumber() {
return this.privRecogNumber;
}
// The number of bytes sent for the current connection.
// Counter is reset to 0 each time a connection is established.
get bytesSent() {
return this.privBytesSent;
}
listenForServiceTelemetry(eventSource) {
if (!!this.privServiceTelemetryListener) {
this.privDetachables.push(eventSource.attachListener(this.privServiceTelemetryListener));
}
}
startNewRecognition() {
this.privIsSpeechEnded = false;
this.privIsRecognizing = true;
this.privTurnStartAudioOffset = 0;
this.privLastRecoOffset = 0;
this.privRequestId = createNoDashGuid();
this.privRecogNumber++;
this.privServiceTelemetryListener = new ServiceTelemetryListener(this.privRequestId, this.privAudioSourceId, this.privAudioNodeId);
this.onEvent(new RecognitionTriggeredEvent(this.requestId, this.privSessionId, this.privAudioSourceId, this.privAudioNodeId));
}
onHypothesis(offset) {
if (!this.privHypothesisReceived) {
this.privHypothesisReceived = true;
this.privServiceTelemetryListener.hypothesisReceived(this.privAudioNode.findTimeAtOffset(offset));
}
}
onPhraseRecognized(offset) {
this.privServiceTelemetryListener.phraseReceived(this.privAudioNode.findTimeAtOffset(offset));
this.onServiceRecognized(offset);
}
onServiceRecognized(offset) {
this.privLastRecoOffset = offset;
this.privHypothesisReceived = false;
this.privAudioNode.shrinkBuffers(offset);
}
onAudioSent(bytesSent) {
this.privBytesSent += bytesSent;
}
onStopRecognizing() {
this.onComplete();
}
// Should be called with the audioNode for this session has indicated that it is out of speech.
onSpeechEnded() {
this.privIsSpeechEnded = true;
}
}
//# sourceMappingURL=RequestSession.js.map