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 25KB

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