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

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