-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (27 loc) · 1012 Bytes
/
script.js
File metadata and controls
31 lines (27 loc) · 1012 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
29
30
31
const textArea = document.getElementById('text');
const voiceSelect = document.getElementById('voiceSelect');
const rate = document.getElementById('rate');
const pitch = document.getElementById('pitch');
const speakBtn = document.getElementById('speak');
let voices = [];
function populateVoices() {
voices = speechSynthesis.getVoices();
voiceSelect.innerHTML = '';
voices.forEach((voice, index) => {
const option = document.createElement('option');
option.value = index;
option.textContent = `${voice.name} (${voice.lang})`;
voiceSelect.appendChild(option);
});
}
populateVoices();
speechSynthesis.onvoiceschanged = populateVoices;
speakBtn.addEventListener('click', () => {
const utter = new SpeechSynthesisUtterance(textArea.value);
const selectedVoice = voices[voiceSelect.value];
if (selectedVoice) utter.voice = selectedVoice;
utter.rate = rate.value;
utter.pitch = pitch.value;
utter.volume = 1;
speechSynthesis.speak(utter);
});