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.

espeakng_glue.cpp 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <emscripten.h>
  20. #include "speak_lib.h"
  21. static int gSamplerate = 0;
  22. class eSpeakNGWorker {
  23. public:
  24. eSpeakNGWorker() : rate(espeakRATE_NORMAL), pitch(50), current_voice(NULL) {
  25. if (!gSamplerate) {
  26. gSamplerate = espeak_Initialize(
  27. AUDIO_OUTPUT_SYNCHRONOUS, 100, NULL, espeakINITIALIZE_DONT_EXIT);
  28. }
  29. samplerate = gSamplerate;
  30. voices = espeak_ListVoices(NULL);
  31. }
  32. void synth_(const char* aText, void* aCallback) {
  33. t_espeak_callback* cb = reinterpret_cast<t_espeak_callback*>(aCallback);
  34. espeak_SetSynthCallback(cb);
  35. espeak_SetParameter(espeakPITCH, pitch, 0);
  36. espeak_SetParameter(espeakRATE, rate, 0);
  37. if (current_voice)
  38. espeak_SetVoiceByProperties(current_voice);
  39. else
  40. espeak_SetVoiceByName("default");
  41. espeak_Synth(aText, 0, 0, POS_CHARACTER, 0, 0, NULL, NULL);
  42. // Reset callback so other instances will work too.
  43. espeak_SetSynthCallback(NULL);
  44. }
  45. long set_voice(
  46. const char* aName,
  47. const char* aLang=NULL,
  48. unsigned char aGender=0,
  49. unsigned char aAge=0,
  50. unsigned char aVariant = 0
  51. ) {
  52. long result = 0;
  53. if (aLang || aGender || aAge || aVariant) {
  54. espeak_VOICE props = { 0 };
  55. props.name = aName;
  56. props.languages = aLang;
  57. props.gender = aGender;
  58. props.age = aAge;
  59. props.variant = aVariant;
  60. result = espeak_SetVoiceByProperties(&props);
  61. } else {
  62. result = espeak_SetVoiceByName(aName);
  63. }
  64. // This way we don't need to allocate the name/lang strings to the heap.
  65. // Instead, we store the actual global voice.
  66. current_voice = espeak_GetCurrentVoice();
  67. return result;
  68. }
  69. int getSizeOfEventStruct_() {
  70. return sizeof(espeak_EVENT);
  71. }
  72. const espeak_VOICE** voices;
  73. int samplerate;
  74. int rate;
  75. int pitch;
  76. private:
  77. espeak_VOICE* current_voice;
  78. };
  79. #include <glue.cpp>