Created
January 17, 2017 14:53
-
-
Save Abhishek8394/dbf9338a9baf6ca05639929e0b72404d to your computer and use it in GitHub Desktop.
Basic Speech Recognition Firefox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is a JavaScript Scratchpad. | |
* | |
* Enter some JavaScript, then Right Click or choose from the Execute Menu: | |
* 1. Run to evaluate the selected text (Ctrl+R), | |
* 2. Inspect to bring up an Object Inspector on the result (Ctrl+I), or, | |
* 3. Display to insert the result in a comment after the selection. (Ctrl+L) | |
*/ | |
var SpeechRecognition = SpeechRecognition;// || webkitSpeechRecognition | |
var SpeechGrammarList = SpeechGrammarList;// || webkitSpeechGrammarList | |
var SpeechRecognitionEvent = SpeechRecognitionEvent;// || webkitSpeechRecognitionEvent | |
var colors = [ 'aqua' , 'azure' , 'beige', 'bisque', 'black', 'blue', 'brown', 'chocolate', 'coral', 'crimson', 'cyan', 'fuchsia', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'indigo', 'ivory', 'khaki', 'lavender', 'lime', 'linen', 'magenta', 'maroon', 'moccasin', 'navy', 'olive', 'orange', 'orchid', 'peru', 'pink', 'plum', 'purple', 'red', 'salmon', 'sienna', 'silver', 'snow', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'white', 'yellow']; | |
var grammar = '#JSGF V1.0; grammar colors; public <color> = ' + colors.join(' | ') + ' ;' | |
var recognition = new SpeechRecognition(); | |
var speechRecognitionList = new SpeechGrammarList(); | |
speechRecognitionList.addFromString(grammar, 1); | |
recognition.grammars = speechRecognitionList; | |
//recognition.continuous = false; | |
recognition.lang = 'en-US'; | |
recognition.interimResults = false; | |
recognition.maxAlternatives = 1; | |
var colorHTML= ''; | |
colors.forEach(function(v, i, a){ | |
console.log(v, i); | |
colorHTML += '<span style="background-color:' + v + ';"> ' + v + ' </span>'; | |
}); | |
/* ------------ ISSUE HERE --------------------------------------------------------------------------- */ | |
recognition.start(); | |
console.log('Ready to receive a color command.'); | |
/* ---------------------------------------------------------------------------------------------------- */ | |
recognition.onresult = function(event) { | |
var last = event.results.length - 1; | |
var color = event.results[last][0].transcript; | |
console.log('Confidence: ' + event.results[0][0].confidence); | |
} | |
recognition.onspeechend = function() { | |
recognition.stop(); | |
} | |
recognition.onnomatch = function(event) { | |
diagnostic.textContent = "I didn't recognise that color."; | |
} | |
recognition.onerror = function(event) { | |
diagnostic.textContent = 'Error occurred in recognition: ' + event.error; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment