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

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