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

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