Add voice recognition (tested)

This commit is contained in:
Dennis Eichhorn 2017-08-26 15:53:37 +02:00
parent d02a57beb9
commit a78d931a85
2 changed files with 19 additions and 13 deletions

View File

@ -23,18 +23,20 @@
* *
* @since 1.0.0 * @since 1.0.0
*/ */
jsOMS.UI.Input.Voice.ReadManager = function () jsOMS.UI.Input.Voice.ReadManager = function (lang)
{ {
this.pitch = 1; this.pitch = 1;
this.rate = 1; this.rate = 1;
this.voice = 'en-US'; this.lang = typeof lang === 'undefined' ? 'en-US' : lang;
this.voices = window.speechSynthesis.getVoices(); this.voices = window.speechSynthesis.getVoices();
this.voice = this.voices[0];
}; };
jsOMS.UI.Input.Voice.ReadManager.prototype.read = function(text) jsOMS.UI.Input.Voice.ReadManager.prototype.read = function(text)
{ {
let utter = new SpeechSynthesisUtterance(text); let utter = new SpeechSynthesisUtterance(text);
utter.lang = this.lang;
utter.voice = this.voice; utter.voice = this.voice;
utter.pitch = this.pitch; utter.pitch = this.pitch;
utter.rate = this.rate; utter.rate = this.rate;

View File

@ -28,7 +28,7 @@
this.app = app; this.app = app;
this.recognition = new SpeechRecognition(); this.recognition = new SpeechRecognition();
this.speechRecognitionList = new SpeechGrammarList(); this.speechRecognitionList = new SpeechGrammarList();
this.commands = commands; this.commands = typeof commands === 'undefined' ? {} : commands;
this.lang = typeof lang === 'undefined' ? 'en-US' : lang; this.lang = typeof lang === 'undefined' ? 'en-US' : lang;
}; };
@ -39,20 +39,21 @@
this.recognition.lang = this.lang; this.recognition.lang = this.lang;
this.recognition.interimResults = false; this.recognition.interimResults = false;
this.recognition.maxAlternatives = 1; this.recognition.maxAlternatives = 1;
this.recognition.continuous = true;
this.recognition.lang = this.lang;
if(typeof this.commands !== 'undefined') { if(typeof this.commands !== 'undefined') {
this.speechRecognitionList.addFromString(this.commands, 1); this.speechRecognitionList.addFromString(this.getCommandsString(), 1);
recognition.grammars = this.speechRecognitionList; this.recognition.grammars = this.speechRecognitionList;
} }
this.recognition.onstart = function() {}
this.recognition.onresult = function(event) { this.recognition.onresult = function(event) {
console.log(event.results[event.resultIndex][0].transcript);
console.log(event.results[0][0].transcript);
} }
this.recognition.onspeechend = function() { this.recognition.onspeechend = function() {
self.recognition.stop();
} }
this.recognition.onnomatch = function(event) { this.recognition.onnomatch = function(event) {
@ -64,17 +65,20 @@
} }
}; };
jsOMS.UI.Input.Voice.VoiceManager.prototype.getCommandsString = function()
{
return '#JSGF V1.0; grammar phrase; public <phrase> = ' + Object.keys(this.commands).join(' | ') + ' ;';
};
jsOMS.UI.Input.Voice.VoiceManager.prototype.setLanguage = function(lang) jsOMS.UI.Input.Voice.VoiceManager.prototype.setLanguage = function(lang)
{ {
// todo: eventually restart // todo: eventually restart
this.recognition.lang = lang; this.recognition.lang = lang;
}; };
jsOMS.UI.Input.Voice.VoiceManager.prototype.setCommands = function(commands) jsOMS.UI.Input.Voice.VoiceManager.prototype.add = function(command, callback)
{ {
// todo: eventually restart this.commands[command] = callback;
this.speechRecognitionList.addFromString(commands, 1);
recognition.grammars = this.speechRecognitionList;
}; };
jsOMS.UI.Input.Voice.VoiceManager.prototype.start = function() jsOMS.UI.Input.Voice.VoiceManager.prototype.start = function()