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

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