jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/voice/voicereceiver.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-07-20 06:56:24 -07:00
define(['events'], function (events) {
2016-10-17 22:06:48 -07:00
'use strict';
2016-07-20 06:56:24 -07:00
var receiver = {
2016-07-06 11:39:04 -07:00
2016-07-20 06:56:24 -07:00
};
var currentRecognition = null;
2016-07-06 11:39:04 -07:00
function normalizeInput(text, options) {
if (options.requireNamedIdentifier) {
var srch = 'jarvis';
var index = text.toLowerCase().indexOf(srch);
2016-10-17 22:06:48 -07:00
if (index !== -1) {
text = text.substring(index + srch.length);
} else {
return null;
}
}
return text;
}
2016-07-06 11:39:04 -07:00
/// <summary> Starts listening for voice commands </summary>
/// <returns> . </returns>
2016-07-20 06:56:24 -07:00
function listen(options) {
2016-07-06 11:39:04 -07:00
return new Promise(function (resolve, reject) {
cancelListener();
2016-10-17 22:06:48 -07:00
var recognitionObj = window.SpeechRecognition ||
2016-07-06 11:39:04 -07:00
window.webkitSpeechRecognition ||
window.mozSpeechRecognition ||
window.oSpeechRecognition ||
2016-10-17 22:06:48 -07:00
window.msSpeechRecognition;
var recognition = new recognitionObj();
2016-07-20 06:56:24 -07:00
recognition.lang = options.lang;
recognition.continuous = options.continuous || false;
var resultCount = 0;
2016-07-06 11:39:04 -07:00
recognition.onresult = function (event) {
console.log(event);
if (event.results.length > 0) {
2016-07-20 06:56:24 -07:00
var resultInput = event.results[resultCount][0].transcript || '';
resultCount++;
resultInput = normalizeInput(resultInput, options);
if (resultInput) {
if (options.continuous) {
events.trigger(receiver, 'input', [
{
text: resultInput
}
]);
} else {
resolve(resultInput);
}
2016-07-20 06:56:24 -07:00
}
2016-07-06 11:39:04 -07:00
}
};
recognition.onerror = function () {
reject({ error: event.error, message: event.message });
};
recognition.onnomatch = function () {
reject({ error: "no-match" });
};
currentRecognition = recognition;
currentRecognition.start();
});
}
/// <summary> Cancel listener. </summary>
/// <returns> . </returns>
function cancelListener() {
if (currentRecognition) {
currentRecognition.abort();
currentRecognition = null;
}
}
2016-07-20 06:56:24 -07:00
receiver.listen = listen;
receiver.cancel = cancelListener;
2016-07-06 11:39:04 -07:00
2016-07-20 06:56:24 -07:00
/// <summary> An enum constant representing the window. voice input manager option. </summary>
return receiver;
2016-07-06 11:39:04 -07:00
});