eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

speak_lib.cpp 28KB

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