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

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