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

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