-
Notifications
You must be signed in to change notification settings - Fork 5
/
webSpeechAPI.js
48 lines (42 loc) · 1.15 KB
/
webSpeechAPI.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
var recognizing;
if (navigator.userAgent.includes("Firefox")) {
recognition = new SpeechRecognition()
} else {
recognition = new webkitSpeechRecognition()
}
recognition.lang = langSelect.value
recognition.continuous = true;
reset();
recognition.onend = reset;
recognition.onresult = function (event) {
console.log(event)
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
console.log("..")
textArea.value += event.results[i][0].transcript;
}
}
}
function reset() {
recognizing = false;
speechButton.style.color = ""
speechButton.innerHTML = `<span class="material-symbols-outlined">mic</span>`;
chatButton.removeAttribute("disabled")
speakButton.removeAttribute("disabled")
}
function toggleStartStop() {
recognition.lang = langSelect.value
if (recognizing) {
textArea.focus()
recognition.stop();
reset();
} else {
textArea.value = ""
recognition.start();
recognizing = true;
speechButton.style.color = "red"
speechButton.innerHTML = "⏹"
chatButton.setAttribute("disabled", true)
speakButton.setAttribute("disabled", true)
}
}