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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #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. void set_voice(
  46. const char* aName,
  47. const char* aLang,
  48. unsigned char aGender=0,
  49. unsigned char aAge=0,
  50. unsigned char aVariant = 0
  51. ) {
  52. espeak_VOICE props = { 0 };
  53. props.name = aName;
  54. props.languages = aLang;
  55. props.gender = aGender;
  56. props.age = aAge;
  57. props.variant = aVariant;
  58. // This way we don't need to allocate the name/lang strings to the heap.
  59. // Instead, we store the actual global voice.
  60. espeak_SetVoiceByProperties(&props);
  61. current_voice = espeak_GetCurrentVoice();
  62. }
  63. int getSizeOfEventStruct_() {
  64. return sizeof(espeak_EVENT);
  65. }
  66. const espeak_VOICE** voices;
  67. int samplerate;
  68. int rate;
  69. int pitch;
  70. private:
  71. espeak_VOICE* current_voice;
  72. };
  73. #include <glue.cpp>