| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
function eSpeakNG(worker_path, ready_cb) { |
| 19 |
this.worker = new Worker(worker_path); |
| 20 |
this.ready = false; |
| 21 |
this.worker.onmessage = function(e) { |
| 22 |
if (e.data !== 'ready') { |
| 23 |
return; |
| 24 |
} |
| 25 |
this.worker.onmessage = null; |
| 26 |
this.worker.addEventListener('message', this); |
| 27 |
this.ready = true; |
| 28 |
if (ready_cb) { |
| 29 |
ready_cb(); |
| 30 |
} |
| 31 |
}.bind(this); |
| 32 |
} |
| 33 |
|
| 34 |
eSpeakNG.prototype.handleEvent = function (evt) { |
| 35 |
var callback = evt.data.callback; |
| 36 |
if (callback && this[callback]) { |
| 37 |
this[callback].apply(this, evt.data.result); |
| 38 |
if (evt.data.done) { |
| 39 |
delete this[callback]; |
| 40 |
} |
| 41 |
return; |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
function _createAsyncMethod(method) { |
| 46 |
return function() { |
| 47 |
var lastArg = arguments[arguments.length - 1]; |
| 48 |
var message = { method: method, args: Array.prototype.slice.call(arguments, 0) }; |
| 49 |
if (typeof lastArg == 'function') { |
| 50 |
var callback = '_' + method + '_' + Math.random().toString().substring(2) +'_cb'; |
| 51 |
this[callback] = lastArg; |
| 52 |
message.args.pop(); |
| 53 |
message.callback = callback; |
| 54 |
} |
| 55 |
this.worker.postMessage(message); |
| 56 |
}; |
| 57 |
} |
| 58 |
|
| 59 |
for (var method of [ |
| 60 |
'list_voices', |
| 61 |
'get_rate', |
| 62 |
'get_pitch', |
| 63 |
'set_rate', |
| 64 |
'set_pitch', |
| 65 |
'set_voice', |
| 66 |
'synthesize', |
| 67 |
'synthesize_ipa' |
| 68 |
]) { |
| 69 |
eSpeakNG.prototype[method] = _createAsyncMethod(method); |
| 70 |
} |