|
1 | | -var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition |
2 | | -var SpeechGrammarList = SpeechGrammarList || window.webkitSpeechGrammarList |
3 | | -var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent |
4 | | - |
5 | | -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']; |
6 | | - |
7 | | -var recognition = new SpeechRecognition(); |
8 | | -if (SpeechGrammarList) { |
9 | | - // SpeechGrammarList is not currently available in Safari, and does not have any effect in any other browser. |
10 | | - // This code is provided as a demonstration of possible capability. You may choose not to use it. |
11 | | - var speechRecognitionList = new SpeechGrammarList(); |
12 | | - var grammar = '#JSGF V1.0; grammar colors; public <color> = ' + colors.join(' | ') + ' ;' |
13 | | - speechRecognitionList.addFromString(grammar, 1); |
14 | | - recognition.grammars = speechRecognitionList; |
15 | | -} |
| 1 | +var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition; |
| 2 | +var SpeechRecognitionEvent = |
| 3 | + SpeechRecognitionEvent || webkitSpeechRecognitionEvent; |
| 4 | + |
| 5 | +var colors = [ |
| 6 | + "aqua", |
| 7 | + "azure", |
| 8 | + "beige", |
| 9 | + "bisque", |
| 10 | + "black", |
| 11 | + "blue", |
| 12 | + "brown", |
| 13 | + "chocolate", |
| 14 | + "coral", |
| 15 | + "crimson", |
| 16 | + "cyan", |
| 17 | + "fuchsia", |
| 18 | + "ghostwhite", |
| 19 | + "gold", |
| 20 | + "goldenrod", |
| 21 | + "gray", |
| 22 | + "green", |
| 23 | + "indigo", |
| 24 | + "ivory", |
| 25 | + "khaki", |
| 26 | + "lavender", |
| 27 | + "lime", |
| 28 | + "linen", |
| 29 | + "magenta", |
| 30 | + "maroon", |
| 31 | + "moccasin", |
| 32 | + "navy", |
| 33 | + "olive", |
| 34 | + "orange", |
| 35 | + "orchid", |
| 36 | + "peru", |
| 37 | + "pink", |
| 38 | + "plum", |
| 39 | + "purple", |
| 40 | + "red", |
| 41 | + "salmon", |
| 42 | + "sienna", |
| 43 | + "silver", |
| 44 | + "snow", |
| 45 | + "tan", |
| 46 | + "teal", |
| 47 | + "thistle", |
| 48 | + "tomato", |
| 49 | + "turquoise", |
| 50 | + "violet", |
| 51 | + "white", |
| 52 | + "yellow", |
| 53 | +]; |
| 54 | + |
| 55 | +const recognition = new SpeechRecognition(); |
16 | 56 | recognition.continuous = false; |
17 | | -recognition.lang = 'en-US'; |
| 57 | +recognition.lang = "en-US"; |
18 | 58 | recognition.interimResults = false; |
19 | 59 | recognition.maxAlternatives = 1; |
20 | 60 |
|
21 | | -var diagnostic = document.querySelector('.output'); |
22 | | -var bg = document.querySelector('html'); |
23 | | -var hints = document.querySelector('.hints'); |
| 61 | +const diagnostic = document.querySelector(".output"); |
| 62 | +const bg = document.querySelector("html"); |
| 63 | +const hints = document.querySelector(".hints"); |
| 64 | +const startBtn = document.querySelector("button"); |
24 | 65 |
|
25 | | -var colorHTML= ''; |
26 | | -colors.forEach(function(v, i, a){ |
| 66 | +let colorHTML = ""; |
| 67 | +colors.forEach(function (v, i, a) { |
27 | 68 | console.log(v, i); |
28 | | - colorHTML += '<span style="background-color:' + v + ';"> ' + v + ' </span>'; |
| 69 | + colorHTML += '<span style="background-color:' + v + ';"> ' + v + " </span>"; |
29 | 70 | }); |
30 | | -hints.innerHTML = 'Tap/click then say a color to change the background color of the app. Try ' + colorHTML + '.'; |
| 71 | +hints.innerHTML = |
| 72 | + "Press the button then say a color to change the background color of the app. Try " + |
| 73 | + colorHTML + |
| 74 | + "."; |
31 | 75 |
|
32 | | -document.body.onclick = function() { |
| 76 | +startBtn.onclick = function () { |
33 | 77 | recognition.start(); |
34 | | - console.log('Ready to receive a color command.'); |
35 | | -} |
36 | | - |
37 | | -recognition.onresult = function(event) { |
38 | | - // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object |
39 | | - // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects. |
40 | | - // It has a getter so it can be accessed like an array |
41 | | - // The first [0] returns the SpeechRecognitionResult at the last position. |
42 | | - // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results. |
43 | | - // These also have getters so they can be accessed like arrays. |
44 | | - // The second [0] returns the SpeechRecognitionAlternative at position 0. |
45 | | - // We then return the transcript property of the SpeechRecognitionAlternative object |
46 | | - var color = event.results[0][0].transcript; |
47 | | - diagnostic.textContent = 'Result received: ' + color + '.'; |
| 78 | + console.log("Ready to receive a color command."); |
| 79 | +}; |
| 80 | + |
| 81 | +recognition.onresult = function (event) { |
| 82 | + const color = event.results[0][0].transcript; |
| 83 | + diagnostic.textContent = "Result received: " + color + "."; |
48 | 84 | bg.style.backgroundColor = color; |
49 | | - console.log('Confidence: ' + event.results[0][0].confidence); |
50 | | -} |
| 85 | + console.log("Confidence: " + event.results[0][0].confidence); |
| 86 | +}; |
51 | 87 |
|
52 | | -recognition.onspeechend = function() { |
| 88 | +recognition.onspeechend = function () { |
53 | 89 | recognition.stop(); |
54 | | -} |
| 90 | +}; |
55 | 91 |
|
56 | | -recognition.onnomatch = function(event) { |
| 92 | +recognition.onnomatch = function (event) { |
57 | 93 | diagnostic.textContent = "I didn't recognise that color."; |
58 | | -} |
| 94 | +}; |
59 | 95 |
|
60 | | -recognition.onerror = function(event) { |
61 | | - diagnostic.textContent = 'Error occurred in recognition: ' + event.error; |
62 | | -} |
| 96 | +recognition.onerror = function (event) { |
| 97 | + diagnostic.textContent = "Error occurred in recognition: " + event.error; |
| 98 | +}; |
0 commit comments