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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2013 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include "StdAfx.h"
  20. #include "speech.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifndef PLATFORM_DOS
  25. #ifdef PLATFORM_WINDOWS
  26. #include <fcntl.h>
  27. #include <io.h>
  28. #include <windows.h>
  29. #include <winreg.h>
  30. #else
  31. #include <unistd.h>
  32. #endif
  33. #endif
  34. #ifndef NEED_GETOPT
  35. #include <getopt.h>
  36. #endif
  37. #include <time.h>
  38. #include <signal.h>
  39. #include <locale.h>
  40. #include <sys/stat.h>
  41. #include "speak_lib.h"
  42. #include "phoneme.h"
  43. #include "synthesize.h"
  44. #include "voice.h"
  45. #include "translate.h"
  46. extern void Write4Bytes(FILE *f, int value);
  47. char path_home[N_PATH_HOME]; // this is the espeak-data directory
  48. char filetype[5];
  49. char wavefile[200];
  50. int (* uri_callback)(int, const char *, const char *) = NULL;
  51. int (* phoneme_callback)(const char *) = NULL;
  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 [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 words per minute, 80 to 450, 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. "\t --ipa=1 Use ties, --ipa=2 Use ZWJ\n"
  93. "--path=\"<path>\"\n"
  94. "\t Specifies the directory containing the espeak-data directory\n"
  95. "--pho Write mbrola phoneme data (.pho) to stdout or to the file in --phonout\n"
  96. "--phonout=\"<filename>\"\n"
  97. "\t Write phoneme output from -x -X --ipa and --pho to this file\n"
  98. "--punct=\"<characters>\"\n"
  99. "\t Speak the names of punctuation characters during speaking. If\n"
  100. "\t =<characters> is omitted, all punctuation is spoken.\n"
  101. "--split=\"<minutes>\"\n"
  102. "\t Starts a new WAV file every <minutes>. Used with -w\n"
  103. "--stdout Write speech output to stdout\n"
  104. "--voices=<language>\n"
  105. "\t List the available voices for the specified language.\n"
  106. "\t If <language> is omitted, then list all voices.\n";
  107. void DisplayVoices(FILE *f_out, char *language);
  108. USHORT voice_pcnt[N_PEAKS+1][3];
  109. int GetFileLength(const char *filename)
  110. {//====================================
  111. struct stat statbuf;
  112. if(stat(filename,&statbuf) != 0)
  113. return(0);
  114. if((statbuf.st_mode & S_IFMT) == S_IFDIR)
  115. // if(S_ISDIR(statbuf.st_mode))
  116. return(-2); // a directory
  117. return(statbuf.st_size);
  118. } // end of GetFileLength
  119. char *Alloc(int size)
  120. {//==================
  121. char *p;
  122. if((p = (char *)malloc(size)) == NULL)
  123. fprintf(stderr,"Can't allocate memory\n");
  124. return(p);
  125. }
  126. void Free(void *ptr)
  127. {//=================
  128. if(ptr != NULL)
  129. free(ptr);
  130. }
  131. void DisplayVoices(FILE *f_out, char *language)
  132. {//============================================
  133. int ix;
  134. const char *p;
  135. int len;
  136. int count;
  137. const espeak_VOICE *v;
  138. const char *lang_name;
  139. char age_buf[12];
  140. const espeak_VOICE **voices;
  141. espeak_VOICE voice_select;
  142. static char genders[4] = {' ','M','F',' '};
  143. if((language != NULL) && (language[0] != 0))
  144. {
  145. // display only voices for the specified language, in order of priority
  146. voice_select.languages = language;
  147. voice_select.age = 0;
  148. voice_select.gender = 0;
  149. voice_select.name = NULL;
  150. voices = espeak_ListVoices(&voice_select);
  151. }
  152. else
  153. {
  154. voices = espeak_ListVoices(NULL);
  155. }
  156. fprintf(f_out,"Pty Language Age/Gender VoiceName File Other Langs\n");
  157. for(ix=0; (v = voices[ix]) != NULL; ix++)
  158. {
  159. count = 0;
  160. p = v->languages;
  161. while(*p != 0)
  162. {
  163. len = strlen(p+1);
  164. lang_name = p+1;
  165. if(v->age == 0)
  166. strcpy(age_buf," ");
  167. else
  168. sprintf(age_buf,"%3d",v->age);
  169. if(count==0)
  170. {
  171. fprintf(f_out,"%2d %-12s%s%c %-17s %-11s ",
  172. p[0],lang_name,age_buf,genders[v->gender],v->name,v->identifier);
  173. }
  174. else
  175. {
  176. fprintf(f_out,"(%s %d)",lang_name,p[0]);
  177. }
  178. count++;
  179. p += len+2;
  180. }
  181. fputc('\n',f_out);
  182. }
  183. } // end of DisplayVoices
  184. void WVoiceChanged(voice_t *wvoice)
  185. {
  186. }
  187. static int OpenWaveFile(const char *path, int rate)
  188. //=================================================
  189. {
  190. // Set the length of 0x7ffff000 for --stdout
  191. // This will be changed to the correct length for -w (write to file)
  192. static unsigned char wave_hdr[44] = {
  193. 'R','I','F','F',0x24,0xf0,0xff,0x7f,'W','A','V','E','f','m','t',' ',
  194. 0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
  195. 2,0,0x10,0,'d','a','t','a', 0x00,0xf0,0xff,0x7f};
  196. if(path == NULL)
  197. return(2);
  198. if(strcmp(path,"stdout")==0)
  199. {
  200. #ifdef PLATFORM_WINDOWS
  201. // prevent Windows adding 0x0d before 0x0a bytes
  202. _setmode(_fileno(stdout), _O_BINARY);
  203. #endif
  204. f_wave = stdout;
  205. }
  206. else
  207. f_wave = fopen(path,"wb");
  208. if(f_wave != NULL)
  209. {
  210. fwrite(wave_hdr,1,24,f_wave);
  211. Write4Bytes(f_wave,rate);
  212. Write4Bytes(f_wave,rate * 2);
  213. fwrite(&wave_hdr[32],1,12,f_wave);
  214. return(0);
  215. }
  216. return(1);
  217. } // end of OpenWaveFile
  218. static void CloseWaveFile()
  219. //=========================
  220. {
  221. unsigned int pos;
  222. if((f_wave == NULL) || (f_wave == stdout))
  223. return;
  224. fflush(f_wave);
  225. pos = ftell(f_wave);
  226. fseek(f_wave,4,SEEK_SET);
  227. Write4Bytes(f_wave,pos - 8);
  228. fseek(f_wave,40,SEEK_SET);
  229. Write4Bytes(f_wave,pos - 44);
  230. fclose(f_wave);
  231. f_wave = NULL;
  232. } // end of CloseWaveFile
  233. void MarkerEvent(int type, unsigned int char_position, int value, int value2, unsigned char *out_ptr)
  234. {//======================================================================================
  235. // Do nothing in the command-line version.
  236. if(type == 2)
  237. end_of_sentence = 1;
  238. } // end of MarkerEvent
  239. static int WavegenFile(void)
  240. {//=========================
  241. int finished;
  242. unsigned char wav_outbuf[1024];
  243. char fname[210];
  244. out_ptr = out_start = wav_outbuf;
  245. out_end = wav_outbuf + sizeof(wav_outbuf);
  246. finished = WavegenFill(0);
  247. if(quiet)
  248. return(finished);
  249. if(f_wave == NULL)
  250. {
  251. sprintf(fname,"%s_%.2d%s",wavefile,++wavefile_count,filetype);
  252. if(OpenWaveFile(fname, samplerate) != 0)
  253. return(1);
  254. }
  255. if(end_of_sentence)
  256. {
  257. end_of_sentence = 0;
  258. if((samples_split > 0 ) && (samples_total > samples_split))
  259. {
  260. CloseWaveFile();
  261. samples_total = 0;
  262. }
  263. }
  264. if(f_wave != NULL)
  265. {
  266. samples_total += (out_ptr - wav_outbuf)/2;
  267. fwrite(wav_outbuf, 1, out_ptr - wav_outbuf, f_wave);
  268. }
  269. return(finished);
  270. } // end of WavegenFile
  271. static void init_path(char *argv0, char *path_specified)
  272. {//=====================================================
  273. if(path_specified)
  274. {
  275. sprintf(path_home,"%s/espeak-data",path_specified);
  276. return;
  277. }
  278. #ifdef PLATFORM_WINDOWS
  279. HKEY RegKey;
  280. unsigned long size;
  281. unsigned long var_type;
  282. char *p;
  283. char *env;
  284. unsigned char buf[sizeof(path_home)-12];
  285. if(((env = getenv("ESPEAK_DATA_PATH")) != NULL) && ((strlen(env)+12) < sizeof(path_home)))
  286. {
  287. sprintf(path_home,"%s\\espeak-data",env);
  288. if(GetFileLength(path_home) == -2)
  289. return; // an espeak-data directory exists in the directory specified by environment variable
  290. }
  291. strcpy(path_home,argv0);
  292. if((p = strrchr(path_home,'\\')) != NULL)
  293. {
  294. strcpy(&p[1],"espeak-data");
  295. if(GetFileLength(path_home) == -2)
  296. return; // an espeak-data directory exists in the same directory as the espeak program
  297. }
  298. // otherwise, look in the Windows Registry
  299. buf[0] = 0;
  300. RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Speech\\Voices\\Tokens\\eSpeak", 0, KEY_READ, &RegKey);
  301. size = sizeof(buf);
  302. var_type = REG_SZ;
  303. RegQueryValueEx(RegKey, "path", 0, &var_type, buf, &size);
  304. sprintf(path_home,"%s\\espeak-data",buf);
  305. #else
  306. #ifdef PLATFORM_DOS
  307. strcpy(path_home,PATH_ESPEAK_DATA);
  308. #else
  309. char *env;
  310. if((env = getenv("ESPEAK_DATA_PATH")) != NULL)
  311. {
  312. snprintf(path_home,sizeof(path_home),"%s/espeak-data",env);
  313. if(GetFileLength(path_home) == -2)
  314. return; // an espeak-data directory exists
  315. }
  316. snprintf(path_home,sizeof(path_home),"%s/espeak-data",getenv("HOME"));
  317. if(access(path_home,R_OK) != 0)
  318. {
  319. strcpy(path_home,PATH_ESPEAK_DATA);
  320. }
  321. #endif
  322. #endif
  323. }
  324. static int initialise(void)
  325. {//========================
  326. int param;
  327. int result;
  328. int srate = 22050; // default sample rate
  329. // It seems that the wctype functions don't work until the locale has been set
  330. // to something other than the default "C". Then, not only Latin1 but also the
  331. // other characters give the correct results with iswalpha() etc.
  332. #ifdef PLATFORM_RISCOS
  333. setlocale(LC_CTYPE,"ISO8859-1");
  334. #else
  335. if(setlocale(LC_CTYPE,"en_US.UTF-8") == NULL)
  336. {
  337. if(setlocale(LC_CTYPE,"UTF-8") == NULL)
  338. setlocale(LC_CTYPE,"");
  339. }
  340. #endif
  341. if((result = LoadPhData(&srate)) != 1)
  342. {
  343. if(result == -1)
  344. {
  345. fprintf(stderr,"Failed to load espeak-data\n");
  346. exit(1);
  347. }
  348. else
  349. fprintf(stderr,"Wrong version of espeak-data 0x%x (expects 0x%x) at %s\n",result,version_phdata,path_home);
  350. }
  351. WavegenInit(srate,0);
  352. LoadConfig();
  353. SetVoiceStack(NULL, "");
  354. SynthesizeInit();
  355. for(param=0; param<N_SPEECH_PARAM; param++)
  356. param_stack[0].parameter[param] = param_defaults[param];
  357. return(0);
  358. }
  359. static void StopSpeak(int unused)
  360. {//==============================
  361. signal(SIGINT,SIG_IGN);
  362. // DEBUG
  363. // printf("\n*** Interrupting speech output (use Ctrl-D to actually quit).\n");
  364. fflush(stdout);
  365. SpeakNextClause(NULL,NULL,5);
  366. signal(SIGINT,StopSpeak);
  367. } // end of StopSpeak()
  368. #ifdef NEED_GETOPT
  369. struct option {
  370. char *name;
  371. int has_arg;
  372. int *flag;
  373. int val;
  374. };
  375. int optind;
  376. static int optional_argument;
  377. static const char *arg_opts = "abfgklpsvw"; // which options have arguments
  378. static char *opt_string="";
  379. #define no_argument 0
  380. #define required_argument 1
  381. #define optional_argument 2
  382. #endif
  383. int main (int argc, char **argv)
  384. //==============================
  385. {
  386. static struct option long_options[] =
  387. {
  388. /* These options set a flag. */
  389. // {"verbose", no_argument, &verbose_flag, 1},
  390. // {"brief", no_argument, &verbose_flag, 0},
  391. /* These options don't set a flag.
  392. We distinguish them by their indices. */
  393. {"help", no_argument, 0, 'h'},
  394. {"stdin", no_argument, 0, 0x100},
  395. {"compile-debug", optional_argument, 0, 0x101},
  396. {"compile", optional_argument, 0, 0x102},
  397. {"punct", optional_argument, 0, 0x103},
  398. {"voices", optional_argument, 0, 0x104},
  399. {"stdout", no_argument, 0, 0x105},
  400. {"split", optional_argument, 0, 0x106},
  401. {"path", required_argument, 0, 0x107},
  402. {"phonout", required_argument, 0, 0x108},
  403. {"pho", no_argument, 0, 0x109},
  404. {"ipa", optional_argument, 0, 0x10a},
  405. {"version", no_argument, 0, 0x10b},
  406. {0, 0, 0, 0}
  407. };
  408. static const char *err_load = "Failed to read ";
  409. FILE *f_text=NULL;
  410. const char *p_text=NULL;
  411. char *data_path = NULL; // use default path for espeak-data
  412. int option_index = 0;
  413. int c;
  414. int value;
  415. int speed=175;
  416. int ix;
  417. char *optarg2;
  418. int amp = 100; // default
  419. int wordgap = 0;
  420. int speaking = 0;
  421. int flag_stdin = 0;
  422. int flag_compile = 0;
  423. int pitch_adjustment = 50;
  424. espeak_VOICE voice_select;
  425. char filename[200];
  426. char voicename[40];
  427. voicename[0] = 0;
  428. mbrola_name[0] = 0;
  429. wavefile[0] = 0;
  430. filename[0] = 0;
  431. option_linelength = 0;
  432. option_phonemes = 0;
  433. option_waveout = 0;
  434. option_wordgap = 0;
  435. option_endpause = 1;
  436. option_phoneme_input = 1;
  437. option_multibyte = espeakCHARS_AUTO; // auto
  438. f_trans = stdout;
  439. #ifdef NEED_GETOPT
  440. optind = 1;
  441. opt_string = "";
  442. while(optind < argc)
  443. {
  444. int len;
  445. char *p;
  446. if((c = *opt_string) == 0)
  447. {
  448. opt_string = argv[optind];
  449. if(opt_string[0] != '-')
  450. break;
  451. optind++;
  452. opt_string++;
  453. c = *opt_string;
  454. }
  455. opt_string++;
  456. p = optarg2 = opt_string;
  457. if(c == '-')
  458. {
  459. if(p[0] == 0)
  460. break; // -- means don't interpret further - as commands
  461. opt_string="";
  462. for(ix=0; ;ix++)
  463. {
  464. if(long_options[ix].name == 0)
  465. break;
  466. len = strlen(long_options[ix].name);
  467. if(memcmp(long_options[ix].name,p,len)==0)
  468. {
  469. c = long_options[ix].val;
  470. optarg2 = NULL;
  471. if((long_options[ix].has_arg != 0) && (p[len]=='='))
  472. {
  473. optarg2 = &p[len+1];
  474. }
  475. break;
  476. }
  477. }
  478. }
  479. else
  480. if(strchr(arg_opts,c) != NULL)
  481. {
  482. opt_string="";
  483. if(optarg2[0]==0)
  484. {
  485. // the option's value is in the next argument
  486. optarg2 = argv[optind++];
  487. }
  488. }
  489. #else
  490. while(true)
  491. {
  492. 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
  493. long_options, &option_index);
  494. /* Detect the end of the options. */
  495. if (c == -1)
  496. break;
  497. optarg2 = optarg;
  498. #endif
  499. switch (c)
  500. {
  501. case 'b':
  502. // input character encoding, 8bit, 16bit, UTF8
  503. option_multibyte = espeakCHARS_8BIT;
  504. if((sscanf(optarg2,"%d",&value) == 1) && (value <= 4))
  505. option_multibyte= value;
  506. break;
  507. case 'h':
  508. init_path(argv[0],data_path);
  509. printf("\nspeak text-to-speech: %s Data at: %s\n%s",version_string,path_home,help_text);
  510. exit(0);
  511. case 'k':
  512. option_capitals = atoi(optarg2);
  513. break;
  514. case 'x':
  515. option_phonemes = 1;
  516. break;
  517. case 'X':
  518. option_phonemes = 2;
  519. break;
  520. case 'm':
  521. option_ssml = 1;
  522. break;
  523. case 'p':
  524. pitch_adjustment = atoi(optarg2);
  525. if(pitch_adjustment > 99) pitch_adjustment = 99;
  526. break;
  527. case 'q':
  528. quiet = 1;
  529. break;
  530. case 'f':
  531. strncpy0(filename,optarg2,sizeof(filename));
  532. break;
  533. case 'l':
  534. value = 0;
  535. value = atoi(optarg2);
  536. option_linelength = value;
  537. break;
  538. case 'a':
  539. amp = atoi(optarg2);
  540. break;
  541. case 's':
  542. speed = atoi(optarg2);
  543. break;
  544. case 'g':
  545. wordgap = atoi(optarg2);
  546. break;
  547. case 'v':
  548. strncpy0(voicename,optarg2,sizeof(voicename));
  549. break;
  550. case 'w':
  551. option_waveout = 1;
  552. strncpy0(wavefile,optarg2,sizeof(wavefile));
  553. break;
  554. case 'z':
  555. option_endpause = 0;
  556. break;
  557. case 0x100: // --stdin
  558. flag_stdin = 1;
  559. break;
  560. case 0x105: // --stdout
  561. option_waveout = 1;
  562. strcpy(wavefile,"stdout");
  563. break;
  564. case 0x101: // --compile-debug
  565. case 0x102: // --compile
  566. if(optarg2 != NULL)
  567. strncpy0(voicename,optarg2,sizeof(voicename));
  568. flag_compile = c;
  569. break;
  570. case 0x103: // --punct
  571. option_punctuation = 1;
  572. if(optarg2 != NULL)
  573. {
  574. ix = 0;
  575. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  576. option_punctlist[N_PUNCTLIST-1] = 0;
  577. option_punctuation = 2;
  578. }
  579. break;
  580. case 0x104: // --voices
  581. init_path(argv[0],data_path);
  582. DisplayVoices(stdout,optarg2);
  583. exit(0);
  584. case 0x106: // -- split
  585. if(optarg2 == NULL)
  586. samples_split = 30; // default 30 minutes
  587. else
  588. samples_split = atoi(optarg2);
  589. break;
  590. case 0x107: // --path
  591. data_path = optarg2;
  592. break;
  593. case 0x108: // --phonout
  594. if((f_trans = fopen(optarg2,"w")) == NULL)
  595. {
  596. fprintf(stderr,"Can't write to: %s\n",optarg2);
  597. f_trans = stderr;
  598. }
  599. break;
  600. case 0x109: // --pho
  601. option_mbrola_phonemes = 16;
  602. break;
  603. case 0x10a: // --ipa
  604. option_phonemes = 3;
  605. if(optarg2 != NULL)
  606. {
  607. value = -1;
  608. sscanf(optarg2,"%d",&value);
  609. if((value<0) || (value>2))
  610. {
  611. fprintf(stderr,"Bad value for -ipa=\n");
  612. value = 0;
  613. }
  614. option_phonemes += value;
  615. }
  616. break;
  617. case 0x10b: // --version
  618. init_path(argv[0],data_path);
  619. printf("speak text-to-speech: %s Data at: %s\n",version_string,path_home);
  620. exit(0);
  621. default:
  622. exit(0);
  623. }
  624. }
  625. init_path(argv[0],data_path);
  626. initialise();
  627. if(flag_compile)
  628. {
  629. LoadVoice(voicename,5);
  630. #ifdef PLATFORM_DOS
  631. char path_dsource[sizeof(path_home)+20];
  632. strcpy(path_dsource,path_home);
  633. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  634. strcat(path_dsource,"dictsource\\");
  635. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  636. #else
  637. #ifdef PLATFORM_WINDOWS
  638. char path_dsource[sizeof(path_home)+20];
  639. strcpy(path_dsource,path_home);
  640. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  641. strcat(path_dsource,"dictsource\\");
  642. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  643. #else
  644. CompileDictionary(NULL,dictionary_name,NULL,NULL, flag_compile & 0x1);
  645. #endif
  646. #endif
  647. exit(0);
  648. }
  649. if(voicename[0] == 0)
  650. strcpy(voicename,"default");
  651. if(SetVoiceByName(voicename) != EE_OK)
  652. {
  653. memset(&voice_select,0,sizeof(voice_select));
  654. voice_select.languages = voicename;
  655. if(SetVoiceByProperties(&voice_select) != EE_OK)
  656. {
  657. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  658. exit(2);
  659. }
  660. }
  661. SetParameter(espeakRATE,speed,0);
  662. SetParameter(espeakVOLUME,amp,0);
  663. SetParameter(espeakCAPITALS,option_capitals,0);
  664. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  665. SetParameter(espeakWORDGAP,wordgap,0);
  666. if(pitch_adjustment != 50)
  667. {
  668. SetParameter(espeakPITCH,pitch_adjustment,0);
  669. }
  670. DoVoiceChange(voice);
  671. if(filename[0]==0)
  672. {
  673. if((optind < argc) && (flag_stdin == 0))
  674. {
  675. // there's a non-option parameter, and no -f or --stdin
  676. // use it as text
  677. p_text = argv[optind];
  678. }
  679. else
  680. {
  681. f_text = stdin;
  682. if(flag_stdin == 0)
  683. option_linelength = -1; // single input lines on stdin
  684. }
  685. }
  686. else
  687. {
  688. f_text = fopen(filename,"r");
  689. }
  690. if((f_text == NULL) && (p_text == NULL))
  691. {
  692. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  693. exit(1);
  694. }
  695. if(option_waveout || quiet)
  696. {
  697. if(quiet)
  698. {
  699. // no sound output
  700. OpenWaveFile(NULL,samplerate);
  701. option_waveout = 1;
  702. }
  703. else
  704. {
  705. // write sound output to a WAV file
  706. samples_split = (samplerate * samples_split) * 60;
  707. if(samples_split)
  708. {
  709. // don't open the wav file until we start generating speech
  710. char *extn;
  711. extn = strrchr(wavefile,'.');
  712. if((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4))
  713. {
  714. strcpy(filetype,extn);
  715. *extn = 0;
  716. }
  717. }
  718. else
  719. if(OpenWaveFile(wavefile,samplerate) != 0)
  720. {
  721. fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
  722. exit(3);
  723. }
  724. }
  725. InitText(0);
  726. SpeakNextClause(f_text,p_text,0);
  727. ix = 1;
  728. for(;;)
  729. {
  730. if(WavegenFile() != 0)
  731. {
  732. if(ix == 0)
  733. break; // finished, wavegen command queue is empty
  734. }
  735. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  736. {
  737. ix = SpeakNextClause(NULL,NULL,1);
  738. }
  739. }
  740. CloseWaveFile();
  741. }
  742. else
  743. {
  744. // Silence on ^C or SIGINT
  745. // signal(SIGINT,StopSpeak);
  746. // output sound using portaudio
  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. #ifdef USE_PORTAUDIO
  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. #else
  778. fprintf(stderr,"-w option must be used because the program was built without a sound interface\n");
  779. #endif // USE_PORTAUDIO
  780. }
  781. if((f_trans != stdout) && (f_trans != stderr))
  782. fclose(f_trans); // needed for WinCe
  783. return(0);
  784. }