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.

speak_lib.cpp 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2011 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include "StdAfx.h"
  20. #include "stdio.h"
  21. #include "ctype.h"
  22. #include "string.h"
  23. #include "stdlib.h"
  24. #include "wchar.h"
  25. #include "locale.h"
  26. #include <assert.h>
  27. #include <time.h>
  28. #include "speech.h"
  29. #include <sys/stat.h>
  30. #ifndef PLATFORM_WINDOWS
  31. #include <unistd.h>
  32. #endif
  33. #include "speak_lib.h"
  34. #include "phoneme.h"
  35. #include "synthesize.h"
  36. #include "voice.h"
  37. #include "translate.h"
  38. #include "debug.h"
  39. #include "fifo.h"
  40. #include "event.h"
  41. #include "wave.h"
  42. unsigned char *outbuf=NULL;
  43. espeak_EVENT *event_list=NULL;
  44. int event_list_ix=0;
  45. int n_event_list;
  46. long count_samples;
  47. void* my_audio=NULL;
  48. static unsigned int my_unique_identifier=0;
  49. static void* my_user_data=NULL;
  50. static espeak_AUDIO_OUTPUT my_mode=AUDIO_OUTPUT_SYNCHRONOUS;
  51. static int synchronous_mode = 1;
  52. static int out_samplerate = 0;
  53. static int voice_samplerate = 22050;
  54. t_espeak_callback* synth_callback = NULL;
  55. int (* uri_callback)(int, const char *, const char *) = NULL;
  56. int (* phoneme_callback)(const char *) = NULL;
  57. char path_home[N_PATH_HOME]; // this is the espeak-data directory
  58. void WVoiceChanged(voice_t *wvoice)
  59. {//=================================
  60. // Voice change in wavegen
  61. voice_samplerate = wvoice->samplerate;
  62. }
  63. #ifdef USE_ASYNC
  64. static int dispatch_audio(short* outbuf, int length, espeak_EVENT* event)
  65. {//======================================================================
  66. ENTER("dispatch_audio");
  67. int a_wave_can_be_played = fifo_is_command_enabled();
  68. #ifdef DEBUG_ENABLED
  69. SHOW("*** dispatch_audio > uid=%d, [write=%p (%d bytes)], sample=%d, a_wave_can_be_played = %d\n",
  70. (event) ? event->unique_identifier : 0, wave_test_get_write_buffer(), 2*length,
  71. (event) ? event->sample : 0,
  72. a_wave_can_be_played);
  73. #endif
  74. switch(my_mode)
  75. {
  76. case AUDIO_OUTPUT_PLAYBACK:
  77. {
  78. int event_type=0;
  79. int event_data=0;
  80. if(event)
  81. {
  82. event_type = event->type;
  83. event_data = event->id.number;
  84. }
  85. if(event_type == espeakEVENT_SAMPLERATE)
  86. {
  87. voice_samplerate = event->id.number;
  88. if(out_samplerate != voice_samplerate)
  89. {
  90. if(out_samplerate != 0)
  91. {
  92. // sound was previously open with a different sample rate
  93. wave_close(my_audio);
  94. sleep(1);
  95. }
  96. out_samplerate = voice_samplerate;
  97. wave_init(voice_samplerate);
  98. wave_set_callback_is_output_enabled( fifo_is_command_enabled);
  99. my_audio = wave_open("alsa");
  100. event_init();
  101. }
  102. }
  103. if (outbuf && length && a_wave_can_be_played)
  104. {
  105. wave_write (my_audio, (char*)outbuf, 2*length);
  106. }
  107. while(a_wave_can_be_played) {
  108. // TBD: some event are filtered here but some insight might be given
  109. // TBD: in synthesise.cpp for avoiding to create WORDs with size=0.
  110. // TBD: For example sentence "or ALT)." returns three words
  111. // "or", "ALT" and "".
  112. // TBD: the last one has its size=0.
  113. if (event && (event->type == espeakEVENT_WORD) && (event->length==0))
  114. {
  115. break;
  116. }
  117. espeak_ERROR a_error = event_declare(event);
  118. if (a_error != EE_BUFFER_FULL)
  119. {
  120. break;
  121. }
  122. SHOW_TIME("dispatch_audio > EE_BUFFER_FULL\n");
  123. usleep(10000);
  124. a_wave_can_be_played = fifo_is_command_enabled();
  125. }
  126. }
  127. break;
  128. case AUDIO_OUTPUT_RETRIEVAL:
  129. if (synth_callback)
  130. {
  131. synth_callback(outbuf, length, event);
  132. }
  133. break;
  134. case AUDIO_OUTPUT_SYNCHRONOUS:
  135. case AUDIO_OUTPUT_SYNCH_PLAYBACK:
  136. break;
  137. }
  138. if (!a_wave_can_be_played)
  139. {
  140. SHOW_TIME("dispatch_audio > synth must be stopped!\n");
  141. }
  142. SHOW_TIME("LEAVE dispatch_audio\n");
  143. return (a_wave_can_be_played==0); // 1 = stop synthesis
  144. }
  145. static int create_events(short* outbuf, int length, espeak_EVENT* event, uint32_t the_write_pos)
  146. {//=====================================================================
  147. int finished;
  148. int i=0;
  149. // The audio data are written to the output device.
  150. // The list of events in event_list (index: event_list_ix) is read:
  151. // Each event is declared to the "event" object which stores them internally.
  152. // The event object is responsible of calling the external callback
  153. // as soon as the relevant audio sample is played.
  154. do
  155. { // for each event
  156. espeak_EVENT* event;
  157. if (event_list_ix == 0)
  158. {
  159. event = NULL;
  160. }
  161. else
  162. {
  163. event = event_list + i;
  164. #ifdef DEBUG_ENABLED
  165. SHOW("Synthesize: event->sample(%d) + %d = %d\n", event->sample, the_write_pos, event->sample + the_write_pos);
  166. #endif
  167. event->sample += the_write_pos;
  168. }
  169. #ifdef DEBUG_ENABLED
  170. SHOW("*** Synthesize: i=%d (event_list_ix=%d), length=%d\n",i,event_list_ix,length);
  171. #endif
  172. finished = dispatch_audio((short *)outbuf, length, event);
  173. length = 0; // the wave data are played once.
  174. i++;
  175. } while((i < event_list_ix) && !finished);
  176. return finished;
  177. }
  178. int sync_espeak_terminated_msg( uint32_t unique_identifier, void* user_data)
  179. {//=====================================================================
  180. ENTER("sync_espeak_terminated_msg");
  181. int finished=0;
  182. memset(event_list, 0, 2*sizeof(espeak_EVENT));
  183. event_list[0].type = espeakEVENT_MSG_TERMINATED;
  184. event_list[0].unique_identifier = unique_identifier;
  185. event_list[0].user_data = user_data;
  186. event_list[1].type = espeakEVENT_LIST_TERMINATED;
  187. event_list[1].unique_identifier = unique_identifier;
  188. event_list[1].user_data = user_data;
  189. if (my_mode==AUDIO_OUTPUT_PLAYBACK)
  190. {
  191. while(1)
  192. {
  193. espeak_ERROR a_error = event_declare(event_list);
  194. if (a_error != EE_BUFFER_FULL)
  195. {
  196. break;
  197. }
  198. SHOW_TIME("sync_espeak_terminated_msg > EE_BUFFER_FULL\n");
  199. usleep(10000);
  200. }
  201. }
  202. else
  203. {
  204. if (synth_callback)
  205. {
  206. finished=synth_callback(NULL,0,event_list);
  207. }
  208. }
  209. return finished;
  210. }
  211. #endif
  212. static void select_output(espeak_AUDIO_OUTPUT output_type)
  213. {//=======================================================
  214. my_mode = output_type;
  215. my_audio = NULL;
  216. synchronous_mode = 1;
  217. option_waveout = 1; // inhibit portaudio callback from wavegen.cpp
  218. out_samplerate = 0;
  219. switch(my_mode)
  220. {
  221. case AUDIO_OUTPUT_PLAYBACK:
  222. // wave_init() is now called just before the first wave_write()
  223. synchronous_mode = 0;
  224. break;
  225. case AUDIO_OUTPUT_RETRIEVAL:
  226. synchronous_mode = 0;
  227. break;
  228. case AUDIO_OUTPUT_SYNCHRONOUS:
  229. break;
  230. case AUDIO_OUTPUT_SYNCH_PLAYBACK:
  231. option_waveout = 0;
  232. WavegenInitSound();
  233. break;
  234. }
  235. } // end of select_output
  236. int GetFileLength(const char *filename)
  237. {//====================================
  238. struct stat statbuf;
  239. if(stat(filename,&statbuf) != 0)
  240. return(0);
  241. if((statbuf.st_mode & S_IFMT) == S_IFDIR)
  242. // if(S_ISDIR(statbuf.st_mode))
  243. return(-2); // a directory
  244. return(statbuf.st_size);
  245. } // end of GetFileLength
  246. char *Alloc(int size)
  247. {//==================
  248. char *p;
  249. if((p = (char *)malloc(size)) == NULL)
  250. fprintf(stderr,"Can't allocate memory\n"); // I was told that size+1 fixes a crash on 64-bit systems
  251. return(p);
  252. }
  253. void Free(void *ptr)
  254. {//=================
  255. if(ptr != NULL)
  256. free(ptr);
  257. }
  258. static void init_path(const char *path)
  259. {//====================================
  260. #ifdef PLATFORM_WINDOWS
  261. HKEY RegKey;
  262. unsigned long size;
  263. unsigned long var_type;
  264. char *env;
  265. unsigned char buf[sizeof(path_home)-13];
  266. if(path != NULL)
  267. {
  268. sprintf(path_home,"%s/espeak-data",path);
  269. return;
  270. }
  271. if((env = getenv("ESPEAK_DATA_PATH")) != NULL)
  272. {
  273. sprintf(path_home,"%s/espeak-data",env);
  274. if(GetFileLength(path_home) == -2)
  275. return; // an espeak-data directory exists
  276. }
  277. buf[0] = 0;
  278. RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Speech\\Voices\\Tokens\\eSpeak", 0, KEY_READ, &RegKey);
  279. size = sizeof(buf);
  280. var_type = REG_SZ;
  281. RegQueryValueExA(RegKey, "path", 0, &var_type, buf, &size);
  282. sprintf(path_home,"%s\\espeak-data",buf);
  283. #else
  284. char *env;
  285. if(path != NULL)
  286. {
  287. snprintf(path_home,sizeof(path_home),"%s/espeak-data",path);
  288. return;
  289. }
  290. // check for environment variable
  291. if((env = getenv("ESPEAK_DATA_PATH")) != NULL)
  292. {
  293. snprintf(path_home,sizeof(path_home),"%s/espeak-data",env);
  294. if(GetFileLength(path_home) == -2)
  295. return; // an espeak-data directory exists
  296. }
  297. snprintf(path_home,sizeof(path_home),"%s/espeak-data",getenv("HOME"));
  298. if(access(path_home,R_OK) != 0)
  299. {
  300. strcpy(path_home,PATH_ESPEAK_DATA);
  301. }
  302. #endif
  303. }
  304. static int initialise(int control)
  305. {//===============================
  306. int param;
  307. int result;
  308. LoadConfig();
  309. WavegenInit(22050,0); // 22050
  310. if((result = LoadPhData()) != 1)
  311. {
  312. if(result == -1)
  313. {
  314. fprintf(stderr,"Failed to load espeak-data\n");
  315. if((control & espeakINITIALIZE_DONT_EXIT) == 0)
  316. {
  317. exit(1);
  318. }
  319. }
  320. else
  321. fprintf(stderr,"Wrong version of espeak-data 0x%x (expects 0x%x) at %s\n",result,version_phdata,path_home);
  322. }
  323. memset(&current_voice_selected,0,sizeof(current_voice_selected));
  324. SetVoiceStack(NULL, "");
  325. SynthesizeInit();
  326. InitNamedata();
  327. for(param=0; param<N_SPEECH_PARAM; param++)
  328. param_stack[0].parameter[param] = param_defaults[param];
  329. return(0);
  330. }
  331. static espeak_ERROR Synthesize(unsigned int unique_identifier, const void *text, int flags)
  332. {//========================================================================================
  333. // Fill the buffer with output sound
  334. int length;
  335. int finished = 0;
  336. int count_buffers = 0;
  337. #ifdef USE_ASYNC
  338. uint32_t a_write_pos=0;
  339. #endif
  340. #ifdef DEBUG_ENABLED
  341. ENTER("Synthesize");
  342. if (text)
  343. {
  344. SHOW("Synthesize > uid=%d, flags=%d, >>>text=%s<<<\n", unique_identifier, flags, text);
  345. }
  346. #endif
  347. if((outbuf==NULL) || (event_list==NULL))
  348. return(EE_INTERNAL_ERROR); // espeak_Initialize() has not been called
  349. option_multibyte = flags & 7;
  350. option_ssml = flags & espeakSSML;
  351. option_phoneme_input = flags & espeakPHONEMES;
  352. option_endpause = flags & espeakENDPAUSE;
  353. count_samples = 0;
  354. #ifdef USE_ASYNC
  355. if(my_mode == AUDIO_OUTPUT_PLAYBACK)
  356. {
  357. a_write_pos = wave_get_write_position(my_audio);
  358. }
  359. #endif
  360. if(translator == NULL)
  361. {
  362. SetVoiceByName("default");
  363. }
  364. SpeakNextClause(NULL,text,0);
  365. if(my_mode == AUDIO_OUTPUT_SYNCH_PLAYBACK)
  366. {
  367. for(;;)
  368. {
  369. #ifdef PLATFORM_WINDOWS
  370. Sleep(300); // 0.3s
  371. #else
  372. #ifdef USE_NANOSLEEP
  373. struct timespec period;
  374. struct timespec remaining;
  375. period.tv_sec = 0;
  376. period.tv_nsec = 300000000; // 0.3 sec
  377. nanosleep(&period,&remaining);
  378. #else
  379. sleep(1);
  380. #endif
  381. #endif
  382. if(SynthOnTimer() != 0)
  383. break;
  384. }
  385. return(EE_OK);
  386. }
  387. for(;;)
  388. {
  389. #ifdef DEBUG_ENABLED
  390. SHOW("Synthesize > %s\n","for (next)");
  391. #endif
  392. out_ptr = outbuf;
  393. out_end = &outbuf[outbuf_size];
  394. event_list_ix = 0;
  395. WavegenFill(0);
  396. length = (out_ptr - outbuf)/2;
  397. count_samples += length;
  398. event_list[event_list_ix].type = espeakEVENT_LIST_TERMINATED; // indicates end of event list
  399. event_list[event_list_ix].unique_identifier = my_unique_identifier;
  400. event_list[event_list_ix].user_data = my_user_data;
  401. count_buffers++;
  402. if (my_mode==AUDIO_OUTPUT_PLAYBACK)
  403. {
  404. #ifdef USE_ASYNC
  405. finished = create_events((short *)outbuf, length, event_list, a_write_pos);
  406. length = 0; // the wave data are played once.
  407. #endif
  408. }
  409. else
  410. {
  411. finished = synth_callback((short *)outbuf, length, event_list);
  412. }
  413. if(finished)
  414. {
  415. SpeakNextClause(NULL,0,2); // stop
  416. break;
  417. }
  418. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  419. {
  420. if(WcmdqUsed() == 0)
  421. {
  422. // don't process the next clause until the previous clause has finished generating speech.
  423. // This ensures that <audio> tag (which causes end-of-clause) is at a sound buffer boundary
  424. event_list[0].type = espeakEVENT_LIST_TERMINATED;
  425. event_list[0].unique_identifier = my_unique_identifier;
  426. event_list[0].user_data = my_user_data;
  427. if(SpeakNextClause(NULL,NULL,1)==0)
  428. {
  429. #ifdef USE_ASYNC
  430. if (my_mode==AUDIO_OUTPUT_PLAYBACK)
  431. {
  432. dispatch_audio(NULL, 0, NULL); // TBD: test case
  433. }
  434. else
  435. {
  436. synth_callback(NULL, 0, event_list); // NULL buffer ptr indicates end of data
  437. }
  438. #else
  439. synth_callback(NULL, 0, event_list); // NULL buffer ptr indicates end of data
  440. #endif
  441. break;
  442. }
  443. }
  444. }
  445. }
  446. return(EE_OK);
  447. } // end of Synthesize
  448. #ifdef DEBUG_ENABLED
  449. static const char* label[] = {
  450. "END_OF_EVENT_LIST",
  451. "WORD",
  452. "SENTENCE",
  453. "MARK",
  454. "PLAY",
  455. "END"};
  456. #endif
  457. void MarkerEvent(int type, unsigned int char_position, int value, unsigned char *out_ptr)
  458. {//======================================================================================
  459. // type: 1=word, 2=sentence, 3=named mark, 4=play audio, 5=end
  460. ENTER("MarkerEvent");
  461. espeak_EVENT *ep;
  462. double time;
  463. if((event_list == NULL) || (event_list_ix >= (n_event_list-2)))
  464. return;
  465. ep = &event_list[event_list_ix++];
  466. ep->type = (espeak_EVENT_TYPE)type;
  467. ep->unique_identifier = my_unique_identifier;
  468. ep->user_data = my_user_data;
  469. ep->text_position = char_position & 0xffffff;
  470. ep->length = char_position >> 24;
  471. time = (double(count_samples + mbrola_delay + (out_ptr - out_start)/2)*1000.0)/samplerate;
  472. ep->audio_position = int(time);
  473. ep->sample = (count_samples + mbrola_delay + (out_ptr - out_start)/2);
  474. #ifdef DEBUG_ENABLED
  475. SHOW("MarkerEvent > count_samples=%d, out_ptr=%x, out_start=0x%x\n",count_samples, out_ptr, out_start);
  476. SHOW("*** MarkerEvent > type=%s, uid=%d, text_pos=%d, length=%d, audio_position=%d, sample=%d\n",
  477. label[ep->type], ep->unique_identifier, ep->text_position, ep->length,
  478. ep->audio_position, ep->sample);
  479. #endif
  480. if((type == espeakEVENT_MARK) || (type == espeakEVENT_PLAY))
  481. ep->id.name = &namedata[value];
  482. else
  483. ep->id.number = value;
  484. } // end of MarkerEvent
  485. espeak_ERROR sync_espeak_Synth(unsigned int unique_identifier, const void *text, size_t size,
  486. unsigned int position, espeak_POSITION_TYPE position_type,
  487. unsigned int end_position, unsigned int flags, void* user_data)
  488. {//===========================================================================
  489. #ifdef DEBUG_ENABLED
  490. ENTER("sync_espeak_Synth");
  491. SHOW("sync_espeak_Synth > position=%d, position_type=%d, end_position=%d, flags=%d, user_data=0x%x, text=%s\n", position, position_type, end_position, flags, user_data, text);
  492. #endif
  493. espeak_ERROR aStatus;
  494. InitText(flags);
  495. my_unique_identifier = unique_identifier;
  496. my_user_data = user_data;
  497. switch(position_type)
  498. {
  499. case POS_CHARACTER:
  500. skip_characters = position;
  501. break;
  502. case POS_WORD:
  503. skip_words = position;
  504. break;
  505. case POS_SENTENCE:
  506. skip_sentences = position;
  507. break;
  508. }
  509. if(skip_characters || skip_words || skip_sentences)
  510. skipping_text = 1;
  511. end_character_position = end_position;
  512. aStatus = Synthesize(unique_identifier, text, flags);
  513. #ifdef USE_ASYNC
  514. wave_flush(my_audio);
  515. #endif
  516. SHOW_TIME("LEAVE sync_espeak_Synth");
  517. return aStatus;
  518. } // end of sync_espeak_Synth
  519. espeak_ERROR sync_espeak_Synth_Mark(unsigned int unique_identifier, const void *text, size_t size,
  520. const char *index_mark, unsigned int end_position,
  521. unsigned int flags, void* user_data)
  522. {//=========================================================================
  523. espeak_ERROR aStatus;
  524. InitText(flags);
  525. my_unique_identifier = unique_identifier;
  526. my_user_data = user_data;
  527. if(index_mark != NULL)
  528. {
  529. strncpy0(skip_marker, index_mark, sizeof(skip_marker));
  530. skipping_text = 1;
  531. }
  532. end_character_position = end_position;
  533. aStatus = Synthesize(unique_identifier, text, flags | espeakSSML);
  534. SHOW_TIME("LEAVE sync_espeak_Synth_Mark");
  535. return (aStatus);
  536. } // end of sync_espeak_Synth_Mark
  537. void sync_espeak_Key(const char *key)
  538. {//==================================
  539. // symbolic name, symbolicname_character - is there a system resource of symbolic names per language?
  540. int letter;
  541. int ix;
  542. ix = utf8_in(&letter,key);
  543. if(key[ix] == 0)
  544. {
  545. // a single character
  546. sync_espeak_Char(letter);
  547. return;
  548. }
  549. my_unique_identifier = 0;
  550. my_user_data = NULL;
  551. Synthesize(0, key,0); // speak key as a text string
  552. }
  553. void sync_espeak_Char(wchar_t character)
  554. {//=====================================
  555. // is there a system resource of character names per language?
  556. char buf[80];
  557. my_unique_identifier = 0;
  558. my_user_data = NULL;
  559. sprintf(buf,"<say-as interpret-as=\"tts:char\">&#%d;</say-as>",character);
  560. Synthesize(0, buf,espeakSSML);
  561. }
  562. void sync_espeak_SetPunctuationList(const wchar_t *punctlist)
  563. {//==========================================================
  564. // Set the list of punctuation which are spoken for "some".
  565. my_unique_identifier = 0;
  566. my_user_data = NULL;
  567. wcsncpy(option_punctlist, punctlist, N_PUNCTLIST);
  568. option_punctlist[N_PUNCTLIST-1] = 0;
  569. } // end of sync_espeak_SetPunctuationList
  570. #pragma GCC visibility push(default)
  571. ESPEAK_API void espeak_SetSynthCallback(t_espeak_callback* SynthCallback)
  572. {//======================================================================
  573. ENTER("espeak_SetSynthCallback");
  574. synth_callback = SynthCallback;
  575. #ifdef USE_ASYNC
  576. event_set_callback(synth_callback);
  577. #endif
  578. }
  579. ESPEAK_API void espeak_SetUriCallback(int (* UriCallback)(int, const char*, const char *))
  580. {//=======================================================================================
  581. ENTER("espeak_SetUriCallback");
  582. uri_callback = UriCallback;
  583. }
  584. ESPEAK_API void espeak_SetPhonemeCallback(int (* PhonemeCallback)(const char*))
  585. {//===========================================================================
  586. phoneme_callback = PhonemeCallback;
  587. }
  588. ESPEAK_API int espeak_Initialize(espeak_AUDIO_OUTPUT output_type, int buf_length, const char *path, int options)
  589. {//=============================================================================================================
  590. ENTER("espeak_Initialize");
  591. int param;
  592. // It seems that the wctype functions don't work until the locale has been set
  593. // to something other than the default "C". Then, not only Latin1 but also the
  594. // other characters give the correct results with iswalpha() etc.
  595. #ifdef PLATFORM_RISCOS
  596. setlocale(LC_CTYPE,"ISO8859-1");
  597. #else
  598. if(setlocale(LC_CTYPE,"en_US.UTF-8") == NULL)
  599. {
  600. if(setlocale(LC_CTYPE,"UTF-8") == NULL)
  601. setlocale(LC_CTYPE,"");
  602. }
  603. #endif
  604. init_path(path);
  605. initialise(options);
  606. select_output(output_type);
  607. if(f_logespeak)
  608. {
  609. fprintf(f_logespeak,"INIT mode %d options 0x%x\n",output_type,options);
  610. }
  611. // buflength is in mS, allocate 2 bytes per sample
  612. if(buf_length == 0)
  613. buf_length = 200;
  614. outbuf_size = (buf_length * samplerate)/500;
  615. outbuf = (unsigned char*)realloc(outbuf,outbuf_size);
  616. if((out_start = outbuf) == NULL)
  617. return(EE_INTERNAL_ERROR);
  618. // allocate space for event list. Allow 200 events per second.
  619. // Add a constant to allow for very small buf_length
  620. n_event_list = (buf_length*200)/1000 + 20;
  621. if((event_list = (espeak_EVENT *)realloc(event_list,sizeof(espeak_EVENT) * n_event_list)) == NULL)
  622. return(EE_INTERNAL_ERROR);
  623. option_phonemes = 0;
  624. option_mbrola_phonemes = 0;
  625. option_phoneme_events = (options & 1);
  626. VoiceReset(0);
  627. // SetVoiceByName("default");
  628. for(param=0; param<N_SPEECH_PARAM; param++)
  629. param_stack[0].parameter[param] = param_defaults[param];
  630. SetParameter(espeakRATE,175,0);
  631. SetParameter(espeakVOLUME,100,0);
  632. SetParameter(espeakCAPITALS,option_capitals,0);
  633. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  634. SetParameter(espeakWORDGAP,0,0);
  635. // DoVoiceChange(voice);
  636. #ifdef USE_ASYNC
  637. fifo_init();
  638. #endif
  639. return(samplerate);
  640. }
  641. ESPEAK_API espeak_ERROR espeak_Synth(const void *text, size_t size,
  642. unsigned int position,
  643. espeak_POSITION_TYPE position_type,
  644. unsigned int end_position, unsigned int flags,
  645. unsigned int* unique_identifier, void* user_data)
  646. {//=====================================================================================
  647. #ifdef DEBUG_ENABLED
  648. ENTER("espeak_Synth");
  649. SHOW("espeak_Synth > position=%d, position_type=%d, end_position=%d, flags=%d, user_data=0x%x, text=%s\n", position, position_type, end_position, flags, user_data, text);
  650. #endif
  651. if(f_logespeak)
  652. {
  653. fprintf(f_logespeak,"\nSYNTH posn %d %d %d flags 0x%x\n%s\n",position,end_position,position_type,flags, (const char *)text);
  654. fflush(f_logespeak);
  655. }
  656. espeak_ERROR a_error=EE_INTERNAL_ERROR;
  657. static unsigned int temp_identifier;
  658. if (unique_identifier == NULL)
  659. {
  660. unique_identifier = &temp_identifier;
  661. }
  662. *unique_identifier = 0;
  663. if(synchronous_mode)
  664. {
  665. return(sync_espeak_Synth(0,text,size,position,position_type,end_position,flags,user_data));
  666. }
  667. #ifdef USE_ASYNC
  668. // Create the text command
  669. t_espeak_command* c1 = create_espeak_text(text, size, position, position_type, end_position, flags, user_data);
  670. // Retrieve the unique identifier
  671. *unique_identifier = c1->u.my_text.unique_identifier;
  672. // Create the "terminated msg" command (same uid)
  673. t_espeak_command* c2 = create_espeak_terminated_msg(*unique_identifier, user_data);
  674. // Try to add these 2 commands (single transaction)
  675. if (c1 && c2)
  676. {
  677. a_error = fifo_add_commands(c1, c2);
  678. if (a_error != EE_OK)
  679. {
  680. delete_espeak_command(c1);
  681. delete_espeak_command(c2);
  682. c1=c2=NULL;
  683. }
  684. }
  685. else
  686. {
  687. delete_espeak_command(c1);
  688. delete_espeak_command(c2);
  689. }
  690. #endif
  691. return a_error;
  692. } // end of espeak_Synth
  693. ESPEAK_API espeak_ERROR espeak_Synth_Mark(const void *text, size_t size,
  694. const char *index_mark,
  695. unsigned int end_position,
  696. unsigned int flags,
  697. unsigned int* unique_identifier,
  698. void* user_data)
  699. {//=========================================================================
  700. #ifdef DEBUG_ENABLED
  701. ENTER("espeak_Synth_Mark");
  702. SHOW("espeak_Synth_Mark > index_mark=%s, end_position=%d, flags=%d, text=%s\n", index_mark, end_position, flags, text);
  703. #endif
  704. espeak_ERROR a_error=EE_OK;
  705. static unsigned int temp_identifier;
  706. if(f_logespeak)
  707. {
  708. fprintf(f_logespeak,"\nSYNTH MARK %s posn %d flags 0x%x\n%s\n",index_mark,end_position,flags, (const char *)text);
  709. }
  710. if (unique_identifier == NULL)
  711. {
  712. unique_identifier = &temp_identifier;
  713. }
  714. *unique_identifier = 0;
  715. if(synchronous_mode)
  716. {
  717. return(sync_espeak_Synth_Mark(0,text,size,index_mark,end_position,flags,user_data));
  718. }
  719. #ifdef USE_ASYNC
  720. // Create the mark command
  721. t_espeak_command* c1 = create_espeak_mark(text, size, index_mark, end_position,
  722. flags, user_data);
  723. // Retrieve the unique identifier
  724. *unique_identifier = c1->u.my_mark.unique_identifier;
  725. // Create the "terminated msg" command (same uid)
  726. t_espeak_command* c2 = create_espeak_terminated_msg(*unique_identifier, user_data);
  727. // Try to add these 2 commands (single transaction)
  728. if (c1 && c2)
  729. {
  730. a_error = fifo_add_commands(c1, c2);
  731. if (a_error != EE_OK)
  732. {
  733. delete_espeak_command(c1);
  734. delete_espeak_command(c2);
  735. c1=c2=NULL;
  736. }
  737. }
  738. else
  739. {
  740. delete_espeak_command(c1);
  741. delete_espeak_command(c2);
  742. }
  743. #endif
  744. return a_error;
  745. } // end of espeak_Synth_Mark
  746. ESPEAK_API espeak_ERROR espeak_Key(const char *key)
  747. {//================================================
  748. ENTER("espeak_Key");
  749. // symbolic name, symbolicname_character - is there a system resource of symbolicnames per language
  750. if(f_logespeak)
  751. {
  752. fprintf(f_logespeak,"\nKEY %s\n",key);
  753. }
  754. espeak_ERROR a_error = EE_OK;
  755. if(synchronous_mode)
  756. {
  757. sync_espeak_Key(key);
  758. return(EE_OK);
  759. }
  760. #ifdef USE_ASYNC
  761. t_espeak_command* c = create_espeak_key( key, NULL);
  762. a_error = fifo_add_command(c);
  763. if (a_error != EE_OK)
  764. {
  765. delete_espeak_command(c);
  766. }
  767. #endif
  768. return a_error;
  769. }
  770. ESPEAK_API espeak_ERROR espeak_Char(wchar_t character)
  771. {//===========================================
  772. ENTER("espeak_Char");
  773. // is there a system resource of character names per language?
  774. if(f_logespeak)
  775. {
  776. fprintf(f_logespeak,"\nCHAR U+%x\n",character);
  777. }
  778. #ifdef USE_ASYNC
  779. espeak_ERROR a_error;
  780. if(synchronous_mode)
  781. {
  782. sync_espeak_Char(character);
  783. return(EE_OK);
  784. }
  785. t_espeak_command* c = create_espeak_char( character, NULL);
  786. a_error = fifo_add_command(c);
  787. if (a_error != EE_OK)
  788. {
  789. delete_espeak_command(c);
  790. }
  791. return a_error;
  792. #else
  793. sync_espeak_Char(character);
  794. return(EE_OK);
  795. #endif
  796. }
  797. ESPEAK_API espeak_ERROR espeak_SetVoiceByName(const char *name)
  798. {//============================================================
  799. ENTER("espeak_SetVoiceByName");
  800. //#ifdef USE_ASYNC
  801. // I don't think there's a need to queue change voice requests
  802. #ifdef deleted
  803. espeak_ERROR a_error;
  804. if(synchronous_mode)
  805. {
  806. return(SetVoiceByName(name));
  807. }
  808. t_espeak_command* c = create_espeak_voice_name(name);
  809. a_error = fifo_add_command(c);
  810. if (a_error != EE_OK)
  811. {
  812. delete_espeak_command(c);
  813. }
  814. return a_error;
  815. #else
  816. return(SetVoiceByName(name));
  817. #endif
  818. } // end of espeak_SetVoiceByName
  819. ESPEAK_API espeak_ERROR espeak_SetVoiceByProperties(espeak_VOICE *voice_selector)
  820. {//==============================================================================
  821. ENTER("espeak_SetVoiceByProperties");
  822. //#ifdef USE_ASYNC
  823. #ifdef deleted
  824. espeak_ERROR a_error;
  825. if(synchronous_mode)
  826. {
  827. return(SetVoiceByProperties(voice_selector));
  828. }
  829. t_espeak_command* c = create_espeak_voice_spec( voice_selector);
  830. a_error = fifo_add_command(c);
  831. if (a_error != EE_OK)
  832. {
  833. delete_espeak_command(c);
  834. }
  835. return a_error;
  836. #else
  837. return(SetVoiceByProperties(voice_selector));
  838. #endif
  839. } // end of espeak_SetVoiceByProperties
  840. ESPEAK_API int espeak_GetParameter(espeak_PARAMETER parameter, int current)
  841. {//========================================================================
  842. ENTER("espeak_GetParameter");
  843. // current: 0=default value, 1=current value
  844. if(current)
  845. {
  846. return(param_stack[0].parameter[parameter]);
  847. }
  848. else
  849. {
  850. return(param_defaults[parameter]);
  851. }
  852. } // end of espeak_GetParameter
  853. ESPEAK_API espeak_ERROR espeak_SetParameter(espeak_PARAMETER parameter, int value, int relative)
  854. {//=============================================================================================
  855. ENTER("espeak_SetParameter");
  856. if(f_logespeak)
  857. {
  858. fprintf(f_logespeak,"SETPARAM %d %d %d\n",parameter,value,relative);
  859. }
  860. #ifdef USE_ASYNC
  861. espeak_ERROR a_error;
  862. if(synchronous_mode)
  863. {
  864. SetParameter(parameter,value,relative);
  865. return(EE_OK);
  866. }
  867. t_espeak_command* c = create_espeak_parameter(parameter, value, relative);
  868. a_error = fifo_add_command(c);
  869. if (a_error != EE_OK)
  870. {
  871. delete_espeak_command(c);
  872. }
  873. return a_error;
  874. #else
  875. SetParameter(parameter,value,relative);
  876. return(EE_OK);
  877. #endif
  878. }
  879. ESPEAK_API espeak_ERROR espeak_SetPunctuationList(const wchar_t *punctlist)
  880. {//================================================================
  881. ENTER("espeak_SetPunctuationList");
  882. // Set the list of punctuation which are spoken for "some".
  883. #ifdef USE_ASYNC
  884. espeak_ERROR a_error;
  885. if(synchronous_mode)
  886. {
  887. sync_espeak_SetPunctuationList(punctlist);
  888. return(EE_OK);
  889. }
  890. t_espeak_command* c = create_espeak_punctuation_list( punctlist);
  891. a_error = fifo_add_command(c);
  892. if (a_error != EE_OK)
  893. {
  894. delete_espeak_command(c);
  895. }
  896. return a_error;
  897. #else
  898. sync_espeak_SetPunctuationList(punctlist);
  899. return(EE_OK);
  900. #endif
  901. } // end of espeak_SetPunctuationList
  902. ESPEAK_API void espeak_SetPhonemeTrace(int value, FILE *stream)
  903. {//============================================================
  904. ENTER("espeak_SetPhonemes");
  905. /* Controls the output of phoneme symbols for the text
  906. bits 0-3:
  907. value=0 No phoneme output (default)
  908. value=1 Output the translated phoneme symbols for the text
  909. value=2 as (1), but also output a trace of how the translation was done (matching rules and list entries)
  910. value=3 as (1), but produces IPA phoneme names rather than ascii
  911. bit 4: produce mbrola pho data
  912. */
  913. option_phonemes = value & 3;
  914. option_mbrola_phonemes = value & 16;
  915. f_trans = stream;
  916. if(stream == NULL)
  917. f_trans = stderr;
  918. } // end of espeak_SetPhonemes
  919. ESPEAK_API void espeak_CompileDictionary(const char *path, FILE *log, int flags)
  920. {//=============================================================================
  921. ENTER("espeak_CompileDictionary");
  922. CompileDictionary(path, dictionary_name, log, NULL, flags);
  923. } // end of espeak_CompileDirectory
  924. ESPEAK_API espeak_ERROR espeak_Cancel(void)
  925. {//===============================
  926. #ifdef USE_ASYNC
  927. ENTER("espeak_Cancel");
  928. fifo_stop();
  929. event_clear_all();
  930. if(my_mode == AUDIO_OUTPUT_PLAYBACK)
  931. {
  932. wave_close(my_audio);
  933. }
  934. SHOW_TIME("espeak_Cancel > LEAVE");
  935. #endif
  936. embedded_value[EMBED_T] = 0; // reset echo for pronunciation announcements
  937. return EE_OK;
  938. } // end of espeak_Cancel
  939. ESPEAK_API int espeak_IsPlaying(void)
  940. {//==================================
  941. // ENTER("espeak_IsPlaying");
  942. #ifdef USE_ASYNC
  943. if((my_mode == AUDIO_OUTPUT_PLAYBACK) && wave_is_busy(my_audio))
  944. return(1);
  945. return(fifo_is_busy());
  946. #else
  947. return(0);
  948. #endif
  949. } // end of espeak_IsPlaying
  950. ESPEAK_API espeak_ERROR espeak_Synchronize(void)
  951. {//=============================================
  952. #ifdef USE_ASYNC
  953. SHOW_TIME("espeak_Synchronize > ENTER");
  954. while (espeak_IsPlaying())
  955. {
  956. usleep(20000);
  957. }
  958. #endif
  959. SHOW_TIME("espeak_Synchronize > LEAVE");
  960. return EE_OK;
  961. } // end of espeak_Synchronize
  962. extern void FreePhData(void);
  963. extern void FreeVoiceList(void);
  964. ESPEAK_API espeak_ERROR espeak_Terminate(void)
  965. {//===========================================
  966. ENTER("espeak_Terminate");
  967. #ifdef USE_ASYNC
  968. fifo_stop();
  969. fifo_terminate();
  970. event_terminate();
  971. if(my_mode == AUDIO_OUTPUT_PLAYBACK)
  972. {
  973. wave_close(my_audio);
  974. wave_terminate();
  975. out_samplerate = 0;
  976. }
  977. #endif
  978. Free(event_list);
  979. event_list = NULL;
  980. Free(outbuf);
  981. outbuf = NULL;
  982. FreePhData();
  983. FreeVoiceList();
  984. if(f_logespeak)
  985. {
  986. fclose(f_logespeak);
  987. f_logespeak = NULL;
  988. }
  989. return EE_OK;
  990. } // end of espeak_Terminate
  991. ESPEAK_API const char *espeak_Info(const char **ptr)
  992. {//=================================================
  993. if(ptr != NULL)
  994. {
  995. *ptr = path_home;
  996. }
  997. return(version_string);
  998. }
  999. #pragma GCC visibility pop