UNPKG

tsbase

Version:

Base class libraries for TypeScript

77 lines 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpeechRecognizer = void 0; var tslib_1 = require("tslib"); var Strings_1 = require("../../System/Strings"); var ISpeechRecognition_1 = require("./ISpeechRecognition"); var SpeechRecognizer = /** @class */ (function () { function SpeechRecognizer(speechRecognition) { if (speechRecognition === void 0) { speechRecognition = window && window.webkitSpeechRecognition ? new window.webkitSpeechRecognition() : null; } if (!speechRecognition) { throw new Error('No speech recognition service is available.'); } else { this.speechRecognition = speechRecognition; } this.speechRecognition.continuous = false; this.speechRecognition.interimResults = false; } SpeechRecognizer.prototype.Listen = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve) { var result = Strings_1.Strings.Empty; var resultEvent = function (e) { result = Array.from(e.results)[0][0].transcript; }; var endEvent = function () { _this.speechRecognition.removeEventListener(ISpeechRecognition_1.SpeechRecognitionEvents.Result, resultEvent); _this.speechRecognition.removeEventListener(ISpeechRecognition_1.SpeechRecognitionEvents.End, endEvent); resolve(result); }; _this.speechRecognition.addEventListener(ISpeechRecognition_1.SpeechRecognitionEvents.Result, resultEvent); _this.speechRecognition.addEventListener(ISpeechRecognition_1.SpeechRecognitionEvents.End, endEvent); _this.speechRecognition.start(); })]; }); }); }; SpeechRecognizer.prototype.HandleSpeechCommands = function (commands, until) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _loop_1, this_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _loop_1 = function () { var transcript, command; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this_1.Listen()]; case 1: transcript = _b.sent(); command = commands.find(function (c) { return c.Condition(transcript); }); return [4 /*yield*/, (command === null || command === void 0 ? void 0 : command.Action())]; case 2: _b.sent(); return [2 /*return*/]; } }); }; this_1 = this; _a.label = 1; case 1: return [5 /*yield**/, _loop_1()]; case 2: _a.sent(); _a.label = 3; case 3: if (!until()) return [3 /*break*/, 1]; _a.label = 4; case 4: return [2 /*return*/]; } }); }); }; return SpeechRecognizer; }()); exports.SpeechRecognizer = SpeechRecognizer; //# sourceMappingURL=SpeechRecognizer.js.map