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

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