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.

espeak.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /***************************************************************************
  2. * Copyright (C) 2006 to 2013 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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #ifndef NEED_GETOPT
  23. #include <getopt.h>
  24. #endif
  25. #include <time.h>
  26. #include <sys/stat.h>
  27. #include "speak_lib.h"
  28. // This version of the command-line speak program uses the
  29. // libespeak.so.1 library
  30. static const char *help_text =
  31. "\nespeak [options] [\"<words>\"]\n\n"
  32. "-f <text file> Text file to speak\n"
  33. "--stdin Read text input from stdin instead of a file\n\n"
  34. "If neither -f nor --stdin, then <words> are spoken, or if none then text\n"
  35. "is spoken from stdin, each line separately.\n\n"
  36. "-a <integer>\n"
  37. "\t Amplitude, 0 to 200, default is 100\n"
  38. "-g <integer>\n"
  39. "\t Word gap. Pause between words, units of 10mS at the default speed\n"
  40. "-k <integer>\n"
  41. "\t Indicate capital letters with: 1=sound, 2=the word \"capitals\",\n"
  42. "\t higher values indicate a pitch increase (try -k20).\n"
  43. "-l <integer>\n"
  44. "\t Line length. If not zero (which is the default), consider\n"
  45. "\t lines less than this length as end-of-clause\n"
  46. "-p <integer>\n"
  47. "\t Pitch adjustment, 0 to 99, default is 50\n"
  48. "-s <integer>\n"
  49. "\t Speed in words per minute, 80 to 450, default is 175\n"
  50. "-v <voice name>\n"
  51. "\t Use voice file of this name from espeak-data/voices\n"
  52. "-w <wave file name>\n"
  53. "\t Write speech to this WAV file, rather than speaking it directly\n"
  54. "-b\t Input text encoding, 1=UTF8, 2=8 bit, 4=16 bit \n"
  55. "-m\t Interpret SSML markup, and ignore other < > tags\n"
  56. "-q\t Quiet, don't produce any speech (may be useful with -x)\n"
  57. "-x\t Write phoneme mnemonics to stdout\n"
  58. "-X\t Write phonemes mnemonics and translation trace to stdout\n"
  59. "-z\t No final sentence pause at the end of the text\n"
  60. "--compile=<voice name>\n"
  61. "\t Compile pronunciation rules and dictionary from the current\n"
  62. "\t directory. <voice name> specifies the language\n"
  63. "--ipa Write phonemes to stdout using International Phonetic Alphabet\n"
  64. "\t --ipa=1 Use ties, --ipa=2 Use ZWJ\n"
  65. "--path=\"<path>\"\n"
  66. "\t Specifies the directory containing the espeak-data directory\n"
  67. "--pho Write mbrola phoneme data (.pho) to stdout or to the file in --phonout\n"
  68. "--phonout=\"<filename>\"\n"
  69. "\t Write phoneme output from -x -X --ipa and --pho to this file\n"
  70. "--punct=\"<characters>\"\n"
  71. "\t Speak the names of punctuation characters during speaking. If\n"
  72. "\t =<characters> is omitted, all punctuation is spoken.\n"
  73. "--split=\"<minutes>\"\n"
  74. "\t Starts a new WAV file every <minutes>. Used with -w\n"
  75. "--stdout Write speech output to stdout\n"
  76. "--voices=<language>\n"
  77. "\t List the available voices for the specified language.\n"
  78. "\t If <language> is omitted, then list all voices.\n";
  79. int samplerate;
  80. int quiet = 0;
  81. unsigned int samples_total = 0;
  82. unsigned int samples_split = 0;
  83. unsigned int samples_split_seconds = 0;
  84. unsigned int wavefile_count = 0;
  85. FILE *f_wavfile = NULL;
  86. char filetype[5];
  87. char wavefile[200];
  88. int GetFileLength(const char *filename)
  89. {//====================================
  90. struct stat statbuf;
  91. if(stat(filename,&statbuf) != 0)
  92. return(0);
  93. if((statbuf.st_mode & S_IFMT) == S_IFDIR)
  94. return(-2); // a directory
  95. return(statbuf.st_size);
  96. } // end of GetFileLength
  97. void strncpy0(char *dest, const char *source, int size)
  98. {//====================================================
  99. if(source!=NULL)
  100. {
  101. strncpy(dest,source,size);
  102. dest[size-1] = 0;
  103. }
  104. }
  105. void DisplayVoices(FILE *f_out, char *language)
  106. {//============================================
  107. int ix;
  108. const char *p;
  109. int len;
  110. int count;
  111. const espeak_VOICE *v;
  112. const char *lang_name;
  113. char age_buf[12];
  114. const espeak_VOICE **voices;
  115. espeak_VOICE voice_select;
  116. static char genders[4] = {' ','M','F',' '};
  117. if((language != NULL) && (language[0] != 0))
  118. {
  119. // display only voices for the specified language, in order of priority
  120. voice_select.languages = language;
  121. voice_select.age = 0;
  122. voice_select.gender = 0;
  123. voice_select.name = NULL;
  124. voices = espeak_ListVoices(&voice_select);
  125. }
  126. else
  127. {
  128. voices = espeak_ListVoices(NULL);
  129. }
  130. fprintf(f_out,"Pty Language Age/Gender VoiceName File Other Langs\n");
  131. for(ix=0; (v = voices[ix]) != NULL; ix++)
  132. {
  133. count = 0;
  134. p = v->languages;
  135. while(*p != 0)
  136. {
  137. len = strlen(p+1);
  138. lang_name = p+1;
  139. if(v->age == 0)
  140. strcpy(age_buf," ");
  141. else
  142. sprintf(age_buf,"%3d",v->age);
  143. if(count==0)
  144. {
  145. fprintf(f_out,"%2d %-12s%s%c %-17s %-11s ",
  146. p[0],lang_name,age_buf,genders[v->gender],v->name,v->identifier);
  147. }
  148. else
  149. {
  150. fprintf(f_out,"(%s %d)",lang_name,p[0]);
  151. }
  152. count++;
  153. p += len+2;
  154. }
  155. fputc('\n',f_out);
  156. }
  157. } // end of DisplayVoices
  158. static void Write4Bytes(FILE *f, int value)
  159. {//=================================
  160. // Write 4 bytes to a file, least significant first
  161. int ix;
  162. for(ix=0; ix<4; ix++)
  163. {
  164. fputc(value & 0xff,f);
  165. value = value >> 8;
  166. }
  167. }
  168. int OpenWavFile(char *path, int rate)
  169. //===================================
  170. {
  171. static unsigned char wave_hdr[44] = {
  172. 'R','I','F','F',0x24,0xf0,0xff,0x7f,'W','A','V','E','f','m','t',' ',
  173. 0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
  174. 2,0,0x10,0,'d','a','t','a', 0x00,0xf0,0xff,0x7f};
  175. if(path == NULL)
  176. return(2);
  177. if(path[0] == 0)
  178. return(0);
  179. if(strcmp(path,"stdout")==0)
  180. f_wavfile = stdout;
  181. else
  182. f_wavfile = fopen(path,"wb");
  183. if(f_wavfile == NULL)
  184. {
  185. fprintf(stderr,"Can't write to: '%s'\n",path);
  186. return(1);
  187. }
  188. fwrite(wave_hdr,1,24,f_wavfile);
  189. Write4Bytes(f_wavfile,rate);
  190. Write4Bytes(f_wavfile,rate * 2);
  191. fwrite(&wave_hdr[32],1,12,f_wavfile);
  192. return(0);
  193. } // end of OpenWavFile
  194. static void CloseWavFile()
  195. //========================
  196. {
  197. unsigned int pos;
  198. if((f_wavfile==NULL) || (f_wavfile == stdout))
  199. return;
  200. fflush(f_wavfile);
  201. pos = ftell(f_wavfile);
  202. fseek(f_wavfile,4,SEEK_SET);
  203. Write4Bytes(f_wavfile,pos - 8);
  204. fseek(f_wavfile,40,SEEK_SET);
  205. Write4Bytes(f_wavfile,pos - 44);
  206. fclose(f_wavfile);
  207. f_wavfile = NULL;
  208. } // end of CloseWavFile
  209. static int SynthCallback(short *wav, int numsamples, espeak_EVENT *events)
  210. {//========================================================================
  211. char fname[210];
  212. if(quiet) return(0); // -q quiet mode
  213. if(wav == NULL)
  214. {
  215. CloseWavFile();
  216. return(0);
  217. }
  218. while(events->type != 0)
  219. {
  220. if(events->type == espeakEVENT_SAMPLERATE)
  221. {
  222. samplerate = events->id.number;
  223. samples_split = samples_split_seconds * samplerate;
  224. }
  225. else
  226. if(events->type == espeakEVENT_SENTENCE)
  227. {
  228. // start a new WAV file when the limit is reached, at this sentence boundary
  229. if((samples_split > 0) && (samples_total > samples_split))
  230. {
  231. CloseWavFile();
  232. samples_total = 0;
  233. wavefile_count++;
  234. }
  235. }
  236. events++;
  237. }
  238. if(f_wavfile == NULL)
  239. {
  240. if(samples_split > 0)
  241. {
  242. sprintf(fname,"%s_%.2d%s",wavefile,wavefile_count+1,filetype);
  243. if(OpenWavFile(fname, samplerate) != 0)
  244. return(1);
  245. }
  246. else
  247. {
  248. if(OpenWavFile(wavefile, samplerate) != 0)
  249. return(1);
  250. }
  251. }
  252. if(numsamples > 0)
  253. {
  254. samples_total += numsamples;
  255. fwrite(wav,numsamples*2,1,f_wavfile);
  256. }
  257. return(0);
  258. }
  259. static void PrintVersion()
  260. {//=======================
  261. const char *version;
  262. const char *path_data;
  263. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 0, NULL, espeakINITIALIZE_DONT_EXIT);
  264. version = espeak_Info(&path_data);
  265. printf("eSpeak text-to-speech: %s Data at: %s\n", version, path_data);
  266. }
  267. #ifdef NEED_GETOPT
  268. struct option {
  269. char *name;
  270. int has_arg;
  271. int *flag;
  272. int val;
  273. };
  274. int optind;
  275. static int optional_argument;
  276. static const char *arg_opts = "abfgklpsvw"; // which options have arguments
  277. static char *opt_string="";
  278. #define no_argument 0
  279. #define required_argument 1
  280. #define optional_argument 2
  281. #endif
  282. int main (int argc, char **argv)
  283. //==============================
  284. {
  285. static struct option long_options[] =
  286. {
  287. /* These options set a flag. */
  288. // {"verbose", no_argument, &verbose_flag, 1},
  289. // {"brief", no_argument, &verbose_flag, 0},
  290. /* These options don't set a flag.
  291. We distinguish them by their indices. */
  292. {"help", no_argument, 0, 'h'},
  293. {"stdin", no_argument, 0, 0x100},
  294. {"compile-debug", optional_argument, 0, 0x101},
  295. {"compile", optional_argument, 0, 0x102},
  296. {"punct", optional_argument, 0, 0x103},
  297. {"voices", optional_argument, 0, 0x104},
  298. {"stdout", no_argument, 0, 0x105},
  299. {"split", optional_argument, 0, 0x106},
  300. {"path", required_argument, 0, 0x107},
  301. {"phonout", required_argument, 0, 0x108},
  302. {"pho", no_argument, 0, 0x109},
  303. {"ipa", optional_argument, 0, 0x10a},
  304. {"version", no_argument, 0, 0x10b},
  305. {0, 0, 0, 0}
  306. };
  307. static const char* err_load = "Failed to read ";
  308. FILE *f_text=NULL;
  309. char *p_text=NULL;
  310. FILE *f_phonemes_out = stdout;
  311. char *data_path = NULL; // use default path for espeak-data
  312. int option_index = 0;
  313. int c;
  314. int ix;
  315. char *optarg2;
  316. int value;
  317. int flag_stdin = 0;
  318. int flag_compile = 0;
  319. int filesize = 0;
  320. int synth_flags = espeakCHARS_AUTO | espeakPHONEMES | espeakENDPAUSE;
  321. int volume = -1;
  322. int speed = -1;
  323. int pitch = -1;
  324. int wordgap = -1;
  325. int option_capitals = -1;
  326. int option_punctuation = -1;
  327. int option_phonemes = 0;
  328. int option_mbrola_phonemes = 0;
  329. int option_linelength = 0;
  330. int option_waveout = 0;
  331. espeak_VOICE voice_select;
  332. char filename[200];
  333. char voicename[40];
  334. #define N_PUNCTLIST 100
  335. wchar_t option_punctlist[N_PUNCTLIST];
  336. voicename[0] = 0;
  337. wavefile[0] = 0;
  338. filename[0] = 0;
  339. option_punctlist[0] = 0;
  340. #ifdef NEED_GETOPT
  341. optind = 1;
  342. opt_string = "";
  343. while(optind < argc)
  344. {
  345. int len;
  346. char *p;
  347. if((c = *opt_string) == 0)
  348. {
  349. opt_string = argv[optind];
  350. if(opt_string[0] != '-')
  351. break;
  352. optind++;
  353. opt_string++;
  354. c = *opt_string;
  355. }
  356. opt_string++;
  357. p = optarg2 = opt_string;
  358. if(c == '-')
  359. {
  360. if(p[0] == 0)
  361. break; // -- means don't interpret further - as commands
  362. opt_string="";
  363. for(ix=0; ;ix++)
  364. {
  365. if(long_options[ix].name == 0)
  366. break;
  367. len = strlen(long_options[ix].name);
  368. if(memcmp(long_options[ix].name,p,len)==0)
  369. {
  370. c = long_options[ix].val;
  371. optarg2 = NULL;
  372. if((long_options[ix].has_arg != 0) && (p[len]=='='))
  373. {
  374. optarg2 = &p[len+1];
  375. }
  376. break;
  377. }
  378. }
  379. }
  380. else
  381. if(strchr(arg_opts,c) != NULL)
  382. {
  383. opt_string="";
  384. if(optarg2[0]==0)
  385. {
  386. // the option's value is in the next argument
  387. optarg2 = argv[optind++];
  388. }
  389. }
  390. #else
  391. while(true)
  392. {
  393. c = getopt_long (argc, argv, "a:b:f:g:hk:l:mp:qs:v:w:xXz",
  394. long_options, &option_index);
  395. /* Detect the end of the options. */
  396. if (c == -1)
  397. break;
  398. optarg2 = optarg;
  399. #endif
  400. switch (c)
  401. {
  402. case 'b':
  403. // input character encoding, 8bit, 16bit, UTF8
  404. if((sscanf(optarg2,"%d",&value) == 1) && (value <= 4))
  405. synth_flags |= value;
  406. else
  407. synth_flags |= espeakCHARS_8BIT;
  408. break;
  409. case 'h':
  410. printf("\n");
  411. PrintVersion();
  412. printf("%s", help_text);
  413. exit(0);
  414. break;
  415. case 'k':
  416. option_capitals = atoi(optarg2);
  417. break;
  418. case 'x':
  419. option_phonemes = 1;
  420. break;
  421. case 'X':
  422. option_phonemes = 2;
  423. break;
  424. case 'm':
  425. synth_flags |= espeakSSML;
  426. break;
  427. case 'p':
  428. pitch = atoi(optarg2);
  429. break;
  430. case 'q':
  431. quiet = 1;
  432. break;
  433. case 'f':
  434. strncpy0(filename,optarg2,sizeof(filename));
  435. break;
  436. case 'l':
  437. option_linelength = atoi(optarg2);
  438. break;
  439. case 'a':
  440. volume = atoi(optarg2);
  441. break;
  442. case 's':
  443. speed = atoi(optarg2);
  444. break;
  445. case 'g':
  446. wordgap = atoi(optarg2);
  447. break;
  448. case 'v':
  449. strncpy0(voicename,optarg2,sizeof(voicename));
  450. break;
  451. case 'w':
  452. option_waveout = 1;
  453. strncpy0(wavefile,optarg2,sizeof(filename));
  454. break;
  455. case 'z': // remove pause from the end of a sentence
  456. synth_flags &= ~espeakENDPAUSE;
  457. break;
  458. case 0x100: // --stdin
  459. flag_stdin = 1;
  460. break;
  461. case 0x105: // --stdout
  462. option_waveout = 1;
  463. strcpy(wavefile,"stdout");
  464. break;
  465. case 0x101: // --compile-debug
  466. case 0x102: // --compile
  467. strncpy0(voicename,optarg2,sizeof(voicename));
  468. flag_compile = c;
  469. quiet = 1;
  470. break;
  471. case 0x103: // --punct
  472. option_punctuation = 1;
  473. if(optarg2 != NULL)
  474. {
  475. ix = 0;
  476. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  477. option_punctlist[N_PUNCTLIST-1] = 0;
  478. option_punctuation = 2;
  479. }
  480. break;
  481. case 0x104: // --voices
  482. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS,0,data_path,0);
  483. DisplayVoices(stdout,optarg2);
  484. exit(0);
  485. case 0x106: // -- split
  486. if(optarg2 == NULL)
  487. samples_split_seconds = 30 * 60; // default 30 minutes
  488. else
  489. samples_split_seconds = atoi(optarg2) * 60;
  490. break;
  491. case 0x107: // --path
  492. data_path = optarg2;
  493. break;
  494. case 0x108: // --phonout
  495. if((f_phonemes_out = fopen(optarg2,"w")) == NULL)
  496. {
  497. fprintf(stderr,"Can't write to: %s\n",optarg2);
  498. }
  499. break;
  500. case 0x109: // --pho
  501. option_mbrola_phonemes = 16;
  502. break;
  503. case 0x10a: // --ipa
  504. option_phonemes = 3;
  505. if(optarg2 != NULL)
  506. {
  507. value = -1;
  508. sscanf(optarg2,"%d",&value);
  509. if((value<0) || (value>2))
  510. {
  511. fprintf(stderr,"Bad value for -ipa=\n");
  512. value = 0;
  513. }
  514. option_phonemes += value;
  515. }
  516. break;
  517. case 0x10b: // -version
  518. PrintVersion();
  519. exit(0);
  520. default:
  521. exit(0);
  522. }
  523. }
  524. if(option_waveout || quiet)
  525. {
  526. // writing to a file (or no output), we can use synchronous mode
  527. samplerate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS,0,data_path,0);
  528. samples_split = samplerate * samples_split_seconds;
  529. espeak_SetSynthCallback(SynthCallback);
  530. if(samples_split)
  531. {
  532. char *extn;
  533. extn = strrchr(wavefile,'.');
  534. if((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4))
  535. {
  536. strcpy(filetype,extn);
  537. *extn = 0;
  538. }
  539. }
  540. }
  541. else
  542. {
  543. // play the sound output
  544. samplerate = espeak_Initialize(AUDIO_OUTPUT_PLAYBACK,0,data_path,0);
  545. }
  546. if(voicename[0] == 0)
  547. strcpy(voicename,"default");
  548. if(espeak_SetVoiceByName(voicename) != EE_OK)
  549. {
  550. memset(&voice_select,0,sizeof(voice_select));
  551. voice_select.languages = voicename;
  552. if(espeak_SetVoiceByProperties(&voice_select) != EE_OK)
  553. {
  554. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  555. exit(2);
  556. }
  557. }
  558. if(flag_compile)
  559. {
  560. // This must be done after the voice is set
  561. espeak_CompileDictionary("", stderr, flag_compile & 0x1);
  562. exit(0);
  563. }
  564. // set any non-default values of parameters. This must be done after espeak_Initialize()
  565. if(speed > 0)
  566. espeak_SetParameter(espeakRATE,speed,0);
  567. if(volume >= 0)
  568. espeak_SetParameter(espeakVOLUME,volume,0);
  569. if(pitch >= 0)
  570. espeak_SetParameter(espeakPITCH,pitch,0);
  571. if(option_capitals >= 0)
  572. espeak_SetParameter(espeakCAPITALS,option_capitals,0);
  573. if(option_punctuation >= 0)
  574. espeak_SetParameter(espeakPUNCTUATION,option_punctuation,0);
  575. if(wordgap >= 0)
  576. espeak_SetParameter(espeakWORDGAP,wordgap,0);
  577. if(option_linelength > 0)
  578. espeak_SetParameter(espeakLINELENGTH,option_linelength,0);
  579. if(option_punctuation == 2)
  580. espeak_SetPunctuationList(option_punctlist);
  581. espeak_SetPhonemeTrace(option_phonemes | option_mbrola_phonemes,f_phonemes_out);
  582. if(filename[0]==0)
  583. {
  584. if((optind < argc) && (flag_stdin == 0))
  585. {
  586. // there's a non-option parameter, and no -f or --stdin
  587. // use it as text
  588. p_text = argv[optind];
  589. }
  590. else
  591. {
  592. f_text = stdin;
  593. if(flag_stdin == 0)
  594. {
  595. flag_stdin = 2;
  596. }
  597. }
  598. }
  599. else
  600. {
  601. filesize = GetFileLength(filename);
  602. f_text = fopen(filename,"r");
  603. }
  604. if((f_text == NULL) && (p_text == NULL))
  605. {
  606. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  607. exit(1);
  608. }
  609. if(p_text != NULL)
  610. {
  611. int size;
  612. size = strlen(p_text);
  613. espeak_Synth(p_text,size+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  614. }
  615. else
  616. if(flag_stdin)
  617. {
  618. int max = 1000;
  619. p_text = (char *)malloc(max);
  620. if(flag_stdin == 2)
  621. {
  622. // line by line input on stdin
  623. while(fgets(p_text,max,stdin) != NULL)
  624. {
  625. p_text[max-1] = 0;
  626. espeak_Synth(p_text,max,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  627. }
  628. }
  629. else
  630. {
  631. // bulk input on stdin
  632. ix = 0;
  633. while(!feof(stdin))
  634. {
  635. p_text[ix++] = fgetc(stdin);
  636. if(ix >= (max-1))
  637. {
  638. max += 1000;
  639. p_text = (char *)realloc(p_text,max);
  640. }
  641. }
  642. if(ix > 0)
  643. {
  644. p_text[ix-1] = 0;
  645. espeak_Synth(p_text,ix+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  646. }
  647. }
  648. }
  649. else
  650. if(f_text != NULL)
  651. {
  652. if((p_text = (char *)malloc(filesize+1)) == NULL)
  653. {
  654. fprintf(stderr,"Failed to allocate memory %d bytes",filesize);
  655. exit(3);
  656. }
  657. fread(p_text,1,filesize,f_text);
  658. p_text[filesize]=0;
  659. espeak_Synth(p_text,filesize+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  660. fclose(f_text);
  661. }
  662. if(espeak_Synchronize() != EE_OK)
  663. {
  664. fprintf(stderr, "espeak_Synchronize() failed, maybe error when opening output device\n");
  665. exit(4);
  666. }
  667. if(f_phonemes_out != stdout)
  668. fclose(f_phonemes_out); // needed for WinCE
  669. return(0);
  670. }