-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
56 lines (42 loc) · 1.29 KB
/
script.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
46
47
48
49
50
51
52
53
54
txtinput = document.getElementById('txtinput');
rateI = document.getElementById('rateI');
InputRate = document.getElementById('InputRate');
pitchI = document.getElementById('pitchI');
InputPitch = document.getElementById('InputPitch');
InputVoice = document.getElementById('InputVoice');
speakBtn = document.getElementById('speakBtn');
InputRate.addEventListener('change',function(){
rateI.textContent = InputRate.value
})
InputPitch.addEventListener('change',function(){
pitchI.textContent = InputPitch.value
})
synth = window.speechSynthesis;
getVoices = () => {
allVoices = synth.getVoices();
allVoices.forEach(voice => {
option = document.createElement('option')
option.textContent = voice.name;
option.setAttribute('data-voice',voice.name);
InputVoice.appendChild(option)
})
}
speechSynthesis.onvoiceschanged = getVoices
getVoices()
function speakIT(){
if(txtinput.value!=''){
sayThis = new SpeechSynthesisUtterance(txtinput.value);
selectedVoice = InputVoice.selectedOptions[0].getAttribute('data-voice');
allVoices.forEach(voice => {
if(selectedVoice == voice.name){
sayThis.voice = voice;
}
})
sayThis.rate = InputRate.value
sayThis.pitch = InputPitch.value
synth.speak(sayThis)
}
}
speakBtn.addEventListener('click',function(){
speakIT()
})