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 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. }
  179. static int OpenWaveFile(const char *path, int rate)
  180. {
  181. // Set the length of 0x7ffff000 for --stdout
  182. // This will be changed to the correct length for -w (write to file)
  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. };
  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. }
  215. static void CloseWaveFile()
  216. {
  217. unsigned int pos;
  218. if((f_wave == NULL) || (f_wave == stdout))
  219. return;
  220. fflush(f_wave);
  221. pos = ftell(f_wave);
  222. fseek(f_wave,4,SEEK_SET);
  223. Write4Bytes(f_wave,pos - 8);
  224. fseek(f_wave,40,SEEK_SET);
  225. Write4Bytes(f_wave,pos - 44);
  226. fclose(f_wave);
  227. f_wave = NULL;
  228. }
  229. static int WavegenFile(void)
  230. {
  231. int finished;
  232. unsigned char wav_outbuf[1024];
  233. char fname[210];
  234. out_ptr = out_start = wav_outbuf;
  235. out_end = wav_outbuf + sizeof(wav_outbuf);
  236. finished = WavegenFill(0);
  237. if(quiet)
  238. return(finished);
  239. if(f_wave == NULL)
  240. {
  241. sprintf(fname,"%s_%.2d%s",wavefile,++wavefile_count,filetype);
  242. if(OpenWaveFile(fname, samplerate) != 0)
  243. return(1);
  244. }
  245. if(end_of_sentence)
  246. {
  247. end_of_sentence = 0;
  248. if((samples_split > 0 ) && (samples_total > samples_split))
  249. {
  250. CloseWaveFile();
  251. samples_total = 0;
  252. }
  253. }
  254. if(f_wave != NULL)
  255. {
  256. samples_total += (out_ptr - wav_outbuf)/2;
  257. fwrite(wav_outbuf, 1, out_ptr - wav_outbuf, f_wave);
  258. }
  259. return(finished);
  260. }
  261. static void init_path(char *argv0, char *path_specified)
  262. {
  263. if(path_specified)
  264. {
  265. sprintf(path_home,"%s/espeak-data",path_specified);
  266. return;
  267. }
  268. #ifdef PLATFORM_WINDOWS
  269. HKEY RegKey;
  270. unsigned long size;
  271. unsigned long var_type;
  272. char *p;
  273. char *env;
  274. unsigned char buf[sizeof(path_home)-12];
  275. if(((env = getenv("ESPEAK_DATA_PATH")) != NULL) && ((strlen(env)+12) < sizeof(path_home)))
  276. {
  277. sprintf(path_home,"%s\\espeak-data",env);
  278. if(GetFileLength(path_home) == -2)
  279. return; // an espeak-data directory exists in the directory specified by environment variable
  280. }
  281. strcpy(path_home,argv0);
  282. if((p = strrchr(path_home,'\\')) != NULL)
  283. {
  284. strcpy(&p[1],"espeak-data");
  285. if(GetFileLength(path_home) == -2)
  286. return; // an espeak-data directory exists in the same directory as the espeak program
  287. }
  288. // otherwise, look in the Windows Registry
  289. buf[0] = 0;
  290. RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Speech\\Voices\\Tokens\\eSpeak", 0, KEY_READ, &RegKey);
  291. size = sizeof(buf);
  292. var_type = REG_SZ;
  293. RegQueryValueEx(RegKey, "path", 0, &var_type, buf, &size);
  294. sprintf(path_home,"%s\\espeak-data",buf);
  295. #else
  296. #ifdef PLATFORM_DOS
  297. strcpy(path_home,PATH_ESPEAK_DATA);
  298. #else
  299. char *env;
  300. if((env = getenv("ESPEAK_DATA_PATH")) != NULL)
  301. {
  302. snprintf(path_home,sizeof(path_home),"%s/espeak-data",env);
  303. if(GetFileLength(path_home) == -2)
  304. return; // an espeak-data directory exists
  305. }
  306. snprintf(path_home,sizeof(path_home),"%s/espeak-data",getenv("HOME"));
  307. if(access(path_home,R_OK) != 0)
  308. {
  309. strcpy(path_home,PATH_ESPEAK_DATA);
  310. }
  311. #endif
  312. #endif
  313. }
  314. static int initialise(void)
  315. {
  316. int param;
  317. int result;
  318. int srate = 22050; // default sample rate
  319. // It seems that the wctype functions don't work until the locale has been set
  320. // to something other than the default "C". Then, not only Latin1 but also the
  321. // other characters give the correct results with iswalpha() etc.
  322. #ifdef PLATFORM_RISCOS
  323. setlocale(LC_CTYPE,"ISO8859-1");
  324. #else
  325. if(setlocale(LC_CTYPE,"en_US.UTF-8") == NULL)
  326. {
  327. if(setlocale(LC_CTYPE,"UTF-8") == NULL)
  328. setlocale(LC_CTYPE,"");
  329. }
  330. #endif
  331. if((result = LoadPhData(&srate)) != 1)
  332. {
  333. if(result == -1)
  334. {
  335. fprintf(stderr,"Failed to load espeak-data\n");
  336. exit(1);
  337. }
  338. else
  339. fprintf(stderr,"Wrong version of espeak-data 0x%x (expects 0x%x) at %s\n",result,version_phdata,path_home);
  340. }
  341. WavegenInit(srate,0);
  342. LoadConfig();
  343. SetVoiceStack(NULL, "");
  344. SynthesizeInit();
  345. for(param=0; param<N_SPEECH_PARAM; param++)
  346. param_stack[0].parameter[param] = param_defaults[param];
  347. return(0);
  348. }
  349. #ifdef NEED_GETOPT
  350. struct option {
  351. char *name;
  352. int has_arg;
  353. int *flag;
  354. int val;
  355. };
  356. int optind;
  357. static int optional_argument;
  358. static const char *arg_opts = "abfgklpsvw"; // which options have arguments
  359. static char *opt_string="";
  360. #define no_argument 0
  361. #define required_argument 1
  362. #define optional_argument 2
  363. #endif
  364. int main (int argc, char **argv)
  365. {
  366. static struct option long_options[] =
  367. {
  368. {"help", no_argument, 0, 'h'},
  369. {"stdin", no_argument, 0, 0x100},
  370. {"compile-debug", optional_argument, 0, 0x101},
  371. {"compile", optional_argument, 0, 0x102},
  372. {"punct", optional_argument, 0, 0x103},
  373. {"voices", optional_argument, 0, 0x104},
  374. {"stdout", no_argument, 0, 0x105},
  375. {"split", optional_argument, 0, 0x106},
  376. {"path", required_argument, 0, 0x107},
  377. {"phonout", required_argument, 0, 0x108},
  378. {"pho", no_argument, 0, 0x109},
  379. {"ipa", optional_argument, 0, 0x10a},
  380. {"version", no_argument, 0, 0x10b},
  381. {"sep", optional_argument, 0, 0x10c},
  382. {"tie", optional_argument, 0, 0x10d},
  383. {0, 0, 0, 0}
  384. };
  385. static const char *err_load = "Failed to read ";
  386. FILE *f_text=NULL;
  387. const char *p_text=NULL;
  388. char *data_path = NULL; // use default path for espeak-data
  389. int option_index = 0;
  390. int c;
  391. int value;
  392. int speed=175;
  393. int ix;
  394. char *optarg2;
  395. int amp = 100; // default
  396. int wordgap = 0;
  397. int flag_stdin = 0;
  398. int flag_compile = 0;
  399. int pitch_adjustment = 50;
  400. int phoneme_options = 0;
  401. int phonemes_separator = 0;
  402. espeak_VOICE voice_select;
  403. char filename[200];
  404. char voicename[40];
  405. int speaking = 0;
  406. voicename[0] = 0;
  407. mbrola_name[0] = 0;
  408. wavefile[0] = 0;
  409. filename[0] = 0;
  410. option_linelength = 0;
  411. option_phonemes = 0;
  412. option_waveout = 0;
  413. option_wordgap = 0;
  414. option_endpause = 1;
  415. option_phoneme_input = 1;
  416. option_multibyte = espeakCHARS_AUTO; // auto
  417. f_trans = stdout;
  418. #ifdef NEED_GETOPT
  419. optind = 1;
  420. opt_string = "";
  421. while(optind < argc)
  422. {
  423. int len;
  424. char *p;
  425. if((c = *opt_string) == 0)
  426. {
  427. opt_string = argv[optind];
  428. if(opt_string[0] != '-')
  429. break;
  430. optind++;
  431. opt_string++;
  432. c = *opt_string;
  433. }
  434. opt_string++;
  435. p = optarg2 = opt_string;
  436. if(c == '-')
  437. {
  438. if(p[0] == 0)
  439. break; // -- means don't interpret further - as commands
  440. opt_string="";
  441. for(ix=0;; ix++)
  442. {
  443. if(long_options[ix].name == 0)
  444. break;
  445. len = strlen(long_options[ix].name);
  446. if(memcmp(long_options[ix].name,p,len)==0)
  447. {
  448. c = long_options[ix].val;
  449. optarg2 = NULL;
  450. if((long_options[ix].has_arg != 0) && (p[len]=='='))
  451. {
  452. optarg2 = &p[len+1];
  453. }
  454. break;
  455. }
  456. }
  457. }
  458. else
  459. if(strchr(arg_opts,c) != NULL)
  460. {
  461. opt_string="";
  462. if(optarg2[0]==0)
  463. {
  464. // the option's value is in the next argument
  465. optarg2 = argv[optind++];
  466. }
  467. }
  468. #else
  469. while(true)
  470. {
  471. 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
  472. long_options, &option_index);
  473. /* Detect the end of the options. */
  474. if (c == -1)
  475. break;
  476. optarg2 = optarg;
  477. #endif
  478. switch (c)
  479. {
  480. case 'b':
  481. // input character encoding, 8bit, 16bit, UTF8
  482. option_multibyte = espeakCHARS_8BIT;
  483. if((sscanf(optarg2,"%d",&value) == 1) && (value <= 4))
  484. option_multibyte= value;
  485. break;
  486. case 'h':
  487. init_path(argv[0],data_path);
  488. printf("\nspeak text-to-speech: %s Data at: %s\n%s",version_string,path_home,help_text);
  489. exit(0);
  490. case 'k':
  491. option_capitals = atoi(optarg2);
  492. break;
  493. case 'x':
  494. phoneme_options |= espeakPHONEMES_SHOW;
  495. break;
  496. case 'X':
  497. phoneme_options |= espeakPHONEMES_TRACE;
  498. break;
  499. case 'm':
  500. option_ssml = 1;
  501. break;
  502. case 'p':
  503. pitch_adjustment = atoi(optarg2);
  504. if(pitch_adjustment > 99) pitch_adjustment = 99;
  505. break;
  506. case 'q':
  507. quiet = 1;
  508. break;
  509. case 'f':
  510. strncpy0(filename,optarg2,sizeof(filename));
  511. break;
  512. case 'l':
  513. value = 0;
  514. value = atoi(optarg2);
  515. option_linelength = value;
  516. break;
  517. case 'a':
  518. amp = atoi(optarg2);
  519. break;
  520. case 's':
  521. speed = atoi(optarg2);
  522. break;
  523. case 'g':
  524. wordgap = atoi(optarg2);
  525. break;
  526. case 'v':
  527. strncpy0(voicename,optarg2,sizeof(voicename));
  528. break;
  529. case 'w':
  530. option_waveout = 1;
  531. strncpy0(wavefile,optarg2,sizeof(wavefile));
  532. break;
  533. case 'z':
  534. option_endpause = 0;
  535. break;
  536. case 0x100: // --stdin
  537. flag_stdin = 1;
  538. break;
  539. case 0x105: // --stdout
  540. option_waveout = 1;
  541. strcpy(wavefile,"stdout");
  542. break;
  543. case 0x101: // --compile-debug
  544. case 0x102: // --compile
  545. if(optarg2 != NULL)
  546. strncpy0(voicename,optarg2,sizeof(voicename));
  547. flag_compile = c;
  548. break;
  549. case 0x103: // --punct
  550. option_punctuation = 1;
  551. if(optarg2 != NULL)
  552. {
  553. ix = 0;
  554. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  555. option_punctlist[N_PUNCTLIST-1] = 0;
  556. option_punctuation = 2;
  557. }
  558. break;
  559. case 0x104: // --voices
  560. init_path(argv[0],data_path);
  561. DisplayVoices(stdout,optarg2);
  562. exit(0);
  563. case 0x106: // -- split
  564. if(optarg2 == NULL)
  565. samples_split = 30; // default 30 minutes
  566. else
  567. samples_split = atoi(optarg2);
  568. break;
  569. case 0x107: // --path
  570. data_path = optarg2;
  571. break;
  572. case 0x108: // --phonout
  573. if((f_trans = fopen(optarg2,"w")) == NULL)
  574. {
  575. fprintf(stderr,"Can't write to: %s\n",optarg2);
  576. f_trans = stderr;
  577. }
  578. break;
  579. case 0x109: // --pho
  580. phoneme_options |= espeakPHONEMES_MBROLA;
  581. break;
  582. case 0x10a: // --ipa
  583. phoneme_options |= espeakPHONEMES_IPA;
  584. if(optarg2 != NULL)
  585. {
  586. // deprecated and obsolete
  587. switch(atoi(optarg2))
  588. {
  589. case 1:
  590. phonemes_separator = '_';
  591. break;
  592. case 2:
  593. phonemes_separator = 0x0361;
  594. phoneme_options |= espeakPHONEMES_TIE;
  595. break;
  596. case 3:
  597. phonemes_separator = 0x200d; // ZWJ
  598. phoneme_options |= espeakPHONEMES_TIE;
  599. break;
  600. }
  601. }
  602. break;
  603. case 0x10b: // --version
  604. init_path(argv[0],data_path);
  605. printf("speak text-to-speech: %s Data at: %s\n",version_string,path_home);
  606. exit(0);
  607. case 0x10c: // --sep
  608. phoneme_options |= espeakPHONEMES_SHOW;
  609. if(optarg2 == 0)
  610. phonemes_separator = ' ';
  611. else
  612. utf8_in(&phonemes_separator, optarg2);
  613. if(phonemes_separator == 'z')
  614. phonemes_separator = 0x200c; // ZWNJ
  615. break;
  616. case 0x10d: // --tie
  617. phoneme_options |= (espeakPHONEMES_SHOW | espeakPHONEMES_TIE);
  618. if(optarg2 == 0)
  619. phonemes_separator = 0x0361; // default: combining-double-inverted-breve
  620. else
  621. utf8_in(&phonemes_separator, optarg2);
  622. if(phonemes_separator == 'z')
  623. phonemes_separator = 0x200d; // ZWJ
  624. break;
  625. default:
  626. exit(0);
  627. }
  628. }
  629. init_path(argv[0],data_path);
  630. initialise();
  631. if(voicename[0] == 0)
  632. strcpy(voicename,"default");
  633. if(SetVoiceByName(voicename) != EE_OK)
  634. {
  635. memset(&voice_select,0,sizeof(voice_select));
  636. voice_select.languages = voicename;
  637. if(SetVoiceByProperties(&voice_select) != EE_OK)
  638. {
  639. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  640. exit(2);
  641. }
  642. }
  643. if(flag_compile)
  644. {
  645. #ifdef PLATFORM_DOS
  646. char path_dsource[sizeof(path_home)+20];
  647. strcpy(path_dsource,path_home);
  648. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  649. strcat(path_dsource,"dictsource\\");
  650. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  651. #else
  652. #ifdef PLATFORM_WINDOWS
  653. char path_dsource[sizeof(path_home)+20];
  654. strcpy(path_dsource,path_home);
  655. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  656. strcat(path_dsource,"dictsource\\");
  657. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  658. #else
  659. CompileDictionary(NULL,dictionary_name,NULL,NULL, flag_compile & 0x1);
  660. #endif
  661. #endif
  662. exit(0);
  663. }
  664. SetParameter(espeakRATE,speed,0);
  665. SetParameter(espeakVOLUME,amp,0);
  666. SetParameter(espeakCAPITALS,option_capitals,0);
  667. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  668. SetParameter(espeakWORDGAP,wordgap,0);
  669. option_phonemes = phoneme_options | (phonemes_separator << 8);
  670. if(pitch_adjustment != 50)
  671. {
  672. SetParameter(espeakPITCH,pitch_adjustment,0);
  673. }
  674. DoVoiceChange(voice);
  675. if(filename[0]==0)
  676. {
  677. if((optind < argc) && (flag_stdin == 0))
  678. {
  679. // there's a non-option parameter, and no -f or --stdin
  680. // use it as text
  681. p_text = argv[optind];
  682. }
  683. else
  684. {
  685. f_text = stdin;
  686. if(flag_stdin == 0)
  687. option_linelength = -1; // single input lines on stdin
  688. }
  689. }
  690. else
  691. {
  692. f_text = fopen(filename,"r");
  693. }
  694. if((f_text == NULL) && (p_text == NULL))
  695. {
  696. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  697. exit(1);
  698. }
  699. if(option_waveout || quiet)
  700. {
  701. if(quiet)
  702. {
  703. // no sound output
  704. OpenWaveFile(NULL,samplerate);
  705. option_waveout = 1;
  706. }
  707. else
  708. {
  709. // write sound output to a WAV file
  710. samples_split = (samplerate * samples_split) * 60;
  711. if(samples_split)
  712. {
  713. // don't open the wav file until we start generating speech
  714. char *extn;
  715. extn = strrchr(wavefile,'.');
  716. if((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4))
  717. {
  718. strcpy(filetype,extn);
  719. *extn = 0;
  720. }
  721. }
  722. else
  723. if(OpenWaveFile(wavefile,samplerate) != 0)
  724. {
  725. fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
  726. exit(3);
  727. }
  728. }
  729. InitText(0);
  730. SpeakNextClause(f_text,p_text,0);
  731. ix = 1;
  732. for(;; )
  733. {
  734. if(WavegenFile() != 0)
  735. {
  736. if(ix == 0)
  737. break; // finished, wavegen command queue is empty
  738. }
  739. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  740. {
  741. ix = SpeakNextClause(NULL,NULL,1);
  742. }
  743. }
  744. CloseWaveFile();
  745. }
  746. else
  747. {
  748. WavegenInitSound();
  749. InitText(0);
  750. SpeakNextClause(f_text,p_text,0);
  751. if(option_quiet)
  752. {
  753. while(SpeakNextClause(NULL,NULL,1) != 0);
  754. return(0);
  755. }
  756. speaking = 1;
  757. while(speaking)
  758. {
  759. // NOTE: if nanosleep() isn't recognised on your system, try replacing
  760. // this by sleep(1);
  761. #ifdef PLATFORM_WINDOWS
  762. Sleep(300); // 0.3s
  763. #else
  764. #ifdef USE_NANOSLEEP
  765. struct timespec period;
  766. struct timespec remaining;
  767. period.tv_sec = 0;
  768. period.tv_nsec = 300000000; // 0.3 sec
  769. nanosleep(&period,&remaining);
  770. #else
  771. sleep(1);
  772. #endif
  773. #endif
  774. if(SynthOnTimer() != 0)
  775. speaking = 0;
  776. }
  777. }
  778. if((f_trans != stdout) && (f_trans != stderr))
  779. fclose(f_trans); // needed for WinCe
  780. return(0);
  781. }