eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

post.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2014-2016 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. // Make this a worker
  86. if (typeof WorkerGlobalScope !== 'undefined') {
  87. var worker;
  88. Module.postRun.push(function () {
  89. worker = new eSpeakNGWorker();
  90. postMessage('ready');
  91. });
  92. onmessage = function(e) {
  93. if (!worker) {
  94. throw 'eSpeakNGWorker worker not initialized';
  95. }
  96. var args = e.data.args;
  97. var message = { callback: e.data.callback, done: true };
  98. if (e.data.method == 'synthesize') {
  99. args.push(function(samples, events) {
  100. postMessage(
  101. { callback: e.data.callback,
  102. result: [samples.buffer, events] }, [samples.buffer]);
  103. });
  104. }
  105. message.result = [worker[e.data.method].apply(worker, args)];
  106. if (e.data.callback)
  107. postMessage(message);
  108. }
  109. }