-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
144 lines (102 loc) · 2.82 KB
/
main.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// INIT speechsynth API
const synth = window.speechSynthesis;
// DOM Elements
const rate = document.querySelector('#rate');
const textForm = document.querySelector('form');
const textInput = document.querySelector('#text-input');
const voiceSelect = document.querySelector('#voice-select');
const rateValue = document.querySelector('#rate-value');
const pitch = document.querySelector('#pitch');
const pitchValue = document.querySelector('#pitch-value');
//INIT the voices array!
let voices = [];
const getVoices = () => {
voices = synth.getVoices();
// loop through voices and create an option for each one!
voices.forEach(voice => {
//create option Element
const option = document.createElement('option');
option.textContent = voice.name + '(' + voice.lang + ')';
// Set needed opton attributes
option.setAttribute('data-lang', voice.lang);
option.setAttribute('data-name', voice.name);
voiceSelect.appendChild(option);
});
};
// getVoices();
if(synth.onvoiceschanged !== undefined){
synth.onvoiceschanged =getVoices;
}
// Speak
const speak = () => {
// check if speaking!
if(synth.speaking){
console.error('Already Speaking!');
synth.cancel();
}
if(textInput.value !== ''){
// Get speak text!
var speakText = new SpeechSynthesisUtterance(textInput.value);
// Speak end
speakText.onend = e => {
console.log('Done Speaking!');
}
speakText.onerror = e => {
console.error('Something went wrong!');
}
// Selected Voice!
const selectedVoice = voiceSelect.selectedOptions[0].getAttribute('data-name');
// Loop through voices!
voices.forEach(voice => {
if(voice.name === selectedVoice){
speakText.voice = voice;
}
});
speakText.rate = rate.value;
speakText.pitch = pitch.value;
// Speak
synth.speak(speakText);
}
};
// textForm.addEventListener('submit', e => {
// e.preventDefault();
// speak();
// textInput.blur();
// });
// Speak Button
//
$("#speakbtn").click( function(e) {
e.preventDefault();
$('#pausebtn').text("Pause");
speak();
textInput.blur();
});
$('#pausebtn').click(function(e) {
e.preventDefault();
if($(this).text() == "Pause" && synth.speaking){
$(this).text("Play");
synth.pause();
}
else if($(this).text() == "Play"){
$(this).text("Pause");
synth.resume();
}
else {
alert("Can't Pause! 'coz nothing's playing!");
}
})
$('#cancelbtn').click(function(e){
e.preventDefault();
synth.cancel();
$('#pausebtn').text("Pause");
})
// Rate value change!
rate.addEventListener('change', e => {
rateValue.textContent = rate.value;
})
// Rate value change!
pitch.addEventListener('change', e => {
pitchValue.textContent = pitch.value;
})
// Voice Select Change
voiceSelect.addEventListener('change', e => speak());