-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (46 loc) · 2.29 KB
/
script.js
File metadata and controls
57 lines (46 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
var navElement = ['rojo', 'verde', 'azul', 'amarillo', 'magenta', 'cian', 'blanco', 'negro'];
var grammar = '#JSGF V1.0; grammar navElement; public <navElement> = ' + navElement.join(' | ') + ' ;';
var testBtn = document.querySelector('button');
function testSpeech() {
testBtn.disabled = true;
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.lang = 'es';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.start();
recognition.onresult = function(event) {
// The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object
// The SpeechRecognitionResultList object contains SpeechRecognitionResult objects.
// It has a getter so it can be accessed like an array
// The first [0] returns the SpeechRecognitionResult at position 0.
// Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results.
// These also have getters so they can be accessed like arrays.
// The second [0] returns the SpeechRecognitionAlternative at position 0.
// We then return the transcript property of the SpeechRecognitionAlternative object
var speechResult = event.results[0][0].transcript;
for (i = 0; i < navElement.length; i++) {
if (speechResult == navElement[i]) {
var url = document.getElementById(navElement[i]);
/*url.classList.toggle("mdl-layout__tab-panel is-active");*/
url.className = "mdl-layout__tab-panel is-active";
/*window.location.assign(url);*/
url.click();
}
}
console.log('Confidence: ' + event.results[0][0].confidence);
}
recognition.onspeechend = function() {
recognition.stop();
testBtn.disabled = false;
}
recognition.onerror = function(event) {
testBtn.disabled = false;
}
}
testBtn.addEventListener('click', testSpeech);