-
Notifications
You must be signed in to change notification settings - Fork 33
/
textToSpeech.js
49 lines (40 loc) · 1.58 KB
/
textToSpeech.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
let speech = new SpeechSynthesisUtterance();
speech.lang = "en";
let voices = [];
window.speechSynthesis.onvoiceschanged = () => {
voices = window.speechSynthesis.getVoices();
speech.voice = voices[0];
let voiceSelect = document.querySelector("#voices");
voices.forEach((voice, i) => (voiceSelect.options[i] = new Option(voice.name, i)));
};
document.querySelector("#rate").addEventListener("input", () => {
const rate = document.querySelector("#rate").value;
speech.rate = rate;
document.querySelector("#rate-label").innerHTML = rate;
});
document.querySelector("#volume").addEventListener("input", () => {
const volume = document.querySelector("#volume").value;
speech.volume = volume;
document.querySelector("#volume-label").innerHTML = volume;
});
document.querySelector("#pitch").addEventListener("input", () => {
const pitch = document.querySelector("#pitch").value;
speech.pitch = pitch;
document.querySelector("#pitch-label").innerHTML = pitch;
});
document.querySelector("#voices").addEventListener("change", () => {
speech.voice = voices[document.querySelector("#voices").value];
});
document.querySelector("#start").addEventListener("click", () => {
speech.text = document.querySelector("textarea").value;
window.speechSynthesis.speak(speech);
});
document.querySelector("#pause").addEventListener("click", () => {
window.speechSynthesis.pause();
});
document.querySelector("#resume").addEventListener("click", () => {
window.speechSynthesis.resume();
});
document.querySelector("#cancel").addEventListener("click", () => {
window.speechSynthesis.cancel();
});