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

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