eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

speak-ng.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /***************************************************************************
  2. * Copyright (C) 2005 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 "speech.h"
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #ifndef PLATFORM_DOS
  27. #ifdef PLATFORM_WINDOWS
  28. #include <fcntl.h>
  29. #include <io.h>
  30. #include <windows.h>
  31. #include <winreg.h>
  32. #else
  33. #include <unistd.h>
  34. #endif
  35. #endif
  36. #ifndef NEED_GETOPT
  37. #include <getopt.h>
  38. #endif
  39. #include <time.h>
  40. #include <signal.h>
  41. #include <locale.h>
  42. #include <sys/stat.h>
  43. #include "speak_lib.h"
  44. #include "phoneme.h"
  45. #include "synthesize.h"
  46. #include "voice.h"
  47. #include "translate.h"
  48. extern void Write4Bytes(FILE *f, int value);
  49. char path_home[N_PATH_HOME]; // this is the espeak-data directory
  50. char filetype[5];
  51. char wavefile[200];
  52. FILE *f_wave = NULL;
  53. int quiet = 0;
  54. unsigned int samples_total = 0;
  55. unsigned int samples_split = 0;
  56. unsigned int wavefile_count = 0;
  57. int end_of_sentence = 0;
  58. static const char *help_text =
  59. "\nspeak-ng [options] [\"<words>\"]\n\n"
  60. "-f <text file> Text file to speak\n"
  61. "--stdin Read text input from stdin instead of a file\n\n"
  62. "If neither -f nor --stdin, then <words> are spoken, or if none then text\n"
  63. "is spoken from stdin, each line separately.\n\n"
  64. "-a <integer>\n"
  65. "\t Amplitude, 0 to 200, default is 100\n"
  66. "-g <integer>\n"
  67. "\t Word gap. Pause between words, units of 10mS at the default speed\n"
  68. "-k <integer>\n"
  69. "\t Indicate capital letters with: 1=sound, 2=the word \"capitals\",\n"
  70. "\t higher values indicate a pitch increase (try -k20).\n"
  71. "-l <integer>\n"
  72. "\t Line length. If not zero (which is the default), consider\n"
  73. "\t lines less than this length as end-of-clause\n"
  74. "-p <integer>\n"
  75. "\t Pitch adjustment, 0 to 99, default is 50\n"
  76. "-s <integer>\n"
  77. "\t Speed in approximate words per minute. The default is 175\n"
  78. "-v <voice name>\n"
  79. "\t Use voice file of this name from espeak-data/voices\n"
  80. "-w <wave file name>\n"
  81. "\t Write speech to this WAV file, rather than speaking it directly\n"
  82. "-b\t Input text encoding, 1=UTF8, 2=8 bit, 4=16 bit \n"
  83. "-m\t Interpret SSML markup, and ignore other < > tags\n"
  84. "-q\t Quiet, don't produce any speech (may be useful with -x)\n"
  85. "-x\t Write phoneme mnemonics to stdout\n"
  86. "-X\t Write phonemes mnemonics and translation trace to stdout\n"
  87. "-z\t No final sentence pause at the end of the text\n"
  88. "--compile=<voice name>\n"
  89. "\t Compile pronunciation rules and dictionary from the current\n"
  90. "\t directory. <voice name> specifies the language\n"
  91. "--ipa Write phonemes to stdout using International Phonetic Alphabet\n"
  92. "--path=\"<path>\"\n"
  93. "\t Specifies the directory containing the espeak-data directory\n"
  94. "--pho Write mbrola phoneme data (.pho) to stdout or to the file in --phonout\n"
  95. "--phonout=\"<filename>\"\n"
  96. "\t Write phoneme output from -x -X --ipa and --pho to this file\n"
  97. "--punct=\"<characters>\"\n"
  98. "\t Speak the names of punctuation characters during speaking. If\n"
  99. "\t =<characters> is omitted, all punctuation is spoken.\n"
  100. "--sep=<character>\n"
  101. "\t Separate phonemes (from -x --ipa) with <character>.\n"
  102. "\t Default is space, z means ZWJN character.\n"
  103. "--split=<minutes>\n"
  104. "\t Starts a new WAV file every <minutes>. Used with -w\n"
  105. "--stdout Write speech output to stdout\n"
  106. "--tie=<character>\n"
  107. "\t Use a tie character within multi-letter phoneme names.\n"
  108. "\t Default is U+361, z means ZWJ character.\n"
  109. "--version Shows version number and date, and location of espeak-data\n"
  110. "--voices=<language>\n"
  111. "\t List the available voices for the specified language.\n"
  112. "\t If <language> is omitted, then list all voices.\n";
  113. void DisplayVoices(FILE *f_out, char *language);
  114. USHORT voice_pcnt[N_PEAKS+1][3];
  115. void DisplayVoices(FILE *f_out, char *language)
  116. {//============================================
  117. int ix;
  118. const char *p;
  119. int len;
  120. int count;
  121. int c;
  122. int j;
  123. const espeak_VOICE *v;
  124. const char *lang_name;
  125. char age_buf[12];
  126. char buf[80];
  127. const espeak_VOICE **voices;
  128. espeak_VOICE voice_select;
  129. static char genders[4] = {'-','M','F','-'};
  130. if((language != NULL) && (language[0] != 0))
  131. {
  132. // display only voices for the specified language, in order of priority
  133. voice_select.languages = language;
  134. voice_select.age = 0;
  135. voice_select.gender = 0;
  136. voice_select.name = NULL;
  137. voices = espeak_ListVoices(&voice_select);
  138. }
  139. else
  140. {
  141. voices = espeak_ListVoices(NULL);
  142. }
  143. fprintf(f_out,"Pty Language Age/Gender VoiceName File Other Languages\n");
  144. for(ix=0; (v = voices[ix]) != NULL; ix++)
  145. {
  146. count = 0;
  147. p = v->languages;
  148. while(*p != 0)
  149. {
  150. len = strlen(p+1);
  151. lang_name = p+1;
  152. if(v->age == 0)
  153. strcpy(age_buf," ");
  154. else
  155. sprintf(age_buf,"%3d",v->age);
  156. if(count==0)
  157. {
  158. for(j=0; j < sizeof(buf); j++)
  159. {
  160. // replace spaces in the name
  161. if((c = v->name[j]) == ' ')
  162. c = '_';
  163. if((buf[j] = c) == 0)
  164. break;
  165. }
  166. fprintf(f_out,"%2d %-12s%s%c %-20s %-13s ",
  167. p[0],lang_name,age_buf,genders[v->gender],buf,v->identifier);
  168. }
  169. else
  170. {
  171. fprintf(f_out,"(%s %d)",lang_name,p[0]);
  172. }
  173. count++;
  174. p += len+2;
  175. }
  176. fputc('\n',f_out);
  177. }
  178. } // end of DisplayVoices
  179. static int OpenWaveFile(const char *path, int rate)
  180. //=================================================
  181. {
  182. // Set the length of 0x7ffff000 for --stdout
  183. // This will be changed to the correct length for -w (write to file)
  184. static unsigned char wave_hdr[44] = {
  185. 'R','I','F','F',0x24,0xf0,0xff,0x7f,'W','A','V','E','f','m','t',' ',
  186. 0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
  187. 2,0,0x10,0,'d','a','t','a', 0x00,0xf0,0xff,0x7f};
  188. if(path == NULL)
  189. return(2);
  190. while(isspace(*path)) path++;
  191. f_wave = NULL;
  192. if(path[0] != 0)
  193. {
  194. if(strcmp(path,"stdout")==0)
  195. {
  196. #ifdef PLATFORM_WINDOWS
  197. // prevent Windows adding 0x0d before 0x0a bytes
  198. _setmode(_fileno(stdout), _O_BINARY);
  199. #endif
  200. f_wave = stdout;
  201. }
  202. else
  203. f_wave = fopen(path,"wb");
  204. }
  205. if(f_wave != NULL)
  206. {
  207. fwrite(wave_hdr,1,24,f_wave);
  208. Write4Bytes(f_wave,rate);
  209. Write4Bytes(f_wave,rate * 2);
  210. fwrite(&wave_hdr[32],1,12,f_wave);
  211. return(0);
  212. }
  213. return(1);
  214. } // end of OpenWaveFile
  215. static void CloseWaveFile()
  216. //=========================
  217. {
  218. unsigned int pos;
  219. if((f_wave == NULL) || (f_wave == stdout))
  220. return;
  221. fflush(f_wave);
  222. pos = ftell(f_wave);
  223. fseek(f_wave,4,SEEK_SET);
  224. Write4Bytes(f_wave,pos - 8);
  225. fseek(f_wave,40,SEEK_SET);
  226. Write4Bytes(f_wave,pos - 44);
  227. fclose(f_wave);
  228. f_wave = NULL;
  229. } // end of CloseWaveFile
  230. static int WavegenFile(void)
  231. {//=========================
  232. int finished;
  233. unsigned char wav_outbuf[1024];
  234. char fname[210];
  235. out_ptr = out_start = wav_outbuf;
  236. out_end = wav_outbuf + sizeof(wav_outbuf);
  237. finished = WavegenFill(0);
  238. if(quiet)
  239. return(finished);
  240. if(f_wave == NULL)
  241. {
  242. sprintf(fname,"%s_%.2d%s",wavefile,++wavefile_count,filetype);
  243. if(OpenWaveFile(fname, samplerate) != 0)
  244. return(1);
  245. }
  246. if(end_of_sentence)
  247. {
  248. end_of_sentence = 0;
  249. if((samples_split > 0 ) && (samples_total > samples_split))
  250. {
  251. CloseWaveFile();
  252. samples_total = 0;
  253. }
  254. }
  255. if(f_wave != NULL)
  256. {
  257. samples_total += (out_ptr - wav_outbuf)/2;
  258. fwrite(wav_outbuf, 1, out_ptr - wav_outbuf, f_wave);
  259. }
  260. return(finished);
  261. } // end of WavegenFile
  262. static void init_path(char *argv0, char *path_specified)
  263. {//=====================================================
  264. if(path_specified)
  265. {
  266. sprintf(path_home,"%s/espeak-data",path_specified);
  267. return;
  268. }
  269. #ifdef PLATFORM_WINDOWS
  270. HKEY RegKey;
  271. unsigned long size;
  272. unsigned long var_type;
  273. char *p;
  274. char *env;
  275. unsigned char buf[sizeof(path_home)-12];
  276. if(((env = getenv("ESPEAK_DATA_PATH")) != NULL) && ((strlen(env)+12) < sizeof(path_home)))
  277. {
  278. sprintf(path_home,"%s\\espeak-data",env);
  279. if(GetFileLength(path_home) == -2)
  280. return; // an espeak-data directory exists in the directory specified by environment variable
  281. }
  282. strcpy(path_home,argv0);
  283. if((p = strrchr(path_home,'\\')) != NULL)
  284. {
  285. strcpy(&p[1],"espeak-data");
  286. if(GetFileLength(path_home) == -2)
  287. return; // an espeak-data directory exists in the same directory as the espeak program
  288. }
  289. // otherwise, look in the Windows Registry
  290. buf[0] = 0;
  291. RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Speech\\Voices\\Tokens\\eSpeak", 0, KEY_READ, &RegKey);
  292. size = sizeof(buf);
  293. var_type = REG_SZ;
  294. RegQueryValueEx(RegKey, "path", 0, &var_type, buf, &size);
  295. sprintf(path_home,"%s\\espeak-data",buf);
  296. #else
  297. #ifdef PLATFORM_DOS
  298. strcpy(path_home,PATH_ESPEAK_DATA);
  299. #else
  300. char *env;
  301. if((env = getenv("ESPEAK_DATA_PATH")) != NULL)
  302. {
  303. snprintf(path_home,sizeof(path_home),"%s/espeak-data",env);
  304. if(GetFileLength(path_home) == -2)
  305. return; // an espeak-data directory exists
  306. }
  307. snprintf(path_home,sizeof(path_home),"%s/espeak-data",getenv("HOME"));
  308. if(access(path_home,R_OK) != 0)
  309. {
  310. strcpy(path_home,PATH_ESPEAK_DATA);
  311. }
  312. #endif
  313. #endif
  314. }
  315. static int initialise(void)
  316. {//========================
  317. int param;
  318. int result;
  319. int srate = 22050; // default sample rate
  320. // It seems that the wctype functions don't work until the locale has been set
  321. // to something other than the default "C". Then, not only Latin1 but also the
  322. // other characters give the correct results with iswalpha() etc.
  323. #ifdef PLATFORM_RISCOS
  324. setlocale(LC_CTYPE,"ISO8859-1");
  325. #else
  326. if(setlocale(LC_CTYPE,"en_US.UTF-8") == NULL)
  327. {
  328. if(setlocale(LC_CTYPE,"UTF-8") == NULL)
  329. setlocale(LC_CTYPE,"");
  330. }
  331. #endif
  332. if((result = LoadPhData(&srate)) != 1)
  333. {
  334. if(result == -1)
  335. {
  336. fprintf(stderr,"Failed to load espeak-data\n");
  337. exit(1);
  338. }
  339. else
  340. fprintf(stderr,"Wrong version of espeak-data 0x%x (expects 0x%x) at %s\n",result,version_phdata,path_home);
  341. }
  342. WavegenInit(srate,0);
  343. LoadConfig();
  344. SetVoiceStack(NULL, "");
  345. SynthesizeInit();
  346. for(param=0; param<N_SPEECH_PARAM; param++)
  347. param_stack[0].parameter[param] = param_defaults[param];
  348. return(0);
  349. }
  350. #ifdef NEED_GETOPT
  351. struct option {
  352. char *name;
  353. int has_arg;
  354. int *flag;
  355. int val;
  356. };
  357. int optind;
  358. static int optional_argument;
  359. static const char *arg_opts = "abfgklpsvw"; // which options have arguments
  360. static char *opt_string="";
  361. #define no_argument 0
  362. #define required_argument 1
  363. #define optional_argument 2
  364. #endif
  365. int main (int argc, char **argv)
  366. //==============================
  367. {
  368. static struct option long_options[] =
  369. {
  370. /* These options set a flag. */
  371. // {"verbose", no_argument, &verbose_flag, 1},
  372. // {"brief", no_argument, &verbose_flag, 0},
  373. /* These options don't set a flag.
  374. We distinguish them by their indices. */
  375. {"help", no_argument, 0, 'h'},
  376. {"stdin", no_argument, 0, 0x100},
  377. {"compile-debug", optional_argument, 0, 0x101},
  378. {"compile", optional_argument, 0, 0x102},
  379. {"punct", optional_argument, 0, 0x103},
  380. {"voices", optional_argument, 0, 0x104},
  381. {"stdout", no_argument, 0, 0x105},
  382. {"split", optional_argument, 0, 0x106},
  383. {"path", required_argument, 0, 0x107},
  384. {"phonout", required_argument, 0, 0x108},
  385. {"pho", no_argument, 0, 0x109},
  386. {"ipa", optional_argument, 0, 0x10a},
  387. {"version", no_argument, 0, 0x10b},
  388. {"sep", optional_argument, 0, 0x10c},
  389. {"tie", optional_argument, 0, 0x10d},
  390. {0, 0, 0, 0}
  391. };
  392. static const char *err_load = "Failed to read ";
  393. FILE *f_text=NULL;
  394. const char *p_text=NULL;
  395. char *data_path = NULL; // use default path for espeak-data
  396. int option_index = 0;
  397. int c;
  398. int value;
  399. int speed=175;
  400. int ix;
  401. char *optarg2;
  402. int amp = 100; // default
  403. int wordgap = 0;
  404. int flag_stdin = 0;
  405. int flag_compile = 0;
  406. int pitch_adjustment = 50;
  407. int phoneme_options = 0;
  408. int phonemes_separator = 0;
  409. espeak_VOICE voice_select;
  410. char filename[200];
  411. char voicename[40];
  412. #ifdef USE_PORTAUDIO
  413. int speaking = 0;
  414. #endif
  415. voicename[0] = 0;
  416. mbrola_name[0] = 0;
  417. wavefile[0] = 0;
  418. filename[0] = 0;
  419. option_linelength = 0;
  420. option_phonemes = 0;
  421. option_waveout = 0;
  422. option_wordgap = 0;
  423. option_endpause = 1;
  424. option_phoneme_input = 1;
  425. option_multibyte = espeakCHARS_AUTO; // auto
  426. f_trans = stdout;
  427. #ifdef NEED_GETOPT
  428. optind = 1;
  429. opt_string = "";
  430. while(optind < argc)
  431. {
  432. int len;
  433. char *p;
  434. if((c = *opt_string) == 0)
  435. {
  436. opt_string = argv[optind];
  437. if(opt_string[0] != '-')
  438. break;
  439. optind++;
  440. opt_string++;
  441. c = *opt_string;
  442. }
  443. opt_string++;
  444. p = optarg2 = opt_string;
  445. if(c == '-')
  446. {
  447. if(p[0] == 0)
  448. break; // -- means don't interpret further - as commands
  449. opt_string="";
  450. for(ix=0; ;ix++)
  451. {
  452. if(long_options[ix].name == 0)
  453. break;
  454. len = strlen(long_options[ix].name);
  455. if(memcmp(long_options[ix].name,p,len)==0)
  456. {
  457. c = long_options[ix].val;
  458. optarg2 = NULL;
  459. if((long_options[ix].has_arg != 0) && (p[len]=='='))
  460. {
  461. optarg2 = &p[len+1];
  462. }
  463. break;
  464. }
  465. }
  466. }
  467. else
  468. if(strchr(arg_opts,c) != NULL)
  469. {
  470. opt_string="";
  471. if(optarg2[0]==0)
  472. {
  473. // the option's value is in the next argument
  474. optarg2 = argv[optind++];
  475. }
  476. }
  477. #else
  478. while(true)
  479. {
  480. c = getopt_long (argc, argv, "a:b:f:g:hk:l:p:qs:v:w:xXmz", // NOTE: also change arg_opts to indicate which commands have a numeric value
  481. long_options, &option_index);
  482. /* Detect the end of the options. */
  483. if (c == -1)
  484. break;
  485. optarg2 = optarg;
  486. #endif
  487. switch (c)
  488. {
  489. case 'b':
  490. // input character encoding, 8bit, 16bit, UTF8
  491. option_multibyte = espeakCHARS_8BIT;
  492. if((sscanf(optarg2,"%d",&value) == 1) && (value <= 4))
  493. option_multibyte= value;
  494. break;
  495. case 'h':
  496. init_path(argv[0],data_path);
  497. printf("\nspeak text-to-speech: %s Data at: %s\n%s",version_string,path_home,help_text);
  498. exit(0);
  499. case 'k':
  500. option_capitals = atoi(optarg2);
  501. break;
  502. case 'x':
  503. phoneme_options |= espeakPHONEMES_SHOW;
  504. break;
  505. case 'X':
  506. phoneme_options |= espeakPHONEMES_TRACE;
  507. break;
  508. case 'm':
  509. option_ssml = 1;
  510. break;
  511. case 'p':
  512. pitch_adjustment = atoi(optarg2);
  513. if(pitch_adjustment > 99) pitch_adjustment = 99;
  514. break;
  515. case 'q':
  516. quiet = 1;
  517. break;
  518. case 'f':
  519. strncpy0(filename,optarg2,sizeof(filename));
  520. break;
  521. case 'l':
  522. value = 0;
  523. value = atoi(optarg2);
  524. option_linelength = value;
  525. break;
  526. case 'a':
  527. amp = atoi(optarg2);
  528. break;
  529. case 's':
  530. speed = atoi(optarg2);
  531. break;
  532. case 'g':
  533. wordgap = atoi(optarg2);
  534. break;
  535. case 'v':
  536. strncpy0(voicename,optarg2,sizeof(voicename));
  537. break;
  538. case 'w':
  539. option_waveout = 1;
  540. strncpy0(wavefile,optarg2,sizeof(wavefile));
  541. break;
  542. case 'z':
  543. option_endpause = 0;
  544. break;
  545. case 0x100: // --stdin
  546. flag_stdin = 1;
  547. break;
  548. case 0x105: // --stdout
  549. option_waveout = 1;
  550. strcpy(wavefile,"stdout");
  551. break;
  552. case 0x101: // --compile-debug
  553. case 0x102: // --compile
  554. if(optarg2 != NULL)
  555. strncpy0(voicename,optarg2,sizeof(voicename));
  556. flag_compile = c;
  557. break;
  558. case 0x103: // --punct
  559. option_punctuation = 1;
  560. if(optarg2 != NULL)
  561. {
  562. ix = 0;
  563. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  564. option_punctlist[N_PUNCTLIST-1] = 0;
  565. option_punctuation = 2;
  566. }
  567. break;
  568. case 0x104: // --voices
  569. init_path(argv[0],data_path);
  570. DisplayVoices(stdout,optarg2);
  571. exit(0);
  572. case 0x106: // -- split
  573. if(optarg2 == NULL)
  574. samples_split = 30; // default 30 minutes
  575. else
  576. samples_split = atoi(optarg2);
  577. break;
  578. case 0x107: // --path
  579. data_path = optarg2;
  580. break;
  581. case 0x108: // --phonout
  582. if((f_trans = fopen(optarg2,"w")) == NULL)
  583. {
  584. fprintf(stderr,"Can't write to: %s\n",optarg2);
  585. f_trans = stderr;
  586. }
  587. break;
  588. case 0x109: // --pho
  589. phoneme_options |= espeakPHONEMES_MBROLA;
  590. break;
  591. case 0x10a: // --ipa
  592. phoneme_options |= espeakPHONEMES_IPA;
  593. if(optarg2 != NULL)
  594. {
  595. // deprecated and obsolete
  596. switch(atoi(optarg2))
  597. {
  598. case 1:
  599. phonemes_separator = '_';
  600. break;
  601. case 2:
  602. phonemes_separator = 0x0361;
  603. phoneme_options |= espeakPHONEMES_TIE;
  604. break;
  605. case 3:
  606. phonemes_separator = 0x200d; // ZWJ
  607. phoneme_options |= espeakPHONEMES_TIE;
  608. break;
  609. }
  610. }
  611. break;
  612. case 0x10b: // --version
  613. init_path(argv[0],data_path);
  614. printf("speak text-to-speech: %s Data at: %s\n",version_string,path_home);
  615. exit(0);
  616. case 0x10c: // --sep
  617. phoneme_options |= espeakPHONEMES_SHOW;
  618. if(optarg2 == 0)
  619. phonemes_separator = ' ';
  620. else
  621. utf8_in(&phonemes_separator, optarg2);
  622. if(phonemes_separator == 'z')
  623. phonemes_separator = 0x200c; // ZWNJ
  624. break;
  625. case 0x10d: // --tie
  626. phoneme_options |= (espeakPHONEMES_SHOW | espeakPHONEMES_TIE);
  627. if(optarg2 == 0)
  628. phonemes_separator = 0x0361; // default: combining-double-inverted-breve
  629. else
  630. utf8_in(&phonemes_separator, optarg2);
  631. if(phonemes_separator == 'z')
  632. phonemes_separator = 0x200d; // ZWJ
  633. break;
  634. default:
  635. exit(0);
  636. }
  637. }
  638. init_path(argv[0],data_path);
  639. initialise();
  640. if(voicename[0] == 0)
  641. strcpy(voicename,"default");
  642. if(SetVoiceByName(voicename) != EE_OK)
  643. {
  644. memset(&voice_select,0,sizeof(voice_select));
  645. voice_select.languages = voicename;
  646. if(SetVoiceByProperties(&voice_select) != EE_OK)
  647. {
  648. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  649. exit(2);
  650. }
  651. }
  652. if(flag_compile)
  653. {
  654. #ifdef PLATFORM_DOS
  655. char path_dsource[sizeof(path_home)+20];
  656. strcpy(path_dsource,path_home);
  657. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  658. strcat(path_dsource,"dictsource\\");
  659. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  660. #else
  661. #ifdef PLATFORM_WINDOWS
  662. char path_dsource[sizeof(path_home)+20];
  663. strcpy(path_dsource,path_home);
  664. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  665. strcat(path_dsource,"dictsource\\");
  666. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  667. #else
  668. CompileDictionary(NULL,dictionary_name,NULL,NULL, flag_compile & 0x1);
  669. #endif
  670. #endif
  671. exit(0);
  672. }
  673. SetParameter(espeakRATE,speed,0);
  674. SetParameter(espeakVOLUME,amp,0);
  675. SetParameter(espeakCAPITALS,option_capitals,0);
  676. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  677. SetParameter(espeakWORDGAP,wordgap,0);
  678. option_phonemes = phoneme_options | (phonemes_separator << 8);
  679. if(pitch_adjustment != 50)
  680. {
  681. SetParameter(espeakPITCH,pitch_adjustment,0);
  682. }
  683. DoVoiceChange(voice);
  684. if(filename[0]==0)
  685. {
  686. if((optind < argc) && (flag_stdin == 0))
  687. {
  688. // there's a non-option parameter, and no -f or --stdin
  689. // use it as text
  690. p_text = argv[optind];
  691. }
  692. else
  693. {
  694. f_text = stdin;
  695. if(flag_stdin == 0)
  696. option_linelength = -1; // single input lines on stdin
  697. }
  698. }
  699. else
  700. {
  701. f_text = fopen(filename,"r");
  702. }
  703. if((f_text == NULL) && (p_text == NULL))
  704. {
  705. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  706. exit(1);
  707. }
  708. if(option_waveout || quiet)
  709. {
  710. if(quiet)
  711. {
  712. // no sound output
  713. OpenWaveFile(NULL,samplerate);
  714. option_waveout = 1;
  715. }
  716. else
  717. {
  718. // write sound output to a WAV file
  719. samples_split = (samplerate * samples_split) * 60;
  720. if(samples_split)
  721. {
  722. // don't open the wav file until we start generating speech
  723. char *extn;
  724. extn = strrchr(wavefile,'.');
  725. if((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4))
  726. {
  727. strcpy(filetype,extn);
  728. *extn = 0;
  729. }
  730. }
  731. else
  732. if(OpenWaveFile(wavefile,samplerate) != 0)
  733. {
  734. fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
  735. exit(3);
  736. }
  737. }
  738. InitText(0);
  739. SpeakNextClause(f_text,p_text,0);
  740. ix = 1;
  741. for(;;)
  742. {
  743. if(WavegenFile() != 0)
  744. {
  745. if(ix == 0)
  746. break; // finished, wavegen command queue is empty
  747. }
  748. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  749. {
  750. ix = SpeakNextClause(NULL,NULL,1);
  751. }
  752. }
  753. CloseWaveFile();
  754. }
  755. else
  756. {
  757. // output sound using portaudio
  758. WavegenInitSound();
  759. InitText(0);
  760. SpeakNextClause(f_text,p_text,0);
  761. if(option_quiet)
  762. {
  763. while(SpeakNextClause(NULL,NULL,1) != 0);
  764. return(0);
  765. }
  766. #ifdef USE_PORTAUDIO
  767. speaking = 1;
  768. while(speaking)
  769. {
  770. // NOTE: if nanosleep() isn't recognised on your system, try replacing
  771. // this by sleep(1);
  772. #ifdef PLATFORM_WINDOWS
  773. Sleep(300); // 0.3s
  774. #else
  775. #ifdef USE_NANOSLEEP
  776. struct timespec period;
  777. struct timespec remaining;
  778. period.tv_sec = 0;
  779. period.tv_nsec = 300000000; // 0.3 sec
  780. nanosleep(&period,&remaining);
  781. #else
  782. sleep(1);
  783. #endif
  784. #endif
  785. if(SynthOnTimer() != 0)
  786. speaking = 0;
  787. }
  788. #else
  789. fprintf(stderr,"-w option must be used because the program was built without a sound interface\n");
  790. #endif // USE_PORTAUDIO
  791. }
  792. if((f_trans != stdout) && (f_trans != stderr))
  793. fclose(f_trans); // needed for WinCe
  794. return(0);
  795. }