-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
28 lines (28 loc) · 922 Bytes
/
main.js
File metadata and controls
28 lines (28 loc) · 922 Bytes
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
const text = document.querySelector('#text');
const btn=document.querySelector('#generate-btn');
function generateAudio(){
const SpeachSynthesis= window.speechSynthesis;
const enteredText = text.value;
if (!SpeachSynthesis.speaking&&text.value.trim()!=="") {
const newSpeach = new SpeechSynthesisUtterance(text.value);
console.log(newSpeach);
newSpeach.rate=0.5;
SpeachSynthesis.speak(newSpeach);
text.value="";
btn.textContent ='sound is playing...';
btn.style.color='green';
newSpeach.onend=()=>{
btn.textContent='Generate audio';
btn.style.color='black';
}
}
else if (text.value.trim()==="") {
btn.textContent='Enter something!';
btn.style.color='red';
}
}
document.addEventListener('keydown',(event)=>{
if (event.key==='Enter') {
generateAudio();
}
});