-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
124 lines (102 loc) · 3.06 KB
/
script.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const startBtn1 = document.getElementById('startExample1');
startBtn1.addEventListener('click', () => example1());
function example1() {
const output = document.getElementById('output1');
const action = document.getElementById('action1');
let Speech;
if ("webkitSpeechRecognition" in window) {
Speech = webkitSpeechRecognition;
} else if ("SpeechRecognition" in window) {
Speech = SpeechRecognition;
} else {
startBtn.disabled = true;
output.value = 'This browser does not support SpeechRecognition APIs.';
};
const recognition = new Speech();
recognition.continuous = false;
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.onstart = () => {
action.innerHTML = 'Listening, please speak...';
};
recognition.onspeechend = () => {
action.innerHTML = 'Stopped listening.';
recognition.stop();
}
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
const confidence = event.results[0][0].confidence;
output.value = 'Text: ' + transcript + '\nConfidence: ' + confidence * 100 + '%';
};
recognition.start();
}
const startBtn2 = document.getElementById('startExample2');
startBtn2.addEventListener('click', () => example2());
const 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'
];
function example2() {
const output = document.getElementById('output2');
const action = document.getElementById('action2');
let Speech;
if ("webkitSpeechRecognition" in window) {
Speech = webkitSpeechRecognition;
} else if ("SpeechRecognition" in window) {
Speech = SpeechRecognition;
} else {
startBtn.disabled = true;
output.value = 'This browser does not support SpeechRecognition APIs.';
};
const recognition = new Speech();
recognition.continuous = false;
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
let speechRecognitionList;
if ("SpeechGrammarList" in window) {
speechRecognitionList = new SpeechGrammarList();
const grammar = '#JSGF V1.0; grammar colors; public <color> = ' + colors.join(' | ') + ' ;'
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
}
recognition.onstart = () => {
action.innerHTML = 'Listening, please speak...';
};
recognition.onspeechend = () => {
action.innerHTML = 'Stopped listening.';
recognition.stop();
}
recognition.onnomatch = () => {
action.innerHTML = 'Could not find that color.';
}
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
output.value = 'Color: ' + transcript;
output.style.backgroundColor = transcript;
};
recognition.start();
}