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 23KB

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