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.

speak_lib.c 25KB

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