mirror of
https://github.com/Karaka-Management/jsOMS.git
synced 2026-02-16 01:08:41 +00:00
Add voice recognition (tested)
This commit is contained in:
parent
d02a57beb9
commit
a78d931a85
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user