Debian Codegraph @

emscripten/post.js

raw symbols SCIP
1 /*
2 * Copyright (C) 2014-2017 Eitan Isaacson
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see: <http://www.gnu.org/licenses/>.
16 */
17
18 eSpeakNGWorker.prototype.list_voices = function() {
19 var voices = [];
20 var i;
21 for (var voice = this.get_voices(i = 0); voice.ptr != 0; voice = this.get_voices(++i)) {
22 var v = {
23 name: voice.get_name(),
24 identifier: voice.get_identifier(),
25 languages: [],
26 }
27
28 var ii = 0;
29 var byte = voice.get_languages(ii);
30
31 function nullTerminatedString(offset) {
32 var str = '';
33 var index = offset;
34 var b = voice.get_languages(index++);
35 while (b != 0) {
36 str += String.fromCharCode(b);
37 b = voice.get_languages(index++);
38 }
39
40 return str;
41 }
42
43 while (byte != 0) {
44 var lang = { priority: byte, name: nullTerminatedString(++ii) }
45 v.languages.push(lang);
46 ii += lang.name.length + 1;
47 byte = voice.get_languages(ii);
48 }
49
50 voices.push(v);
51
52 }
53 return voices;
54 };
55
56 var eventTypes = [
57 'list_terminated',
58 'word',
59 'sentence',
60 'mark',
61 'play',
62 'end',
63 'msg_terminated',
64 'phoneme',
65 'samplerate'
66 ]
67
68 eSpeakNGWorker.prototype.synthesize = function (aText, aCallback) {
69 var eventStructSize = this.getSizeOfEventStruct_();
70 function cb(ptr, length, events_pointer) {
71 var data = new Float32Array(length*2);
72 for (var i = 0; i < length; i++) {
73 data[i*2] = Math.max(-1, Math.min(1, getValue(ptr + i*2, 'i16') / 32768));
74 data[i*2+1] = data[i*2];
75 }
76 var events = [];
77 var ptr = events_pointer;
78 for (ev = wrapPointer(ptr, espeak_EVENT);
79 ev.get_type() != Module.espeakEVENT_LIST_TERMINATED;
80 ev = wrapPointer((ptr += eventStructSize), espeak_EVENT)) {
81 events.push({
82 type: eventTypes[ev.get_type()],
83 text_position: ev.get_text_position(),
84 word_length: ev.get_length(),
85 audio_position: ev.get_audio_position()
86 });
87 }
88 return aCallback(data, events) ? 1 : 0;
89 }
90
91 var fp = addFunction(cb);
92 this.synth_(aText, fp);
93 removeFunction(fp);
94 };
95
96 eSpeakNGWorker.prototype.synthesize_ipa = function (aText, aCallback) {
97
98 // Use a unique temp file for the worker. Avoid collisions, just in case.
99 var ipaVirtualFileName = "espeak-ng-ipa-tmp-" + Math.random().toString().substring(2);
100
101 var res = "";
102 var code = this.synth_ipa_(aText, ipaVirtualFileName);
103
104 if(code == 0)
105 res = FS.readFile(ipaVirtualFileName, { encoding: 'utf8' })
106
107 // Clean up the tmp file
108 FS.unlink(ipaVirtualFileName);
109
110 var ret = {
111 code: code,
112 ipa: res
113 }
114
115 return ret;
116 };
117
118 // Make this a worker
119
120 if (typeof WorkerGlobalScope !== 'undefined') {
121 var worker;
122
123 Module.postRun = Module.postRun || [];
124
125 Module.postRun.push(function () {
126 worker = new eSpeakNGWorker();
127 postMessage('ready');
128 });
129
130 onmessage = function(e) {
131
132 if (!worker) {
133 throw 'eSpeakNGWorker worker not initialized';
134 }
135 var args = e.data.args;
136 var message = { callback: e.data.callback, done: true };
137 if (e.data.method == 'synthesize') {
138 args.push(function(samples, events) {
139 postMessage(
140 { callback: e.data.callback,
141 result: [samples.buffer, events] }, [samples.buffer]);
142 });
143 }
144 message.result = [worker[e.data.method].apply(worker, args)];
145 if (e.data.callback)
146 postMessage(message);
147 }
148 }