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

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