| 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 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <emscripten.h> |
| 21 | #include "speak_lib.h" |
| 22 | |
| 23 | static int gSamplerate = 0; |
| 24 | |
| 25 | class eSpeakNGWorker { |
| 26 | public: |
| 27 | eSpeakNGWorker() : rate(espeakRATE_NORMAL), pitch(50), current_voice(NULL) { |
| 28 | if (!gSamplerate) { |
| 29 | gSamplerate = espeak_Initialize( |
| 30 | AUDIO_OUTPUT_SYNCHRONOUS, 100, NULL, espeakINITIALIZE_DONT_EXIT); |
| 31 | } |
| 32 | samplerate = gSamplerate; |
| 33 | voices = espeak_ListVoices(NULL); |
| 34 | } |
| 35 | |
| 36 | void synth_(const char* aText, void* aCallback) { |
| 37 | t_espeak_callback* cb = reinterpret_cast<t_espeak_callback*>(aCallback); |
| 38 | espeak_SetSynthCallback(cb); |
| 39 | espeak_SetParameter(espeakPITCH, pitch, 0); |
| 40 | espeak_SetParameter(espeakRATE, rate, 0); |
| 41 | |
| 42 | if (current_voice) |
| 43 | espeak_SetVoiceByProperties(current_voice); |
| 44 | else |
| 45 | espeak_SetVoiceByName("default"); |
| 46 | |
| 47 | espeak_Synth(aText, 0, 0, POS_CHARACTER, 0, 0, NULL, NULL); |
| 48 | |
| 49 | // Reset callback so other instances will work too. |
| 50 | espeak_SetSynthCallback(NULL); |
| 51 | } |
| 52 | |
| 53 | int synth_ipa_(const char* aText, const char* virtualFileName) { |
| 54 | |
| 55 | /* phoneme_mode |
| 56 | bit 1: 0=eSpeak's ascii phoneme names, 1= International Phonetic Alphabet (as UTF-8 characters). |
| 57 | bit 7: use (bits 8-23) as a tie within multi-letter phonemes names |
| 58 | bits 8-23: separator character, between phoneme names |
| 59 | */ |
| 60 | |
| 61 | espeak_SetSynthCallback(NULL); |
| 62 | |
| 63 | int phoneme_options = (1 << 1); // Use IPA |
| 64 | int use_custom_phoneme_separator = (0 << 7); |
| 65 | int phonemes_separator = ' '; // Use a default value |
| 66 | |
| 67 | int phoneme_conf = phoneme_options | (phonemes_separator << 8); |
| 68 | |
| 69 | FILE* f_phonemes_out = fopen(virtualFileName,"wb"); |
| 70 | if(!f_phonemes_out) |
| 71 | return -1; |
| 72 | |
| 73 | //espeak_ng_InitializeOutput(ENOUTPUT_MODE_SYNCHRONOUS, 0, NULL); |
| 74 | espeak_SetPhonemeTrace(phoneme_conf, f_phonemes_out); |
| 75 | espeak_Synth(aText, 0, 0, POS_CHARACTER, 0, 0, NULL, NULL); |
| 76 | espeak_SetPhonemeTrace(0, NULL); |
| 77 | fclose(f_phonemes_out); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | long set_voice( |
| 83 | const char* aName, |
| 84 | const char* aLang=NULL, |
| 85 | unsigned char aGender=0, |
| 86 | unsigned char aAge=0, |
| 87 | unsigned char aVariant = 0 |
| 88 | ) { |
| 89 | long result = 0; |
| 90 | if (aLang || aGender || aAge || aVariant) { |
| 91 | espeak_VOICE props = { 0 }; |
| 92 | props.name = aName; |
| 93 | props.languages = aLang; |
| 94 | props.gender = aGender; |
| 95 | props.age = aAge; |
| 96 | props.variant = aVariant; |
| 97 | result = espeak_SetVoiceByProperties(&props); |
| 98 | } else { |
| 99 | result = espeak_SetVoiceByName(aName); |
| 100 | } |
| 101 | |
| 102 | // This way we don't need to allocate the name/lang strings to the heap. |
| 103 | // Instead, we store the actual global voice. |
| 104 | current_voice = espeak_GetCurrentVoice(); |
| 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | int getSizeOfEventStruct_() { |
| 110 | return sizeof(espeak_EVENT); |
| 111 | } |
| 112 | |
| 113 | const espeak_VOICE** voices; |
| 114 | int samplerate; |
| 115 | int rate; |
| 116 | int pitch; |
| 117 | |
| 118 | private: |
| 119 | espeak_VOICE* current_voice; |
| 120 | }; |
| 121 | |
| 122 | #include <glue.cpp> |
| 123 |