-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.js
253 lines (224 loc) · 7.91 KB
/
site.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
window.utterances = [];
// from https://stackoverflow.com/a/50285928
var options = {};
options['separateWordSearch'] = false;
options['diacritics'] = false;
options['debug'] = false;
options['acrossElements'] = true;
//options["exclude"] = ['.ignore-tts']
var playing = false;
var paused = true;
let voices = [];
const voicesOptions = document.getElementById('voices');
const hasSynth = 'speechSynthesis' in window;
$(function () {
$('[data-toggle="tooltip"]').tooltip();
const synth = window.speechSynthesis;
let voices = [];
if ('speechSynthesis' in window) {
// Speech Synthesis supported
} else {
// Speech Synthesis Not Supported
alert("Sorry, your browser doesn't support text to speech!");
}
populateVoiceList();
if (typeof synth !== 'undefined' && synth.onvoiceschanged !== undefined) {
synth.onvoiceschanged = populateVoiceList;
}
var selectedLang = 'en-US';
var selectedVoice = '$("#voices").find("option").attr("data-pos")';
$('.dropdown-menu').click(function (e) {
e.stopPropagation();
});
$('#dropdownMenuLink').click(function (e) {
//e.stopPropagation();
loadChunks('#main');
});
$('#nextButton').click(function (e) {
var val = chunkIndex + 1;
if (val > maxChunkIndex) {
val = 0;
}
updateChunkIndex(val);
markCurrentIndex();
document.getElementById('text-loc').innerText = val + '/' + (maxChunkIndex);
document.getElementById('textSlider').value = parseInt(val);
if (playing && !paused) {
speakNext();
}
});
$('#prevButton').click(function (e) {
var val = chunkIndex - 1;
if (val < 0) {
val = maxChunkIndex;
}
updateChunkIndex(val);
markCurrentIndex();
document.getElementById('text-loc').innerText = val + '/' + (maxChunkIndex);
document.getElementById('textSlider').value = parseInt(val);
if (playing && !paused) {
speakNext();
}
});
$("#voices").change(function (e) {
e.stopPropagation();
selectedVoice = $(this).find("option:selected").attr("data-pos");
selectedLang = $(this).find("option:selected").attr("data-lang");
updateVoice(voices[selectedVoice]);
updateLang(selectedLang);
});
function populateVoiceList() {
voices = synth.getVoices();
console.log('voices: ', voices, voices.length);
if (typeof speechSynthesis === 'undefined') {
return;
}
voices.forEach((item, index) => {
const option = document.createElement('option');
option.textContent = `${item.name} (${item.lang})`;
option.setAttribute('data-lang', item.lang);
option.setAttribute('data-name', item.name);
option.setAttribute('data-voice-uri', item.voiceURI);
option.setAttribute('data-pos', index);
if (item.lang === 'en-US') {
option.selected = true;
}
document.getElementById("voices").appendChild(option);
});
}
document.getElementById('speedSlider').addEventListener('input', function () {
updateRate(this.value);
document.getElementById('text-speed').innerText = this.value;
});
var sliderTimeout = null
document.getElementById('textSlider').addEventListener('input', function () {
if (sliderTimeout) {
clearTimeout(sliderTimeout);
}
var captured = parseInt(document.getElementById('textSlider').value);
if(captured > maxChunkIndex) {
captured = maxChunkIndex;
}else if(captured < 0){
captured = 0;
}
sliderTimeout = setTimeout(() => {
if(playing && paused) {
paused = false; // don't want to resume at last paused
playing = false;
}
updateChunkIndex(captured);
markCurrentIndex();
document.getElementById('text-loc').innerText = (captured) + '/' + (maxChunkIndex);
if (playing && !paused) {
speakNext();
}
sliderTimeout = null;
}, 10);
});
document.getElementById('playButton').addEventListener('click', function () {
//create an utterance as you normally would...
if (playing && paused) {
paused = false;
synth.resume();
return;
}
gtag('event', 'view_item', {
'currency': "USD",
'value': '0.00',
'items': [{
'id': 'audio-tts-start',
'name': window.location,
'category': 'listening'
}]
});
speakNext();
});
window.addEventListener("beforeChunk", (ev) => {
document.getElementById('text-loc').innerText = (chunkIndex + 1) + '/' + (maxChunkIndex);
});
window.addEventListener("loadedChunks", (ev) => {
document.getElementById('textSlider').max = ev.detail.count;
document.getElementById('text-loc').innerText = '0/' + (maxChunkIndex);
});
var speakTimeout = null;
function speakNext() {
paused = false;
if (speakTimeout) {
clearTimeout(speakTimeout);
}
speakTimeout = setTimeout(() => {
synth.cancel();
playing = true;
var utterance = new SpeechSynthesisUtterance();
utterance.lang = selectedLang;
utterance.voice = voices[selectedVoice];
utterance.rate = rate;
utterances.push(utterance); // keep in memeory?
// utterance.addEventListener('boundary', function (evt) {
// onboundaryHandler(evt);
// console.log(evt);
// });
// utterance.onboundary = onboundaryHandler;
//pass it into the chunking function to have it played out.
//you can set the max number of characters by changing the chunkLength property below.
//a callback function can also be added that will fire once the entire text has been spoken.
//findChunks(utterance);
speechUtteranceChunker(utterance, {
chunkLength: 120
}, function () {
//some code to execute when done
speakTimeout = null;
console.log('done');
markInstance.unmark();
});
}, 5);
}
document.getElementById('stopButton').addEventListener('click', function () {
if (synth) {
synth.pause();
paused = true;
// synth.cancel();
if (playing) {
gtag('event', 'view_item', {
'currency': "USD",
'value': '0.00',
'items': [{
'id': 'audio-tts-stop',
'name': window.location,
'category': 'listening'
}]
});
}
}
});
addEventListener("beforeunload", (event) => {
synth.pause();
paused = true;
playing = false;
synth.cancel();
if (playing) {
gtag('event', 'view_item', {
'currency': "USD",
'value': '0.00',
'items': [{
'id': 'audio-tts-unload',
'name': window.location,
'category': 'listening'
}]
});
}
});
$('[data-toggle="tooltip"]').on('shown.bs.tooltip', function () {
// do something…
//console.log('shown tooltip', this);
gtag('event', 'view_item', {
'currency': "USD",
'value': '0.00',
'items': [{
'id': 'tooltip',
'name': this.dataset.originalTitle,
'category': 'tooltip'
}]
});
});
});