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

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