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

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