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

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