-
Notifications
You must be signed in to change notification settings - Fork 1
/
recognition.js
43 lines (38 loc) · 1.46 KB
/
recognition.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
if ("webkitSpeechRecognition" in window) {
let speechRecognition = new webkitSpeechRecognition();
let final_transcript = "";
speechRecognition.continuous = true;
speechRecognition.interimResults = true;
// speechRecognition.lang = document.querySelector("#select_dialect").value;
speechRecognition.onstart = () => {
document.querySelector("#status").style.display = "block";
};
speechRecognition.onerror = () => {
document.querySelector("#status").style.display = "none";
console.log("Speech Recognition Error");
};
speechRecognition.onend = () => {
document.querySelector("#status").style.display = "none";
console.log("Speech Recognition Ended");
};
speechRecognition.onresult = (event) => {
let interim_transcript = "";
for (let i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
document.querySelector("#final").innerHTML = final_transcript;
document.querySelector("#interim").innerHTML = interim_transcript;
};
document.querySelector("#start").onclick = () => {
speechRecognition.start();
};
document.querySelector("#stop").onclick = () => {
speechRecognition.stop();
};
} else {
console.log("Speech Recognition Not Available");
}