-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
87 lines (83 loc) · 2.56 KB
/
Copy pathmain.js
File metadata and controls
87 lines (83 loc) · 2.56 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
85
86
87
//init SpeechSynth API
const synth = window.speechSynthesis;
//DOM elements
const text_form = document.querySelector('form');
const text_input = document.querySelector('#text-input');
const voice_select = document.querySelector('#voice-select');
const rate = document.querySelector('#rate');
const rate_val = document.querySelector('#rate-value');
const pitch = document.querySelector('#pitch');
const pitch_val = document.querySelector('#pitch-value');
const body = document.querySelector('body');
//init voices array
let voices = [];
const get_voices = () => {
voices = synth.getVoices();
//loop through voices and create an option for each one
voices.forEach(voice => {
//create an option element
const option = document.createElement('option');
//fill option with a voice and language
option.textContent = voice.name + '(' + voice.lang + ')';
//set needed option attributes
option.setAttribute('data-lang', voice.lang);
option.setAttribute('data-name', voice.name);
voice_select.appendChild(option);
});
};
get_voices();
if (synth.onvoiceschanged !== undefined) {
synth.onvoiceschanged = get_voices;
}
//speak
const speak = () => {
//runs as soon as 'language / voice' is selected or 'speak it' is clicked
//check if already speaking
if (synth.speaking) {
console.error('already speaking...');
return;
}
if (text_input.value !== '') {
//add background animation
body.style.background = '#000 url(wave.gif)';
body.style.backgroundRepeat = 'repeat-x';
body.style.backgroundSize = '100% 100%';
//get speak text
const speak_text = new SpeechSynthesisUtterance(text_input.value);
//speak end
speak_text.onend = e => {
console.log('done speaking...');
body.style.background = '#000';
}
//speak error
speak_text.onerror = e => {
console.error('something went wrong :(');
}
//specify voice to use
const selected_voice = voice_select.selectedOptions[0].getAttribute('data-name');
//loop through voices
voices.forEach(voice => {
if (voice.name === selected_voice) {
speak_text.voice = voice;
}
});
//set rate and pitch
speak_text.rate = rate.value;
speak_text.pitch = pitch.value;
//speak
synth.speak(speak_text);
}
};
//event listeners
//1. form submission
text_form.addEventListener('submit', e => {
e.preventDefault();
speak();
text_input.blur();
});
//2. rate value change
rate.addEventListener('change', e => rate_val.textContent = rate.value);
//3. pitch value change
pitch.addEventListener('change', e => pitch_val.textContent = pitch.value);
//4. voice select change
voice_select.addEventListener('change', e => speak());