tsbase
Version:
Base class libraries for TypeScript
41 lines • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpeechRecognizer = void 0;
const Strings_1 = require("../../System/Strings");
const ISpeechRecognition_1 = require("./ISpeechRecognition");
class SpeechRecognizer {
constructor(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;
}
async Listen() {
return new Promise((resolve) => {
let result = Strings_1.Strings.Empty;
const resultEvent = (e) => { result = Array.from(e.results)[0][0].transcript; };
const endEvent = () => {
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();
});
}
async HandleSpeechCommands(commands, until) {
do {
const transcript = await this.Listen();
const command = commands.find(c => c.Condition(transcript));
await (command === null || command === void 0 ? void 0 : command.Action());
} while (!until());
}
}
exports.SpeechRecognizer = SpeechRecognizer;
//# sourceMappingURL=SpeechRecognizer.js.map