-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (74 loc) · 2.46 KB
/
Copy pathscript.js
File metadata and controls
84 lines (74 loc) · 2.46 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Text To Speech Variables
const textarea = document.querySelector("textarea"),
voiceList = document.querySelector("select"),
speechBtn = document.getElementById("convert_speech"),
clearBtn = document.getElementById("clearBtn");
let synth = speechSynthesis,
isSpeaking = true,
utterance;
voices();
textarea.addEventListener("input", function () {
if (textarea.value.length > 0) {
// If the textarea has content, add the "show" class to display the button
clearBtn.classList.add("show");
clearBtn.classList.remove("hide");
} else {
// If the textarea is empty, add the "hide" class to hide the button
clearBtn.classList.add("hide");
clearBtn.classList.remove("show");
}
});
// Clear Button Function
clearBtn.addEventListener("click", e =>{
textarea.value = "";
clearBtn.classList.add("hide");
clearBtn.classList.remove("show");
});
// Select Voice Function
function voices(){
for(let voice of synth.getVoices()){
let selected = voice.name === "Google US English" ? "selected" : "";
let option = `<option value="${voice.name}" ${selected}>${voice.name} (${voice.lang})</option>`;
voiceList.insertAdjacentHTML("beforeend", option);
}
}
synth.addEventListener("voiceschanged", voices);
// Text To Speech Function
function textToSpeech(text){
utterance = new SpeechSynthesisUtterance(text);
for(let voice of synth.getVoices()){
if(voice.name === voiceList.value){
utterance.voice = voice;
}
}
synth.speak(utterance);
}
// Speech Button Function
speechBtn.addEventListener("click", e =>{
e.preventDefault();
if(textarea.value !== ""){
if(!synth.speaking){
textToSpeech(textarea.value);
}
if(textarea.value.length > 20){
setInterval(()=>{
if(!synth.speaking && !isSpeaking){
isSpeaking = true;
speechBtn.innerText = "Convert To Speech";
}else{
}
}, 500);
if(isSpeaking){
synth.resume();
isSpeaking = false;
speechBtn.innerText = "Pause";
}else{
synth.pause();
isSpeaking = true;
speechBtn.innerText = "Resume";
}
}else{
speechBtn.innerText = "Convert To Speech";
}
}
});