-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
112 lines (101 loc) · 3.2 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
const recordBtn = document.querySelector(".record"),
result = document.querySelector(".result"),
downloadBtn = document.querySelector(".download"),
inputLanguage = document.querySelector("#language"),
clearBtn = document.querySelector(".clear");
let SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition,
recognition,
recording = false;
function populateLanguages() {
languages.forEach((lang) => {
const option = document.createElement("option");
option.value = lang.code;
option.innerHTML = lang.name;
inputLanguage.appendChild(option);
});
}
populateLanguages();
function speechToText() {
try {
recognition = new SpeechRecognition();
recognition.lang = inputLanguage.value;
recognition.interimResults = true;
recordBtn.classList.add("recording");
recordBtn.querySelector("p").innerHTML = "Listening...";
recognition.start();
recognition.onresult = (event) => {
const speechResult = event.results[0][0].transcript;
//detect when intrim results
if (event.results[0].isFinal) {
result.innerHTML += " " + speechResult;
result.querySelector("p").remove();
} else {
//creative p with class interim if not already there
if (!document.querySelector(".interim")) {
const interim = document.createElement("p");
interim.classList.add("interim");
result.appendChild(interim);
}
//update the interim p with the speech result
document.querySelector(".interim").innerHTML = " " + speechResult;
}
downloadBtn.disabled = false;
};
recognition.onspeechend = () => {
speechToText();
};
recognition.onerror = (event) => {
stopRecording();
if (event.error === "no-speech") {
alert("No speech was detected. Stopping...");
} else if (event.error === "audio-capture") {
alert(
"No microphone was found. Ensure that a microphone is installed."
);
} else if (event.error === "not-allowed") {
alert("Permission to use microphone is blocked.");
} else if (event.error === "aborted") {
alert("Listening Stopped.");
} else {
alert("Error occurred in recognition: " + event.error);
}
};
} catch (error) {
recording = false;
console.log(error);
}
}
recordBtn.addEventListener("click", () => {
if (!recording) {
speechToText();
recording = true;
} else {
stopRecording();
}
});
function stopRecording() {
recognition.stop();
recordBtn.querySelector("p").innerHTML = "Start Listening";
recordBtn.classList.remove("recording");
recording = false;
}
function download() {
const text = result.innerText;
const filename = "speech.txt";
const element = document.createElement("a");
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(text)
);
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
downloadBtn.addEventListener("click", download);
clearBtn.addEventListener("click", () => {
result.innerHTML = "";
downloadBtn.disabled = true;
});