-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (43 loc) · 1.51 KB
/
script.js
File metadata and controls
50 lines (43 loc) · 1.51 KB
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
const message = new SpeechSynthesisUtterance();
let voices = [];
const voicesDropdown = document.querySelector("#voices");
const rate = document.querySelector("#rate");
const pitch = document.querySelector("#pitch");
const text = document.querySelector("#text");
const speakButton = document.querySelector("#speak");
const stopButton = document.querySelector("#stop");
message.text= text.value;
function populateVoices() {
voices= this.getVoices();
for(let index= 0; index < voices.length; index++){
const voiceOption = document.createElement("option");
voiceOption.setAttribute("value", voices[index].name);
voiceOption.innerHTML = `${voices[index].name} (${voices[index].lang})`;
voicesDropdown.appendChild(voiceOption);
}
}
function setVoice(){
for(let index= 0; index < voices.length; index++){
if(voices[index].name == this.value){
message.voice = voices[index];
}
}
toggle(true);
}
function toggle(startOver){
speechSynthesis.cancel();
if(startOver){
speechSynthesis.speak(message);
}
}
function setOption(){
message[this.id] = this.value;
toggle(true);
}
speechSynthesis.addEventListener("voiceschanged", populateVoices);
voicesDropdown.addEventListener("change", setVoice);
rate.addEventListener("change", setOption);
pitch.addEventListener("change", setOption);
text.addEventListener("change", setOption);
speakButton.addEventListener("click", () => toggle(true));
stopButton.addEventListener("click", () => toggle(false));