post.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. eSpeakNGWorker.prototype.list_voices = function() {
  18. var voices = [];
  19. var i;
  20. for (var voice = this.get_voices(i = 0); voice.ptr != 0; voice = this.get_voices(++i)) {
  21. var v = {
  22. name: voice.get_name(),
  23. identifier: voice.get_identifier(),
  24. languages: [],
  25. }
  26. var ii = 0;
  27. var byte = voice.get_languages(ii);
  28. function nullTerminatedString(offset) {
  29. var str = '';
  30. var index = offset;
  31. var b = voice.get_languages(index++);
  32. while (b != 0) {
  33. str += String.fromCharCode(b);
  34. b = voice.get_languages(index++);
  35. }
  36. return str;
  37. }
  38. while (byte != 0) {
  39. var lang = { priority: byte, name: nullTerminatedString(++ii) }
  40. v.languages.push(lang);
  41. ii += lang.name.length + 1;
  42. byte = voice.get_languages(ii);
  43. }
  44. voices.push(v);
  45. }
  46. return voices;
  47. };
  48. var eventTypes = [
  49. 'list_terminated',
  50. 'word',
  51. 'sentence',
  52. 'mark',
  53. 'play',
  54. 'end',
  55. 'msg_terminated',
  56. 'phoneme',
  57. 'samplerate'
  58. ]
  59. eSpeakNGWorker.prototype.synthesize = function (aText, aCallback) {
  60. var eventStructSize = this.getSizeOfEventStruct_();
  61. function cb(ptr, length, events_pointer) {
  62. var data = new Float32Array(length*2);
  63. for (var i = 0; i < length; i++) {
  64. data[i*2] = Math.max(-1, Math.min(1, getValue(ptr + i*2, 'i16') / 32768));
  65. data[i*2+1] = data[i*2];
  66. }
  67. var events = [];
  68. var ptr = events_pointer;
  69. for (ev = wrapPointer(ptr, espeak_EVENT);
  70. ev.get_type() != Module.espeakEVENT_LIST_TERMINATED;
  71. ev = wrapPointer((ptr += eventStructSize), espeak_EVENT)) {
  72. events.push({
  73. type: eventTypes[ev.get_type()],
  74. text_position: ev.get_text_position(),
  75. word_length: ev.get_length(),
  76. audio_position: ev.get_audio_position()
  77. });
  78. }
  79. return aCallback(data, events) ? 1 : 0;
  80. }
  81. var fp = Runtime.addFunction(cb);
  82. this.synth_(aText, fp);
  83. Runtime.removeFunction(fp);
  84. };
  85. eSpeakNGWorker.prototype.synthesize_ipa = function (aText, aCallback) {
  86. // Use a unique temp file for the worker. Avoid collisions, just in case.
  87. var ipaVirtualFileName = "espeak-ng-ipa-tmp-" + Math.random().toString().substring(2);
  88. var res = "";
  89. var code = this.synth_ipa_(aText, ipaVirtualFileName);
  90. if(code == 0)
  91. res = FS.readFile(ipaVirtualFileName, { encoding: 'utf8' })
  92. // Clean up the tmp file
  93. FS.unlink(ipaVirtualFileName);
  94. var ret = {
  95. code: code,
  96. ipa: res
  97. }
  98. return ret;
  99. };
  100. // Make this a worker
  101. if (typeof WorkerGlobalScope !== 'undefined') {
  102. var worker;
  103. Module.postRun.push(function () {
  104. worker = new eSpeakNGWorker();
  105. postMessage('ready');
  106. });
  107. onmessage = function(e) {
  108. if (!worker) {
  109. throw 'eSpeakNGWorker worker not initialized';
  110. }
  111. var args = e.data.args;
  112. var message = { callback: e.data.callback, done: true };
  113. if (e.data.method == 'synthesize') {
  114. args.push(function(samples, events) {
  115. postMessage(
  116. { callback: e.data.callback,
  117. result: [samples.buffer, events] }, [samples.buffer]);
  118. });
  119. }
  120. message.result = [worker[e.data.method].apply(worker, args)];
  121. if (e.data.callback)
  122. postMessage(message);
  123. }
  124. }