-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrates.js
220 lines (187 loc) · 6.02 KB
/
rates.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
'use strict';
const RATES = ['1/2.5', '1/2.25', '1/2' ,'1/1.75', '1/1.5', '1/1.25', '1', '1.25', '1.5', '1.75', '2', '2.25', '2.5'];
function popupateCell(cell, voice, text, normalDuration) {
if (voice.voiceURI.startsWith('Google') && parseFloat(cell.dataset.rate) > 2) {
return Promise.resolve(normalDuration);
}
if (cell.dataset.duration) {
return Promise.resolve(parseFloat(cell.dataset.duration));
}
return new Promise(resolve => {
let u = new SpeechSynthesisUtterance(text);
let now = 0;
u.rate = eval(cell.dataset.rate);
u.voice = voice;
u.onstart = e => {
now = Date.now();
cell.classList.add('warning');
};
u.onerror = console.log.bind(console);
u.onend = e => {
cell.classList.remove('warning');
let duration = Date.now() - now;
cell.dataset.duration = duration;
cell.dataset.value = normalDuration ? normalDuration / duration : 1;
cell.textContent = parseFloat(cell.dataset.value).toPrecision(3);
resolve(duration);
};
speechSynthesis.speak(u);
});
}
function createRow() {
let results = document.querySelector('#results tbody');
let row = document.createElement('tr');
results.appendChild(row);
return row;
}
function doVoice(voice, text) {
let selector = `#results tr[data-voice="${voice.voiceURI}"] td.ratecell`
let normalRateCell = document.querySelector(selector + '[data-rate="1"]');
let ratecells = document.querySelectorAll(selector + ':not([data-rate="1"])');
return popupateCell(normalRateCell, voice, text).then(normalDuration => {
return Promise.all(
Array.from(ratecells).map(cell => popupateCell(cell, voice, text, normalDuration)));
});
}
function buildColumnHeaders() {
let row = createRow();
let th = document.createElement('th');
th.textContent = 'Voice';
row.appendChild(th);
for (let rateString of RATES) {
th = document.createElement('th');
th.textContent = rateString;
th.className = 'ratecell';
row.appendChild(th);
}
}
function buildVoiceRow(voice, values) {
let row = createRow();
row.className = 'result';
row.dataset.voice = voice.voiceURI;
let td = document.createElement('td');
td.textContent = voice.name;
row.appendChild(td);
for (let i in RATES) {
let rateString = RATES[i];
td = document.createElement('td');
td.className = 'ratecell';
td.dataset.rate = rateString;
if (values && values[i]) {
td.dataset.value = values[i];
td.textContent = parseFloat(values[i]).toPrecision(3);
}
row.appendChild(td);
}
}
function updatePlayState(playing) {
let button = document.getElementById("startstop");
button.classList.toggle('btn-success', !playing);
button.classList.toggle('btn-danger', playing);
button.classList.toggle('playing', playing);
button.textContent = playing ? 'Stop' : 'Start';
}
function stop() {
speechSynthesis.cancel();
updatePlayState(false);
}
function start() {
updatePlayState(true);
let text = document.getElementById('texttospeak').value;
let selectedOptions = document.getElementById('voices').selectedOptions;
let selectedVoices = new Set(Array.from(selectedOptions).map(o => o.value));
let voices = speechSynthesis.getVoices().filter(v => selectedVoices.has(v.voiceURI));
let p = Promise.resolve();
for (let voice of voices) {
let v = voice;
if (!document.querySelector(`#results tr[data-voice="${voice.voiceURI}"]`)) {
buildVoiceRow(voice);
}
p = p.then(() => {
return doVoice(v, text);
});
}
p.then(() => {
updatePlayState(false);
plot();
updatePermalink();
});
}
function parseQueryString() {
let query = location.search.substring(1);
if (query) {
let rows = query.split('&').map(e => JSON.parse(decodeURIComponent(e)));
for (let values of rows) {
let voice = { voiceURI: values.shift(), name: values.shift() };
buildVoiceRow(voice, values);
}
}
plot();
}
function encodeArray(array) {
return '?' + array.map(value => encodeURIComponent(JSON.stringify(value))).join('&');
}
function updatePermalink() {
let rows = Array.from(document.querySelectorAll('#results tr.result'));
if (!rows.length)
return;
let data = [];
for (let row of rows) {
let v = Array.from(row.querySelectorAll('td')).map(cell => cell.dataset.value || cell.textContent);
v.unshift(row.dataset.voice);
data.push(v);
}
let permalink = document.getElementById('permalink');
permalink.href = location;
permalink.search = encodeArray(data);
}
function plot() {
let series = [RATES.map(s => Math.log10(eval(s)))];
let rows = Array.from(document.querySelectorAll('#results tr.result'));
for (let row of rows) {
series.push(Array.from(row.querySelectorAll('td.ratecell')).map(
cell => Math.log10(parseFloat(cell.dataset.value))));
}
var data = {
labels: RATES,
// Our series array that contains series objects or in this case series data arrays
series: series
};
new Chartist.Line('.ct-chart', data, {
axisY: { showLabel: false, offset: 0 },
chartPadding: { top: 15, right: 15, bottom: 5, left: 10 }
});
};
function initVoices() {
let voices = speechSynthesis.getVoices();
startButton.disabled = false;
var selectElm = document.querySelector('#voices');
selectElm.innerHTML = '';
voices.sort((a, b) => a.name.localeCompare(b.name));
for (var i=0;i < voices.length;i++) {
var option = document.createElement('option');
option.innerHTML = voices[i].name + ' (' + voices[i].lang + ')';
option.setAttribute('value', voices[i].voiceURI);
option.voice = voices[i];
if (voices[i].isDefault) {
option.selected = true;
}
selectElm.appendChild(option);
}
}
let startButton = document.getElementById("startstop");
startButton.addEventListener("click", e => {
if (e.target.classList.contains('playing')) {
stop(e.target);
} else {
start(e.target);
}
});
buildColumnHeaders();
parseQueryString();
updatePermalink();
if (speechSynthesis.getVoices().length === 0) {
speechSynthesis.addEventListener('voiceschanged', initVoices);
} else {
initVoices();
}