-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunkify.js
160 lines (142 loc) · 4.49 KB
/
chunkify.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
/**
* Chunkify
* Google Chrome Speech Synthesis Chunking Pattern
* Fixes inconsistencies with speaking long texts in speechUtterance objects
* Licensed under the MIT License
*
* Peter Woolley and Brett Zamir
*/
var totalTextUsed = 0;
var lastChunk = '';
var synth = window.speechSynthesis;
var rate = 1.0;
var voice = null;
var lang = 'en-US';
var multiLineRegex = /((?:[^.!?]+[:).!?]+\n?)),?/g;
var chunks = [];
var chunkIndex = 0;
var maxChunkIndex = 0;
var markInstance = null;
var synth = window.speechSynthesis;
var loadChunks = function(element) {
if(chunks.length > 0) return;
var markEle = document.querySelector(element);
var text = markEle.innerText;
var chunkArr = text.match(multiLineRegex);
if(chunkArr === null) {
console.log('no chunks found');
}
else {
chunks = chunkArr;
maxChunkIndex = chunks.length;
markInstance = new Mark(markEle);
console.log('chunks loaded');
}
window.dispatchEvent(new CustomEvent('loadedChunks', { detail: { count: maxChunkIndex } }));
//console.log(chunks);
}
var updateChunkIndex = function(index) {
chunkIndex = index;
// if(chunkIndex < 0) {
// chunkIndex = maxChunkIndex - 1;
// }else if(chunkIndex >= maxChunkIndex) {
// chunkIndex = 0;
// }
}
var speechUtteranceChunker = function (utt, settings, callback) {
if(!chunks || chunks.length === 0) {
alert('Error loading text. Please refresh the page and try again.');
return;
}
settings = settings || {};
var newUtt;
var txt = chunks[chunkIndex];
if (utt.voice && utt.voice.voiceURI === 'native') { // Not part of the spec, this prob doesn't work
newUtt = utt;
newUtt.text = txt;
newUtt.addEventListener('end', function () {
if (speechUtteranceChunker.cancel) {
speechUtteranceChunker.cancel = false;
}
if (callback !== undefined) {
callback();
}
});
}
else {
var currChunk = chunks[chunkIndex];
console.log(currChunk);
if (currChunk === undefined || currChunk === null || currChunk.length <= 2){
//call once all text has been spoken...
if (callback !== undefined) {
callback();
console.log('doing the callback because we are done');
}
return;
}
var chunk = currChunk;
lastChunk = chunk;
newUtt = new SpeechSynthesisUtterance(chunk);
newUtt.rate = rate;
newUtt.voice = voice || utt.voice;
newUtt.lang = lang || utt.lang;
var x;
for (x in utt) {
if (utt.hasOwnProperty(x) && x !== 'text') {
newUtt[x] = utt[x];
}
}
newUtt.addEventListener('end', function () {
chunkIndex++;
if (speechUtteranceChunker.cancel) {
speechUtteranceChunker.cancel = false;
return;
}
settings.offset = settings.offset || 0;
settings.offset += chunk.length - 1;
speechUtteranceChunker(utt, settings, callback);
});
}
//if (lastChunk.length > 2) {
if (settings.modifier) {
settings.modifier(newUtt);
}
window.dispatchEvent(new CustomEvent('beforeChunk', { detail: { utterance: newUtt } }));
onboundaryHandler(newUtt.text);
console.log(newUtt); //IMPORTANT!! Do not remove: Logging the object out fixes some onend firing issues.
//placing the speak invocation inside a callback fixes ordering and onend issues.
setTimeout(function () {
synth.speak(newUtt);
}, 0);
// }
};
function updateRate(iRate) {
rate = iRate;
}
function updateVoice(iVoice) {
voice = iVoice;
}
function updateLang(iLang) {
lang = iLang;
}
function onboundaryHandler(word) {
performMark(word);
};
function markCurrentIndex(){
performMark(chunks[chunkIndex]);
}
function performMark(words) {
// Remove previous marked elements and mark
// the new keyword inside the context
markInstance.unmark({
done: function () {
markInstance.mark(words.trim(), options);
var marks = document.getElementsByTagName('mark');
if (marks && marks.length > 0)
marks[0].scrollIntoView({ behavior: 'smooth', block: 'center' });
else if(chunkIndex === 0){
window.scrollTo(0, 0);
}
}
});
};