12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
-
-
- function eSpeakNG(worker_path, ready_cb) {
- this.worker = new Worker(worker_path);
- this.ready = false;
- this.worker.onmessage = function(e) {
- if (e.data !== 'ready') {
- return;
- }
- this.worker.onmessage = null;
- this.worker.addEventListener('message', this);
- this.ready = true;
- if (ready_cb) {
- ready_cb();
- }
- }.bind(this);
- }
-
- eSpeakNG.prototype.handleEvent = function (evt) {
- var callback = evt.data.callback;
- if (callback && this[callback]) {
- this[callback].apply(this, evt.data.result);
- if (evt.data.done) {
- delete this[callback];
- }
- return;
- }
- };
-
- function _createAsyncMethod(method) {
- return function() {
- var lastArg = arguments[arguments.length - 1];
- var message = { method: method, args: Array.prototype.slice.call(arguments, 0) };
- if (typeof lastArg == 'function') {
- var callback = '_' + method + '_' + Math.random().toString().substring(2) +'_cb';
- this[callback] = lastArg;
- message.args.pop();
- message.callback = callback;
- }
- this.worker.postMessage(message);
- };
- }
-
- for (var method of [
- 'list_voices',
- 'get_rate',
- 'get_pitch',
- 'set_rate',
- 'set_pitch',
- 'set_voice',
- 'synthesize',
- 'synthesize_ipa'
- ]) {
- eSpeakNG.prototype[method] = _createAsyncMethod(method);
- }
|