Skip to content

Instantly share code, notes, and snippets.

@smwhr
Last active January 28, 2021 16:48
Show Gist options
  • Save smwhr/cd3e37195afbaef511dbfb10a930c981 to your computer and use it in GitHub Desktop.
Save smwhr/cd3e37195afbaef511dbfb10a930c981 to your computer and use it in GitHub Desktop.
Parseur vocal pour Parchment
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent
var commands = [ 'nord', 'sud', 'est', 'ouest', 'examiner', 'regarder', 'inventaire', 'encore', 'prendre'];
var grammar = '#JSGF V1.0; grammar commands; public <command> = ' + commands.join(' | ') + ' ;'
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.continuous = false;
recognition.lang = 'fr-FR';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
let enterCommand = (cmd) => {
$(".TextInput").val(cmd);
const ke = new KeyboardEvent('keydown', {
bubbles: true, cancelable: true, keyCode: 13
});
document.body.dispatchEvent(ke);
}
let clearAndListen = () => {
recognition.stop();
setTimeout( () => recognition.start(), 400);
}
recognition.onresult = function(event) {
var command = event.results[0][0].transcript;
console.log('Result received: ' + command + '.');
enterCommand(command)
console.log('Confidence: ' + event.results[0][0].confidence);
}
recognition.onspeechend = function() {
clearAndListen()
}
recognition.onnomatch = function(event) {
console.error("I didn't recognise that command.");
}
recognition.onerror = function(event) {
console.error('Error occurred in recognition: ' + event.error);
clearAndListen()
}
clearAndListen()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment