Repository navigation
Expand file tree
/
Copy pathtest.html
More file actions
177 lines (161 loc) · 6.64 KB
/
Copy pathtest.html
File metadata and controls
177 lines (161 loc) · 6.64 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<title>Test TTSReader Engine</title>
<style>
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<input type="text" placeholder="server token" id="tokenInput"/>
<br/>
<select id="voice_selector"></select>
<div>
<button id="toggle_speech_btn" onclick="toggleSpeak()">Speak</button>
</div>
<br/><br/>
<ul id="voicesList"></ul>
<main>
<p>1. Some text to read. </p>
<p>2. Hello!</p>
<p>3. Ronen. </p>
<p>3.1 some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. some very long text to test google termination bug. </p>
<p>4. Some more text to read. </p>
<p>5. Some text to read. </p>
<p>6. Some text to read. </p>
<p>7. Some text to read. </p>
<p>8. Some text to read. </p>
<p>9. Some text to read. </p>
<p>10. Some text to read. </p>
</main>
<!--<script src="https://unpkg.com/ttsreader@1.0.27/build/bundle.min.js"></script>-->
<script src="build/bundle.js"></script>
<script>
function populateVoices(voices) {
console.log('init tts with voices: ', voices);
let el = document.querySelector('#voice_selector');
if (el) {
for (const voice of voices) {
let option = document.createElement("option");
option.innerHTML = voice.name + ", " + voice.lang + ", " + voice.voiceURI;
option.value = voice.voiceURI;
el.append(option);
}
}
document.getElementById('voice_selector').value = tts.getVoiceURI();
}
function onStart (ev) {
console.log('tts started, at: ', new Date());
updateUiByState();
}
function onEnd(ev) {
console.log('tts done, with ev = ', ev);
if (shouldSpeak) {
caretProgress();
if (currentEl) {
let txt = currentEl.innerText;
let bufferArray = [];
let savedCurrentEl = currentEl;
// We'll populate the next 5 elements in the buffer, then we'll set currentElement to the current one.
for (let i = 0; i < 5; i++) {
caretProgress();
if (currentEl) {
bufferArray.push(currentEl.innerText);
}
}
currentEl = savedCurrentEl;
speakOutText(txt, bufferArray);
} else {
speak(); // ie start from beginning
}
}
}
function speakOutText(txt, bufferArray) {
// Previously: tts.speakOut(txt);
// New - try the server approach:
let bufferUtts = [];
for (let i = 0; i < bufferArray.length; i++) {
bufferUtts.push({text: bufferArray[i], voiceURI: document.getElementById("voice_selector").value, rate: 1.0});
}
tts.speakAndBuffer({text: txt, voiceURI: document.getElementById("voice_selector").value, rate: 1.0},
bufferUtts,
document.querySelector("#tokenInput").value);
}
let tts = wsGlobals.TtsEngine;
let shouldSpeak = false;
let currentEl;
//console.log(tts);
tts.init({
onInit: (voices) => {
populateVoices(voices);
},
onStart: () => {
onStart();
},
onDone: () => {
onEnd();
}
}, true);
function speak() {
shouldSpeak = true;
if (!currentEl) {
caretProgress();
}
tts.setVoiceByUri(document.getElementById("voice_selector").value);
let txt = currentEl.innerText;
let bufferArray = [];
let savedCurrentEl = currentEl;
// We'll populate the next 5 elements in the buffer, then we'll set currentElement to the current one.
for (let i = 0; i < 5; i++) {
caretProgress();
if (currentEl) {
bufferArray.push(currentEl.innerText);
}
}
currentEl = savedCurrentEl;
speakOutText(txt, bufferArray);
document.getElementById('toggle_speech_btn').innerHTML = "Stop";
}
function stop() {
shouldSpeak = false;
tts.stop();
updateUiByState();
document.getElementById('toggle_speech_btn').innerHTML = "Speak";
}
function toggleSpeak() {
if (shouldSpeak) {
stop();
} else {
speak();
}
}
function caretProgress() {
if (!currentEl) {
currentEl = document.querySelector('main');
currentEl = currentEl.firstElementChild;
} else {
if (currentEl instanceof Element)
currentEl = currentEl.nextElementSibling;
}
}
function updateUiByState() {
if (shouldSpeak) {
if (currentEl) {
document.querySelectorAll("p").forEach((el) => {
if (el!=currentEl) {
el.classList.remove("highlight");
} else {
currentEl.classList.add("highlight");
}
});
currentEl.scrollIntoView();
}
}
}
</script>
</body>
</html>