| 1 | /* |
| 2 | * Copyright (C) 2005 to 2013 by Jonathan Duddington |
| 3 | * email: [email protected] |
| 4 | * Copyright (C) 2013-2017 Reece H. Dunn |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, see: <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "espeak__config.h" |
| 21 | |
| 22 | #include <assert.h> |
| 23 | #include <ctype.h> |
| 24 | #include <errno.h> |
| 25 | #include <locale.h> |
| 26 | #include <stdbool.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <wchar.h> |
| 35 | |
| 36 | #if USE_LIBPCAUDIO |
| 37 | #include <pcaudiolib/audio.h> |
| 38 | #endif |
| 39 | |
| 40 | #if defined(_WIN32) || defined(_WIN64) |
| 41 | #include <fcntl.h> |
| 42 | #include <io.h> |
| 43 | #include <windows.h> |
| 44 | #include <winreg.h> |
| 45 | #endif |
| 46 | |
| 47 | #include "espeak_ng.h" |
| 48 | #include "speak_lib.h" |
| 49 | #include "encoding.h" |
| 50 | |
| 51 | #include "espeak_praat.h" |
| 52 | |
| 53 | #include "speech.h" |
| 54 | #include "common.h" // for GetFileLength |
| 55 | #include "dictionary.h" // for GetTranslatedPhonemeString, strncpy0 |
| 56 | //ppgb #include "espeak_command.h" // for delete_espeak_command, SetParameter, (ppgb:) sync_espeak_Char |
| 57 | #include "setlengths.h" //ppgb: for SetParameter (instead of espeak_command.h) |
| 58 | #include "event.h" // for event_declare, event_clear_all, eve... |
| 59 | //ppgb #include "fifo.h" // for fifo_add_command, fifo_add_commands |
| 60 | #include "langopts.h" // for LoadConfig |
| 61 | #include "mbrola.h" // for mbrola_delay |
| 62 | #include "readclause.h" // for PARAM_STACK, param_stack |
| 63 | #include "synthdata.h" // for FreePhData, LoadPhData |
| 64 | #include "synthesize.h" // for SpeakNextClause, Generate, Synthesi... |
| 65 | #include "translate.h" // for p_decoder, InitText, translator |
| 66 | #include "voice.h" // for FreeVoiceList, VoiceReset, current_... |
| 67 | #include "wavegen.h" // for WavegenFill, WavegenInit, WcmdqUsed |
| 68 | |
| 69 | static unsigned char *outbuf = NULL; |
| 70 | static int outbuf_size = 0; |
| 71 | static unsigned char *out_start; |
| 72 | |
| 73 | espeak_EVENT *event_list = NULL; |
| 74 | static int event_list_ix = 0; |
| 75 | static int n_event_list; |
| 76 | static long count_samples; |
| 77 | #if USE_LIBPCAUDIO |
| 78 | static struct audio_object *my_audio = NULL; |
| 79 | #endif |
| 80 | |
| 81 | static unsigned int my_unique_identifier = 0; |
| 82 | static void *my_user_data = NULL; |
| 83 | static espeak_ng_OUTPUT_MODE my_mode = ENOUTPUT_MODE_SYNCHRONOUS; |
| 84 | static int out_samplerate = 0; |
| 85 | static int voice_samplerate = 22050; |
| 86 | static const int min_buffer_length = 60; // minimum buffer length in ms |
| 87 | static espeak_ng_STATUS err = ENS_OK; |
| 88 | |
| 89 | static t_espeak_callback *synth_callback = NULL; |
| 90 | |
| 91 | char path_home[N_PATH_HOME]; // this is the espeak-ng-data directory |
| 92 | extern int saved_parameters[N_SPEECH_PARAM]; // Parameters saved on synthesis start |
| 93 | |
| 94 | void cancel_audio(void) |
| 95 | { |
| 96 | #if USE_LIBPCAUDIO |
| 97 | if ((my_mode & ENOUTPUT_MODE_SPEAK_AUDIO) == ENOUTPUT_MODE_SPEAK_AUDIO) { |
| 98 | audio_object_flush(my_audio); |
| 99 | } |
| 100 | #endif |
| 101 | } |
| 102 | |
| 103 | static int dispatch_audio(short *outbuff, int length, espeak_EVENT *event) |
| 104 | { |
| 105 | int a_wave_can_be_played = 1; |
| 106 | #if USE_ASYNC |
| 107 | if ((my_mode & ENOUTPUT_MODE_SYNCHRONOUS) == 0) |
| 108 | a_wave_can_be_played = fifo_is_command_enabled(); |
| 109 | #endif |
| 110 | |
| 111 | switch ((int)my_mode) |
| 112 | { |
| 113 | case ENOUTPUT_MODE_SPEAK_AUDIO: |
| 114 | case ENOUTPUT_MODE_SPEAK_AUDIO | ENOUTPUT_MODE_SYNCHRONOUS: |
| 115 | { |
| 116 | int event_type = 0; |
| 117 | if (event) |
| 118 | event_type = event->type; |
| 119 | |
| 120 | if (event_type == espeakEVENT_SAMPLERATE) { |
| 121 | voice_samplerate = event->id.number; |
| 122 | |
| 123 | if (out_samplerate != voice_samplerate) { |
| 124 | #if USE_LIBPCAUDIO |
| 125 | if (out_samplerate != 0) { |
| 126 | // sound was previously open with a different sample rate |
| 127 | audio_object_close(my_audio); |
| 128 | out_samplerate = 0; |
| 129 | } |
| 130 | #endif |
| 131 | #if USE_LIBPCAUDIO |
| 132 | int error = audio_object_open(my_audio, AUDIO_OBJECT_FORMAT_S16LE, voice_samplerate, 1); |
| 133 | if (error != 0) { |
| 134 | fprintf(stderr, "error: %s\n", audio_object_strerror(my_audio, error)); |
| 135 | err = ENS_AUDIO_ERROR; |
| 136 | return -1; |
| 137 | } |
| 138 | #endif |
| 139 | out_samplerate = voice_samplerate; |
| 140 | #if USE_ASYNC |
| 141 | if ((my_mode & ENOUTPUT_MODE_SYNCHRONOUS) == 0) |
| 142 | event_init(); |
| 143 | #endif |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #if USE_LIBPCAUDIO |
| 148 | if (out_samplerate == 0) { |
| 149 | int error = audio_object_open(my_audio, AUDIO_OBJECT_FORMAT_S16LE, voice_samplerate, 1); |
| 150 | if (error != 0) { |
| 151 | fprintf(stderr, "error: %s\n", audio_object_strerror(my_audio, error)); |
| 152 | err = ENS_AUDIO_ERROR; |
| 153 | return -1; |
| 154 | } |
| 155 | out_samplerate = voice_samplerate; |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | #if USE_LIBPCAUDIO |
| 160 | if (outbuff && length && a_wave_can_be_played) { |
| 161 | int error = audio_object_write(my_audio, (char *)outbuff, 2*length); |
| 162 | if (error != 0) |
| 163 | fprintf(stderr, "error: %s\n", audio_object_strerror(my_audio, error)); |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #if USE_ASYNC |
| 168 | while (event && a_wave_can_be_played) { |
| 169 | // TBD: some event are filtered here but some insight might be given |
| 170 | // TBD: in synthesise.cpp for avoiding to create WORDs with size=0. |
| 171 | // TBD: For example sentence "or ALT)." returns three words |
| 172 | // "or", "ALT" and "". |
| 173 | // TBD: the last one has its size=0. |
| 174 | if ((event->type == espeakEVENT_WORD) && (event->length == 0)) |
| 175 | break; |
| 176 | if ((my_mode & ENOUTPUT_MODE_SYNCHRONOUS) == 0) { |
| 177 | err = event_declare(event); |
| 178 | if (err != ENS_EVENT_BUFFER_FULL) |
| 179 | break; |
| 180 | usleep(10000); |
| 181 | a_wave_can_be_played = fifo_is_command_enabled(); |
| 182 | } else |
| 183 | break; |
| 184 | } |
| 185 | #endif |
| 186 | } |
| 187 | break; |
| 188 | case 0: |
| 189 | if (synth_callback) |
| 190 | synth_callback(outbuff, length, event); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | return a_wave_can_be_played == 0; // 1 = stop synthesis, -1 = error |
| 195 | } |
| 196 | |
| 197 | static int create_events(short *outbuff, int length, espeak_EVENT *eventList) |
| 198 | { |
| 199 | int finished; |
| 200 | int i = 0; |
| 201 | |
| 202 | // The audio data are written to the output device. |
| 203 | // The list of events in eventList (index: event_list_ix) is read: |
| 204 | // Each event is declared to the "event" object which stores them internally. |
| 205 | // The event object is responsible of calling the external callback |
| 206 | // as soon as the relevant audio sample is played. |
| 207 | |
| 208 | do { // for each event |
| 209 | espeak_EVENT *event; |
| 210 | if (event_list_ix == 0) |
| 211 | event = NULL; |
| 212 | else |
| 213 | event = eventList + i; |
| 214 | finished = dispatch_audio((short *)outbuff, length, event); |
| 215 | length = 0; // the wave data are played once. |
| 216 | i++; |
| 217 | } while ((i < event_list_ix) && !finished); |
| 218 | return finished; |
| 219 | } |
| 220 | |
| 221 | #if USE_ASYNC |
| 222 | |
| 223 | int sync_espeak_terminated_msg(uint32_t unique_identifier, void *user_data) |
| 224 | { |
| 225 | int finished = 0; |
| 226 | |
| 227 | memset(event_list, 0, 2*sizeof(espeak_EVENT)); |
| 228 | |
| 229 | event_list[0].type = espeakEVENT_MSG_TERMINATED; |
| 230 | event_list[0].unique_identifier = unique_identifier; |
| 231 | event_list[0].user_data = user_data; |
| 232 | event_list[1].type = espeakEVENT_LIST_TERMINATED; |
| 233 | event_list[1].unique_identifier = unique_identifier; |
| 234 | event_list[1].user_data = user_data; |
| 235 | |
| 236 | if (my_mode == ENOUTPUT_MODE_SPEAK_AUDIO) { |
| 237 | while (1) { |
| 238 | err = event_declare(event_list); |
| 239 | if (err != ENS_EVENT_BUFFER_FULL) |
| 240 | break; |
| 241 | usleep(10000); |
| 242 | } |
| 243 | } else if (synth_callback) |
| 244 | finished = synth_callback(NULL, 0, event_list); |
| 245 | return finished; |
| 246 | } |
| 247 | |
| 248 | #endif |
| 249 | |
| 250 | #if 0 /* ppgb: not used in Praat */ |
| 251 | static int check_data_path(const char *path, int allow_directory) |
| 252 | { |
| 253 | if (!path) return 0; |
| 254 | |
| 255 | snprintf(path_home, sizeof(path_home), "%s/espeak-ng-data", path); |
| 256 | if (GetFileLength(path_home) == -EISDIR) |
| 257 | return 1; |
| 258 | |
| 259 | if (!allow_directory) |
| 260 | return 0; |
| 261 | |
| 262 | snprintf(path_home, sizeof(path_home), "%s", path); |
| 263 | return GetFileLength(path_home) == -EISDIR; |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | #pragma GCC visibility push(default) |
| 268 | |
| 269 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_InitializeOutput(espeak_ng_OUTPUT_MODE output_mode, int buffer_length, const char *device) |
| 270 | { |
| 271 | (void)device; // unused if USE_LIBPCAUDIO is not defined |
| 272 | |
| 273 | my_mode = output_mode; |
| 274 | out_samplerate = 0; |
| 275 | |
| 276 | #if USE_LIBPCAUDIO |
| 277 | if (((my_mode & ENOUTPUT_MODE_SPEAK_AUDIO) == ENOUTPUT_MODE_SPEAK_AUDIO) && (my_audio == NULL)) |
| 278 | my_audio = create_audio_device_object(device, "eSpeak", "Text-to-Speech"); |
| 279 | #endif |
| 280 | |
| 281 | #if USE_ASYNC |
| 282 | if ((my_mode & ENOUTPUT_MODE_SYNCHRONOUS) == 0) fifo_init(); |
| 283 | #endif |
| 284 | |
| 285 | // Don't allow buffer be smaller than safe minimum |
| 286 | if (buffer_length < min_buffer_length) |
| 287 | buffer_length = min_buffer_length; |
| 288 | |
| 289 | // allocate 2 bytes per sample |
| 290 | // Always round up to the nearest sample and the nearest byte. |
| 291 | int millisamples = buffer_length * samplerate; |
| 292 | outbuf_size = (millisamples + 1000 - millisamples % 1000) / 500; |
| 293 | out_start = (unsigned char *)realloc(outbuf, outbuf_size); |
| 294 | if (out_start == NULL) |
| 295 | return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 296 | else |
| 297 | outbuf = out_start; |
| 298 | |
| 299 | // allocate space for event list. Allow 200 events per second. |
| 300 | // Add a constant to allow for very small buffer_length |
| 301 | n_event_list = (buffer_length*200)/1000 + 20; |
| 302 | espeak_EVENT *new_event_list = (espeak_EVENT *)realloc(event_list, sizeof(espeak_EVENT) * n_event_list); |
| 303 | if (new_event_list == NULL) |
| 304 | return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 305 | event_list = new_event_list; |
| 306 | |
| 307 | return ENS_OK; |
| 308 | } |
| 309 | |
| 310 | /* ppgb: in Praat always called with path == nullptr */ |
| 311 | ESPEAK_NG_API void espeak_ng_InitializePath(const char *path) |
| 312 | { |
| 313 | #if 1 /* ppgb: in Praat we should ignore everything that eSpeak is trying to do here: registries, environment variables... */ |
| 314 | Melder_assert (! path); |
| 315 | strcpy(path_home, PATH_ESPEAK_DATA); |
| 316 | #else |
| 317 | if (check_data_path(path, 1)) |
| 318 | return; |
| 319 | |
| 320 | #if PLATFORM_WINDOWS |
| 321 | HKEY RegKey; |
| 322 | unsigned long size; |
| 323 | unsigned long var_type; |
| 324 | unsigned char buf[sizeof(path_home)-13]; |
| 325 | |
| 326 | if (check_data_path(getenv("ESPEAK_DATA_PATH"), 1)) |
| 327 | return; |
| 328 | |
| 329 | buf[0] = 0; |
| 330 | RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\eSpeak NG", 0, KEY_READ, &RegKey); |
| 331 | if (RegKey == NULL) |
| 332 | RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\WOW6432Node\\eSpeak NG", 0, KEY_READ, &RegKey); |
| 333 | size = sizeof(buf); |
| 334 | var_type = REG_SZ; |
| 335 | RegQueryValueExA(RegKey, "Path", 0, &var_type, buf, &size); |
| 336 | |
| 337 | if (check_data_path((char *)buf, 1)) //ppgb casty |
| 338 | return; |
| 339 | #elif !defined(PLATFORM_DOS) |
| 340 | if (check_data_path(getenv("ESPEAK_DATA_PATH"), 1)) |
| 341 | return; |
| 342 | |
| 343 | if (check_data_path(getenv("HOME"), 0)) |
| 344 | return; |
| 345 | #endif |
| 346 | |
| 347 | strcpy(path_home, PATH_ESPEAK_DATA); |
| 348 | #endif /* ppgb */ |
| 349 | } |
| 350 | |
| 351 | const int param_defaults[N_SPEECH_PARAM] = { |
| 352 | 0, // silence (internal use) |
| 353 | espeakRATE_NORMAL, // rate wpm |
| 354 | 100, // volume |
| 355 | 50, // pitch |
| 356 | 50, // range |
| 357 | 0, // punctuation |
| 358 | 0, // capital letters |
| 359 | 0, // wordgap |
| 360 | 0, // options |
| 361 | 0, // intonation |
| 362 | 100, // ssml break mul |
| 363 | 0, |
| 364 | 0, // emphasis |
| 365 | 0, // line length |
| 366 | 0, // voice type |
| 367 | }; |
| 368 | |
| 369 | |
| 370 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Initialize(espeak_ng_ERROR_CONTEXT *context) |
| 371 | { |
| 372 | int param; |
| 373 | int srate = 22050; // default sample rate 22050 Hz |
| 374 | |
| 375 | /* |
| 376 | (Paul Boersma 20240426:) |
| 377 | When using this library in an app, e.g. Praat, |
| 378 | we should not set the locale, because it will interfere with the locale |
| 379 | that has been set in praat_init(). |
| 380 | To nevertheless experiment with setting the locale here, |
| 381 | remove the space from "set locale" in the definition of SET_LOCALE |
| 382 | and set USE_SET_LOCALE_IN_THIS_LIBRARY to 1 instead of 0. |
| 383 | (The space is needed to be able to automatically determine that the name |
| 384 | of the function does not appear in the present source file `speech.cpp`.) |
| 385 | */ |
| 386 | #define SET_LOCALE set locale /* should normally contain a space between "set" and "locale" */ |
| 387 | #define USE_SET_LOCALE_IN_THIS_LIBRARY 0 /* should normally be 0 */ |
| 388 | #if USE_SET_LOCALE_IN_THIS_LIBRARY |
| 389 | if (SET_LOCALE(LC_CTYPE, "C.UTF-8") == NULL) { |
| 390 | if (SET_LOCALE(LC_CTYPE, "UTF-8") == NULL) { |
| 391 | if (SET_LOCALE(LC_CTYPE, "en_US.UTF-8") == NULL) |
| 392 | SET_LOCALE(LC_CTYPE, ""); |
| 393 | } |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | espeak_ng_STATUS result = LoadPhData(&srate, context); |
| 398 | if (result != ENS_OK) |
| 399 | return result; |
| 400 | |
| 401 | WavegenInit(srate, 0); |
| 402 | LoadConfig(); |
| 403 | |
| 404 | espeak_VOICE *current_voice_selected = espeak_GetCurrentVoice(); |
| 405 | memset(current_voice_selected, 0, sizeof(espeak_VOICE)); |
| 406 | SetVoiceStack(NULL, ""); |
| 407 | SynthesizeInit(); |
| 408 | InitNamedata(); |
| 409 | |
| 410 | VoiceReset(0); |
| 411 | |
| 412 | for (param = 0; param < N_SPEECH_PARAM; param++) |
| 413 | param_stack[0].parameter[param] = saved_parameters[param] = param_defaults[param]; |
| 414 | |
| 415 | SetParameter(espeakRATE, espeakRATE_NORMAL, 0); |
| 416 | SetParameter(espeakVOLUME, 100, 0); |
| 417 | SetParameter(espeakCAPITALS, option_capitals, 0); |
| 418 | SetParameter(espeakPUNCTUATION, option_punctuation, 0); |
| 419 | SetParameter(espeakWORDGAP, 0, 0); |
| 420 | |
| 421 | option_phonemes = 0; |
| 422 | option_phoneme_events = 0; |
| 423 | |
| 424 | // Seed random generator |
| 425 | espeak_srand(time(NULL)); |
| 426 | |
| 427 | return ENS_OK; |
| 428 | } |
| 429 | |
| 430 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetPhonemeEvents(int enable, int ipa) { |
| 431 | option_phoneme_events = 0; |
| 432 | if (enable) { |
| 433 | option_phoneme_events |= espeakINITIALIZE_PHONEME_EVENTS; |
| 434 | if (ipa) { |
| 435 | option_phoneme_events |= espeakINITIALIZE_PHONEME_IPA; |
| 436 | } |
| 437 | } |
| 438 | return ENS_OK; |
| 439 | } |
| 440 | |
| 441 | ESPEAK_NG_API int espeak_ng_GetSampleRate(void) |
| 442 | { |
| 443 | return samplerate; |
| 444 | } |
| 445 | |
| 446 | #pragma GCC visibility pop |
| 447 | |
| 448 | static espeak_ng_STATUS Synthesize(unsigned int unique_identifier, const void *text, int flags) |
| 449 | { |
| 450 | // Fill the buffer with output sound |
| 451 | int length; |
| 452 | int finished = 0; |
| 453 | |
| 454 | if ((outbuf == NULL) || (event_list == NULL)) |
| 455 | return ENS_NOT_INITIALIZED; |
| 456 | |
| 457 | option_ssml = flags & espeakSSML; |
| 458 | option_phoneme_input = flags & espeakPHONEMES; |
| 459 | option_endpause = flags & espeakENDPAUSE; |
| 460 | |
| 461 | count_samples = 0; |
| 462 | |
| 463 | espeak_ng_STATUS status; |
| 464 | if (translator == NULL) { |
| 465 | status = espeak_ng_SetVoiceByName(ESPEAKNG_DEFAULT_VOICE); |
| 466 | if (status != ENS_OK) |
| 467 | return status; |
| 468 | } |
| 469 | |
| 470 | if (p_decoder == NULL) |
| 471 | p_decoder = create_text_decoder(); |
| 472 | |
| 473 | status = text_decoder_decode_string_multibyte(p_decoder, text, translator->encoding, flags); |
| 474 | if (status != ENS_OK) |
| 475 | return status; |
| 476 | |
| 477 | SpeakNextClause(0); |
| 478 | |
| 479 | for (;;) { |
| 480 | out_ptr = outbuf; |
| 481 | out_end = &outbuf[outbuf_size]; |
| 482 | event_list_ix = 0; |
| 483 | WavegenFill(); |
| 484 | |
| 485 | length = (out_ptr - outbuf)/2; |
| 486 | count_samples += length; |
| 487 | event_list[event_list_ix].type = espeakEVENT_LIST_TERMINATED; // indicates end of event list |
| 488 | event_list[event_list_ix].unique_identifier = unique_identifier; |
| 489 | event_list[event_list_ix].user_data = my_user_data; |
| 490 | |
| 491 | if ((my_mode & ENOUTPUT_MODE_SPEAK_AUDIO) == ENOUTPUT_MODE_SPEAK_AUDIO) { |
| 492 | finished = create_events((short *)outbuf, length, event_list); |
| 493 | if (finished < 0) |
| 494 | return ENS_AUDIO_ERROR; |
| 495 | } else if (synth_callback) |
| 496 | finished = synth_callback((short *)outbuf, length, event_list); |
| 497 | if (finished) { |
| 498 | SpeakNextClause(2); // stop |
| 499 | return ENS_SPEECH_STOPPED; |
| 500 | } |
| 501 | |
| 502 | if (Generate(phoneme_list, &n_phoneme_list, 1) == 0) { |
| 503 | if (WcmdqUsed() == 0) { |
| 504 | // don't process the next clause until the previous clause has finished generating speech. |
| 505 | // This ensures that <audio> tag (which causes end-of-clause) is at a sound buffer boundary |
| 506 | |
| 507 | event_list[0].type = espeakEVENT_LIST_TERMINATED; |
| 508 | event_list[0].unique_identifier = my_unique_identifier; |
| 509 | event_list[0].user_data = my_user_data; |
| 510 | |
| 511 | if (SpeakNextClause(1) == 0) { |
| 512 | finished = 0; |
| 513 | if ((my_mode & ENOUTPUT_MODE_SPEAK_AUDIO) == ENOUTPUT_MODE_SPEAK_AUDIO) { |
| 514 | if (dispatch_audio(NULL, 0, NULL) < 0) |
| 515 | return ENS_AUDIO_ERROR; |
| 516 | } else if (synth_callback) |
| 517 | finished = synth_callback(NULL, 0, event_list); // NULL buffer ptr indicates end of data |
| 518 | if (finished) { |
| 519 | SpeakNextClause(2); // stop |
| 520 | return ENS_SPEECH_STOPPED; |
| 521 | } |
| 522 | return ENS_OK; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void MarkerEvent(int type, unsigned int char_position, int value, int value2, unsigned char *outptr) |
| 530 | { |
| 531 | /* |
| 532 | ppgb: |
| 533 | This function can be called only from: |
| 534 | - WavegenSetVoice(), with `type` == espeakEVENT_SAMPLERATE |
| 535 | - WavegenFill2(), with `type` is one of: |
| 536 | - espeakEVENT_SENTENCE |
| 537 | - espeakEVENT_WORD |
| 538 | - espeakEVENT_PHONEME |
| 539 | - espeakEVENT_END |
| 540 | */ |
| 541 | //TRACE |
| 542 | trace (type, U" ", char_position, U" ", value, U" ", value2); |
| 543 | // type: 1=word, 2=sentence, 3=named mark, 4=play audio, 5=end, 7=phoneme |
| 544 | espeak_EVENT *ep; |
| 545 | double time; |
| 546 | |
| 547 | if ((event_list == NULL) || (event_list_ix >= (n_event_list-2))) |
| 548 | return; |
| 549 | |
| 550 | ep = &event_list[event_list_ix++]; |
| 551 | ep->type = (espeak_EVENT_TYPE)type; |
| 552 | ep->unique_identifier = my_unique_identifier; |
| 553 | ep->user_data = my_user_data; |
| 554 | ep->text_position = char_position & 0xffffff; |
| 555 | ep->length = char_position >> 24; |
| 556 | |
| 557 | #if !USE_MBROLA |
| 558 | static const int mbrola_delay = 0; |
| 559 | #endif |
| 560 | |
| 561 | time = ((double)(count_samples + mbrola_delay + (outptr - out_start)/2)*1000.0)/samplerate; |
| 562 | ep->audio_position = (int)time; |
| 563 | ep->sample = (count_samples + mbrola_delay + (outptr - out_start)/2); |
| 564 | |
| 565 | if ((type == espeakEVENT_MARK) || (type == espeakEVENT_PLAY)) |
| 566 | ep->id.name = &namedata[value]; |
| 567 | else if (type == espeakEVENT_PHONEME) { |
| 568 | int *p; |
| 569 | p = (int *)(ep->id.string); |
| 570 | p[0] = value; |
| 571 | p[1] = value2; // ppgb-espeak |
| 572 | } else |
| 573 | ep->id.number = value; |
| 574 | } |
| 575 | |
| 576 | espeak_ng_STATUS sync_espeak_Synth(unsigned int unique_identifier, const void *text, |
| 577 | unsigned int position, espeak_POSITION_TYPE position_type, |
| 578 | unsigned int end_position, unsigned int flags, void *user_data) |
| 579 | { |
| 580 | InitText(flags); |
| 581 | my_unique_identifier = unique_identifier; |
| 582 | my_user_data = user_data; |
| 583 | |
| 584 | for (int i = 0; i < N_SPEECH_PARAM; i++) |
| 585 | saved_parameters[i] = param_stack[0].parameter[i]; |
| 586 | |
| 587 | switch (position_type) |
| 588 | { |
| 589 | case POS_CHARACTER: |
| 590 | skip_characters = position; |
| 591 | break; |
| 592 | case POS_WORD: |
| 593 | skip_words = position; |
| 594 | break; |
| 595 | case POS_SENTENCE: |
| 596 | skip_sentences = position; |
| 597 | break; |
| 598 | |
| 599 | } |
| 600 | if (skip_characters || skip_words || skip_sentences) |
| 601 | skipping_text = true; |
| 602 | |
| 603 | end_character_position = end_position; |
| 604 | |
| 605 | espeak_ng_STATUS aStatus = Synthesize(unique_identifier, text, flags); |
| 606 | #if USE_LIBPCAUDIO |
| 607 | if ((my_mode & ENOUTPUT_MODE_SPEAK_AUDIO) == ENOUTPUT_MODE_SPEAK_AUDIO) { |
| 608 | int error = (aStatus == ENS_SPEECH_STOPPED) |
| 609 | ? audio_object_flush(my_audio) |
| 610 | : audio_object_drain(my_audio); |
| 611 | if (error != 0) |
| 612 | fprintf(stderr, "error: %s\n", audio_object_strerror(my_audio, error)); |
| 613 | } |
| 614 | #endif |
| 615 | |
| 616 | return aStatus; |
| 617 | } |
| 618 | |
| 619 | espeak_ng_STATUS sync_espeak_Synth_Mark(unsigned int unique_identifier, const void *text, |
| 620 | const char *index_mark, unsigned int end_position, |
| 621 | unsigned int flags, void *user_data) |
| 622 | { |
| 623 | InitText(flags); |
| 624 | |
| 625 | my_unique_identifier = unique_identifier; |
| 626 | my_user_data = user_data; |
| 627 | |
| 628 | if (index_mark != NULL) { |
| 629 | strncpy0(skip_marker, index_mark, sizeof(skip_marker)); |
| 630 | skipping_text = true; |
| 631 | } |
| 632 | |
| 633 | end_character_position = end_position; |
| 634 | |
| 635 | return Synthesize(unique_identifier, text, flags | espeakSSML); |
| 636 | } |
| 637 | |
| 638 | /*ppgb:*/ static //ppgb: and put this function before sync_espeak_Key() |
| 639 | espeak_ng_STATUS sync_espeak_Char(wchar_t character) |
| 640 | { |
| 641 | // is there a system resource of character names per language? |
| 642 | char buf[80]; |
| 643 | my_unique_identifier = 0; |
| 644 | my_user_data = NULL; |
| 645 | |
| 646 | sprintf(buf, "<say-as interpret-as=\"tts:char\">&#%d;</say-as>", character); |
| 647 | return Synthesize(0, buf, espeakSSML); |
| 648 | } |
| 649 | |
| 650 | /*ppgb:*/ static |
| 651 | espeak_ng_STATUS sync_espeak_Key(const char *key) |
| 652 | { |
| 653 | // symbolic name, symbolicname_character - is there a system resource of symbolic names per language? |
| 654 | int letter; |
| 655 | int ix; |
| 656 | |
| 657 | ix = utf8_in(&letter, key); |
| 658 | if (key[ix] == 0) // a single character |
| 659 | return sync_espeak_Char(letter); |
| 660 | |
| 661 | my_unique_identifier = 0; |
| 662 | my_user_data = NULL; |
| 663 | return Synthesize(0, key, 0); // speak key as a text string |
| 664 | } |
| 665 | |
| 666 | /*ppgb:*/ static |
| 667 | void sync_espeak_SetPunctuationList(const wchar_t *punctlist) |
| 668 | { |
| 669 | // Set the list of punctuation which are spoken for "some". |
| 670 | my_unique_identifier = 0; |
| 671 | my_user_data = NULL; |
| 672 | |
| 673 | option_punctlist[0] = 0; |
| 674 | if (punctlist != NULL) { |
| 675 | wcsncpy(option_punctlist, punctlist, N_PUNCTLIST); |
| 676 | option_punctlist[N_PUNCTLIST-1] = 0; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | #pragma GCC visibility push(default) |
| 681 | |
| 682 | ESPEAK_API void espeak_SetSynthCallback(t_espeak_callback *SynthCallback) |
| 683 | { |
| 684 | synth_callback = SynthCallback; |
| 685 | #if USE_ASYNC |
| 686 | event_set_callback(synth_callback); |
| 687 | #endif |
| 688 | } |
| 689 | |
| 690 | ESPEAK_NG_API espeak_ng_STATUS |
| 691 | espeak_ng_Synthesize(const void *text, size_t size, |
| 692 | unsigned int position, |
| 693 | espeak_POSITION_TYPE position_type, |
| 694 | unsigned int end_position, unsigned int flags, |
| 695 | unsigned int *unique_identifier, void *user_data) |
| 696 | { |
| 697 | (void)size; // unused in non-async modes |
| 698 | |
| 699 | unsigned int temp_identifier; |
| 700 | |
| 701 | if (unique_identifier == NULL) |
| 702 | unique_identifier = &temp_identifier; |
| 703 | *unique_identifier = 0; |
| 704 | |
| 705 | if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS) |
| 706 | return sync_espeak_Synth(0, text, position, position_type, end_position, flags, user_data); |
| 707 | |
| 708 | #if USE_ASYNC |
| 709 | // Create the text command |
| 710 | t_espeak_command *c1 = create_espeak_text(text, size, position, position_type, end_position, flags, user_data); |
| 711 | if (c1) { |
| 712 | // Retrieve the unique identifier |
| 713 | *unique_identifier = c1->u.my_text.unique_identifier; |
| 714 | } |
| 715 | |
| 716 | // Create the "terminated msg" command (same uid) |
| 717 | t_espeak_command *c2 = create_espeak_terminated_msg(*unique_identifier, user_data); |
| 718 | |
| 719 | // Try to add these 2 commands (single transaction) |
| 720 | if (c1 && c2) { |
| 721 | espeak_ng_STATUS status = fifo_add_commands(c1, c2); |
| 722 | if (status != ENS_OK) { |
| 723 | delete_espeak_command(c1); |
| 724 | delete_espeak_command(c2); |
| 725 | } |
| 726 | return status; |
| 727 | } |
| 728 | |
| 729 | delete_espeak_command(c1); |
| 730 | delete_espeak_command(c2); |
| 731 | return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 732 | #else |
| 733 | return sync_espeak_Synth(0, text, position, position_type, end_position, flags, user_data); |
| 734 | #endif |
| 735 | } |
| 736 | |
| 737 | ESPEAK_NG_API espeak_ng_STATUS |
| 738 | espeak_ng_SynthesizeMark(const void *text, |
| 739 | size_t size, |
| 740 | const char *index_mark, |
| 741 | unsigned int end_position, |
| 742 | unsigned int flags, |
| 743 | unsigned int *unique_identifier, |
| 744 | void *user_data) |
| 745 | { |
| 746 | (void)size; // unused in non-async modes |
| 747 | |
| 748 | unsigned int temp_identifier; |
| 749 | |
| 750 | if (unique_identifier == NULL) |
| 751 | unique_identifier = &temp_identifier; |
| 752 | *unique_identifier = 0; |
| 753 | |
| 754 | if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS) |
| 755 | return sync_espeak_Synth_Mark(0, text, index_mark, end_position, flags, user_data); |
| 756 | |
| 757 | #if USE_ASYNC |
| 758 | // Create the mark command |
| 759 | t_espeak_command *c1 = create_espeak_mark(text, size, index_mark, end_position, |
| 760 | flags, user_data); |
| 761 | if (c1) { |
| 762 | // Retrieve the unique identifier |
| 763 | *unique_identifier = c1->u.my_mark.unique_identifier; |
| 764 | } |
| 765 | |
| 766 | // Create the "terminated msg" command (same uid) |
| 767 | t_espeak_command *c2 = create_espeak_terminated_msg(*unique_identifier, user_data); |
| 768 | |
| 769 | // Try to add these 2 commands (single transaction) |
| 770 | if (c1 && c2) { |
| 771 | espeak_ng_STATUS status = fifo_add_commands(c1, c2); |
| 772 | if (status != ENS_OK) { |
| 773 | delete_espeak_command(c1); |
| 774 | delete_espeak_command(c2); |
| 775 | } |
| 776 | return status; |
| 777 | } |
| 778 | |
| 779 | delete_espeak_command(c1); |
| 780 | delete_espeak_command(c2); |
| 781 | return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 782 | #else |
| 783 | return sync_espeak_Synth_Mark(0, text, index_mark, end_position, flags, user_data); |
| 784 | #endif |
| 785 | } |
| 786 | |
| 787 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SpeakKeyName(const char *key_name) |
| 788 | { |
| 789 | // symbolic name, symbolicname_character - is there a system resource of symbolicnames per language |
| 790 | |
| 791 | if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS) |
| 792 | return sync_espeak_Key(key_name); |
| 793 | |
| 794 | #if USE_ASYNC |
| 795 | t_espeak_command *c = create_espeak_key(key_name, NULL); |
| 796 | espeak_ng_STATUS status = fifo_add_command(c); |
| 797 | if (status != ENS_OK) |
| 798 | delete_espeak_command(c); |
| 799 | return status; |
| 800 | #else |
| 801 | return sync_espeak_Key(key_name); |
| 802 | #endif |
| 803 | } |
| 804 | |
| 805 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SpeakCharacter(wchar_t character) |
| 806 | { |
| 807 | // is there a system resource of character names per language? |
| 808 | |
| 809 | #if USE_ASYNC |
| 810 | if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS) |
| 811 | return sync_espeak_Char(character); |
| 812 | |
| 813 | t_espeak_command *c = create_espeak_char(character, NULL); |
| 814 | espeak_ng_STATUS status = fifo_add_command(c); |
| 815 | if (status != ENS_OK) |
| 816 | delete_espeak_command(c); |
| 817 | return status; |
| 818 | #else |
| 819 | return sync_espeak_Char(character); |
| 820 | #endif |
| 821 | } |
| 822 | |
| 823 | ESPEAK_API int espeak_GetParameter(espeak_PARAMETER parameter, int current) |
| 824 | { |
| 825 | // current: 0=default value, 1=current value |
| 826 | if (current) |
| 827 | return param_stack[0].parameter[parameter]; |
| 828 | return param_defaults[parameter]; |
| 829 | } |
| 830 | |
| 831 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetParameter(espeak_PARAMETER parameter, int value, int relative) |
| 832 | { |
| 833 | #if USE_ASYNC |
| 834 | if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS) |
| 835 | return SetParameter(parameter, value, relative); |
| 836 | |
| 837 | t_espeak_command *c = create_espeak_parameter(parameter, value, relative); |
| 838 | |
| 839 | espeak_ng_STATUS status = fifo_add_command(c); |
| 840 | if (status != ENS_OK) |
| 841 | delete_espeak_command(c); |
| 842 | return status; |
| 843 | #else |
| 844 | return SetParameter(parameter, value, relative); |
| 845 | #endif |
| 846 | } |
| 847 | |
| 848 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetPunctuationList(const wchar_t *punctlist) |
| 849 | { |
| 850 | // Set the list of punctuation which are spoken for "some". |
| 851 | |
| 852 | #if USE_ASYNC |
| 853 | if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS) { |
| 854 | sync_espeak_SetPunctuationList(punctlist); |
| 855 | return ENS_OK; |
| 856 | } |
| 857 | |
| 858 | t_espeak_command *c = create_espeak_punctuation_list(punctlist); |
| 859 | espeak_ng_STATUS status = fifo_add_command(c); |
| 860 | if (status != ENS_OK) |
| 861 | delete_espeak_command(c); |
| 862 | return status; |
| 863 | #else |
| 864 | sync_espeak_SetPunctuationList(punctlist); |
| 865 | return ENS_OK; |
| 866 | #endif |
| 867 | } |
| 868 | |
| 869 | ESPEAK_API void espeak_SetPhonemeTrace(int phonememode, FILE *stream) |
| 870 | { |
| 871 | /* phonememode: Controls the output of phoneme symbols for the text |
| 872 | bits 0-2: |
| 873 | value=0 No phoneme output (default) |
| 874 | value=1 Output the translated phoneme symbols for the text |
| 875 | value=2 as (1), but produces IPA phoneme names rather than ascii |
| 876 | bit 3: output a trace of how the translation was done (showing the matching rules and list entries) |
| 877 | bit 4: produce pho data for mbrola |
| 878 | bit 7: use (bits 8-23) as a tie within multi-letter phonemes names |
| 879 | bits 8-23: separator character, between phoneme names |
| 880 | |
| 881 | stream output stream for the phoneme symbols (and trace). If stream=NULL then it uses stdout. |
| 882 | */ |
| 883 | |
| 884 | option_phonemes = phonememode; |
| 885 | f_trans = stream; |
| 886 | if (stream == NULL) |
| 887 | f_trans = stderr; |
| 888 | } |
| 889 | |
| 890 | ESPEAK_API const char *espeak_TextToPhonemes(const void **textptr, int textmode, int phonememode) |
| 891 | { |
| 892 | /* phoneme_mode |
| 893 | bit 1: 0=eSpeak's ascii phoneme names, 1= International Phonetic Alphabet (as UTF-8 characters). |
| 894 | bit 7: use (bits 8-23) as a tie within multi-letter phonemes names |
| 895 | bits 8-23: separator character, between phoneme names |
| 896 | */ |
| 897 | |
| 898 | if (p_decoder == NULL) |
| 899 | p_decoder = create_text_decoder(); |
| 900 | |
| 901 | if (text_decoder_decode_string_multibyte(p_decoder, *textptr, translator->encoding, textmode) != ENS_OK) |
| 902 | return NULL; |
| 903 | |
| 904 | TranslateClause(translator, NULL, NULL); |
| 905 | *textptr = text_decoder_get_buffer(p_decoder); |
| 906 | |
| 907 | return GetTranslatedPhonemeString(phonememode); |
| 908 | } |
| 909 | |
| 910 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Cancel(void) |
| 911 | { |
| 912 | #if USE_ASYNC |
| 913 | fifo_stop(); |
| 914 | event_clear_all(); |
| 915 | #endif |
| 916 | |
| 917 | #if USE_LIBPCAUDIO |
| 918 | if ((my_mode & ENOUTPUT_MODE_SPEAK_AUDIO) == ENOUTPUT_MODE_SPEAK_AUDIO) |
| 919 | audio_object_flush(my_audio); |
| 920 | #endif |
| 921 | embedded_value[EMBED_T] = 0; // reset echo for pronunciation announcements |
| 922 | |
| 923 | for (int i = 0; i < N_SPEECH_PARAM; i++) |
| 924 | SetParameter(i, saved_parameters[i], 0); |
| 925 | |
| 926 | return ENS_OK; |
| 927 | } |
| 928 | |
| 929 | ESPEAK_API int espeak_IsPlaying(void) |
| 930 | { |
| 931 | #if USE_ASYNC |
| 932 | return fifo_is_busy(); |
| 933 | #else |
| 934 | return 0; |
| 935 | #endif |
| 936 | } |
| 937 | |
| 938 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Synchronize(void) |
| 939 | { |
| 940 | espeak_ng_STATUS berr = err; |
| 941 | #if USE_ASYNC |
| 942 | while (espeak_IsPlaying()) |
| 943 | usleep(20000); |
| 944 | #endif |
| 945 | err = ENS_OK; |
| 946 | return berr; |
| 947 | } |
| 948 | |
| 949 | ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Terminate(void) |
| 950 | { |
| 951 | #if USE_ASYNC |
| 952 | fifo_stop(); |
| 953 | fifo_terminate(); |
| 954 | event_terminate(); |
| 955 | #endif |
| 956 | |
| 957 | if ((my_mode & ENOUTPUT_MODE_SPEAK_AUDIO) == ENOUTPUT_MODE_SPEAK_AUDIO) { |
| 958 | #if USE_LIBPCAUDIO |
| 959 | audio_object_close(my_audio); |
| 960 | audio_object_destroy(my_audio); |
| 961 | my_audio = NULL; |
| 962 | #endif |
| 963 | out_samplerate = 0; |
| 964 | } |
| 965 | |
| 966 | free(event_list); |
| 967 | event_list = NULL; |
| 968 | |
| 969 | free(outbuf); |
| 970 | outbuf = NULL; |
| 971 | |
| 972 | FreePhData(); |
| 973 | FreeVoiceList(); |
| 974 | |
| 975 | DeleteTranslator(translator); |
| 976 | translator = NULL; |
| 977 | |
| 978 | if (p_decoder != NULL) { |
| 979 | destroy_text_decoder(p_decoder); |
| 980 | p_decoder = NULL; |
| 981 | } |
| 982 | |
| 983 | WavegenFini(); |
| 984 | |
| 985 | return ENS_OK; |
| 986 | } |
| 987 | |
| 988 | static const char version_string[] = PACKAGE_VERSION; |
| 989 | ESPEAK_API const char *espeak_Info(const char **ptr) |
| 990 | { |
| 991 | if (ptr != NULL) |
| 992 | *ptr = path_home; |
| 993 | return version_string; |
| 994 | } |
| 995 | |
| 996 | #pragma GCC visibility pop |