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

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