eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

speech.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * Copyright (C) 2005 to 2013 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2013-2016 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. #include "config.h"
  20. #include "errno.h"
  21. #include "stdio.h"
  22. #include "ctype.h"
  23. #include "string.h"
  24. #include "stdlib.h"
  25. #include "wchar.h"
  26. #include "locale.h"
  27. #include <assert.h>
  28. #include <time.h>
  29. #include "speech.h"
  30. #include <sys/stat.h>
  31. #ifdef PLATFORM_WINDOWS
  32. #include <fcntl.h>
  33. #include <io.h>
  34. #include <windows.h>
  35. #include <winreg.h>
  36. #else /* PLATFORM_POSIX */
  37. #include <unistd.h>
  38. #endif
  39. #include "espeak_ng.h"
  40. #include "speak_lib.h"
  41. #include "phoneme.h"
  42. #include "synthesize.h"
  43. #include "voice.h"
  44. #include "translate.h"
  45. #include "fifo.h"
  46. #include "event.h"
  47. #include "wave.h"
  48. #ifndef S_ISDIR
  49. #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
  50. #endif
  51. unsigned char *outbuf = NULL;
  52. espeak_EVENT *event_list = NULL;
  53. int event_list_ix = 0;
  54. int n_event_list;
  55. long count_samples;
  56. void *my_audio = NULL;
  57. static const char *option_device = NULL;
  58. static unsigned int my_unique_identifier = 0;
  59. static void *my_user_data = NULL;
  60. static espeak_ng_OUTPUT_MODE my_mode = ENOUTPUT_MODE_SYNCHRONOUS;
  61. static int out_samplerate = 0;
  62. static int voice_samplerate = 22050;
  63. static espeak_ng_STATUS err = ENS_OK;
  64. t_espeak_callback *synth_callback = NULL;
  65. int (*uri_callback)(int, const char *, const char *) = NULL;
  66. int (*phoneme_callback)(const char *) = NULL;
  67. char path_home[N_PATH_HOME]; // this is the espeak-data directory
  68. extern int saved_parameters[N_SPEECH_PARAM]; // Parameters saved on synthesis start
  69. void WVoiceChanged(voice_t *wvoice)
  70. {
  71. // Voice change in wavegen
  72. voice_samplerate = wvoice->samplerate;
  73. }
  74. #ifdef USE_ASYNC
  75. static int dispatch_audio(short *outbuf, int length, espeak_EVENT *event)
  76. {
  77. int a_wave_can_be_played = fifo_is_command_enabled();
  78. switch (my_mode)
  79. {
  80. case ENOUTPUT_MODE_SPEAK_AUDIO:
  81. {
  82. int event_type = 0;
  83. if (event)
  84. event_type = event->type;
  85. if (event_type == espeakEVENT_SAMPLERATE) {
  86. voice_samplerate = event->id.number;
  87. if (out_samplerate != voice_samplerate) {
  88. if (out_samplerate != 0) {
  89. // sound was previously open with a different sample rate
  90. wave_close(my_audio);
  91. sleep(1);
  92. }
  93. out_samplerate = voice_samplerate;
  94. my_audio = wave_open(voice_samplerate, option_device);
  95. if (!my_audio) {
  96. err = ENS_AUDIO_ERROR;
  97. return -1;
  98. }
  99. wave_set_callback_is_output_enabled(fifo_is_command_enabled);
  100. event_init();
  101. }
  102. }
  103. if (outbuf && length && a_wave_can_be_played) {
  104. wave_write(my_audio, (char *)outbuf, 2*length);
  105. }
  106. while (a_wave_can_be_played) {
  107. // TBD: some event are filtered here but some insight might be given
  108. // TBD: in synthesise.cpp for avoiding to create WORDs with size=0.
  109. // TBD: For example sentence "or ALT)." returns three words
  110. // "or", "ALT" and "".
  111. // TBD: the last one has its size=0.
  112. if (event && (event->type == espeakEVENT_WORD) && (event->length == 0))
  113. break;
  114. espeak_ERROR a_error = event_declare(event);
  115. if (a_error != EE_BUFFER_FULL)
  116. break;
  117. usleep(10000);
  118. a_wave_can_be_played = fifo_is_command_enabled();
  119. }
  120. }
  121. break;
  122. case 0:
  123. if (synth_callback)
  124. synth_callback(outbuf, length, event);
  125. break;
  126. }
  127. return a_wave_can_be_played == 0; // 1 = stop synthesis, -1 = error
  128. }
  129. static int create_events(short *outbuf, int length, espeak_EVENT *event_list, uint32_t the_write_pos)
  130. {
  131. int finished;
  132. int i = 0;
  133. // The audio data are written to the output device.
  134. // The list of events in event_list (index: event_list_ix) is read:
  135. // Each event is declared to the "event" object which stores them internally.
  136. // The event object is responsible of calling the external callback
  137. // as soon as the relevant audio sample is played.
  138. do { // for each event
  139. espeak_EVENT *event;
  140. if (event_list_ix == 0)
  141. event = NULL;
  142. else {
  143. event = event_list + i;
  144. event->sample += the_write_pos;
  145. }
  146. finished = dispatch_audio((short *)outbuf, length, event);
  147. length = 0; // the wave data are played once.
  148. i++;
  149. } while ((i < event_list_ix) && !finished);
  150. return finished;
  151. }
  152. int sync_espeak_terminated_msg(uint32_t unique_identifier, void *user_data)
  153. {
  154. int finished = 0;
  155. memset(event_list, 0, 2*sizeof(espeak_EVENT));
  156. event_list[0].type = espeakEVENT_MSG_TERMINATED;
  157. event_list[0].unique_identifier = unique_identifier;
  158. event_list[0].user_data = user_data;
  159. event_list[1].type = espeakEVENT_LIST_TERMINATED;
  160. event_list[1].unique_identifier = unique_identifier;
  161. event_list[1].user_data = user_data;
  162. if (my_mode == ENOUTPUT_MODE_SPEAK_AUDIO) {
  163. while (1) {
  164. espeak_ERROR a_error = event_declare(event_list);
  165. if (a_error != EE_BUFFER_FULL)
  166. break;
  167. usleep(10000);
  168. }
  169. } else {
  170. if (synth_callback)
  171. finished = synth_callback(NULL, 0, event_list);
  172. }
  173. return finished;
  174. }
  175. #endif
  176. #pragma GCC visibility push(default)
  177. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_InitializeOutput(espeak_ng_OUTPUT_MODE output_mode, int buffer_length, const char *device)
  178. {
  179. option_device = device;
  180. my_mode = output_mode;
  181. my_audio = NULL;
  182. option_waveout = 1; // inhibit portaudio callback from wavegen.cpp
  183. out_samplerate = 0;
  184. if (output_mode == (ENOUTPUT_MODE_SYNCHRONOUS | ENOUTPUT_MODE_SPEAK_AUDIO)) {
  185. option_waveout = 0;
  186. WavegenInitSound();
  187. }
  188. // buflength is in mS, allocate 2 bytes per sample
  189. if ((buffer_length == 0) || (output_mode & ENOUTPUT_MODE_SPEAK_AUDIO))
  190. buffer_length = 200;
  191. outbuf_size = (buffer_length * samplerate)/500;
  192. outbuf = (unsigned char *)realloc(outbuf, outbuf_size);
  193. if ((out_start = outbuf) == NULL)
  194. return ENOMEM;
  195. // allocate space for event list. Allow 200 events per second.
  196. // Add a constant to allow for very small buf_length
  197. n_event_list = (buffer_length*200)/1000 + 20;
  198. if ((event_list = (espeak_EVENT *)realloc(event_list, sizeof(espeak_EVENT) * n_event_list)) == NULL)
  199. return ENOMEM;
  200. return ENS_OK;
  201. }
  202. int GetFileLength(const char *filename)
  203. {
  204. struct stat statbuf;
  205. if (stat(filename, &statbuf) != 0)
  206. return 0;
  207. if (S_ISDIR(statbuf.st_mode))
  208. return -2; // a directory
  209. return statbuf.st_size;
  210. }
  211. #pragma GCC visibility pop
  212. char *Alloc(int size)
  213. {
  214. char *p;
  215. if ((p = (char *)malloc(size)) == NULL)
  216. fprintf(stderr, "Can't allocate memory\n"); // I was told that size+1 fixes a crash on 64-bit systems
  217. return p;
  218. }
  219. void Free(void *ptr)
  220. {
  221. if (ptr != NULL)
  222. free(ptr);
  223. }
  224. #pragma GCC visibility push(default)
  225. ESPEAK_NG_API void espeak_ng_InitializePath(const char *path)
  226. {
  227. if (path != NULL) {
  228. sprintf(path_home, "%s/espeak-data", path);
  229. return;
  230. }
  231. #ifdef PLATFORM_WINDOWS
  232. HKEY RegKey;
  233. unsigned long size;
  234. unsigned long var_type;
  235. char *env;
  236. unsigned char buf[sizeof(path_home)-13];
  237. if ((env = getenv("ESPEAK_DATA_PATH")) != NULL) {
  238. sprintf(path_home, "%s/espeak-data", env);
  239. if (GetFileLength(path_home) == -2)
  240. return; // an espeak-data directory exists
  241. }
  242. buf[0] = 0;
  243. RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Speech\\Voices\\Tokens\\eSpeak", 0, KEY_READ, &RegKey);
  244. size = sizeof(buf);
  245. var_type = REG_SZ;
  246. RegQueryValueExA(RegKey, "path", 0, &var_type, buf, &size);
  247. sprintf(path_home, "%s\\espeak-data", buf);
  248. #elif defined(PLATFORM_DOS)
  249. strcpy(path_home, PATH_ESPEAK_DATA);
  250. #else
  251. char *env;
  252. // check for environment variable
  253. if ((env = getenv("ESPEAK_DATA_PATH")) != NULL) {
  254. snprintf(path_home, sizeof(path_home), "%s/espeak-data", env);
  255. if (GetFileLength(path_home) == -2)
  256. return; // an espeak-data directory exists
  257. }
  258. snprintf(path_home, sizeof(path_home), "%s/espeak-data", getenv("HOME"));
  259. if (access(path_home, R_OK) != 0)
  260. strcpy(path_home, PATH_ESPEAK_DATA);
  261. #endif
  262. }
  263. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Initialize(void)
  264. {
  265. int param;
  266. int srate = 22050; // default sample rate 22050 Hz
  267. // It seems that the wctype functions don't work until the locale has been set
  268. // to something other than the default "C". Then, not only Latin1 but also the
  269. // other characters give the correct results with iswalpha() etc.
  270. if (setlocale(LC_CTYPE, "C.UTF-8") == NULL) {
  271. if (setlocale(LC_CTYPE, "UTF-8") == NULL) {
  272. if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL)
  273. setlocale(LC_CTYPE, "");
  274. }
  275. }
  276. espeak_ng_STATUS result = LoadPhData(&srate);
  277. if (result != ENS_OK)
  278. return result;
  279. WavegenInit(srate, 0);
  280. LoadConfig();
  281. memset(&current_voice_selected, 0, sizeof(current_voice_selected));
  282. SetVoiceStack(NULL, "");
  283. SynthesizeInit();
  284. InitNamedata();
  285. VoiceReset(0);
  286. for (param = 0; param < N_SPEECH_PARAM; param++)
  287. param_stack[0].parameter[param] = param_defaults[param];
  288. SetParameter(espeakRATE, 175, 0);
  289. SetParameter(espeakVOLUME, 100, 0);
  290. SetParameter(espeakCAPITALS, option_capitals, 0);
  291. SetParameter(espeakPUNCTUATION, option_punctuation, 0);
  292. SetParameter(espeakWORDGAP, 0, 0);
  293. #ifdef USE_ASYNC
  294. fifo_init();
  295. #endif
  296. option_phonemes = 0;
  297. option_phoneme_events = 0;
  298. return ENS_OK;
  299. }
  300. ESPEAK_NG_API int espeak_ng_GetSampleRate(void)
  301. {
  302. return samplerate;
  303. }
  304. #pragma GCC visibility pop
  305. static espeak_ng_STATUS Synthesize(unsigned int unique_identifier, const void *text, int flags)
  306. {
  307. // Fill the buffer with output sound
  308. int length;
  309. int finished = 0;
  310. int count_buffers = 0;
  311. #ifdef USE_ASYNC
  312. uint32_t a_write_pos = 0;
  313. #endif
  314. if ((outbuf == NULL) || (event_list == NULL))
  315. return ENS_NOT_INITIALIZED;
  316. option_multibyte = flags & 7;
  317. option_ssml = flags & espeakSSML;
  318. option_phoneme_input = flags & espeakPHONEMES;
  319. option_endpause = flags & espeakENDPAUSE;
  320. count_samples = 0;
  321. #ifdef USE_ASYNC
  322. if (my_mode == ENOUTPUT_MODE_SPEAK_AUDIO)
  323. a_write_pos = wave_get_write_position(my_audio);
  324. #endif
  325. if (translator == NULL)
  326. espeak_SetVoiceByName("default");
  327. SpeakNextClause(NULL, text, 0);
  328. if (my_mode == (ENOUTPUT_MODE_SYNCHRONOUS | ENOUTPUT_MODE_SPEAK_AUDIO)) {
  329. for (;;) {
  330. #ifdef PLATFORM_WINDOWS
  331. Sleep(300); // 0.3s
  332. #else
  333. #ifdef USE_NANOSLEEP
  334. struct timespec period;
  335. struct timespec remaining;
  336. period.tv_sec = 0;
  337. period.tv_nsec = 300000000; // 0.3 sec
  338. nanosleep(&period, &remaining);
  339. #else
  340. sleep(1);
  341. #endif
  342. #endif
  343. if (SynthOnTimer() != 0)
  344. break;
  345. }
  346. return ENS_OK;
  347. }
  348. for (;;) {
  349. out_ptr = outbuf;
  350. out_end = &outbuf[outbuf_size];
  351. event_list_ix = 0;
  352. WavegenFill();
  353. length = (out_ptr - outbuf)/2;
  354. count_samples += length;
  355. event_list[event_list_ix].type = espeakEVENT_LIST_TERMINATED; // indicates end of event list
  356. event_list[event_list_ix].unique_identifier = unique_identifier;
  357. event_list[event_list_ix].user_data = my_user_data;
  358. count_buffers++;
  359. if (my_mode == ENOUTPUT_MODE_SPEAK_AUDIO) {
  360. #ifdef USE_ASYNC
  361. finished = create_events((short *)outbuf, length, event_list, a_write_pos);
  362. if (finished < 0)
  363. return ENS_AUDIO_ERROR;
  364. length = 0; // the wave data are played once.
  365. #endif
  366. } else
  367. finished = synth_callback((short *)outbuf, length, event_list);
  368. if (finished) {
  369. SpeakNextClause(NULL, 0, 2); // stop
  370. break;
  371. }
  372. if (Generate(phoneme_list, &n_phoneme_list, 1) == 0) {
  373. if (WcmdqUsed() == 0) {
  374. // don't process the next clause until the previous clause has finished generating speech.
  375. // This ensures that <audio> tag (which causes end-of-clause) is at a sound buffer boundary
  376. event_list[0].type = espeakEVENT_LIST_TERMINATED;
  377. event_list[0].unique_identifier = my_unique_identifier;
  378. event_list[0].user_data = my_user_data;
  379. if (SpeakNextClause(NULL, NULL, 1) == 0) {
  380. #ifdef USE_ASYNC
  381. if (my_mode == ENOUTPUT_MODE_SPEAK_AUDIO) {
  382. if (dispatch_audio(NULL, 0, NULL) < 0)
  383. return ENS_AUDIO_ERROR;
  384. } else
  385. synth_callback(NULL, 0, event_list); // NULL buffer ptr indicates end of data
  386. #else
  387. synth_callback(NULL, 0, event_list); // NULL buffer ptr indicates end of data
  388. #endif
  389. break;
  390. }
  391. }
  392. }
  393. }
  394. return ENS_OK;
  395. }
  396. void MarkerEvent(int type, unsigned int char_position, int value, int value2, unsigned char *out_ptr)
  397. {
  398. // type: 1=word, 2=sentence, 3=named mark, 4=play audio, 5=end, 7=phoneme
  399. espeak_EVENT *ep;
  400. double time;
  401. if ((event_list == NULL) || (event_list_ix >= (n_event_list-2)))
  402. return;
  403. ep = &event_list[event_list_ix++];
  404. ep->type = (espeak_EVENT_TYPE)type;
  405. ep->unique_identifier = my_unique_identifier;
  406. ep->user_data = my_user_data;
  407. ep->text_position = char_position & 0xffffff;
  408. ep->length = char_position >> 24;
  409. time = ((double)(count_samples + mbrola_delay + (out_ptr - out_start)/2)*1000.0)/samplerate;
  410. ep->audio_position = (int)time;
  411. ep->sample = (count_samples + mbrola_delay + (out_ptr - out_start)/2);
  412. if ((type == espeakEVENT_MARK) || (type == espeakEVENT_PLAY))
  413. ep->id.name = &namedata[value];
  414. else if (type == espeakEVENT_PHONEME) {
  415. int *p;
  416. p = (int *)(ep->id.string);
  417. p[0] = value;
  418. p[1] = value2;
  419. } else
  420. ep->id.number = value;
  421. }
  422. espeak_ng_STATUS sync_espeak_Synth(unsigned int unique_identifier, const void *text,
  423. unsigned int position, espeak_POSITION_TYPE position_type,
  424. unsigned int end_position, unsigned int flags, void *user_data)
  425. {
  426. InitText(flags);
  427. my_unique_identifier = unique_identifier;
  428. my_user_data = user_data;
  429. for (int i = 0; i < N_SPEECH_PARAM; i++)
  430. saved_parameters[i] = param_stack[0].parameter[i];
  431. switch (position_type)
  432. {
  433. case POS_CHARACTER:
  434. skip_characters = position;
  435. break;
  436. case POS_WORD:
  437. skip_words = position;
  438. break;
  439. case POS_SENTENCE:
  440. skip_sentences = position;
  441. break;
  442. }
  443. if (skip_characters || skip_words || skip_sentences)
  444. skipping_text = 1;
  445. end_character_position = end_position;
  446. espeak_ng_STATUS aStatus = Synthesize(unique_identifier, text, flags);
  447. #ifdef USE_ASYNC
  448. wave_flush(my_audio);
  449. #endif
  450. return aStatus;
  451. }
  452. espeak_ng_STATUS sync_espeak_Synth_Mark(unsigned int unique_identifier, const void *text,
  453. const char *index_mark, unsigned int end_position,
  454. unsigned int flags, void *user_data)
  455. {
  456. InitText(flags);
  457. my_unique_identifier = unique_identifier;
  458. my_user_data = user_data;
  459. if (index_mark != NULL) {
  460. strncpy0(skip_marker, index_mark, sizeof(skip_marker));
  461. skipping_text = 1;
  462. }
  463. end_character_position = end_position;
  464. return Synthesize(unique_identifier, text, flags | espeakSSML);
  465. }
  466. espeak_ng_STATUS sync_espeak_Key(const char *key)
  467. {
  468. // symbolic name, symbolicname_character - is there a system resource of symbolic names per language?
  469. int letter;
  470. int ix;
  471. ix = utf8_in(&letter, key);
  472. if (key[ix] == 0) // a single character
  473. return sync_espeak_Char(letter);
  474. my_unique_identifier = 0;
  475. my_user_data = NULL;
  476. return Synthesize(0, key, 0); // speak key as a text string
  477. }
  478. espeak_ng_STATUS sync_espeak_Char(wchar_t character)
  479. {
  480. // is there a system resource of character names per language?
  481. char buf[80];
  482. my_unique_identifier = 0;
  483. my_user_data = NULL;
  484. sprintf(buf, "<say-as interpret-as=\"tts:char\">&#%d;</say-as>", character);
  485. return Synthesize(0, buf, espeakSSML);
  486. }
  487. void sync_espeak_SetPunctuationList(const wchar_t *punctlist)
  488. {
  489. // Set the list of punctuation which are spoken for "some".
  490. my_unique_identifier = 0;
  491. my_user_data = NULL;
  492. option_punctlist[0] = 0;
  493. if (punctlist != NULL) {
  494. wcsncpy(option_punctlist, punctlist, N_PUNCTLIST);
  495. option_punctlist[N_PUNCTLIST-1] = 0;
  496. }
  497. }
  498. #pragma GCC visibility push(default)
  499. ESPEAK_API void espeak_SetSynthCallback(t_espeak_callback *SynthCallback)
  500. {
  501. synth_callback = SynthCallback;
  502. #ifdef USE_ASYNC
  503. event_set_callback(synth_callback);
  504. #endif
  505. }
  506. ESPEAK_API void espeak_SetUriCallback(int (*UriCallback)(int, const char *, const char *))
  507. {
  508. uri_callback = UriCallback;
  509. }
  510. ESPEAK_API void espeak_SetPhonemeCallback(int (*PhonemeCallback)(const char *))
  511. {
  512. phoneme_callback = PhonemeCallback;
  513. }
  514. ESPEAK_NG_API espeak_ng_STATUS
  515. espeak_ng_Synthesize(const void *text, size_t size,
  516. unsigned int position,
  517. espeak_POSITION_TYPE position_type,
  518. unsigned int end_position, unsigned int flags,
  519. unsigned int *unique_identifier, void *user_data)
  520. {
  521. (void)size; // unused in non-async modes
  522. static unsigned int temp_identifier;
  523. if (unique_identifier == NULL)
  524. unique_identifier = &temp_identifier;
  525. *unique_identifier = 0;
  526. if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS)
  527. return sync_espeak_Synth(0, text, position, position_type, end_position, flags, user_data);
  528. #ifdef USE_ASYNC
  529. // Create the text command
  530. t_espeak_command *c1 = create_espeak_text(text, size, position, position_type, end_position, flags, user_data);
  531. // Retrieve the unique identifier
  532. *unique_identifier = c1->u.my_text.unique_identifier;
  533. // Create the "terminated msg" command (same uid)
  534. t_espeak_command *c2 = create_espeak_terminated_msg(*unique_identifier, user_data);
  535. // Try to add these 2 commands (single transaction)
  536. if (c1 && c2) {
  537. espeak_ng_STATUS status = fifo_add_commands(c1, c2);
  538. if (status != ENS_OK) {
  539. delete_espeak_command(c1);
  540. delete_espeak_command(c2);
  541. }
  542. return status;
  543. }
  544. delete_espeak_command(c1);
  545. delete_espeak_command(c2);
  546. return ENOMEM;
  547. #else
  548. return sync_espeak_Synth(0, text, position, position_type, end_position, flags, user_data);
  549. #endif
  550. }
  551. ESPEAK_NG_API espeak_ng_STATUS
  552. espeak_ng_SynthesizeMark(const void *text,
  553. size_t size,
  554. const char *index_mark,
  555. unsigned int end_position,
  556. unsigned int flags,
  557. unsigned int *unique_identifier,
  558. void *user_data)
  559. {
  560. (void)size; // unused in non-async modes
  561. static unsigned int temp_identifier;
  562. if (unique_identifier == NULL)
  563. unique_identifier = &temp_identifier;
  564. *unique_identifier = 0;
  565. if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS)
  566. return sync_espeak_Synth_Mark(0, text, index_mark, end_position, flags, user_data);
  567. #ifdef USE_ASYNC
  568. // Create the mark command
  569. t_espeak_command *c1 = create_espeak_mark(text, size, index_mark, end_position,
  570. flags, user_data);
  571. // Retrieve the unique identifier
  572. *unique_identifier = c1->u.my_mark.unique_identifier;
  573. // Create the "terminated msg" command (same uid)
  574. t_espeak_command *c2 = create_espeak_terminated_msg(*unique_identifier, user_data);
  575. // Try to add these 2 commands (single transaction)
  576. if (c1 && c2) {
  577. espeak_ng_STATUS status = fifo_add_commands(c1, c2);
  578. if (status != ENS_OK) {
  579. delete_espeak_command(c1);
  580. delete_espeak_command(c2);
  581. }
  582. return status;
  583. }
  584. delete_espeak_command(c1);
  585. delete_espeak_command(c2);
  586. return ENOMEM;
  587. #else
  588. return sync_espeak_Synth_Mark(0, text, index_mark, end_position, flags, user_data);
  589. #endif
  590. }
  591. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SpeakKeyName(const char *key_name)
  592. {
  593. // symbolic name, symbolicname_character - is there a system resource of symbolicnames per language
  594. if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS)
  595. return sync_espeak_Key(key_name);
  596. #ifdef USE_ASYNC
  597. t_espeak_command *c = create_espeak_key(key_name, NULL);
  598. espeak_ng_STATUS status = fifo_add_command(c);
  599. if (status != ENS_OK)
  600. delete_espeak_command(c);
  601. return status;
  602. #else
  603. return sync_espeak_Key(key_name);
  604. #endif
  605. }
  606. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SpeakCharacter(wchar_t character)
  607. {
  608. // is there a system resource of character names per language?
  609. #ifdef USE_ASYNC
  610. if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS)
  611. return sync_espeak_Char(character);
  612. t_espeak_command *c = create_espeak_char(character, NULL);
  613. espeak_ng_STATUS status = fifo_add_command(c);
  614. if (status != ENS_OK)
  615. delete_espeak_command(c);
  616. return status;
  617. #else
  618. return sync_espeak_Char(character);
  619. #endif
  620. }
  621. ESPEAK_API int espeak_GetParameter(espeak_PARAMETER parameter, int current)
  622. {
  623. // current: 0=default value, 1=current value
  624. if (current)
  625. return param_stack[0].parameter[parameter];
  626. return param_defaults[parameter];
  627. }
  628. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetParameter(espeak_PARAMETER parameter, int value, int relative)
  629. {
  630. #ifdef USE_ASYNC
  631. if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS)
  632. return SetParameter(parameter, value, relative);
  633. t_espeak_command *c = create_espeak_parameter(parameter, value, relative);
  634. espeak_ng_STATUS status = fifo_add_command(c);
  635. if (status != EE_OK)
  636. delete_espeak_command(c);
  637. return status;
  638. #else
  639. return SetParameter(parameter, value, relative);
  640. #endif
  641. }
  642. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_SetPunctuationList(const wchar_t *punctlist)
  643. {
  644. // Set the list of punctuation which are spoken for "some".
  645. #ifdef USE_ASYNC
  646. if (my_mode & ENOUTPUT_MODE_SYNCHRONOUS) {
  647. sync_espeak_SetPunctuationList(punctlist);
  648. return ENS_OK;
  649. }
  650. t_espeak_command *c = create_espeak_punctuation_list(punctlist);
  651. espeak_ng_STATUS status = fifo_add_command(c);
  652. if (status != ENS_OK)
  653. delete_espeak_command(c);
  654. return status;
  655. #else
  656. sync_espeak_SetPunctuationList(punctlist);
  657. return ENS_OK;
  658. #endif
  659. }
  660. ESPEAK_API void espeak_SetPhonemeTrace(int phonememode, FILE *stream)
  661. {
  662. /* phonememode: Controls the output of phoneme symbols for the text
  663. bits 0-2:
  664. value=0 No phoneme output (default)
  665. value=1 Output the translated phoneme symbols for the text
  666. value=2 as (1), but produces IPA phoneme names rather than ascii
  667. bit 3: output a trace of how the translation was done (showing the matching rules and list entries)
  668. bit 4: produce pho data for mbrola
  669. bit 7: use (bits 8-23) as a tie within multi-letter phonemes names
  670. bits 8-23: separator character, between phoneme names
  671. stream output stream for the phoneme symbols (and trace). If stream=NULL then it uses stdout.
  672. */
  673. option_phonemes = phonememode;
  674. f_trans = stream;
  675. if (stream == NULL)
  676. f_trans = stderr;
  677. }
  678. ESPEAK_API const char *espeak_TextToPhonemes(const void **textptr, int textmode, int phonememode)
  679. {
  680. /* phoneme_mode
  681. bit 1: 0=eSpeak's ascii phoneme names, 1= International Phonetic Alphabet (as UTF-8 characters).
  682. bit 7: use (bits 8-23) as a tie within multi-letter phonemes names
  683. bits 8-23: separator character, between phoneme names
  684. */
  685. option_multibyte = textmode & 7;
  686. *textptr = TranslateClause(translator, NULL, *textptr, NULL, NULL);
  687. return GetTranslatedPhonemeString(phonememode);
  688. }
  689. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Cancel(void)
  690. {
  691. #ifdef USE_ASYNC
  692. fifo_stop();
  693. event_clear_all();
  694. if (my_mode == ENOUTPUT_MODE_SPEAK_AUDIO)
  695. wave_close(my_audio);
  696. #endif
  697. embedded_value[EMBED_T] = 0; // reset echo for pronunciation announcements
  698. for (int i = 0; i < N_SPEECH_PARAM; i++)
  699. SetParameter(i, saved_parameters[i], 0);
  700. return ENS_OK;
  701. }
  702. ESPEAK_API int espeak_IsPlaying(void)
  703. {
  704. #ifdef USE_ASYNC
  705. if ((my_mode == ENOUTPUT_MODE_SPEAK_AUDIO) && wave_is_busy(my_audio))
  706. return 1;
  707. return fifo_is_busy();
  708. #else
  709. return 0;
  710. #endif
  711. }
  712. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Synchronize(void)
  713. {
  714. espeak_ng_STATUS berr = err;
  715. #ifdef USE_ASYNC
  716. while (espeak_IsPlaying())
  717. usleep(20000);
  718. #endif
  719. err = ENS_OK;
  720. return berr;
  721. }
  722. extern void FreePhData(void);
  723. extern void FreeVoiceList(void);
  724. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Terminate(void)
  725. {
  726. #ifdef USE_ASYNC
  727. fifo_stop();
  728. fifo_terminate();
  729. event_terminate();
  730. if (my_mode == ENOUTPUT_MODE_SPEAK_AUDIO) {
  731. wave_close(my_audio);
  732. wave_terminate();
  733. out_samplerate = 0;
  734. }
  735. #endif
  736. Free(event_list);
  737. event_list = NULL;
  738. Free(outbuf);
  739. outbuf = NULL;
  740. FreePhData();
  741. FreeVoiceList();
  742. return ENS_OK;
  743. }
  744. ESPEAK_API const char *espeak_Info(const char **ptr)
  745. {
  746. if (ptr != NULL)
  747. *ptr = path_home;
  748. return version_string;
  749. }
  750. #pragma GCC visibility pop