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.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 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 "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_phoneme_input = 1;
  408. option_multibyte = espeakCHARS_AUTO; // auto
  409. f_trans = stdout;
  410. init_path(argv[0]);
  411. #ifdef NEED_GETOPT
  412. optind = 1;
  413. while(optind < argc)
  414. {
  415. int len;
  416. char *p;
  417. if((c = *opt_string) == 0)
  418. {
  419. opt_string = argv[optind];
  420. if(opt_string[0] != '-')
  421. break;
  422. optind++;
  423. opt_string++;
  424. c = *opt_string;
  425. }
  426. opt_string++;
  427. p = optarg2 = opt_string;
  428. if(c == '-')
  429. {
  430. opt_string="";
  431. for(ix=0; ;ix++)
  432. {
  433. if(long_options[ix].name == 0)
  434. break;
  435. len = strlen(long_options[ix].name);
  436. if(memcmp(long_options[ix].name,p,len)==0)
  437. {
  438. c = long_options[ix].val;
  439. optarg2 = NULL;
  440. if((long_options[ix].has_arg != 0) && (p[len]=='='))
  441. {
  442. optarg2 = &p[len+1];
  443. }
  444. break;
  445. }
  446. }
  447. }
  448. else
  449. if(strchr(arg_opts,c) != NULL)
  450. {
  451. opt_string="";
  452. if(optarg2[0]==0)
  453. {
  454. // the option's value is in the next argument
  455. optarg2 = argv[optind++];
  456. }
  457. }
  458. #else
  459. while(true)
  460. {
  461. c = getopt_long (argc, argv, "a:bf:g:hk:l:p:qs:v:w:xXmz",
  462. long_options, &option_index);
  463. /* Detect the end of the options. */
  464. if (c == -1)
  465. break;
  466. optarg2 = optarg;
  467. #endif
  468. switch (c)
  469. {
  470. case 'b':
  471. option_multibyte = espeakCHARS_8BIT;
  472. break;
  473. case 'h':
  474. printf("\nspeak text-to-speech: %s\n%s",version_string,help_text);
  475. exit(0);
  476. break;
  477. case 'k':
  478. option_capitals = atoi(optarg2);
  479. break;
  480. case 'x':
  481. option_phonemes = 1;
  482. break;
  483. case 'X':
  484. option_phonemes = 2;
  485. break;
  486. case 'm':
  487. option_ssml = 1;
  488. break;
  489. case 'p':
  490. pitch_adjustment = atoi(optarg2);
  491. if(pitch_adjustment > 99) pitch_adjustment = 99;
  492. break;
  493. case 'q':
  494. quiet = 1;
  495. break;
  496. case 'f':
  497. strncpy0(filename,optarg2,sizeof(filename));
  498. break;
  499. case 'l':
  500. value = 0;
  501. value = atoi(optarg2);
  502. option_linelength = value;
  503. break;
  504. case 'a':
  505. amp = atoi(optarg2);
  506. break;
  507. case 's':
  508. speed = atoi(optarg2);
  509. break;
  510. case 'g':
  511. wordgap = atoi(optarg2);
  512. break;
  513. case 'v':
  514. strncpy0(voicename,optarg2,sizeof(voicename));
  515. break;
  516. case 'w':
  517. option_waveout = 1;
  518. strncpy0(wavefile,optarg2,sizeof(wavefile));
  519. break;
  520. case 'z':
  521. option_endpause = 0;
  522. break;
  523. case 0x100: // --stdin
  524. flag_stdin = 1;
  525. break;
  526. case 0x105: // --stdout
  527. option_waveout = 1;
  528. strcpy(wavefile,"stdout");
  529. break;
  530. case 0x101: // --compile-debug
  531. case 0x102: // --compile
  532. if(optarg2 != NULL)
  533. strncpy0(voicename,optarg2,sizeof(voicename));
  534. flag_compile = c;
  535. break;
  536. case 0x103: // --punct
  537. option_punctuation = 1;
  538. if(optarg2 != NULL)
  539. {
  540. ix = 0;
  541. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  542. option_punctlist[N_PUNCTLIST-1] = 0;
  543. option_punctuation = 2;
  544. }
  545. break;
  546. case 0x104: // --voices
  547. DisplayVoices(stdout,optarg2);
  548. exit(0);
  549. case 0x106: // -- split
  550. if(optarg2 == NULL)
  551. samples_split = 30; // default 30 minutes
  552. else
  553. samples_split = atoi(optarg2);
  554. break;
  555. default:
  556. exit(0);
  557. }
  558. }
  559. initialise();
  560. if(flag_compile)
  561. {
  562. LoadVoice(voicename,5);
  563. #ifdef PLATFORM_DOS
  564. char path_dsource[sizeof(path_home)+20];
  565. strcpy(path_dsource,path_home);
  566. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  567. strcat(path_dsource,"dictsource\\");
  568. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  569. #else
  570. #ifdef PLATFORM_WINDOWS
  571. char path_dsource[sizeof(path_home)+20];
  572. strcpy(path_dsource,path_home);
  573. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  574. strcat(path_dsource,"dictsource\\");
  575. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  576. #else
  577. CompileDictionary(NULL,dictionary_name,NULL,NULL, flag_compile & 0x1);
  578. #endif
  579. #endif
  580. exit(0);
  581. }
  582. if(voicename[0] == 0)
  583. strcpy(voicename,"default");
  584. if(SetVoiceByName(voicename) != EE_OK)
  585. {
  586. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  587. exit(2);
  588. }
  589. SetParameter(espeakRATE,speed,0);
  590. SetParameter(espeakVOLUME,amp,0);
  591. SetParameter(espeakCAPITALS,option_capitals,0);
  592. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  593. SetParameter(espeakWORDGAP,wordgap,0);
  594. if(pitch_adjustment != 50)
  595. {
  596. SetParameter(espeakPITCH,pitch_adjustment,0);
  597. }
  598. DoVoiceChange(voice);
  599. if(filename[0]==0)
  600. {
  601. if((optind < argc) && (flag_stdin == 0))
  602. {
  603. // there's a non-option parameter, and no -f or --stdin
  604. // use it as text
  605. p_text = argv[optind];
  606. }
  607. else
  608. {
  609. f_text = stdin;
  610. if(flag_stdin == 0)
  611. option_linelength = -1; // single input lines on stdin
  612. }
  613. }
  614. else
  615. {
  616. f_text = fopen(filename,"r");
  617. }
  618. if((f_text == NULL) && (p_text == NULL))
  619. {
  620. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  621. exit(1);
  622. }
  623. if(option_waveout || quiet)
  624. {
  625. if(quiet)
  626. {
  627. // no sound output
  628. OpenWaveFile(NULL,samplerate);
  629. option_waveout = 1;
  630. }
  631. else
  632. {
  633. // write sound output to a WAV file
  634. samples_split = (samplerate * samples_split) * 60;
  635. if(samples_split)
  636. {
  637. // don't open the wav file until we start generating speech
  638. char *extn;
  639. extn = strrchr(wavefile,'.');
  640. if((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4))
  641. {
  642. strcpy(filetype,extn);
  643. *extn = 0;
  644. }
  645. }
  646. else
  647. if(OpenWaveFile(wavefile,samplerate) != 0)
  648. {
  649. fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
  650. exit(3);
  651. }
  652. }
  653. InitText(0);
  654. SpeakNextClause(f_text,p_text,0);
  655. ix = 1;
  656. for(;;)
  657. {
  658. if(WavegenFile() != 0)
  659. {
  660. if(ix == 0)
  661. break; // finished, wavegen command queue is empty
  662. }
  663. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  664. {
  665. ix = SpeakNextClause(NULL,NULL,1);
  666. }
  667. }
  668. CloseWaveFile();
  669. }
  670. else
  671. {
  672. // Silence on ^C or SIGINT
  673. // signal(SIGINT,StopSpeak);
  674. // output sound using portaudio
  675. WavegenInitSound();
  676. InitText(0);
  677. SpeakNextClause(f_text,p_text,0);
  678. if(option_quiet)
  679. {
  680. while(SpeakNextClause(NULL,NULL,1) != 0);
  681. return(0);
  682. }
  683. #ifdef USE_PORTAUDIO
  684. speaking = 1;
  685. while(speaking)
  686. {
  687. // NOTE: if nanosleep() isn't recognised on your system, try replacing
  688. // this by sleep(1);
  689. #ifdef PLATFORM_WINDOWS
  690. Sleep(300); // 0.3s
  691. #else
  692. #ifdef USE_NANOSLEEP
  693. struct timespec period;
  694. struct timespec remaining;
  695. period.tv_sec = 0;
  696. period.tv_nsec = 300000000; // 0.3 sec
  697. nanosleep(&period,&remaining);
  698. #else
  699. sleep(1);
  700. #endif
  701. #endif
  702. if(SynthOnTimer() != 0)
  703. speaking = 0;
  704. }
  705. #else
  706. fprintf(stderr,"-w option must be used because the program was built without a sound interface\n");
  707. #endif // USE_PORTAUDIO
  708. }
  709. return(0);
  710. }