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

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