eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

speech.c 24KB

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