-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
60 lines (52 loc) · 1.81 KB
/
index.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
let volume = document.getElementById("volume");
let rate = document.getElementById("rate");
let pitch = document.getElementById("pitch");
let select = document.querySelector("#voices");
let controlButton = document.querySelectorAll("button");
let text = document.getElementById("text");
let sp = new SpeechSynthesisUtterance();
sp.lang = "en";
let spOptions = [];
// Need to call getVoices() -> returns an array of voices
// without this it won't work on android
window.speechSynthesis.getVoices();
window.speechSynthesis.onvoiceschanged = () => {
spOptions = window.speechSynthesis.getVoices();
sp.voice = spOptions[0];
console.log(spOptions);
spOptions.forEach((voice, i) => {
select.options[i] = new Option(voice.name, i);
});
};
volume.addEventListener("input", () => {
const volumeValue = volume.value;
sp.volume = volumeValue;
document.getElementById("label-v").innerHTML = volumeValue;
});
pitch.addEventListener("input", () => {
const pitchValue = pitch.value;
sp.pitch = pitchValue;
document.getElementById("label-p").innerHTML = pitchValue;
});
rate.addEventListener("input", () => {
const rateValue = rate.value;
sp.rate = rateValue;
document.getElementById("label-r").innerHTML = rateValue;
});
select.addEventListener("change", () => {
sp.voice = spOptions[select.value];
});
for (const button of controlButton) {
button.addEventListener("click", (e) => {
if (e.target.id == "start") {
sp.text = text.value;
window.speechSynthesis.speak(sp);
} else if (e.target.id == "resume") {
window.speechSynthesis.resume();
} else if (e.target.id == "pause") {
window.speechSynthesis.pause();
} else if (e.target.id == "cancel") {
window.speechSynthesis.cancel();
}
});
}