forked from benetech/BeneSpeak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.js
367 lines (301 loc) · 12.1 KB
/
speech.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
var BeneSpeak = {
wordHighlightClass: 'ttsWordHL',
sentenceHighlightClass: 'ttsSentHL',
BOUNDARY_CHARS: /[\.,](?=\s|$)|[\!\?\s\"\;\u201c\u201d\u2013\u2014]/g,
SENTENCE_TERMINATORS: /[\.\?\!]/,
WORD_SEPARATORS: /[\s",;\u201c\u201d\u2013\u2014]/,
CONDITIONAL_SEPARATORS: /'\u2018\u2019/,
SpeechData: function() {
this.document = null;
this.utterance = '';
this.words = [];
this.sentences = [];
this._highlightedWord = -1;
this._highlightedSentence = -1;
this._sipStart = null;
this._sipOffset = -1;
this._wipStart = null;
this._wordRects = [];
this._sentenceRects = [];
this.xOffset = 0;
this.yOffset = 0;
},
Fragment: function(range, offset) {
this.range = range;
this.offset = offset;
this.text = this.range.toString();
},
Position: function(node, offset) {
this.node = node;
this.offset = offset;
},
generateSpeechData: function(element) {
r = new BeneSpeak.SpeechData();
BeneSpeak._tokenize(element, r);
return r;
},
_tokenize: function(node, data) {
var d = data;
if (d.document == null) {
d.document = node.ownerDocument;
}
switch(node.nodeType) {
case 1:
var cn = node.childNodes;
// note that element-specific processing can happen here
BeneSpeak._elementStartAnnouncement(d, node);
for (var i = 0; i < cn.length; i++) {
this._tokenize(cn[i], d);
}
// break off the word if this is a block-level element
if (this._isBlockElement(node)) {
BeneSpeak._processBlockBoundary(d, node);
}
BeneSpeak._elementEndAnnouncement(d, node);
break;
case 3:
var t = node.textContent;
// initialize _wipStart if needed
if (d._wipStart == null) {
// seek to first non-whitespace character
var fnwc = /\S/.exec(t);
if (fnwc != null) {
d._wipStart = new BeneSpeak.Position(node, fnwc.index);
}
}
// only proceed if _wipStart could be initialized
if (d._wipStart != null) {
// seek ahead to find boundary characters
var m = this.BOUNDARY_CHARS.exec(t);
while (m != null) {
// initialize _sipStart if needed
if (d._sipStart == null) {
d._sipStart = d._wipStart.copy();
d._sipOffset = d.utterance.length;
}
BeneSpeak._processWordBoundary(d, node, m);
// test against sentence boundaries
if (this.SENTENCE_TERMINATORS.test(m[0]) == true) {
d.utterance += ' ';
BeneSpeak._processSentenceBoundary(d, node, m);
}
m = this.BOUNDARY_CHARS.exec(t);
}
}
break;
}
return d;
},
speak: function(element, callback) {
var data = this.generateSpeechData(element);
chrome.tts.speak(data.utterance, { 'rate' : 1.25, 'onEvent' : function(event) { data.handleTtsEvent(event, callback);}});
},
stop: function(j) {
chrome.tts.stop();
},
_isBlockElement: function(el) {
var style = window.getComputedStyle(el);
return ((style.display.indexOf('inline') == -1) && (style.display.indexOf('ruby') == -1))
},
_processWordBoundary: function(d, node, match) {
var r = d.document.createRange();
r.setStart(d._wipStart.node, d._wipStart.offset);
r.setEnd(node, match.index);
var w = new BeneSpeak.Fragment(r, d.utterance.length);
if (w.text.length > 0) {
d.words.push(w);
}
d.utterance += w.text;
d.utterance += match[0];
// if advancing past the boundary character
// moves us past the end of the textnode, null out
// the word in progress var
if (match.index + 1 < node.textContent.length) {
d._wipStart = new BeneSpeak.Position(node, match.index + 1);
} else {
d._wipStart = null;
}
},
_processSentenceBoundary: function(d, node, match) {
var r = d.document.createRange();
r.setStart(d._sipStart.node, d._sipStart.offset);
r.setEnd(node, match.index + 1);
// we need to find the word whose range start
// matches that of this sentence.
var offset = 0;
for (var j = 0; j < d.words.length; j++) {
var rangeStart = d.words[j].range.startOffset;
if ((rangeStart.startContainer == d._sipStart.node) && (rangeStart.startOffset == d._sipStart.offset)) {
offset = d.words[j].offset;
break;
}
}
var sent = new BeneSpeak.Fragment(r, d._sipOffset);
if (sent.text.length > 0) {
d.sentences.push(sent);
}
d._sipStart = null;
d._sipOffset = 0;
},
_processBlockBoundary: function(d, blockNode) {
if (d._wipStart != null) {
var r = d.document.createRange();
r.setStart(d._wipStart.node, d._wipStart.offset);
r.setEndAfter(blockNode);
var w = new BeneSpeak.Fragment(r, d.utterance.length);
if (w.text.length > 0) {
d.words.push(w);
}
d.utterance += w.text;
d._wipStart = null;
}
if (d._sipStart != null) {
var r = d.document.createRange();
r.setStart(d._sipStart.node, d._sipStart.offset);
r.setEndAfter(blockNode);
var sent = new BeneSpeak.Fragment(r, d._sipOffset);
if (sent.text.length > 0) {
d.sentences.push(sent);
}
d._sipStart = null;
d._sipOffset = 0;
}
d.utterance += '\n';
},
_elementStartAnnouncement: function(d, el) {
var tag = el.tagName.toLowerCase();
if (tag == 'table') {
d.utterance += '\nBegin table. ';
} else if (tag == 'tr') {
d.utterance += '\nBegin table row. ';
} else if (tag == 'td') {
d.utterance += '\nTable cell. ';
} else if (tag == 'ol') {
d.utterance += '\nBegin ordered list. ';
} else if (tag == 'ul') {
d.utterance += '\nBegin list. ';
} else if (tag == 'li') {
d.utterance += '\nList item. ';
}
},
_elementEndAnnouncement: function(d, el) {
var tag = el.tagName.toLowerCase();
if (tag == 'table') {
d.utterance += '\nEnd table. ';
} else if (tag == 'tr') {
d.utterance += '\nEnd table row. ';
} else if (tag == 'ol') {
d.utterance += '\nEnd ordered list. ';
} else if (tag == 'ul') {
d.utterance += '\nEnd list. ';
}
},
};
BeneSpeak.SpeechData.prototype.clearWordHighlight = function() {
while (this._wordRects.length > 0) {
this.document.body.removeChild(this._wordRects.pop());
}
this._highlightedWord = -1;
};
BeneSpeak.SpeechData.prototype.clearSentenceHighlight = function() {
while (this._sentenceRects.length > 0) {
this.document.body.removeChild(this._sentenceRects.pop());
}
this._highlightedSentence = -1;
};
BeneSpeak.SpeechData.prototype.handleTtsEvent = function(event, callback) {
if (event.type == 'word') {
var wordIndex = this.wordAt(event.charIndex);
if (wordIndex >= 0) {
this.highlightWord(wordIndex);
}
var sentenceIndex = this.sentenceAt(event.charIndex);
if (sentenceIndex >= 0) {
this.highlightSentence(sentenceIndex);
}
} else if (event.type == 'interrupted' || event.type == 'end') {
this.clearWordHighlight();
this.clearSentenceHighlight();
if (callback != null) {
callback();
}
}
};
BeneSpeak.SpeechData.prototype.highlightWord = function(idx) {
if (this._highlightedWord != idx) {
this.clearWordHighlight();
this._highlightedWord = idx;
var rects = this.words[idx].range.getClientRects();
for (var i = 0; i < rects.length; i++) {
var div = this.document.createElement('div');
this.document.body.appendChild(div);
div.className = BeneSpeak.wordHighlightClass;
div.style.position = 'absolute';
div.style.top = (rects[i].top + window.scrollY + this.yOffset) + 'px';
div.style.left = (rects[i].left + window.scrollX + this.xOffset) + 'px';
div.style.width = rects[i].width + 'px';
div.style.height = rects[i].height + 'px';
this._wordRects.push(div);
}
}
};
BeneSpeak.SpeechData.prototype.highlightSentence = function(idx) {
if (this._highlightedSentence != idx) {
this.clearSentenceHighlight();
this._highlightedSentence = idx;
var rects = this.sentences[idx].range.getClientRects();
for (var i = 0; i < rects.length; i++) {
var div = this.document.createElement('div');
this.document.body.appendChild(div);
div.className = BeneSpeak.sentenceHighlightClass;
div.style.position = 'absolute';
div.style.top = (rects[i].top + window.scrollY + this.yOffset) + 'px';
div.style.left = (rects[i].left + window.scrollX + this.xOffset) + 'px';
div.style.width = rects[i].width + 'px';
div.style.height = rects[i].height + 'px';
this._sentenceRects.push(div);
}
}
};
BeneSpeak.SpeechData.prototype._getOffset = function(s) {
if (s.slice(-2) == 'px') {
return 0 - parseInt(s.substring(0, s.length - 2));
} else {
return 0;
}
}
// locates the index of the word at the given character offset.
// When startFromBeginning is false (the default) it searches from
// the 0 index, otherwise it starts at the currently-highlighted word.
BeneSpeak.SpeechData.prototype.wordAt = function (charIndex, startFromBeginning) {
var startIndex = (startFromBeginning) ? 0 : (this._highlightedWord == -1) ? 0 : this._highlightedWord;
for (var i = startIndex; i < this.words.length; i++) {
if (charIndex < this.words[i].offset) {
return i - 1;
} if (this.words[i].includes(charIndex)) {
return i;
}
}
return -1;
};
// locates the index of the sentence at the given character offset.
// When startFromBeginning is false (the default) it searches from
// the 0 index, otherwise it starts at the currently-highlighted sentence.
BeneSpeak.SpeechData.prototype.sentenceAt = function (charIndex, startFromBeginning) {
var startIndex = (startFromBeginning) ? 0 : (this._highlightedSentence == -1) ? 0 : this._highlightedSentence;
for (var i = startIndex; i < this.sentences.length; i++) {
if (charIndex < this.sentences[i].offset) {
return i - 1;
} else if (this.sentences[i].includes(charIndex)) {
return i;
}
}
return -1;
};
BeneSpeak.Fragment.prototype.includes = function (index) {
return ((index >= this.offset) && (index < (this.offset + this.text.length)));
};
BeneSpeak.Position.prototype.copy = function () {
var r = new BeneSpeak.Position(this.node, this.offset);
return r;
};