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

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