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

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