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-ng.c 20KB

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