/native/external/espeak/src/speak.cpp

http://eyes-free.googlecode.com/ · C++ · 846 lines · 658 code · 137 blank · 51 comment · 133 complexity · 94e25c8e3b6e3b24bed02e0015e2f0bb MD5 · raw file

  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 "speech.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifndef PLATFORM_DOS
  25. #ifdef PLATFORM_WINDOWS
  26. #include <windows.h>
  27. #include <winreg.h>
  28. #else
  29. #include <unistd.h>
  30. #endif
  31. #endif
  32. #ifndef NEED_GETOPT
  33. #include <getopt.h>
  34. #endif
  35. #include <time.h>
  36. #include <signal.h>
  37. #include <locale.h>
  38. #include <sys/stat.h>
  39. #include "speak_lib.h"
  40. #include "phoneme.h"
  41. #include "synthesize.h"
  42. #include "voice.h"
  43. #include "translate.h"
  44. extern void Write4Bytes(FILE *f, int value);
  45. char path_home[N_PATH_HOME]; // this is the espeak-data directory
  46. char filetype[5];
  47. char wavefile[200];
  48. int (* uri_callback)(int, const char *, const char *) = NULL;
  49. int (* phoneme_callback)(const char *) = NULL;
  50. FILE *f_wave = NULL;
  51. int quiet = 0;
  52. unsigned int samples_total = 0;
  53. unsigned int samples_split = 0;
  54. unsigned int wavefile_count = 0;
  55. int end_of_sentence = 0;
  56. static const char *help_text =
  57. "\nspeak [options] [\"<words>\"]\n\n"
  58. "-f <text file> Text file to speak\n"
  59. "--stdin Read text input from stdin instead of a file\n\n"
  60. "If neither -f nor --stdin, <words> are spoken, or if none then text is\n"
  61. "spoken from stdin, each line separately.\n\n"
  62. "-a <integer>\n"
  63. "\t Amplitude, 0 to 200, default is 100\n"
  64. "-g <integer>\n"
  65. "\t Word gap. Pause between words, units of 10mS at the default speed\n"
  66. "-l <integer>\n"
  67. "\t Line length. If not zero (which is the default), consider\n"
  68. "\t lines less than this length as end-of-clause\n"
  69. "-p <integer>\n"
  70. "\t Pitch adjustment, 0 to 99, default is 50\n"
  71. "-s <integer>\n"
  72. "\t Speed in words per minute 80 to 370, default is 170\n"
  73. "-v <voice name>\n"
  74. "\t Use voice file of this name from espeak-data/voices\n"
  75. "-w <wave file name>\n"
  76. "\t Write output to this WAV file, rather than speaking it directly\n"
  77. "-b\t Input text is 8-bit encoding\n"
  78. "-m\t Interpret SSML markup, and ignore other < > tags\n"
  79. "-q\t Quiet, don't produce any speech (may be useful with -x)\n"
  80. "-x\t Write phoneme mnemonics to stdout\n"
  81. "-X\t Write phonemes mnemonics and translation trace to stdout\n"
  82. "-z\t No final sentence pause at the end of the text\n"
  83. "--stdout Write speech output to stdout\n"
  84. "--compile=<voice name>\n"
  85. "\t Compile the pronunciation rules and dictionary in the current\n"
  86. "\t directory. =<voice name> is optional and specifies which language\n"
  87. "--punct=\"<characters>\"\n"
  88. "\t Speak the names of punctuation characters during speaking. If\n"
  89. "\t =<characters> is omitted, all punctuation is spoken.\n"
  90. "--split=\"<minutes>\"\n"
  91. "\t Starts a new WAV file every <minutes>. Used with -w\n"
  92. "--voices=<language>\n"
  93. "\t List the available voices for the specified language.\n"
  94. "\t If <language> is omitted, then list all voices.\n"
  95. "-k <integer>\n"
  96. "\t Indicate capital letters with: 1=sound, 2=the word \"capitals\",\n"
  97. "\t higher values = a pitch increase (try -k20).\n";
  98. void DisplayVoices(FILE *f_out, char *language);
  99. USHORT voice_pcnt[N_PEAKS+1][3];
  100. int GetFileLength(const char *filename)
  101. {//====================================
  102. struct stat statbuf;
  103. if(stat(filename,&statbuf) != 0)
  104. return(0);
  105. if((statbuf.st_mode & S_IFMT) == S_IFDIR)
  106. // if(S_ISDIR(statbuf.st_mode))
  107. return(-2); // a directory
  108. return(statbuf.st_size);
  109. } // end of GetFileLength
  110. char *Alloc(int size)
  111. {//==================
  112. char *p;
  113. if((p = (char *)malloc(size)) == NULL)
  114. fprintf(stderr,"Can't allocate memory\n");
  115. return(p);
  116. }
  117. void Free(void *ptr)
  118. {//=================
  119. if(ptr != NULL)
  120. free(ptr);
  121. }
  122. void DisplayVoices(FILE *f_out, char *language)
  123. {//============================================
  124. int ix;
  125. const char *p;
  126. int len;
  127. int count;
  128. int scores = 0;
  129. const espeak_VOICE *v;
  130. const char *lang_name;
  131. char age_buf[12];
  132. const espeak_VOICE **voices;
  133. espeak_VOICE voice_select;
  134. static char genders[4] = {' ','M','F',' '};
  135. if((language != NULL) && (language[0] != 0))
  136. {
  137. // display only voices for the specified language, in order of priority
  138. voice_select.languages = language;
  139. voice_select.age = 0;
  140. voice_select.gender = 0;
  141. voice_select.name = NULL;
  142. voices = espeak_ListVoices(&voice_select);
  143. scores = 1;
  144. }
  145. else
  146. {
  147. voices = espeak_ListVoices(NULL);
  148. }
  149. fprintf(f_out,"Pty Language Age/Gender VoiceName File Other Langs\n");
  150. for(ix=0; (v = voices[ix]) != NULL; ix++)
  151. {
  152. count = 0;
  153. p = v->languages;
  154. while(*p != 0)
  155. {
  156. len = strlen(p+1);
  157. lang_name = p+1;
  158. if(v->age == 0)
  159. strcpy(age_buf," ");
  160. else
  161. sprintf(age_buf,"%3d",v->age);
  162. if(count==0)
  163. {
  164. fprintf(f_out,"%2d %-12s%s%c %-17s %-11s ",
  165. p[0],lang_name,age_buf,genders[v->gender],v->name,v->identifier);
  166. }
  167. else
  168. {
  169. fprintf(f_out,"(%s %d)",lang_name,p[0]);
  170. }
  171. count++;
  172. p += len+2;
  173. }
  174. // if(scores)
  175. // fprintf(f_out,"%3d ",v->score);
  176. fputc('\n',f_out);
  177. }
  178. } // end of DisplayVoices
  179. static int OpenWaveFile(const char *path, int rate)
  180. //=================================================
  181. {
  182. // Set the length of 0x7ffff000 for --stdout
  183. // This will be changed to the correct length for -w (write to file)
  184. static unsigned char wave_hdr[44] = {
  185. 'R','I','F','F',0x24,0xf0,0xff,0x7f,'W','A','V','E','f','m','t',' ',
  186. 0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
  187. 2,0,0x10,0,'d','a','t','a', 0x00,0xf0,0xff,0x7f};
  188. if(path == NULL)
  189. return(2);
  190. if(strcmp(path,"stdout")==0)
  191. f_wave = stdout;
  192. else
  193. f_wave = fopen(path,"wb");
  194. if(f_wave != NULL)
  195. {
  196. fwrite(wave_hdr,1,24,f_wave);
  197. Write4Bytes(f_wave,rate);
  198. Write4Bytes(f_wave,rate * 2);
  199. fwrite(&wave_hdr[32],1,12,f_wave);
  200. return(0);
  201. }
  202. return(1);
  203. } // end of OpenWaveFile
  204. static void CloseWaveFile()
  205. //=========================
  206. {
  207. unsigned int pos;
  208. if((f_wave == NULL) || (f_wave == stdout))
  209. return;
  210. fflush(f_wave);
  211. pos = ftell(f_wave);
  212. fseek(f_wave,4,SEEK_SET);
  213. Write4Bytes(f_wave,pos - 8);
  214. fseek(f_wave,40,SEEK_SET);
  215. Write4Bytes(f_wave,pos - 44);
  216. fclose(f_wave);
  217. f_wave = NULL;
  218. } // end of CloseWaveFile
  219. void MarkerEvent(int type, unsigned int char_position, int value, unsigned char *out_ptr)
  220. {//======================================================================================
  221. // Do nothing in the command-line version.
  222. if(type == 2)
  223. end_of_sentence = 1;
  224. } // end of MarkerEvent
  225. static int WavegenFile(void)
  226. {//=========================
  227. int finished;
  228. unsigned char wav_outbuf[512];
  229. char fname[210];
  230. out_ptr = out_start = wav_outbuf;
  231. out_end = wav_outbuf + sizeof(wav_outbuf);
  232. finished = WavegenFill(0);
  233. if(quiet)
  234. return(finished);
  235. if(f_wave == NULL)
  236. {
  237. sprintf(fname,"%s_%.2d%s",wavefile,++wavefile_count,filetype);
  238. if(OpenWaveFile(fname, samplerate) != 0)
  239. return(1);
  240. }
  241. if(end_of_sentence)
  242. {
  243. end_of_sentence = 0;
  244. if((samples_split > 0 ) && (samples_total > samples_split))
  245. {
  246. CloseWaveFile();
  247. samples_total = 0;
  248. }
  249. }
  250. if(f_wave != NULL)
  251. {
  252. samples_total += (out_ptr - wav_outbuf)/2;
  253. fwrite(wav_outbuf, 1, out_ptr - wav_outbuf, f_wave);
  254. }
  255. return(finished);
  256. } // end of WavegenFile
  257. static void init_path(char *argv0)
  258. {//===============================
  259. #ifdef PLATFORM_WINDOWS
  260. HKEY RegKey;
  261. unsigned long size;
  262. unsigned long var_type;
  263. char *p;
  264. char *env;
  265. unsigned char buf[sizeof(path_home)-12];
  266. if(((env = getenv("ESPEAK_DATA_PATH")) != NULL) && ((strlen(env)+12) < sizeof(path_home)))
  267. {
  268. sprintf(path_home,"%s\\espeak-data",env);
  269. if(GetFileLength(path_home) == -2)
  270. return; // an espeak-data directory exists in the directory specified by environment variable
  271. }
  272. strcpy(path_home,argv0);
  273. if((p = strrchr(path_home,'\\')) != NULL)
  274. {
  275. strcpy(&p[1],"espeak-data");
  276. if(GetFileLength(path_home) == -2)
  277. return; // an espeak-data directory exists in the same directory as the espeak program
  278. }
  279. // otherwise, look in the Windows Registry
  280. buf[0] = 0;
  281. RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Speech\\Voices\\Tokens\\eSpeak", 0, KEY_READ, &RegKey);
  282. size = sizeof(buf);
  283. var_type = REG_SZ;
  284. RegQueryValueEx(RegKey, "path", 0, &var_type, buf, &size);
  285. sprintf(path_home,"%s\\espeak-data",buf);
  286. #else
  287. #ifdef PLATFORM_DOS
  288. strcpy(path_home,PATH_ESPEAK_DATA);
  289. #else
  290. char *env;
  291. if((env = getenv("ESPEAK_DATA_PATH")) != NULL)
  292. {
  293. snprintf(path_home,sizeof(path_home),"%s/espeak-data",env);
  294. if(GetFileLength(path_home) == -2)
  295. return; // an espeak-data directory exists
  296. }
  297. snprintf(path_home,sizeof(path_home),"%s/espeak-data",getenv("HOME"));
  298. if(access(path_home,R_OK) != 0)
  299. {
  300. strcpy(path_home,PATH_ESPEAK_DATA);
  301. }
  302. #endif
  303. #endif
  304. }
  305. static int initialise(void)
  306. {//========================
  307. int param;
  308. int result;
  309. // It seems that the wctype functions don't work until the locale has been set
  310. // to something other than the default "C". Then, not only Latin1 but also the
  311. // other characters give the correct results with iswalpha() etc.
  312. #ifdef PLATFORM_RISCOS
  313. setlocale(LC_CTYPE,"ISO8859-1");
  314. #else
  315. if(setlocale(LC_CTYPE,"en_US.UTF-8") == NULL)
  316. {
  317. if(setlocale(LC_CTYPE,"UTF-8") == NULL)
  318. setlocale(LC_CTYPE,"");
  319. }
  320. #endif
  321. WavegenInit(22050,0); // 22050
  322. if((result = LoadPhData()) != 1)
  323. {
  324. if(result == -1)
  325. fprintf(stderr,"Failed to load espeak-data\n");
  326. else
  327. fprintf(stderr,"Wrong version of espeak-data 0x%x (expects 0x%x)\n",result,version_phdata);
  328. }
  329. LoadConfig();
  330. SetVoiceStack(NULL);
  331. SynthesizeInit();
  332. for(param=0; param<N_SPEECH_PARAM; param++)
  333. param_stack[0].parameter[param] = param_defaults[param];
  334. return(0);
  335. }
  336. static void StopSpeak(int unused)
  337. {//==============================
  338. signal(SIGINT,SIG_IGN);
  339. // DEBUG
  340. // printf("\n*** Interrupting speech output (use Ctrl-D to actually quit).\n");
  341. fflush(stdout);
  342. SpeakNextClause(NULL,NULL,5);
  343. signal(SIGINT,StopSpeak);
  344. } // end of StopSpeak()
  345. #ifdef NEED_GETOPT
  346. struct option {
  347. char *name;
  348. int has_arg;
  349. int *flag;
  350. int val;
  351. };
  352. static int optind;
  353. static int optional_argument;
  354. static const char *arg_opts = "afklpsvw"; // which options have arguments
  355. static char *opt_string="";
  356. #define no_argument 0
  357. #define optional_argument 2
  358. #endif
  359. int main (int argc, char **argv)
  360. //==============================
  361. {
  362. static struct option long_options[] =
  363. {
  364. /* These options set a flag. */
  365. // {"verbose", no_argument, &verbose_flag, 1},
  366. // {"brief", no_argument, &verbose_flag, 0},
  367. /* These options don't set a flag.
  368. We distinguish them by their indices. */
  369. {"help", no_argument, 0, 'h'},
  370. {"stdin", no_argument, 0, 0x100},
  371. {"compile-debug", optional_argument, 0, 0x101},
  372. {"compile", optional_argument, 0, 0x102},
  373. {"punct", optional_argument, 0, 0x103},
  374. {"voices", optional_argument, 0, 0x104},
  375. {"stdout", no_argument, 0, 0x105},
  376. {"split", optional_argument, 0, 0x106},
  377. {0, 0, 0, 0}
  378. };
  379. static const char *err_load = "Failed to read ";
  380. FILE *f_text=NULL;
  381. const char *p_text=NULL;
  382. int option_index = 0;
  383. int c;
  384. int value;
  385. int speed=170;
  386. int ix;
  387. char *optarg2;
  388. int amp = 100; // default
  389. int wordgap = 0;
  390. int speaking = 0;
  391. int flag_stdin = 0;
  392. int flag_compile = 0;
  393. int pitch_adjustment = 50;
  394. char filename[120];
  395. char voicename[40];
  396. char dictname[40];
  397. voicename[0] = 0;
  398. mbrola_name[0] = 0;
  399. dictname[0] = 0;
  400. wavefile[0] = 0;
  401. filename[0] = 0;
  402. option_linelength = 0;
  403. option_phonemes = 0;
  404. option_waveout = 0;
  405. option_wordgap = 0;
  406. option_endpause = 1;
  407. option_multibyte = espeakCHARS_AUTO; // auto
  408. f_trans = stdout;
  409. init_path(argv[0]);
  410. #ifdef NEED_GETOPT
  411. optind = 1;
  412. while(optind < argc)
  413. {
  414. int len;
  415. char *p;
  416. if((c = *opt_string) == 0)
  417. {
  418. opt_string = argv[optind];
  419. if(opt_string[0] != '-')
  420. break;
  421. optind++;
  422. opt_string++;
  423. c = *opt_string;
  424. }
  425. opt_string++;
  426. p = optarg2 = opt_string;
  427. if(c == '-')
  428. {
  429. opt_string="";
  430. for(ix=0; ;ix++)
  431. {
  432. if(long_options[ix].name == 0)
  433. break;
  434. len = strlen(long_options[ix].name);
  435. if(memcmp(long_options[ix].name,p,len)==0)
  436. {
  437. c = long_options[ix].val;
  438. optarg2 = NULL;
  439. if((long_options[ix].has_arg != 0) && (p[len]=='='))
  440. {
  441. optarg2 = &p[len+1];
  442. }
  443. break;
  444. }
  445. }
  446. }
  447. else
  448. if(strchr(arg_opts,c) != NULL)
  449. {
  450. opt_string="";
  451. if(optarg2[0]==0)
  452. {
  453. // the option's value is in the next argument
  454. optarg2 = argv[optind++];
  455. }
  456. }
  457. #else
  458. while(true)
  459. {
  460. c = getopt_long (argc, argv, "a:bf:g:hk:l:p:qs:v:w:xXmz",
  461. long_options, &option_index);
  462. /* Detect the end of the options. */
  463. if (c == -1)
  464. break;
  465. optarg2 = optarg;
  466. #endif
  467. switch (c)
  468. {
  469. case 'b':
  470. option_multibyte = espeakCHARS_8BIT;
  471. break;
  472. case 'h':
  473. printf("\nspeak text-to-speech: %s\n%s",version_string,help_text);
  474. exit(0);
  475. break;
  476. case 'k':
  477. option_capitals = atoi(optarg2);
  478. break;
  479. case 'x':
  480. option_phonemes = 1;
  481. break;
  482. case 'X':
  483. option_phonemes = 2;
  484. break;
  485. case 'm':
  486. option_ssml = 1;
  487. break;
  488. case 'p':
  489. pitch_adjustment = atoi(optarg2);
  490. if(pitch_adjustment > 99) pitch_adjustment = 99;
  491. break;
  492. case 'q':
  493. quiet = 1;
  494. break;
  495. case 'f':
  496. strncpy0(filename,optarg2,sizeof(filename));
  497. break;
  498. case 'l':
  499. value = 0;
  500. value = atoi(optarg2);
  501. option_linelength = value;
  502. break;
  503. case 'a':
  504. amp = atoi(optarg2);
  505. break;
  506. case 's':
  507. speed = atoi(optarg2);
  508. break;
  509. case 'g':
  510. wordgap = atoi(optarg2);
  511. break;
  512. case 'v':
  513. strncpy0(voicename,optarg2,sizeof(voicename));
  514. break;
  515. case 'w':
  516. option_waveout = 1;
  517. strncpy0(wavefile,optarg2,sizeof(wavefile));
  518. break;
  519. case 'z':
  520. option_endpause = 0;
  521. break;
  522. case 0x100: // --stdin
  523. flag_stdin = 1;
  524. break;
  525. case 0x105: // --stdout
  526. option_waveout = 1;
  527. strcpy(wavefile,"stdout");
  528. break;
  529. case 0x101: // --compile-debug
  530. case 0x102: // --compile
  531. if(optarg2 != NULL)
  532. strncpy0(voicename,optarg2,sizeof(voicename));
  533. flag_compile = c;
  534. break;
  535. case 0x103: // --punct
  536. option_punctuation = 1;
  537. if(optarg2 != NULL)
  538. {
  539. ix = 0;
  540. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  541. option_punctlist[N_PUNCTLIST-1] = 0;
  542. option_punctuation = 2;
  543. }
  544. break;
  545. case 0x104: // --voices
  546. DisplayVoices(stdout,optarg2);
  547. exit(0);
  548. case 0x106: // -- split
  549. if(optarg2 == NULL)
  550. samples_split = 30; // default 30 minutes
  551. else
  552. samples_split = atoi(optarg2);
  553. break;
  554. default:
  555. exit(0);
  556. }
  557. }
  558. initialise();
  559. if(flag_compile)
  560. {
  561. LoadVoice(voicename,5);
  562. #ifdef PLATFORM_DOS
  563. char path_dsource[sizeof(path_home)+20];
  564. strcpy(path_dsource,path_home);
  565. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  566. strcat(path_dsource,"dictsource\\");
  567. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  568. #else
  569. #ifdef PLATFORM_WINDOWS
  570. char path_dsource[sizeof(path_home)+20];
  571. strcpy(path_dsource,path_home);
  572. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  573. strcat(path_dsource,"dictsource\\");
  574. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  575. #else
  576. CompileDictionary(NULL,dictionary_name,NULL,NULL, flag_compile & 0x1);
  577. #endif
  578. #endif
  579. exit(0);
  580. }
  581. if(voicename[0] == 0)
  582. strcpy(voicename,"default");
  583. if(SetVoiceByName(voicename) != EE_OK)
  584. {
  585. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  586. exit(2);
  587. }
  588. SetParameter(espeakRATE,speed,0);
  589. SetParameter(espeakVOLUME,amp,0);
  590. SetParameter(espeakCAPITALS,option_capitals,0);
  591. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  592. SetParameter(espeakWORDGAP,wordgap,0);
  593. if(pitch_adjustment != 50)
  594. {
  595. SetParameter(espeakPITCH,pitch_adjustment,0);
  596. }
  597. DoVoiceChange(voice);
  598. if(filename[0]==0)
  599. {
  600. if((optind < argc) && (flag_stdin == 0))
  601. {
  602. // there's a non-option parameter, and no -f or --stdin
  603. // use it as text
  604. p_text = argv[optind];
  605. }
  606. else
  607. {
  608. f_text = stdin;
  609. if(flag_stdin == 0)
  610. option_linelength = -1; // single input lines on stdin
  611. }
  612. }
  613. else
  614. {
  615. f_text = fopen(filename,"r");
  616. }
  617. if((f_text == NULL) && (p_text == NULL))
  618. {
  619. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  620. exit(1);
  621. }
  622. if(option_waveout || quiet)
  623. {
  624. if(quiet)
  625. {
  626. // no sound output
  627. OpenWaveFile(NULL,samplerate);
  628. option_waveout = 1;
  629. }
  630. else
  631. {
  632. // write sound output to a WAV file
  633. samples_split = (samplerate * samples_split) * 60;
  634. if(samples_split)
  635. {
  636. // don't open the wav file until we start generating speech
  637. char *extn;
  638. extn = strrchr(wavefile,'.');
  639. if((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4))
  640. {
  641. strcpy(filetype,extn);
  642. *extn = 0;
  643. }
  644. }
  645. else
  646. if(OpenWaveFile(wavefile,samplerate) != 0)
  647. {
  648. fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
  649. exit(3);
  650. }
  651. }
  652. InitText(0);
  653. SpeakNextClause(f_text,p_text,0);
  654. ix = 1;
  655. for(;;)
  656. {
  657. if(WavegenFile() != 0)
  658. {
  659. if(ix == 0)
  660. break; // finished, wavegen command queue is empty
  661. }
  662. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  663. {
  664. ix = SpeakNextClause(NULL,NULL,1);
  665. }
  666. }
  667. CloseWaveFile();
  668. }
  669. else
  670. {
  671. // Silence on ^C or SIGINT
  672. // signal(SIGINT,StopSpeak);
  673. // output sound using portaudio
  674. WavegenInitSound();
  675. InitText(0);
  676. SpeakNextClause(f_text,p_text,0);
  677. if(option_quiet)
  678. {
  679. while(SpeakNextClause(NULL,NULL,1) != 0);
  680. return(0);
  681. }
  682. #ifdef USE_PORTAUDIO
  683. speaking = 1;
  684. while(speaking)
  685. {
  686. // NOTE: if nanosleep() isn't recognised on your system, try replacing
  687. // this by sleep(1);
  688. #ifdef PLATFORM_WINDOWS
  689. Sleep(300); // 0.3s
  690. #else
  691. #ifdef USE_NANOSLEEP
  692. struct timespec period;
  693. struct timespec remaining;
  694. period.tv_sec = 0;
  695. period.tv_nsec = 300000000; // 0.3 sec
  696. nanosleep(&period,&remaining);
  697. #else
  698. sleep(1);
  699. #endif
  700. #endif
  701. if(SynthOnTimer() != 0)
  702. speaking = 0;
  703. }
  704. #else
  705. fprintf(stderr,"-w option must be used because the program was built without a sound interface\n");
  706. #endif // USE_PORTAUDIO
  707. }
  708. return(0);
  709. }