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

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