| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
function PushAudioNode(context, start_callback, end_callback, buffer_size) { |
| 21 |
this.context = context; |
| 22 |
this.start_callback = start_callback; |
| 23 |
this.end_callback = end_callback; |
| 24 |
this.buffer_size = buffer_size || 4096; |
| 25 |
this.samples_queue = []; |
| 26 |
this.scriptNode = context.createScriptProcessor(this.buffer_size, 1, 1); |
| 27 |
this.connected = false; |
| 28 |
this.sinks = []; |
| 29 |
this.startTime = 0; |
| 30 |
this.closed = false; |
| 31 |
this.track_callbacks = new Map(); |
| 32 |
} |
| 33 |
|
| 34 |
PushAudioNode.prototype.push = function(chunk) { |
| 35 |
if (this.closed) { |
| 36 |
throw 'Cannot push more chunks after node was closed'; |
| 37 |
} |
| 38 |
this.samples_queue.push(chunk); |
| 39 |
if (!this.connected) { |
| 40 |
if (!this.sinks.length) { |
| 41 |
throw 'No destination set for PushAudioNode'; |
| 42 |
} |
| 43 |
this._do_connect(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
PushAudioNode.prototype.close = function() { |
| 48 |
this.closed = true; |
| 49 |
} |
| 50 |
|
| 51 |
PushAudioNode.prototype.connect = function(dest) { |
| 52 |
this.sinks.push(dest); |
| 53 |
if (this.samples_queue.length) { |
| 54 |
this._do_connect(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
PushAudioNode.prototype._do_connect = function() { |
| 59 |
if (this.connected) return; |
| 60 |
this.connected = true; |
| 61 |
for (var dest of this.sinks) { |
| 62 |
this.scriptNode.connect(dest); |
| 63 |
} |
| 64 |
this.scriptNode.onaudioprocess = this.handleEvent.bind(this); |
| 65 |
} |
| 66 |
|
| 67 |
PushAudioNode.prototype.disconnect = function() { |
| 68 |
this.scriptNode.onaudioprocess = null; |
| 69 |
this.scriptNode.disconnect(); |
| 70 |
this.connected = false; |
| 71 |
} |
| 72 |
|
| 73 |
PushAudioNode.prototype.addTrackCallback = function(aTimestamp, aCallback) { |
| 74 |
var callbacks = this.track_callbacks.get(aTimestamp) || []; |
| 75 |
callbacks.push(aCallback); |
| 76 |
this.track_callbacks.set(aTimestamp, callbacks); |
| 77 |
} |
| 78 |
|
| 79 |
PushAudioNode.prototype.handleEvent = function(evt) { |
| 80 |
if (!this.startTime) { |
| 81 |
this.startTime = evt.playbackTime; |
| 82 |
if (this.start_callback) { |
| 83 |
this.start_callback(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
var currentTime = evt.playbackTime - this.startTime; |
| 88 |
var playbackDuration = this.scriptNode.bufferSize / this.context.sampleRate; |
| 89 |
for (var entry of this.track_callbacks) { |
| 90 |
var timestamp = entry[0]; |
| 91 |
var callbacks = entry[1]; |
| 92 |
if (timestamp < currentTime) { |
| 93 |
this.track_callbacks.delete(timestamp); |
| 94 |
} else if (timestamp < currentTime + playbackDuration) { |
| 95 |
for (var cb of callbacks) { |
| 96 |
cb(); |
| 97 |
} |
| 98 |
this.track_callbacks.delete(timestamp); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
var offset = 0; |
| 103 |
while (this.samples_queue.length && offset < evt.target.bufferSize) { |
| 104 |
var chunk = this.samples_queue[0]; |
| 105 |
var to_copy = chunk.subarray(0, evt.target.bufferSize - offset); |
| 106 |
if (evt.outputBuffer.copyToChannel) { |
| 107 |
evt.outputBuffer.copyToChannel(to_copy, 0, offset); |
| 108 |
} else { |
| 109 |
evt.outputBuffer.getChannelData(0).set(to_copy, offset); |
| 110 |
} |
| 111 |
offset += to_copy.length; |
| 112 |
chunk = chunk.subarray(to_copy.length); |
| 113 |
if (chunk.length) |
| 114 |
this.samples_queue[0] = chunk; |
| 115 |
else |
| 116 |
this.samples_queue.shift(); |
| 117 |
} |
| 118 |
|
| 119 |
if (!this.samples_queue.length && this.closed) { |
| 120 |
if (this.end_callback) { |
| 121 |
this.end_callback(evt.playbackTime - this.startTime); |
| 122 |
} |
| 123 |
this.disconnect(); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
var ctx = new (window.AudioContext || window.webkitAudioContext)(); |
| 132 |
var tts; |
| 133 |
var pusher; |
| 134 |
var pusher_buffer_size = 4096; |
| 135 |
var chunkID = 0; |
| 136 |
|
| 137 |
function stop() { |
| 138 |
console.log('Inside stop()'); |
| 139 |
if (pusher) { |
| 140 |
console.log(' Calling pusher.disconnect...'); |
| 141 |
pusher.disconnect(); |
| 142 |
console.log(' Calling pusher.disconnect... done'); |
| 143 |
pusher = null; |
| 144 |
} |
| 145 |
console.log('Leaving stop()'); |
| 146 |
} |
| 147 |
|
| 148 |
function speak() { |
| 149 |
console.log('Inside speak()'); |
| 150 |
|
| 151 |
if (ctx.state === 'suspended') { |
| 152 |
console.log('Resuming AudioContext...'); |
| 153 |
ctx.resume(); |
| 154 |
console.log('Resuming AudioContext... done'); |
| 155 |
} |
| 156 |
console.log(' Stopping...'); |
| 157 |
stop(); |
| 158 |
console.log(' Stopping... done'); |
| 159 |
|
| 160 |
console.log(' Setting rate...'); |
| 161 |
tts.set_rate(Number(document.getElementById('rate').value)); |
| 162 |
console.log(' Setting rate... done'); |
| 163 |
console.log(' Setting pitch...'); |
| 164 |
tts.set_pitch(Number(document.getElementById('pitch').value)); |
| 165 |
console.log(' Setting pitch... done'); |
| 166 |
console.log(' Setting voice...'); |
| 167 |
tts.set_voice(document.getElementById('voice').value); |
| 168 |
console.log(' Setting voice... done'); |
| 169 |
|
| 170 |
var now = Date.now(); |
| 171 |
chunkID = 0; |
| 172 |
|
| 173 |
console.log(' Creating pusher...'); |
| 174 |
pusher = new PushAudioNode( |
| 175 |
ctx, |
| 176 |
function() { |
| 177 |
|
| 178 |
}, |
| 179 |
function() { |
| 180 |
|
| 181 |
}, |
| 182 |
pusher_buffer_size |
| 183 |
); |
| 184 |
pusher.connect(ctx.destination); |
| 185 |
console.log(' Creating pusher... done'); |
| 186 |
|
| 187 |
var user_text = document.getElementById('texttospeak').value; |
| 188 |
|
| 189 |
|
| 190 |
console.log(' Calling synthesize...'); |
| 191 |
tts.synthesize( |
| 192 |
user_text, |
| 193 |
function cb(samples, events) { |
| 194 |
console.log(' Receiving synthesis samples...'); |
| 195 |
if (!samples) { |
| 196 |
if (pusher) { |
| 197 |
pusher.close(); |
| 198 |
} |
| 199 |
return; |
| 200 |
} |
| 201 |
if (pusher) { |
| 202 |
|
| 203 |
pusher.push(new Float32Array(samples)); |
| 204 |
++chunkID; |
| 205 |
} |
| 206 |
if (now) { |
| 207 |
|
| 208 |
now = 0; |
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
); |
| 213 |
console.log(' Calling synthesize... done'); |
| 214 |
console.log('Leaving speak()'); |
| 215 |
} |
| 216 |
|
| 217 |
function ipa() { |
| 218 |
|
| 219 |
console.log("Synthesizing ipa ... "); |
| 220 |
var ts = new Date(); |
| 221 |
var user_text = document.getElementById('texttospeak').value; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
tts.set_voice(document.getElementById('voice').value); |
| 226 |
tts.synthesize_ipa(user_text, function(result) { |
| 227 |
var te = new Date(); |
| 228 |
document.getElementById('ipaarea').value = result.ipa; |
| 229 |
console.log("Ipa synthesis done in " + (te-ts) + " ms.") |
| 230 |
}); |
| 231 |
} |
| 232 |
|
| 233 |
function speakAndIpa() { |
| 234 |
speak(); |
| 235 |
ipa(); |
| 236 |
} |
| 237 |
|
| 238 |
function resetPitch() { |
| 239 |
document.getElementById('pitch').value = 50; |
| 240 |
} |
| 241 |
|
| 242 |
function resetRate() { |
| 243 |
document.getElementById('rate').value = 175; |
| 244 |
} |
| 245 |
|
| 246 |
function resetVoice() { |
| 247 |
document.getElementById('default-voice').selected = true; |
| 248 |
} |
| 249 |
|
| 250 |
function initializeDemo() { |
| 251 |
console.log('Creating eSpeakNG instance...'); |
| 252 |
tts = new eSpeakNG( |
| 253 |
'js/espeakng.worker.js', |
| 254 |
function cb1() { |
| 255 |
console.log('Inside cb1'); |
| 256 |
tts.list_voices( |
| 257 |
function cb2(result) { |
| 258 |
console.log('Inside cb2'); |
| 259 |
var sel = document.getElementById('voice'); |
| 260 |
var index = 0; |
| 261 |
for (voice of result) { |
| 262 |
var opt = document.createElement('option'); |
| 263 |
var languages = voice.languages.map(function(lang) { |
| 264 |
return lang.name; |
| 265 |
}).join(", "); |
| 266 |
opt.text = voice.name + ' (' + languages + ')'; |
| 267 |
opt.value = voice.identifier; |
| 268 |
console.log('Adding voice: ' + opt.text); |
| 269 |
sel.add(opt); |
| 270 |
if (voice.name === 'English (Great Britain)') { |
| 271 |
opt.id = 'default-voice'; |
| 272 |
opt.selected = true; |
| 273 |
} |
| 274 |
} |
| 275 |
console.log('Leaving cb2'); |
| 276 |
} |
| 277 |
); |
| 278 |
console.log('Removing loading class...'); |
| 279 |
document.body.classList.remove('loading'); |
| 280 |
console.log('Removing loading class... done'); |
| 281 |
console.log('Leaving cb1'); |
| 282 |
} |
| 283 |
); |
| 284 |
|
| 285 |
console.log('Creating eSpeakNG instance... done'); |
| 286 |
} |