UNPKG

@voice-ping/cognitive-services-speech

Version:

VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft

154 lines (152 loc) 9 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var Exports_1 = require("../sdk/Exports"); var Exports_2 = require("./Exports"); // tslint:disable-next-line:max-classes-per-file var SpeechServiceRecognizer = /** @class */ (function (_super) { __extends(SpeechServiceRecognizer, _super); function SpeechServiceRecognizer(authentication, connectionFactory, audioSource, recognizerConfig, speechRecognizer) { var _this = _super.call(this, authentication, connectionFactory, audioSource, recognizerConfig, speechRecognizer) || this; _this.privSpeechRecognizer = speechRecognizer; if (recognizerConfig.autoDetectSourceLanguages !== undefined) { var sourceLanguages = recognizerConfig.autoDetectSourceLanguages.split(","); _this.privSpeechContext.setSection("languageId", { languages: sourceLanguages, onSuccess: { action: "Recognize" }, onUnknown: { action: "None" } }); _this.privSpeechContext.setSection("phraseOutput", { interimResults: { resultType: "Auto" }, phraseResults: { resultType: "Always" } }); } return _this; } SpeechServiceRecognizer.prototype.processTypeSpecificMessages = function (connectionMessage) { var result; var resultProps = new Exports_1.PropertyCollection(); resultProps.setProperty(Exports_1.PropertyId.SpeechServiceResponse_JsonResult, connectionMessage.textBody); var processed = false; switch (connectionMessage.path.toLowerCase()) { case "speech.hypothesis": case "speech.fragment": var hypothesis = Exports_2.SpeechHypothesis.fromJSON(connectionMessage.textBody); var offset = hypothesis.Offset + this.privRequestSession.currentTurnAudioOffset; result = new Exports_1.SpeechRecognitionResultCustom(this.privRequestSession.requestId, Exports_1.ResultReason.RecognizingSpeech, hypothesis.Text, hypothesis.Duration, offset, hypothesis.Language, hypothesis.LanguageDetectionConfidence, undefined, connectionMessage.textBody, resultProps, this.privRequestSession.requestId); this.privRequestSession.onHypothesis(offset); var ev = new Exports_1.SpeechRecognitionEventArgsCustom(result, hypothesis.Duration, this.privRequestSession.sessionId); if (!!this.privSpeechRecognizer.recognizing) { try { this.privSpeechRecognizer.recognizing(this.privSpeechRecognizer, ev); /* tslint:disable:no-empty */ } catch (error) { // Not going to let errors in the event handler // trip things up. } } processed = true; break; case "speech.phrase": var simple = Exports_2.SimpleSpeechPhrase.fromJSON(connectionMessage.textBody); var resultReason = Exports_2.EnumTranslation.implTranslateRecognitionResult(simple.RecognitionStatus); this.privRequestSession.onPhraseRecognized(this.privRequestSession.currentTurnAudioOffset + simple.Offset + simple.Duration); if (Exports_1.ResultReason.Canceled === resultReason) { var cancelReason = Exports_2.EnumTranslation.implTranslateCancelResult(simple.RecognitionStatus); this.cancelRecognitionLocal(cancelReason, Exports_2.EnumTranslation.implTranslateCancelErrorCode(simple.RecognitionStatus), undefined); } else { if (!(this.privRequestSession.isSpeechEnded && resultReason === Exports_1.ResultReason.NoMatch && simple.RecognitionStatus !== Exports_2.RecognitionStatus.InitialSilenceTimeout)) { if (this.privRecognizerConfig.parameters.getProperty(Exports_2.OutputFormatPropertyName) === Exports_1.OutputFormat[Exports_1.OutputFormat.Simple]) { result = new Exports_1.SpeechRecognitionResultCustom(this.privRequestSession.requestId, resultReason, simple.DisplayText, simple.Duration, simple.Offset + this.privRequestSession.currentTurnAudioOffset, simple.Language, simple.LanguageDetectionConfidence, undefined, connectionMessage.textBody, resultProps, this.privRequestSession.requestId); } else { var detailed = Exports_2.DetailedSpeechPhrase.fromJSON(connectionMessage.textBody); result = new Exports_1.SpeechRecognitionResultCustom(this.privRequestSession.requestId, resultReason, detailed.RecognitionStatus === Exports_2.RecognitionStatus.Success ? detailed.NBest[0].Display : undefined, detailed.Duration, detailed.Offset + this.privRequestSession.currentTurnAudioOffset, detailed.Language, detailed.LanguageDetectionConfidence, undefined, connectionMessage.textBody, resultProps, this.privRequestSession.requestId); } var event_1 = new Exports_1.SpeechRecognitionEventArgsCustom(result, result.offset, this.privRequestSession.sessionId); if (!!this.privSpeechRecognizer.recognized) { try { this.privSpeechRecognizer.recognized(this.privSpeechRecognizer, event_1); /* tslint:disable:no-empty */ } catch (error) { // Not going to let errors in the event handler // trip things up. } } } if (!!this.privSuccessCallback) { try { this.privSuccessCallback(result); } catch (e) { if (!!this.privErrorCallback) { this.privErrorCallback(e); } } // Only invoke the call back once. // and if it's successful don't invoke the // error after that. this.privSuccessCallback = undefined; this.privErrorCallback = undefined; } } processed = true; break; default: break; } return processed; }; // Cancels recognition. SpeechServiceRecognizer.prototype.cancelRecognition = function (sessionId, requestId, cancellationReason, errorCode, error) { var properties = new Exports_1.PropertyCollection(); properties.setProperty(Exports_2.CancellationErrorCodePropertyName, Exports_1.CancellationErrorCode[errorCode]); if (!!this.privSpeechRecognizer.canceled) { var cancelEvent = new Exports_1.SpeechRecognitionCanceledEventArgs(cancellationReason, error, errorCode, undefined, sessionId); try { this.privSpeechRecognizer.canceled(this.privSpeechRecognizer, cancelEvent); /* tslint:disable:no-empty */ } catch (_a) { } } if (!!this.privSuccessCallback) { var result = new Exports_1.SpeechRecognitionResultCustom(requestId, Exports_1.ResultReason.Canceled, undefined, // Text undefined, // Duration undefined, // Offset undefined, // Language undefined, // Language Detection Confidence error, undefined, // Json properties, undefined); try { this.privSuccessCallback(result); this.privSuccessCallback = undefined; /* tslint:disable:no-empty */ } catch (_b) { } } }; return SpeechServiceRecognizer; }(Exports_2.ServiceRecognizerBase)); exports.SpeechServiceRecognizer = SpeechServiceRecognizer; //# sourceMappingURL=SpeechServiceRecognizer.js.map