-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
45 lines (32 loc) · 992 Bytes
/
main.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
let speech = new SpeechSynthesisUtterance();
let voices = [];
let voiceSelect = document.querySelector("select");
function stopSpeech() {
if (window.speechSynthesis.speaking) {
window.speechSynthesis.cancel();
}
}
function populateVoiceList() {
voices = speechSynthesis.getVoices();
speech.voice = voices[0];
voiceSelect.innerHTML = "";
voices.forEach((voice, i) => {
voiceSelect.options[i] = new Option(voice.name, voice.name);
});
}
populateVoiceList();
window.speechSynthesis.onvoiceschanged = () => {
voices = window.speechSynthesis.getVoices();
speech.voice = voices[0];
voices.forEach(
(voice, i) => (voiceSelect.options[i] = new Option(voice.name, i))
);
};
voiceSelect.addEventListener("change", () => {
stopSpeech();
speech.voice = voices[voiceSelect.value];
});
document.querySelector("button").addEventListener("click", () => {
speech.text = document.querySelector("textarea").value;
window.speechSynthesis.speak(speech);
});