| 1 | /* SpeechSynthesizer.cpp |
| 2 | * |
| 3 | * Copyright (C) 2011-2023 David Weenink, 2012,2013,2015-2026 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "espeak_ng.h" |
| 20 | #include "espeak_praat.h" // for theEspeakPraatFileInMemorySet |
| 21 | |
| 22 | #include "SpeechSynthesizer.h" |
| 23 | #include "Strings_extensions.h" |
| 24 | #include "speak_lib.h" |
| 25 | #include "synthdata.h" |
| 26 | #include "encoding.h" |
| 27 | #include "dictionary.h" |
| 28 | #include "translate.h" |
| 29 | #include "voice.h" |
| 30 | |
| 31 | #include "oo_DESTROY.h" |
| 32 | #include "SpeechSynthesizer_def.h" |
| 33 | #include "oo_COPY.h" |
| 34 | #include "SpeechSynthesizer_def.h" |
| 35 | #include "oo_EQUAL.h" |
| 36 | #include "SpeechSynthesizer_def.h" |
| 37 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 38 | #include "SpeechSynthesizer_def.h" |
| 39 | #include "oo_WRITE_TEXT.h" |
| 40 | #include "SpeechSynthesizer_def.h" |
| 41 | #include "oo_WRITE_BINARY.h" |
| 42 | #include "SpeechSynthesizer_def.h" |
| 43 | #include "oo_READ_TEXT.h" |
| 44 | #include "SpeechSynthesizer_def.h" |
| 45 | #include "oo_READ_BINARY.h" |
| 46 | #include "SpeechSynthesizer_def.h" |
| 47 | #include "oo_DESCRIPTION.h" |
| 48 | #include "SpeechSynthesizer_def.h" |
| 49 | |
| 50 | #include "UnicodeData.h" |
| 51 | |
| 52 | extern int option_phoneme_events; // BUG: external declaration outside header file (ppgb 20210307) |
| 53 | |
| 54 | static autostring8 ipa_to_kirshenbaum (conststring32 text) { |
| 55 | const struct ipaksymbol { |
| 56 | char32 ipa; |
| 57 | char32 kirshenbaum; |
| 58 | } ipaksymbols [] = { |
| 59 | {UNICODE_LATIN_SMALL_LETTER_A, U'a'}, // a |
| 60 | {UNICODE_LATIN_SMALL_LETTER_B, U'b'}, // b |
| 61 | {UNICODE_LATIN_SMALL_LETTER_C, U'c'}, // c |
| 62 | {UNICODE_LATIN_SMALL_LETTER_D, U'd'}, // d |
| 63 | {UNICODE_LATIN_SMALL_LETTER_E, U'e'}, // e |
| 64 | {UNICODE_LATIN_SMALL_LETTER_F, U'f'}, // f |
| 65 | {UNICODE_LATIN_SMALL_LETTER_G, U'g'}, // g |
| 66 | {UNICODE_LATIN_SMALL_LETTER_SCRIPT_G, U'g'}, // ɡ |
| 67 | {UNICODE_LATIN_SMALL_LETTER_H, U'h'}, // h |
| 68 | {UNICODE_LATIN_SMALL_LETTER_I, U'i'}, // i |
| 69 | {UNICODE_LATIN_SMALL_LETTER_J, U'j'}, // j |
| 70 | {UNICODE_LATIN_SMALL_LETTER_K, U'k'}, // k |
| 71 | {UNICODE_LATIN_SMALL_LETTER_L, U'l'}, // l |
| 72 | {UNICODE_LATIN_SMALL_LETTER_M, U'm'}, // m |
| 73 | {UNICODE_LATIN_SMALL_LETTER_N, U'n'}, // n |
| 74 | {UNICODE_LATIN_SMALL_LETTER_O, U'o'}, // o |
| 75 | {UNICODE_LATIN_SMALL_LETTER_P, U'p'}, // p |
| 76 | {UNICODE_LATIN_SMALL_LETTER_Q, U'q'}, // q |
| 77 | {UNICODE_LATIN_SMALL_LETTER_TURNED_R, U'r'}, // ɹ |
| 78 | {UNICODE_LATIN_SMALL_LETTER_S, U's'}, // s |
| 79 | {UNICODE_LATIN_SMALL_LETTER_T, U't'}, // t |
| 80 | {UNICODE_LATIN_SMALL_LETTER_U, U'u'}, // u |
| 81 | {UNICODE_LATIN_SMALL_LETTER_V, U'v'}, // v |
| 82 | {UNICODE_LATIN_SMALL_LETTER_W, U'w'}, // w |
| 83 | {UNICODE_LATIN_SMALL_LETTER_X, U'x'}, // x |
| 84 | {UNICODE_LATIN_SMALL_LETTER_Y, U'y'}, // y |
| 85 | {UNICODE_LATIN_SMALL_LETTER_Z, U'z'}, // z |
| 86 | {UNICODE_LATIN_SMALL_LETTER_ALPHA, U'A'}, // ɑ |
| 87 | {UNICODE_GREEK_SMALL_LETTER_BETA, U'B'}, // β |
| 88 | {UNICODE_LATIN_SMALL_LETTER_C_WITH_CEDILLA, U'C'}, // ç |
| 89 | {UNICODE_LATIN_SMALL_LETTER_ETH, U'D'}, // ð |
| 90 | {UNICODE_LATIN_SMALL_LETTER_OPEN_E, U'E'}, // ɛ |
| 91 | // F unused |
| 92 | {UNICODE_LATIN_LETTER_SMALL_CAPITAL_G, U'G'}, // ɢ |
| 93 | {UNICODE_LATIN_SMALL_LETTER_H_WITH_STROKE, U'H'}, // ħ |
| 94 | {UNICODE_LATIN_LETTER_SMALL_CAPITAL_I, U'I'}, // ɪ |
| 95 | {UNICODE_LATIN_SMALL_LETTER_IOTA, U'I'}, // ɩ |
| 96 | {UNICODE_LATIN_SMALL_LETTER_DOTLESS_J_WITH_STROKE, U'J'},// ɟ |
| 97 | // K unused |
| 98 | {UNICODE_LATIN_SMALL_LETTER_L_WITH_MIDDLE_TILDE, U'L'}, // ɫ |
| 99 | {UNICODE_LATIN_LETTER_SMALL_CAPITAL_L, U'L'}, // ʟ |
| 100 | {UNICODE_LATIN_SMALL_LETTER_L_WITH_BELT, U'L'}, // ɬ |
| 101 | {UNICODE_LATIN_SMALL_LETTER_M_WITH_HOOK, U'M'}, // ɱ |
| 102 | {UNICODE_LATIN_SMALL_LETTER_ENG, U'N'}, // ŋ |
| 103 | {UNICODE_LATIN_SMALL_LETTER_OPEN_O, U'O'}, // ɔ |
| 104 | {UNICODE_GREEK_CAPITAL_LETTER_PHI, U'P'}, // Φ |
| 105 | {UNICODE_LATIN_SMALL_LETTER_GAMMA, U'Q'}, // ɣ |
| 106 | {UNICODE_LATIN_SMALL_LETTER_SCHWA_WITH_HOOK, U'R'}, // ɚ |
| 107 | {UNICODE_LATIN_LETTER_SMALL_CAPITAL_R, U'R'}, // ʀ |
| 108 | {UNICODE_LATIN_SMALL_LETTER_ESH, U'S'}, // ʃ |
| 109 | {UNICODE_GREEK_SMALL_LETTER_THETA, U'T'}, // θ |
| 110 | {UNICODE_LATIN_SMALL_LETTER_UPSILON, U'U'}, // ʊ |
| 111 | {UNICODE_LATIN_SMALL_LETTER_CLOSED_OMEGA, U'U'}, // ɷ |
| 112 | {UNICODE_LATIN_SMALL_LETTER_TURNED_V, U'V'}, // ʌ |
| 113 | {UNICODE_LATIN_SMALL_LIGATURE_OE, U'W'}, // œ |
| 114 | {UNICODE_GREEK_SMALL_LETTER_CHI, U'X'}, // χ |
| 115 | {UNICODE_LATIN_SMALL_LETTER_O_WITH_STROKE, U'Y'}, // ø |
| 116 | {UNICODE_LATIN_SMALL_LETTER_EZH, U'Z'}, // ʒ |
| 117 | {UNICODE_LATIN_LETTER_GLOTTAL_STOP, U'?'}, // ʔ |
| 118 | {UNICODE_LATIN_SMALL_LETTER_SCHWA, U'@'}, // ə |
| 119 | {UNICODE_LATIN_SMALL_LETTER_AE, U'&'}, // æ |
| 120 | {UNICODE_LATIN_SMALL_LETTER_R_WITH_FISHHOOK, U'*'} // ɾ |
| 121 | }; |
| 122 | const struct ipaksymbol2 { |
| 123 | char32 ipa; |
| 124 | char32 kirshenbaum[3]; |
| 125 | } ipaksymbols2 [] = { |
| 126 | // alveoalars |
| 127 | {UNICODE_LATIN_LETTER_SMALL_CAPITAL_B, U"b"}, // ʙ unsupported: b<trl> |
| 128 | {UNICODE_LATIN_SMALL_LETTER_B_WITH_HOOK, U"b"}, // ɓ unsupported: b` |
| 129 | {UNICODE_LATIN_SMALL_LETTER_D_WITH_HOOK, U"d"}, // ɗ unsupported: d` |
| 130 | {UNICODE_LATIN_SMALL_LETTER_TURNED_T, U"t"}, // ʇ unsupported: t! |
| 131 | {UNICODE_LATIN_SMALL_LETTER_L_WITH_BELT, U"s"}, // ɬ unsupported: s<lat> |
| 132 | {UNICODE_LATIN_SMALL_LETTER_LEZH, U"z"}, // ɮ unsupported: z<lat> |
| 133 | {UNICODE_LATIN_LETTER_SMALL_CAPITAL_R, U"r"}, // ʀ unsupported: r<trl> |
| 134 | {UNICODE_LATIN_SMALL_LETTER_R_WITH_FISHHOOK, U"*"}, // ɾ |
| 135 | {UNICODE_LATIN_SMALL_LETTER_TURNED_R_WITH_LONG_LEG, U"*"},// ɺ unsupported: *<lat> |
| 136 | {UNICODE_LATIN_LETTER_STRETCHED_C, U"c"}, // ʗ unsupported: c! |
| 137 | {UNICODE_LATIN_LETTER_INVERTED_GLOTTAL_STOP, U"l"}, // ʖ unsupported: l! |
| 138 | /* {, U""}, // |
| 139 | {, U""}, // |
| 140 | {, U""}, // |
| 141 | {, U""}, // |
| 142 | {, U""}, // |
| 143 | {, U""}, // |
| 144 | */ |
| 145 | }; |
| 146 | autostring8 result; |
| 147 | return result; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | Thing_implement (SpeechSynthesizer, Daata, 1); |
| 152 | |
| 153 | void structSpeechSynthesizer :: v1_info () { |
| 154 | SpeechSynthesizer_Parent :: v1_info (); |
| 155 | MelderInfo_writeLine (U"Language: ", our d_languageName.get()); |
| 156 | MelderInfo_writeLine (U"Voice: ", our d_voiceName.get()); |
| 157 | MelderInfo_writeLine (U"Phoneme set: ", our d_phonemeSetName.get()); |
| 158 | MelderInfo_writeLine (U"Input text format: ", (our d_inputTextFormat == SpeechSynthesizer_INPUT_TEXTONLY ? U"text only" : |
| 159 | d_inputTextFormat == SpeechSynthesizer_INPUT_PHONEMESONLY ? U"phonemes only" : U"tagged text")); |
| 160 | MelderInfo_writeLine (U"Input phoneme coding: ", (our d_inputPhonemeCoding == SpeechSynthesizer_PHONEMECODINGS_KIRSHENBAUM ? U"Kirshenbaum" : U"???")); |
| 161 | MelderInfo_writeLine (U"Sampling frequency: ", our d_samplingFrequency, U" Hz"); |
| 162 | MelderInfo_writeLine (U"Word gap: ", our d_wordGap, U" s"); |
| 163 | MelderInfo_writeLine (U"Pitch multiplier: ", our d_pitchAdjustment, U" (0.5-2.0)"); |
| 164 | MelderInfo_writeLine (U"Pitch range multiplier: ", our d_pitchRange, U" (0.0-2.0)"); |
| 165 | MelderInfo_writeLine (U"Speaking rate: ", our d_wordsPerMinute, U" words per minute", |
| 166 | our d_estimateSpeechRate ? U" (but estimated from speech if possible)" : U" (fixed)"); |
| 167 | MelderInfo_writeLine (U"Output phoneme coding: ", |
| 168 | our d_inputPhonemeCoding == SpeechSynthesizer_PHONEMECODINGS_KIRSHENBAUM ? U"Kirshenbaum" : |
| 169 | our d_inputPhonemeCoding == SpeechSynthesizer_PHONEMECODINGS_IPA ? U"IPA" : U"???" |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | static int synthCallback (short *wav, int numsamples, espeak_EVENT *events) |
| 174 | { |
| 175 | char phoneme_name [9]; |
| 176 | if (wav == nullptr) |
| 177 | return 1; |
| 178 | /* |
| 179 | It is essential that the SpeechSynthesizer is identified here by the user_data, |
| 180 | because the espeakEVENT_LIST_TERMINATED event may still be accompanied by |
| 181 | a piece of audio data!! |
| 182 | */ |
| 183 | SpeechSynthesizer me = (SpeechSynthesizer) (events -> user_data); |
| 184 | while (events -> type != espeakEVENT_LIST_TERMINATED) { |
| 185 | if (events -> type == espeakEVENT_SAMPLERATE) { |
| 186 | my d_internalSamplingFrequency = events -> id.number; |
| 187 | } else { |
| 188 | /* |
| 189 | my events = Table "time type type-t t-pos length a-pos sample id uniq"; |
| 190 | 1 2 3 4 5 6 7 8 9 |
| 191 | */ |
| 192 | Table_appendRow (my d_events.get()); |
| 193 | const integer irow = my d_events -> rows.size; |
| 194 | const double time = events -> audio_position * 0.001; |
| 195 | Table_setNumericValue (my d_events.get(), irow, 1, time); |
| 196 | Table_setNumericValue (my d_events.get(), irow, 2, events -> type); |
| 197 | // Column 3 will be filled afterwards |
| 198 | Table_setNumericValue (my d_events.get(), irow, 4, events -> text_position); |
| 199 | Table_setNumericValue (my d_events.get(), irow, 5, events -> length); |
| 200 | Table_setNumericValue (my d_events.get(), irow, 6, events -> audio_position); |
| 201 | Table_setNumericValue (my d_events.get(), irow, 7, events -> sample); |
| 202 | if (events -> type == espeakEVENT_MARK || events -> type == espeakEVENT_PLAY) { |
| 203 | Table_setStringValue (my d_events.get(), irow, 8, Melder_peek8to32_u (events -> id.name)); |
| 204 | } else if (events -> type == espeakEVENT_PHONEME) { |
| 205 | memcpy (phoneme_name, events -> id.string, 8); |
| 206 | phoneme_name [8] = 0; // because id.string is not 0-terminated if 8 chars long |
| 207 | Table_setStringValue (my d_events.get(), irow, 8, Melder_peek8to32_u (phoneme_name)); |
| 208 | } else if (events -> type == espeakEVENT_SAMPLERATE ||1) { |
| 209 | Table_setNumericValue (my d_events.get(), irow, 8, events -> id.number); |
| 210 | } |
| 211 | Table_setNumericValue (my d_events.get(), irow, 9, events -> unique_identifier); |
| 212 | } |
| 213 | events++; |
| 214 | } |
| 215 | if (me) { |
| 216 | my d_wav. resize (my d_numberOfSamples + numsamples); |
| 217 | for (integer i = 1; i <= numsamples; i++) |
| 218 | my d_wav [my d_numberOfSamples + i] = wav [i - 1]; |
| 219 | my d_numberOfSamples = my d_wav.size; // maintain invariant, *after* copy of content |
| 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static conststring32 SpeechSynthesizer_getLanguageCode (SpeechSynthesizer me) { |
| 225 | try { |
| 226 | const integer irow = Table_searchColumn (theSpeechSynthesizerLanguagePropertiesTable, 1, my d_languageName.get()); |
| 227 | Melder_require (irow != 0, |
| 228 | U"Cannot find language \"", my d_languageName.get(), U"\"."); |
| 229 | return Table_getStringValue_a (theSpeechSynthesizerLanguagePropertiesTable, irow, 2); |
| 230 | } catch (MelderError) { |
| 231 | Melder_throw (me, U": Cannot find language code."); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static conststring32 SpeechSynthesizer_getPhonemeSetCode (SpeechSynthesizer me) { |
| 236 | try { |
| 237 | const integer irow = Table_searchColumn (theSpeechSynthesizerLanguagePropertiesTable, 1, my d_phonemeSetName.get()); |
| 238 | Melder_require (irow != 0, |
| 239 | U"Cannot find phoneme set \"", my d_phonemeSetName.get(), U"\"."); |
| 240 | return Table_getStringValue_a (theSpeechSynthesizerLanguagePropertiesTable, irow, 2); |
| 241 | } catch (MelderError) { |
| 242 | Melder_throw (me, U": Cannot find phoneme set code."); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static conststring32 SpeechSynthesizer_getVoiceCode (SpeechSynthesizer me) { |
| 247 | try { |
| 248 | const integer irow = Table_searchColumn (theSpeechSynthesizerVoicePropertiesTable, 1, my d_voiceName.get()); |
| 249 | Melder_require (irow != 0, |
| 250 | U": Cannot find voice \"", my d_voiceName.get(), U"\"."); |
| 251 | return Table_getStringValue_a (theSpeechSynthesizerVoicePropertiesTable, irow, 5); |
| 252 | } catch (MelderError) { |
| 253 | Melder_throw (me, U": Cannot find voice code."); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | autoSpeechSynthesizer SpeechSynthesizer_create (conststring32 languageName, conststring32 voiceName) { |
| 258 | try { |
| 259 | autoSpeechSynthesizer me = Thing_new (SpeechSynthesizer); |
| 260 | |
| 261 | my d_languageName = Melder_dup (languageName); |
| 262 | my d_voiceName = Melder_dup (voiceName); |
| 263 | my d_phonemeSetName = Melder_dup (languageName); |
| 264 | SpeechSynthesizer_checkAndRepairLanguageAndVoiceNames (me.get()); |
| 265 | |
| 266 | SpeechSynthesizer_setTextInputSettings (me.get(), SpeechSynthesizer_INPUT_TAGGEDTEXT, SpeechSynthesizer_PHONEMECODINGS_KIRSHENBAUM); |
| 267 | SpeechSynthesizer_setSpeechOutputSettings (me.get(), 44100.0, 0.01, 1.0, 1.0, 175.0, SpeechSynthesizer_PHONEMECODINGS_IPA); |
| 268 | SpeechSynthesizer_setEstimateSpeechRateFromSpeech (me.get(), true); |
| 269 | return me; |
| 270 | } catch (MelderError) { |
| 271 | Melder_throw (U"SpeechSynthesizer not created."); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void SpeechSynthesizer_setTextInputSettings (SpeechSynthesizer me, int inputTextFormat, int inputPhonemeCoding) { |
| 276 | my d_inputTextFormat = inputTextFormat; |
| 277 | my d_inputPhonemeCoding = inputPhonemeCoding; |
| 278 | } |
| 279 | |
| 280 | void SpeechSynthesizer_setEstimateSpeechRateFromSpeech (SpeechSynthesizer me, bool estimate) { |
| 281 | my d_estimateSpeechRate = estimate; |
| 282 | } |
| 283 | |
| 284 | void SpeechSynthesizer_setSpeechOutputSettings (SpeechSynthesizer me, double samplingFrequency, double wordGap, double pitchAdjustment, double pitchRange, double wordsPerMinute, int outputPhonemeCoding) { |
| 285 | my d_samplingFrequency = samplingFrequency; |
| 286 | my d_wordGap = wordGap; |
| 287 | my d_pitchAdjustment = Melder_clipped (0.5, pitchAdjustment, 2.0); |
| 288 | my d_pitchRange = Melder_clipped (0.0, pitchRange, 2.0); |
| 289 | if (wordsPerMinute <= 0.0) |
| 290 | wordsPerMinute = 175.0; // SMELL: looks like an arbitrary default |
| 291 | my d_wordsPerMinute = Melder_clipped (80.0, wordsPerMinute, 450.0); |
| 292 | my d_outputPhonemeCoding = outputPhonemeCoding; |
| 293 | } |
| 294 | |
| 295 | void SpeechSynthesizer_playText (SpeechSynthesizer me, conststring32 text) { |
| 296 | autoSound thee = SpeechSynthesizer_to_Sound (me, text, nullptr, nullptr); |
| 297 | Sound_play (thee.get(), nullptr, nullptr); |
| 298 | } |
| 299 | |
| 300 | static autoSound buffer_to_Sound (constINTVEC const& wav, double samplingFrequency) |
| 301 | { |
| 302 | try { |
| 303 | const double dx = 1.0 / samplingFrequency; |
| 304 | const double xmax = wav.size * dx; |
| 305 | autoSound thee = Sound_create (1, 0.0, xmax, wav.size, dx, dx / 2.0); |
| 306 | for (integer i = 1; i <= wav.size; i++) |
| 307 | thy z [1] [i] = wav [i] / 32768.0; |
| 308 | return thee; |
| 309 | } catch (MelderError) { |
| 310 | Melder_throw (U"Sound not created from synthesizer data."); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static void IntervalTier_addBoundaryUnsorted (IntervalTier me, integer iinterval, double time, conststring32 newLabel, bool isNewleftLabel) { |
| 315 | //TRACE |
| 316 | trace (U"size before: ", my intervals.size); |
| 317 | for (integer i = 1; i <= my intervals.size; i ++) |
| 318 | trace (U"phoneme ", i, U" ", my intervals.at [i] -> xmin, U" ", my intervals.at [i] -> xmax, |
| 319 | U" ", Melder_length (my intervals.at [i] -> text.get()), U" ", my intervals.at [i] -> text.get()); |
| 320 | |
| 321 | trace (U"isNewLeftLabel: ", isNewleftLabel); |
| 322 | trace (U"iinterval: ", iinterval); |
| 323 | Melder_require (time > my xmin && time < my xmax, |
| 324 | U"The time (", time, U" seconds) is not inside the domain of the interval tier (", my xmin, U" .. ", my xmax, U" seconds)."); |
| 325 | /* |
| 326 | Find interval to split |
| 327 | */ |
| 328 | if (iinterval <= 0) |
| 329 | iinterval = IntervalTier_timeToLowIndex (me, time); |
| 330 | /* |
| 331 | Modify end time of left label |
| 332 | */ |
| 333 | const TextInterval ti = my intervals.at [iinterval]; |
| 334 | trace (U"end time ", ti -> xmax, U" overridden by ", time); |
| 335 | ti -> xmax = time; |
| 336 | if (isNewleftLabel) { |
| 337 | trace (U"label <<", ti -> text.get(), U">> overridden by ", newLabel); |
| 338 | TextInterval_setText (ti, newLabel); |
| 339 | } |
| 340 | autoTextInterval ti_new = TextInterval_create (time, my xmax, (! isNewleftLabel ? newLabel : U"" )); |
| 341 | my intervals. addItem_unsorted_move (ti_new.move()); |
| 342 | |
| 343 | trace (U"size after: ", my intervals.size); |
| 344 | for (integer i = 1; i <= my intervals.size; i ++) |
| 345 | trace (U"phoneme ", i, U" ", my intervals.at [i] -> xmin, U" ", my intervals.at [i] -> xmax, |
| 346 | U" ", Melder_length (my intervals.at [i] -> text.get()), U" ", my intervals.at [i] -> text.get()); |
| 347 | } |
| 348 | |
| 349 | static void Table_setEventTypeString (Table me) { |
| 350 | try { |
| 351 | for (integer i = 1; i <= my rows.size; i ++) { |
| 352 | const int type = Table_getNumericValue_a (me, i, 2); |
| 353 | conststring32 label = U"0"; |
| 354 | if (type == espeakEVENT_WORD) |
| 355 | label = U"word"; |
| 356 | else if (type == espeakEVENT_SENTENCE) |
| 357 | label = U"sent"; |
| 358 | else if (type == espeakEVENT_MARK) |
| 359 | label = U"mark"; |
| 360 | else if (type == espeakEVENT_PLAY) |
| 361 | label = U"play"; |
| 362 | else if (type == espeakEVENT_END) |
| 363 | label = U"s-end"; |
| 364 | else if (type == espeakEVENT_MSG_TERMINATED) |
| 365 | label = U"msg_term"; |
| 366 | else if (type == espeakEVENT_PHONEME) |
| 367 | label = U"phoneme"; |
| 368 | Table_setStringValue (me, i, 3, label); |
| 369 | } |
| 370 | } catch (MelderError) { |
| 371 | Melder_throw (U"Event types not set."); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | static void MelderString_trimWhiteSpaceAtEnd (MelderString *me) { |
| 376 | while (my length > 1 && (my string [my length - 1] == U' ' || my string [my length - 1] == U'\t' |
| 377 | || my string [my length - 1] == U'\r' || my string [my length - 1] == U'\n')) |
| 378 | my string [-- my length] = U'\0'; |
| 379 | } |
| 380 | |
| 381 | static void IntervalTier_mergeSpecialIntervals (IntervalTier me) { |
| 382 | integer intervalIndex = my intervals.size; |
| 383 | TextInterval right = my intervals.at [intervalIndex]; |
| 384 | const integer labelLength_right = TextInterval_labelLength (right); |
| 385 | bool isEmptyInterval_right = labelLength_right == 0 || (labelLength_right == 1 && Melder_equ (right -> text.get(), U"\001")); |
| 386 | while (intervalIndex > 1) { |
| 387 | const TextInterval left = my intervals.at [intervalIndex - 1]; |
| 388 | const integer labelLength_left = TextInterval_labelLength (left); |
| 389 | const bool isEmptyInterval_left = labelLength_left == 0 || (labelLength_left == 1 && Melder_equ (left -> text.get(), U"\001")); |
| 390 | if (isEmptyInterval_right && isEmptyInterval_left) { |
| 391 | /* |
| 392 | Remove right interval and empty left interval |
| 393 | */ |
| 394 | left -> xmax = right -> xmax; |
| 395 | TextInterval_setText (left, U""); |
| 396 | my intervals. removeItem (intervalIndex); |
| 397 | } |
| 398 | right = left; |
| 399 | isEmptyInterval_right = isEmptyInterval_left; |
| 400 | intervalIndex --; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | #if 0 // BUG unused (ppgb 20210307) |
| 405 | /* insert boundary at time t and merge/delete intervals after this time */ |
| 406 | static void IntervalTier_insertBoundaryAndMergeIntervalsAfter (IntervalTier me, double t) { |
| 407 | if (t <= my xmin || t >= my xmax) |
| 408 | return; |
| 409 | |
| 410 | const integer intervalNumber = IntervalTier_timeToLowIndex (me, t); |
| 411 | while (my intervals.size > intervalNumber + 1) |
| 412 | my intervals. removeItem (my intervals.size); |
| 413 | |
| 414 | // there can be maximally one interval left to the right of intervalNumber |
| 415 | const TextInterval ti = my intervals.at [intervalNumber]; |
| 416 | if (ti -> xmin == t) { // if t happens to be on a boundary: remove the next interval if it exists |
| 417 | if (my intervals.size > intervalNumber) |
| 418 | my intervals. removeItem (my intervals .size); |
| 419 | ti -> xmax = my xmax; |
| 420 | TextInterval_setText (ti, U""); |
| 421 | } else { |
| 422 | ti -> xmax = t; |
| 423 | TextInterval last = my intervals.at [my intervals.size]; |
| 424 | last -> xmin = t; |
| 425 | last -> xmax = my xmax; |
| 426 | TextInterval_setText (last, U""); |
| 427 | } |
| 428 | } |
| 429 | #endif |
| 430 | |
| 431 | static bool almost_equal (double t1, double t2) { |
| 432 | // the "=" sign is essential for a difference of zero if t1 == 0 |
| 433 | return fabs (t1 - t2) <= 1e-12 * fabs (t1); |
| 434 | } |
| 435 | |
| 436 | static void IntervalTier_insertEmptyIntervalsFromOtherTier (IntervalTier to, IntervalTier from) { |
| 437 | for (integer iint = 1; iint <= from -> intervals.size; iint ++) { |
| 438 | const TextInterval tifrom = from -> intervals.at [iint]; |
| 439 | if (TextInterval_labelLength (tifrom) == 0) { // found empty interval |
| 440 | const double t_left = tifrom -> xmin, t_right = tifrom -> xmax; |
| 441 | integer intervalIndex_to = IntervalTier_timeToLowIndex (to, t_left); |
| 442 | if (intervalIndex_to > 0) { // insert to the right of intervalIndex_to |
| 443 | const TextInterval tito = to -> intervals.at [intervalIndex_to]; |
| 444 | if (! almost_equal (tito -> xmin, t_left)) { // not on the start boundary of the interval, it cannot be at xmax |
| 445 | autoTextInterval newInterval = TextInterval_create (t_left, tito -> xmax, U""); |
| 446 | tito -> xmax = t_left; |
| 447 | to -> intervals. addItem_move (newInterval.move()); |
| 448 | } |
| 449 | } |
| 450 | intervalIndex_to = IntervalTier_timeToHighIndex (to, t_right); |
| 451 | const TextInterval tito = to -> intervals.at [intervalIndex_to]; |
| 452 | if (intervalIndex_to > 0) { |
| 453 | if (! almost_equal (t_right, tito -> xmax)) { // insert to the left of intervalIndex_to |
| 454 | autoTextInterval newInterval = TextInterval_create (tito -> xmin, t_right, U""); |
| 455 | tito -> xmin = t_right; |
| 456 | to -> intervals. addItem_move (newInterval.move()); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | static void IntervalTier_removeVeryShortIntervals (IntervalTier me) { |
| 464 | integer iint = 1; |
| 465 | while (iint <= my intervals.size) { |
| 466 | const TextInterval ti = my intervals.at [iint]; |
| 467 | if (almost_equal (ti -> xmin, ti -> xmax)) |
| 468 | my intervals. removeItem (iint); |
| 469 | else |
| 470 | iint ++; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | static autoTextGrid Table_to_TextGrid (Table me, conststring32 text, double xmin, double xmax) { |
| 475 | //Table_createWithColumnNames (0, L"time type type-t t-pos length a-pos sample id uniq"); |
| 476 | //TRACE |
| 477 | if (Melder_isTracingLocally) |
| 478 | Table_list (me, false); |
| 479 | try { |
| 480 | const integer textLength = Melder_length (text); |
| 481 | const integer numberOfRows = my rows.size; |
| 482 | const integer timeColumnIndex = Table_columnNameToNumber_e (me, U"time"); |
| 483 | const integer typeColumnIndex = Table_columnNameToNumber_e (me, U"type"); |
| 484 | const integer tposColumnIndex = Table_columnNameToNumber_e (me, U"t-pos"); |
| 485 | const integer idColumnIndex = Table_columnNameToNumber_e (me, U"id"); |
| 486 | autoTextGrid thee = TextGrid_create (xmin, xmax, U"sentence clause word phoneme", U""); |
| 487 | |
| 488 | TextGrid_setIntervalText (thee.get(), 1, 1, text); |
| 489 | |
| 490 | integer p1c = 1, p1w = 1; |
| 491 | double time_phon_p = xmin; |
| 492 | bool wordEnd = false; |
| 493 | autoMelderString mark; |
| 494 | |
| 495 | const IntervalTier clauses = (IntervalTier) thy tiers->at [2]; |
| 496 | const IntervalTier words = (IntervalTier) thy tiers->at [3]; |
| 497 | const IntervalTier phonemes = (IntervalTier) thy tiers->at [4]; |
| 498 | for (integer irow = 1; irow <= numberOfRows; irow ++) { |
| 499 | /*const*/ double time = Table_getNumericValue_a (me, irow, timeColumnIndex); |
| 500 | const int type = Table_getNumericValue_a (me, irow, typeColumnIndex); |
| 501 | const integer pos = Table_getNumericValue_a (me, irow, tposColumnIndex); |
| 502 | integer length; |
| 503 | if (type == espeakEVENT_SENTENCE) { |
| 504 | /* |
| 505 | Insert only a new boundary, no text |
| 506 | (text will be inserted at an "end sentence" event). |
| 507 | */ |
| 508 | if (time > xmin && time < xmax) |
| 509 | IntervalTier_addBoundaryUnsorted (clauses, clauses -> intervals.size, time, U"", true); |
| 510 | p1c = pos; |
| 511 | } else if (type == espeakEVENT_END) { |
| 512 | /* |
| 513 | End of clause: insert new boundary, and fill left interval with text. |
| 514 | */ |
| 515 | length = pos - p1c + 1; |
| 516 | MelderString_ncopy (&mark, text + p1c - 1, length); |
| 517 | MelderString_trimWhiteSpaceAtEnd (& mark); |
| 518 | if (time > xmin && time < xmax) |
| 519 | IntervalTier_addBoundaryUnsorted (clauses, clauses -> intervals.size, time, mark.string, true); |
| 520 | else |
| 521 | TextGrid_setIntervalText (thee.get(), 2, clauses -> intervals.size, mark.string); |
| 522 | p1c = pos; |
| 523 | /* |
| 524 | End of clause always signals "end of a word". |
| 525 | */ |
| 526 | if (pos <= textLength) { |
| 527 | length = pos - p1w + 1; |
| 528 | MelderString_ncopy (&mark, text + p1w - 1, length); |
| 529 | MelderString_trimWhiteSpaceAtEnd (& mark); |
| 530 | if (time > xmin && time < xmax) |
| 531 | IntervalTier_addBoundaryUnsorted (words, words -> intervals.size, time, mark.string, true); |
| 532 | else |
| 533 | TextGrid_setIntervalText (thee.get(), 3, words -> intervals.size, mark.string); |
| 534 | /* |
| 535 | Now the next word event should not trigger setting the left interval text. |
| 536 | */ |
| 537 | wordEnd = false; |
| 538 | } |
| 539 | } else if (type == espeakEVENT_WORD) { |
| 540 | if (pos < p1w) |
| 541 | continue; |
| 542 | if (time > xmin && time < xmax) { |
| 543 | length = pos - p1w; |
| 544 | if (pos == textLength) |
| 545 | length++; |
| 546 | MelderString_ncopy (&mark, text + p1w - 1, length); |
| 547 | MelderString_trimWhiteSpaceAtEnd (& mark); |
| 548 | IntervalTier_addBoundaryUnsorted (words, words -> intervals.size, time, ( wordEnd ? mark.string : U"" ), true); |
| 549 | } |
| 550 | wordEnd = true; |
| 551 | p1w = pos; |
| 552 | } else if (type == espeakEVENT_PHONEME) { |
| 553 | const conststring32 phoneme = Table_getStringValue_a (me, irow, idColumnIndex); |
| 554 | //TRACE |
| 555 | trace (U"found in table the phoneme <<", phoneme, U">>, to be inserted at ", time, U" (usually after ", time_phon_p, U")"); |
| 556 | const TextInterval lastPhonemeInterval = phonemes -> intervals.at [phonemes -> intervals.size]; |
| 557 | if (false) { |
| 558 | if (time > time_phon_p) { |
| 559 | /* |
| 560 | Insert new boundary and label interval with the phoneme. |
| 561 | TODO: Translate the phoneme to the correct notation |
| 562 | */ |
| 563 | if (time > lastPhonemeInterval -> xmin && time < lastPhonemeInterval -> xmax) { |
| 564 | trace (U"add boundary (1) for phoneme <<", phoneme, U">> at ", time); |
| 565 | IntervalTier_addBoundaryUnsorted (phonemes, phonemes -> intervals.size, time, phoneme, false); |
| 566 | } |
| 567 | } else { |
| 568 | /* |
| 569 | Just in case the phoneme starts at xmin, we only need to set interval text. |
| 570 | */ |
| 571 | trace (U"overriding <<", lastPhonemeInterval -> text.get(), U">> with <<", phoneme, U">>"); |
| 572 | TextGrid_setIntervalText (thee.get(), 4, phonemes -> intervals.size, phoneme); |
| 573 | } |
| 574 | } else { |
| 575 | if (time > time_phon_p) { |
| 576 | /* |
| 577 | Insert new boundary and label interval with the phoneme. |
| 578 | TODO: Translate the phoneme to the correct notation |
| 579 | */ |
| 580 | if (time > lastPhonemeInterval -> xmin && time < lastPhonemeInterval -> xmax) { |
| 581 | trace (U"add boundary (1) for phoneme <<", phoneme, U">> at ", time); |
| 582 | IntervalTier_addBoundaryUnsorted (phonemes, phonemes -> intervals.size, time, phoneme, false); |
| 583 | } |
| 584 | } else if (! Melder_equ (phoneme, U"")) { |
| 585 | /* |
| 586 | Just in case the phoneme starts at xmin, we only need to set interval text. |
| 587 | */ |
| 588 | trace (U"overriding <<", lastPhonemeInterval -> text.get(), U">> with <<", phoneme, U">>"); |
| 589 | TextGrid_setIntervalText (thee.get(), 4, phonemes -> intervals.size, phoneme); |
| 590 | } else { |
| 591 | //TRACE |
| 592 | trace (U"stupid repair, houtjes-touwtjes"); |
| 593 | time = 0.001 * lastPhonemeInterval -> xmax + 0.999 * lastPhonemeInterval -> xmin; |
| 594 | trace (U"add boundary (2) for phoneme <<", phoneme, U">> at ", time); |
| 595 | IntervalTier_addBoundaryUnsorted (phonemes, phonemes -> intervals.size, time, phoneme, false); |
| 596 | } |
| 597 | } |
| 598 | time_phon_p = time; |
| 599 | } |
| 600 | } |
| 601 | clauses -> intervals. sort (); |
| 602 | words -> intervals. sort (); |
| 603 | phonemes -> intervals. sort (); |
| 604 | |
| 605 | IntervalTier_mergeSpecialIntervals (phonemes); // Merge neighbouring empty U"" and U"\001" intervals |
| 606 | |
| 607 | IntervalTier_removeVeryShortIntervals (words); |
| 608 | IntervalTier_removeVeryShortIntervals (clauses); |
| 609 | /* |
| 610 | Use empty intervals in phoneme tier for more precision in the word tier. |
| 611 | */ |
| 612 | IntervalTier_insertEmptyIntervalsFromOtherTier (words, phonemes); |
| 613 | IntervalTier_mergeSpecialIntervals (words); // Merge neighbouring empty U"" and U"\001" intervals |
| 614 | |
| 615 | return thee; |
| 616 | } catch (MelderError) { |
| 617 | Melder_throw (U"TextGrid not created from Table with events."); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | static void SpeechSynthesizer_generateSynthesisData (SpeechSynthesizer me, conststring32 text) { |
| 622 | try { |
| 623 | int synth_flags = 0; |
| 624 | espeak_ng_InitializePath (nullptr); // PATH_ESPEAK_DATA |
| 625 | espeak_ng_ERROR_CONTEXT context = { 0 }; |
| 626 | espeak_ng_STATUS status = espeak_ng_Initialize (& context); |
| 627 | Melder_require (status == ENS_OK, |
| 628 | U"Internal eSpeak error. ", status); |
| 629 | if (my d_inputTextFormat == SpeechSynthesizer_INPUT_TAGGEDTEXT) |
| 630 | synth_flags |= espeakSSML; |
| 631 | if (my d_inputTextFormat != SpeechSynthesizer_INPUT_TEXTONLY) |
| 632 | synth_flags |= espeakPHONEMES; |
| 633 | option_phoneme_events = espeakINITIALIZE_PHONEME_EVENTS; // extern int option_phoneme_events; |
| 634 | if (my d_outputPhonemeCoding == SpeechSynthesizer_PHONEMECODINGS_IPA) |
| 635 | option_phoneme_events |= espeakINITIALIZE_PHONEME_IPA; |
| 636 | |
| 637 | espeak_ng_SetParameter (espeakRATE, my d_wordsPerMinute, 0); |
| 638 | /* |
| 639 | pitchAdjustment_0_99 = a * log10 (my d_pitchAdjustment) + b, |
| 640 | where 0.5 <= my d_pitchAdjustment <= 2 |
| 641 | pitchRange_0_99 = my d_pitchRange * 49.5, |
| 642 | where 0 <= my d_pitchRange <= 2 |
| 643 | */ |
| 644 | const int pitchAdjustment_0_99 = (int) ((49.5 / NUMlog10_2) * log10 (my d_pitchAdjustment) + 49.5); // rounded towards zero |
| 645 | espeak_ng_SetParameter (espeakPITCH, pitchAdjustment_0_99, 0); |
| 646 | const int pitchRange_0_99 = (int) (my d_pitchRange * 49.5); // rounded towards zero |
| 647 | espeak_ng_SetParameter (espeakRANGE, pitchRange_0_99, 0); |
| 648 | const conststring32 languageCode = SpeechSynthesizer_getLanguageCode (me); |
| 649 | const conststring32 voiceCode = SpeechSynthesizer_getVoiceCode (me); |
| 650 | |
| 651 | espeak_ng_SetVoiceByName (Melder_peek32to8 (Melder_cat (languageCode, U"+", voiceCode))); |
| 652 | const int wordGap_10ms = my d_wordGap * 100; // eSpeak word gap is in units of 10 ms |
| 653 | espeak_ng_SetParameter (espeakWORDGAP, wordGap_10ms, 0); |
| 654 | espeak_ng_SetParameter (espeakCAPITALS, 0, 0); |
| 655 | espeak_ng_SetParameter (espeakPUNCTUATION, espeakPUNCT_NONE, 0); |
| 656 | |
| 657 | status = espeak_ng_InitializeOutput (ENOUTPUT_MODE_SYNCHRONOUS, 2048, nullptr); |
| 658 | espeak_SetSynthCallback (synthCallback); |
| 659 | if (! Melder_equ (my d_phonemeSetName.get(), my d_languageName.get())) { |
| 660 | const conststring32 phonemeSetCode = SpeechSynthesizer_getPhonemeSetCode (me); |
| 661 | const int index_phon_table_list = LookupPhonemeTable (Melder_peek32to8 (phonemeSetCode)); |
| 662 | if (index_phon_table_list > 0) { |
| 663 | voice -> phoneme_tab_ix = index_phon_table_list; |
| 664 | (void) DoVoiceChange(voice); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | const conststring32 columnNames [] = |
| 669 | { U"time", U"type", U"type-t", U"t-pos", U"length", U"a-pos", U"sample", U"id", U"uniq" }; |
| 670 | my d_events = Table_createWithColumnNames (0, ARRAY_TO_STRVEC (columnNames)); |
| 671 | unsigned int unique_identifier = 0; |
| 672 | #ifdef _WIN32 |
| 673 | conststringW textW = Melder_peek32toW (text); |
| 674 | synth_flags |= espeakCHARS_WCHAR; |
| 675 | espeak_ng_Synthesize (textW, wcslen (textW) + 1, 0, POS_CHARACTER, 0, synth_flags, & unique_identifier, me); |
| 676 | #else |
| 677 | conststring8 textUTF8 = Melder_peek32to8 (text); |
| 678 | synth_flags |= espeakCHARS_UTF8; |
| 679 | espeak_ng_Synthesize (textUTF8, Melder_length_utf8 (text, false) + 1, 0, POS_CHARACTER, 0, synth_flags, & unique_identifier, me); |
| 680 | #endif |
| 681 | |
| 682 | espeak_ng_Terminate (); |
| 683 | } catch (MelderError) { |
| 684 | espeak_Terminate (); |
| 685 | Melder_throw (U"SpeechSynthesizer: synthesis data not generated."); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | autostring32 SpeechSynthesizer_getPhonemesFromText (SpeechSynthesizer me, conststring32 text, bool separateBySpaces) { |
| 690 | try { |
| 691 | /* |
| 692 | Clear the wave buffer. |
| 693 | */ |
| 694 | my d_wav. resize (0); |
| 695 | my d_numberOfSamples = my d_wav.size; // maintain invariant |
| 696 | |
| 697 | SpeechSynthesizer_generateSynthesisData (me, text); |
| 698 | const double dt = 1.0 / my d_internalSamplingFrequency; |
| 699 | const double tmin = 0.0, tmax = my d_wav.size * dt; |
| 700 | double xmin = Table_getNumericValue_a (my d_events.get(), 1, 1); |
| 701 | Melder_clipLeft (tmin, & xmin); |
| 702 | double xmax = Table_getNumericValue_a (my d_events.get(), my d_events -> rows.size, 1); |
| 703 | Melder_clipRight (& xmax, tmax); |
| 704 | autoTextGrid tg = Table_to_TextGrid (my d_events.get(), text, xmin, xmax); |
| 705 | /* |
| 706 | Get phonemes from the phoneme tier |
| 707 | */ |
| 708 | Melder_assert (tg -> tiers->size == 4); |
| 709 | IntervalTier phonemeTier = static_cast<IntervalTier> (tg -> tiers -> at [4]); |
| 710 | const integer numberOfIntervals = phonemeTier -> intervals.size; |
| 711 | Melder_require (numberOfIntervals > 0, |
| 712 | U"There are no phonemes in the tier."); |
| 713 | autoMelderString phonemes; |
| 714 | const conststring32 phonemeSeparator = ( separateBySpaces ? U" " : U"" ); |
| 715 | const conststring32 wordSeparator = ( separateBySpaces ? U" " : U" " ); |
| 716 | for (integer iint = 1; iint <= numberOfIntervals; iint ++) { |
| 717 | TextInterval interval = phonemeTier -> intervals.at [iint]; |
| 718 | const conststring32 phonemeLabel = interval -> text.get(); |
| 719 | const bool isEmptyLabel = Melder_equ (phonemeLabel, U""); |
| 720 | if (isEmptyLabel) { |
| 721 | if (iint > 1 && iint < numberOfIntervals) |
| 722 | MelderString_append (& phonemes, wordSeparator); |
| 723 | } else |
| 724 | MelderString_append (& phonemes, phonemeLabel, (iint < numberOfIntervals ? phonemeSeparator : U"") ); |
| 725 | } |
| 726 | //my d_events. reset(); // ppgb: this was not in the code, but is this in fact superflous? |
| 727 | return Melder_dup (phonemes.string); // TODO: implement MelderString_move() |
| 728 | } catch (MelderError) { |
| 729 | Melder_throw (U"Phonemes not generated."); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | autoSound SpeechSynthesizer_to_Sound (SpeechSynthesizer me, conststring32 text, autoTextGrid *tg, autoTable *events) { |
| 734 | try { |
| 735 | /* |
| 736 | Clear the wave buffer. |
| 737 | */ |
| 738 | my d_wav. resize (0); |
| 739 | my d_numberOfSamples = my d_wav.size; // maintain invariant |
| 740 | |
| 741 | SpeechSynthesizer_generateSynthesisData (me, text); |
| 742 | autoSound thee = buffer_to_Sound (my d_wav.get(), my d_internalSamplingFrequency); |
| 743 | |
| 744 | if (my d_samplingFrequency != my d_internalSamplingFrequency) |
| 745 | thee = Sound_resample (thee.get(), my d_samplingFrequency, 50); |
| 746 | if (tg) { |
| 747 | const double xmin = Melder_clippedRight (Table_getNumericValue_a (my d_events.get(), 1, 1), thy xmin); // ! |
| 748 | const double xmax = Melder_clippedLeft (thy xmax, Table_getNumericValue_a (my d_events.get(), my d_events -> rows.size, 1)); // ! |
| 749 | autoTextGrid tg1 = Table_to_TextGrid (my d_events.get(), text, xmin, xmax); |
| 750 | *tg = TextGrid_extractPart (tg1.get(), thy xmin, thy xmax, 0); |
| 751 | } |
| 752 | if (events) { |
| 753 | Table_setEventTypeString (my d_events.get()); |
| 754 | *events = my d_events.move(); |
| 755 | } |
| 756 | my d_events. reset(); |
| 757 | return thee; |
| 758 | } catch (MelderError) { |
| 759 | espeak_Terminate (); |
| 760 | Melder_throw (U"SpeechSynthesizer: text not converted to Sound."); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | static conststring32 get_wordAfterPrecursor_u8 (constvector<unsigned char> const& text8, conststring32 precursor) { |
| 765 | static char32 word [100]; |
| 766 | /* |
| 767 | 1. Find (first occurrence of) 'precursor' at the start of a line (with optional leading whitespace). |
| 768 | 2. Get the words after 'precursor' (skip leading and trailing whitespace). |
| 769 | */ |
| 770 | autoMelderString regex; |
| 771 | const conststring32 text = Melder_peek8to32_u (reinterpret_cast <const char *> (text8.asArgumentToFunctionThatExpectsZeroBasedArray())); |
| 772 | MelderString_append (& regex, U"^\\s*", precursor, U"\\s+"); |
| 773 | char32 *p = nullptr; |
| 774 | const char32 *pmatch = strstr_regexp (text, regex.string); |
| 775 | if (pmatch) { |
| 776 | pmatch += Melder_length (precursor); // skip 'precursor' |
| 777 | while (*pmatch == U' ' || *pmatch == U'\t') |
| 778 | pmatch ++; // skip whitespace after 'precursor' |
| 779 | p = word; |
| 780 | char32 *p_end = p + 99; |
| 781 | while ((*p = *pmatch ++) && *p != U' ' && *p != U'\t' && *p != U'\n' && *p != U'\r' && p < p_end) |
| 782 | p ++; |
| 783 | *p = U'\0'; |
| 784 | p = word; |
| 785 | } |
| 786 | return p; |
| 787 | } |
| 788 | |
| 789 | static conststring32 get_stringAfterPrecursor_u8 (constvector<unsigned char> const& text8, conststring32 precursor) { |
| 790 | static char32 word [100]; |
| 791 | /* |
| 792 | 1. Find (first occurrence of) 'precursor' at the start of a line (with optional leading whitespace). |
| 793 | 2. Get the words after 'precursor' (skip leading and trailing whitespace). |
| 794 | */ |
| 795 | autoMelderString regex; |
| 796 | const conststring32 text = Melder_peek8to32_u (reinterpret_cast <const char *> (text8.asArgumentToFunctionThatExpectsZeroBasedArray())); |
| 797 | //const conststring32 text = Melder_peek8to32_u ((const char *) & (text8.cells[1])); |
| 798 | MelderString_append (& regex, U"^\\s*", precursor, U"\\s+"); |
| 799 | char32 *p = nullptr; |
| 800 | const char32 *pmatch = strstr_regexp (text, regex.string); |
| 801 | if (pmatch) { |
| 802 | pmatch += Melder_length (precursor); // skip 'precursor' |
| 803 | while (*pmatch == U' ' || *pmatch == U'\t') |
| 804 | pmatch ++; // skip whitespace after 'precursor' |
| 805 | //pmatch --; |
| 806 | p = word; |
| 807 | char32 *p_end = p + 99; |
| 808 | // also discard text after comment '//' |
| 809 | while ((*p = *pmatch ++) && *p != U'\n' && *p != U'\r' && *p != U'/' && *(p+1) != U'/' && p < p_end) |
| 810 | p ++; // copy to end of line |
| 811 | while (*p == U' ' || *p == U'\t' || *p == U'\n' || *p == U'\r') |
| 812 | p --; // remove trailing white space |
| 813 | *(++ p) = U'\0'; |
| 814 | p = word; |
| 815 | } |
| 816 | return p; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | Four global singleton-references. |
| 821 | The four singletons that they refer to contain data, |
| 822 | are to be initialized once during start-up (i.e. during some `praat_xxx_init()`), |
| 823 | and are kept throughout program execution. |
| 824 | */ |
| 825 | Table theSpeechSynthesizerVoicePropertiesTable; |
| 826 | Table theSpeechSynthesizerLanguagePropertiesTable; |
| 827 | STRVEC theSpeechSynthesizerLanguageNames; |
| 828 | STRVEC theSpeechSynthesizerVoiceNames; |
| 829 | |
| 830 | void classSpeechSynthesizer_initClass () { |
| 831 | try { |
| 832 | /* |
| 833 | Initialization is to take place only once. |
| 834 | */ |
| 835 | Melder_assert (! theSpeechSynthesizerVoicePropertiesTable); |
| 836 | Melder_assert (! theSpeechSynthesizerLanguagePropertiesTable); |
| 837 | Melder_assert (! theSpeechSynthesizerLanguageNames); |
| 838 | Melder_assert (! theSpeechSynthesizerVoiceNames); |
| 839 | |
| 840 | (void) theEspeakPraatFileInMemorySet(); // create the singleton now (not strictly necessary) |
| 841 | {// scope |
| 842 | static autoTable me; // singleton |
| 843 | constexpr conststring32 criterion = U"./data/lang/"; // 12 characters |
| 844 | constexpr integer criterionLength = Melder_length (criterion); |
| 845 | FileInMemorySet they = theEspeakPraatFileInMemorySet(); |
| 846 | Melder_assert (they); |
| 847 | const integer numberOfMatches = FileInMemorySet_findNumberOfMatches_path (they, kMelder_string :: CONTAINS, criterion); |
| 848 | Melder_assert (numberOfMatches > 0); |
| 849 | const conststring32 columnNames [] = { U"language name", U"ISO 639 family/language code", U"(internal index)" }; |
| 850 | me = Table_createWithColumnNames (numberOfMatches, ARRAY_TO_STRVEC (columnNames)); // old: Default English |
| 851 | integer irow = 0; |
| 852 | for (integer ifile = 1; ifile <= their size; ifile ++) { |
| 853 | const FileInMemory fim = their at [ifile]; |
| 854 | if (Melder_stringMatchesCriterion (fim -> string.get(), kMelder_string :: CONTAINS, criterion, true)) { |
| 855 | irow ++; |
| 856 | const char32 *word = get_stringAfterPrecursor_u8 (fim -> d_data.get(), U"name"); |
| 857 | Table_setStringValue (me.get(), irow, 1, ( word ? word : & fim -> string [criterionLength] )); |
| 858 | Table_setStringValue (me.get(), irow, 2, & fim -> string [criterionLength]); |
| 859 | Table_setNumericValue (me.get(), irow, 3, ifile); |
| 860 | } |
| 861 | } |
| 862 | Melder_assert (irow == numberOfMatches); |
| 863 | const conststring32 sorters [] = { U"language name" }; |
| 864 | Table_sortRows (me.get(), ARRAY_TO_STRVEC (sorters)); |
| 865 | theSpeechSynthesizerLanguagePropertiesTable = me.get(); |
| 866 | } |
| 867 | {// scope |
| 868 | static autoTable me; // singleton |
| 869 | constexpr conststring32 criterion = U"/voices/!v/"; |
| 870 | FileInMemorySet they = theEspeakPraatFileInMemorySet(); |
| 871 | const integer numberOfMatches = FileInMemorySet_findNumberOfMatches_path (they, kMelder_string :: CONTAINS, criterion); |
| 872 | const conststring32 columnNames [] = { U"voice name", U"gender", U"age", U"variant", U"(internal name)", U"(internal index)" }; |
| 873 | me = Table_createWithColumnNames (numberOfMatches, ARRAY_TO_STRVEC (columnNames)); |
| 874 | integer irow = 0; |
| 875 | for (integer ifile = 1; ifile <= their size; ifile ++) { |
| 876 | const FileInMemory fim = their at [ifile]; |
| 877 | if (Melder_stringMatchesCriterion (fim -> string.get(), kMelder_string :: CONTAINS, criterion, true)) { |
| 878 | irow ++; |
| 879 | const char32 *name = get_stringAfterPrecursor_u8 (fim -> d_data.get(), U"name"); |
| 880 | // The first character of name must be upper case |
| 881 | if (name) { |
| 882 | autoMelderString capitalFirst; |
| 883 | MelderString_copy (& capitalFirst, name); // we cannot modify original |
| 884 | capitalFirst.string [0] = Melder_toUpperCase (name [0]); |
| 885 | Table_setStringValue (me.get(), irow, 1, capitalFirst.string); |
| 886 | } else { |
| 887 | Table_setStringValue (me.get(), irow, 1, str32rchr (fim -> string.get(), U'/') + 1); |
| 888 | } |
| 889 | conststring32 word = get_wordAfterPrecursor_u8 (fim -> d_data.get(), U"gender"); |
| 890 | Table_setStringValue (me.get(), irow, 2, (word ? word : U"0")); |
| 891 | word = get_wordAfterPrecursor_u8 (fim -> d_data.get(), U"age"); |
| 892 | Table_setStringValue (me.get(), irow, 3, (word ? word : U"0")); |
| 893 | word = get_stringAfterPrecursor_u8 (fim -> d_data.get(), U"variant"); |
| 894 | Table_setStringValue (me.get(), irow, 4, (word ? word : U"0")); |
| 895 | Table_setStringValue (me.get(), irow, 5, str32rchr (fim -> string.get(), U'/') + 1); |
| 896 | Table_setNumericValue (me.get(), irow, 6, ifile); |
| 897 | } |
| 898 | } |
| 899 | Melder_assert (irow == numberOfMatches); |
| 900 | const conststring32 sorters [] = { U"voice name" }; |
| 901 | Table_sortRows (me.get(), ARRAY_TO_STRVEC (sorters)); |
| 902 | theSpeechSynthesizerVoicePropertiesTable = me.get(); |
| 903 | } |
| 904 | {// scope |
| 905 | static autoSTRVEC list; // singleton |
| 906 | list = Table_getAllTextsInColumn (theSpeechSynthesizerLanguagePropertiesTable, 1); |
| 907 | theSpeechSynthesizerLanguageNames = list.get(); |
| 908 | } |
| 909 | {// scope |
| 910 | static autoSTRVEC list; // singleton |
| 911 | list = Table_getAllTextsInColumn (theSpeechSynthesizerVoicePropertiesTable, 1); |
| 912 | theSpeechSynthesizerVoiceNames = list.get(); |
| 913 | } |
| 914 | } catch (MelderError) { |
| 915 | Melder_crash (U"eSpeak-Praat initialization not performed."); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | void SpeechSynthesizer_checkAndRepairLanguageAndVoiceNames (SpeechSynthesizer me) { |
| 920 | const bool languageAndPhonemeSetAreIdentical = Melder_equ (my d_languageName.get(), my d_phonemeSetName.get()); |
| 921 | |
| 922 | /* |
| 923 | Changes to the language name. |
| 924 | */ |
| 925 | {// scope |
| 926 | bool languageNameHasBeenRepaired = false; |
| 927 | integer languageIndex = NUMfindFirst (theSpeechSynthesizerLanguageNames, my d_languageName.get()); |
| 928 | if (languageIndex == 0) { // not found |
| 929 | if (my d_languageName [0] >= U'a' && my d_languageName [0] <= U'z') { // lower case? |
| 930 | my d_languageName [0] -= 32; // try upper case instead |
| 931 | languageIndex = NUMfindFirst (theSpeechSynthesizerLanguageNames, my d_languageName.get()); |
| 932 | if (languageIndex > 0) { // FOUND |
| 933 | languageNameHasBeenRepaired = true; |
| 934 | if (languageAndPhonemeSetAreIdentical) |
| 935 | my d_phonemeSetName [0] -= 32; // make the same change to the phoneme set name as to the language name |
| 936 | } else |
| 937 | my d_languageName [0] += 32; // revert to lower case |
| 938 | } else if (my d_languageName [0] >= U'A' && my d_languageName [0] <= U'Z') { // upper case? |
| 939 | my d_languageName [0] += 32; // try lower case instead |
| 940 | languageIndex = NUMfindFirst (theSpeechSynthesizerLanguageNames, my d_languageName.get()); |
| 941 | if (languageIndex > 0) { // FOUND |
| 942 | languageNameHasBeenRepaired = true; |
| 943 | if (languageAndPhonemeSetAreIdentical) |
| 944 | my d_phonemeSetName [0] += 32; // make the same change to the phoneme set name as to the language name |
| 945 | } else |
| 946 | my d_languageName [0] -= 32; // revert to upper case |
| 947 | } |
| 948 | if (! languageNameHasBeenRepaired) { // the case change didn't help |
| 949 | /* |
| 950 | The user could have tried an eSpeak-internal family/language code, or just a language code without family. |
| 951 | */ |
| 952 | Table table = theSpeechSynthesizerLanguagePropertiesTable; |
| 953 | for (integer irow = 1; irow <= table -> rows.size; irow ++) { |
| 954 | conststring32 familyLanguageCode = Table_getStringValue_a (table, irow, 2); |
| 955 | const char32 *slashPosition = str32rchr (familyLanguageCode, U'/'); |
| 956 | conststring32 languageCode = ( slashPosition ? slashPosition + 1 : familyLanguageCode ); |
| 957 | if (Melder_equ (my d_languageName.get(), familyLanguageCode) || Melder_equ (my d_languageName.get(), languageCode)) { // FOUND |
| 958 | my d_languageName = Melder_dup (Table_getStringValue_a (table, irow, 1)); |
| 959 | languageNameHasBeenRepaired = true; |
| 960 | if (languageAndPhonemeSetAreIdentical) |
| 961 | my d_phonemeSetName = Melder_dup (my d_languageName.get()); |
| 962 | break; |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | if (! languageNameHasBeenRepaired) { // neither the case change nor the (family/)language code helped |
| 967 | if (Melder_equ (my d_languageName.get(), U"Default") || Melder_equ (my d_languageName.get(), U"English")) { |
| 968 | my d_languageName = Melder_dup (U"English (Great Britain)"); |
| 969 | languageNameHasBeenRepaired = true; |
| 970 | if (languageAndPhonemeSetAreIdentical) |
| 971 | my d_phonemeSetName = Melder_dup (my d_languageName.get()); |
| 972 | } |
| 973 | } |
| 974 | if (! languageNameHasBeenRepaired) // nothing helped |
| 975 | Melder_throw (U"Unknown language “", my d_languageName.get(), U"”.\n" |
| 976 | "If you think that Praat should know this language, write to the authors ([email protected])."); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | Changes to the voice name. |
| 982 | */ |
| 983 | {// scope |
| 984 | bool voiceNameHasBeenRepaired = false; |
| 985 | integer voiceIndex = NUMfindFirst (theSpeechSynthesizerVoiceNames, my d_voiceName.get()); |
| 986 | if (voiceIndex == 0) { // not found |
| 987 | if (my d_voiceName [0] >= U'a' && my d_voiceName [0] <= U'z') { // lower case? |
| 988 | my d_voiceName [0] -= 32; // try upper case instead |
| 989 | voiceIndex = NUMfindFirst (theSpeechSynthesizerVoiceNames, my d_voiceName.get()); |
| 990 | if (voiceIndex > 0) { // FOUND |
| 991 | voiceNameHasBeenRepaired = true; |
| 992 | } else |
| 993 | my d_voiceName [0] += 32; // revert to lower case |
| 994 | } else if (my d_voiceName [0] >= U'A' && my d_voiceName [0] <= U'Z') { // upper case? |
| 995 | my d_voiceName [0] += 32; // try lower case instead |
| 996 | voiceIndex = NUMfindFirst (theSpeechSynthesizerVoiceNames, my d_voiceName.get()); |
| 997 | if (voiceIndex > 0) { // FOUND |
| 998 | voiceNameHasBeenRepaired = true; |
| 999 | } else |
| 1000 | my d_voiceName [0] -= 32; // revert to upper case |
| 1001 | } |
| 1002 | if (! voiceNameHasBeenRepaired) { // the case change didn't help |
| 1003 | if (Melder_equ (my d_voiceName.get(), U"default")) { |
| 1004 | my d_voiceName = Melder_dup (U"Male1"); // as in a very early version of eSpeak |
| 1005 | voiceNameHasBeenRepaired = true; |
| 1006 | } else if (my d_voiceName [0] == U'f' && my d_voiceName [1] >= U'1' && my d_voiceName [1] <= '5' && my d_voiceName [2] == U'\0') { |
| 1007 | my d_voiceName = Melder_dup (Melder_cat (U"Female", my d_voiceName [1])); // e.g. change "f4" to "Female4" |
| 1008 | voiceNameHasBeenRepaired = true; |
| 1009 | } |
| 1010 | } |
| 1011 | if (! voiceNameHasBeenRepaired) // nothing helped |
| 1012 | Melder_throw (U"Unknown voice “", my d_voiceName.get(), U"”.\n" |
| 1013 | "If you think that Praat should know this voice, write to the authors ([email protected])."); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | If language name and phoneme set name were identical to start with, |
| 1019 | then any change in the language name have already been applied to the phoneme set name. |
| 1020 | So we have to change the phoneme set name only if it is independent from the language name. |
| 1021 | */ |
| 1022 | if (languageAndPhonemeSetAreIdentical) |
| 1023 | return; |
| 1024 | |
| 1025 | /* |
| 1026 | Changes to the phoneme set name. |
| 1027 | */ |
| 1028 | {// scope |
| 1029 | bool phonemeSetNameHasBeenRepaired = false; |
| 1030 | integer languageIndex = NUMfindFirst (theSpeechSynthesizerLanguageNames, my d_phonemeSetName.get()); |
| 1031 | if (languageIndex == 0) { // not found |
| 1032 | if (my d_phonemeSetName [0] >= U'a' && my d_phonemeSetName [0] <= U'z') { // lower case? |
| 1033 | my d_phonemeSetName [0] -= 32; // try upper case instead |
| 1034 | languageIndex = NUMfindFirst (theSpeechSynthesizerLanguageNames, my d_phonemeSetName.get()); |
| 1035 | if (languageIndex > 0) { // FOUND |
| 1036 | phonemeSetNameHasBeenRepaired = true; |
| 1037 | } else |
| 1038 | my d_phonemeSetName [0] += 32; // revert to lower case |
| 1039 | } else if (my d_phonemeSetName [0] >= U'A' && my d_phonemeSetName [0] <= U'Z') { // upper case? |
| 1040 | my d_phonemeSetName [0] += 32; // try lower case instead |
| 1041 | languageIndex = NUMfindFirst (theSpeechSynthesizerLanguageNames, my d_phonemeSetName.get()); |
| 1042 | if (languageIndex > 0) { // FOUND |
| 1043 | phonemeSetNameHasBeenRepaired = true; |
| 1044 | } else |
| 1045 | my d_phonemeSetName [0] -= 32; // revert to upper case |
| 1046 | } |
| 1047 | if (! phonemeSetNameHasBeenRepaired) { // the case change didn't help |
| 1048 | /* |
| 1049 | The user could have tried an eSpeak-internal family/language code. |
| 1050 | */ |
| 1051 | Table table = theSpeechSynthesizerLanguagePropertiesTable; |
| 1052 | for (integer irow = 1; irow <= table -> rows.size; irow ++) { |
| 1053 | conststring32 familyLanguageCode = Table_getStringValue_a (table, irow, 2); |
| 1054 | const char32 *slashPosition = str32rchr (familyLanguageCode, U'/'); |
| 1055 | conststring32 languageCode = ( slashPosition ? slashPosition + 1 : familyLanguageCode ); |
| 1056 | if (Melder_equ (my d_phonemeSetName.get(), familyLanguageCode) || Melder_equ (my d_phonemeSetName.get(), languageCode)) { // FOUND |
| 1057 | my d_phonemeSetName = Melder_dup (Table_getStringValue_a (table, irow, 1)); |
| 1058 | phonemeSetNameHasBeenRepaired = true; |
| 1059 | break; |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | if (! phonemeSetNameHasBeenRepaired) { // neither the case change nor the (family/)language code helped |
| 1064 | if (Melder_equ (my d_phonemeSetName.get(), U"Default") || Melder_equ (my d_phonemeSetName.get(), U"English")) { |
| 1065 | my d_phonemeSetName = Melder_dup (U"English (Great Britain)"); |
| 1066 | phonemeSetNameHasBeenRepaired = true; |
| 1067 | } |
| 1068 | } |
| 1069 | if (! phonemeSetNameHasBeenRepaired) // nothing helped |
| 1070 | Melder_throw (U"Unknown language “", my d_phonemeSetName.get(), U"”.\n" |
| 1071 | "If you think that Praat should know this language, write to the authors ([email protected])."); |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /* End of file SpeechSynthesizer.cpp */ |