espeakng_glue.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. int synth_ipa_(const char* aText, const char* virtualFileName) {
  46. /* phoneme_mode
  47. bit 1: 0=eSpeak's ascii phoneme names, 1= International Phonetic Alphabet (as UTF-8 characters).
  48. bit 7: use (bits 8-23) as a tie within multi-letter phonemes names
  49. bits 8-23: separator character, between phoneme names
  50. */
  51. espeak_SetSynthCallback(NULL);
  52. int phoneme_options = (1 << 1); // Use IPA
  53. int use_custom_phoneme_separator = (0 << 7);
  54. int phonemes_separator = ' '; // Use a default value
  55. int phoneme_conf = phoneme_options | (phonemes_separator << 8);
  56. FILE* f_phonemes_out = fopen(virtualFileName,"wb");
  57. if(!f_phonemes_out)
  58. return -1;
  59. //espeak_ng_InitializeOutput(ENOUTPUT_MODE_SYNCHRONOUS, 0, NULL);
  60. espeak_SetPhonemeTrace(phoneme_conf, f_phonemes_out);
  61. espeak_Synth(aText, 0, 0, POS_CHARACTER, 0, 0, NULL, NULL);
  62. espeak_SetPhonemeTrace(0, NULL);
  63. fclose(f_phonemes_out);
  64. return 0;
  65. }
  66. long set_voice(
  67. const char* aName,
  68. const char* aLang=NULL,
  69. unsigned char aGender=0,
  70. unsigned char aAge=0,
  71. unsigned char aVariant = 0
  72. ) {
  73. long result = 0;
  74. if (aLang || aGender || aAge || aVariant) {
  75. espeak_VOICE props = { 0 };
  76. props.name = aName;
  77. props.languages = aLang;
  78. props.gender = aGender;
  79. props.age = aAge;
  80. props.variant = aVariant;
  81. result = espeak_SetVoiceByProperties(&props);
  82. } else {
  83. result = espeak_SetVoiceByName(aName);
  84. }
  85. // This way we don't need to allocate the name/lang strings to the heap.
  86. // Instead, we store the actual global voice.
  87. current_voice = espeak_GetCurrentVoice();
  88. return result;
  89. }
  90. int getSizeOfEventStruct_() {
  91. return sizeof(espeak_EVENT);
  92. }
  93. const espeak_VOICE** voices;
  94. int samplerate;
  95. int rate;
  96. int pitch;
  97. private:
  98. espeak_VOICE* current_voice;
  99. };
  100. #include <glue.cpp>