-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (50 loc) · 1.82 KB
/
script.js
File metadata and controls
59 lines (50 loc) · 1.82 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
51
52
53
54
55
56
57
58
59
const textarea = document.querySelector("textarea"),
voiceList = document.querySelector("select"),
speechbtn = document.querySelector("button");
let synth = speechSynthesis ,
isSpeaking = true;
voices();
function voices(){
for(let voice of synth.getVoices()){
let selected = voice.name = "Microsoft Hazel Desktop - English (Great Britain)" ? "selected" : "";
//creating option tag with passing voice name
let option = `<option value="${voice.name}" ${selected}>${voice.name} (${voice.lang})</option>`
voiceList.insertAdjacentHTML("beforeend", option); //inserting option tag beforeend of select tag
}
}
synth.addEventListener("voiceschanged" , voices);
function textToSpeach(text){
let utternance = new SpeechSynthesisUtterance(text);
for(let voice of synth.getVoices()){
// if the availble device voice name is equal to the user selected voice name then set the sppech voice to user selected voice
if(voice.name === voiceList.value){
utternance.voice = voice;
}
}
speechSynthesis.speak(utternance);
}
speechbtn.addEventListener("click", e =>{
e.preventDefault();
if(textarea.value !== ""){
if (!synth.speaking) {
textToSpeach(textarea.value);
}
if(textarea.value.length > 80){
if(isSpeaking){
synth.resume();
isSpeaking = false;
speechbtn.innerText = "Pause Speech";
}else{
synth.pause();
isSpeaking = true;
speechbtn.innerText = "Resume Speech";
}
setInterval(()=>{
if (!synth.speaking && !isSpeaking) {
isSpeaking = true;
speechbtn.innerText = "Convert To Speech";
}
});
}
}
})