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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 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. // It seems that the wctype functions don't work until the locale has been set
  328. // to something other than the default "C". Then, not only Latin1 but also the
  329. // other characters give the correct results with iswalpha() etc.
  330. #ifdef PLATFORM_RISCOS
  331. setlocale(LC_CTYPE,"ISO8859-1");
  332. #else
  333. if(setlocale(LC_CTYPE,"en_US.UTF-8") == NULL)
  334. {
  335. if(setlocale(LC_CTYPE,"UTF-8") == NULL)
  336. setlocale(LC_CTYPE,"");
  337. }
  338. #endif
  339. WavegenInit(22050,0); // 22050
  340. if((result = LoadPhData()) != 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. LoadConfig();
  351. SetVoiceStack(NULL, "");
  352. SynthesizeInit();
  353. for(param=0; param<N_SPEECH_PARAM; param++)
  354. param_stack[0].parameter[param] = param_defaults[param];
  355. return(0);
  356. }
  357. static void StopSpeak(int unused)
  358. {//==============================
  359. signal(SIGINT,SIG_IGN);
  360. // DEBUG
  361. // printf("\n*** Interrupting speech output (use Ctrl-D to actually quit).\n");
  362. fflush(stdout);
  363. SpeakNextClause(NULL,NULL,5);
  364. signal(SIGINT,StopSpeak);
  365. } // end of StopSpeak()
  366. #ifdef NEED_GETOPT
  367. struct option {
  368. char *name;
  369. int has_arg;
  370. int *flag;
  371. int val;
  372. };
  373. int optind;
  374. static int optional_argument;
  375. static const char *arg_opts = "abfgklpsvw"; // which options have arguments
  376. static char *opt_string="";
  377. #define no_argument 0
  378. #define required_argument 1
  379. #define optional_argument 2
  380. #endif
  381. int main (int argc, char **argv)
  382. //==============================
  383. {
  384. static struct option long_options[] =
  385. {
  386. /* These options set a flag. */
  387. // {"verbose", no_argument, &verbose_flag, 1},
  388. // {"brief", no_argument, &verbose_flag, 0},
  389. /* These options don't set a flag.
  390. We distinguish them by their indices. */
  391. {"help", no_argument, 0, 'h'},
  392. {"stdin", no_argument, 0, 0x100},
  393. {"compile-debug", optional_argument, 0, 0x101},
  394. {"compile", optional_argument, 0, 0x102},
  395. {"punct", optional_argument, 0, 0x103},
  396. {"voices", optional_argument, 0, 0x104},
  397. {"stdout", no_argument, 0, 0x105},
  398. {"split", optional_argument, 0, 0x106},
  399. {"path", required_argument, 0, 0x107},
  400. {"phonout", required_argument, 0, 0x108},
  401. {"pho", no_argument, 0, 0x109},
  402. {"ipa", no_argument, 0, 0x10a},
  403. {"version", no_argument, 0, 0x10b},
  404. {0, 0, 0, 0}
  405. };
  406. static const char *err_load = "Failed to read ";
  407. FILE *f_text=NULL;
  408. const char *p_text=NULL;
  409. char *data_path = NULL; // use default path for espeak-data
  410. int option_index = 0;
  411. int c;
  412. int value;
  413. int speed=175;
  414. int ix;
  415. char *optarg2;
  416. int amp = 100; // default
  417. int wordgap = 0;
  418. int speaking = 0;
  419. int flag_stdin = 0;
  420. int flag_compile = 0;
  421. int pitch_adjustment = 50;
  422. espeak_VOICE voice_select;
  423. char filename[200];
  424. char voicename[40];
  425. voicename[0] = 0;
  426. mbrola_name[0] = 0;
  427. wavefile[0] = 0;
  428. filename[0] = 0;
  429. option_linelength = 0;
  430. option_phonemes = 0;
  431. option_waveout = 0;
  432. option_wordgap = 0;
  433. option_endpause = 1;
  434. option_phoneme_input = 1;
  435. option_multibyte = espeakCHARS_AUTO; // auto
  436. f_trans = stdout;
  437. #ifdef NEED_GETOPT
  438. optind = 1;
  439. opt_string = "";
  440. while(optind < argc)
  441. {
  442. int len;
  443. char *p;
  444. if((c = *opt_string) == 0)
  445. {
  446. opt_string = argv[optind];
  447. if(opt_string[0] != '-')
  448. break;
  449. optind++;
  450. opt_string++;
  451. c = *opt_string;
  452. }
  453. opt_string++;
  454. p = optarg2 = opt_string;
  455. if(c == '-')
  456. {
  457. if(p[0] == 0)
  458. break; // -- means don't interpret further - as commands
  459. opt_string="";
  460. for(ix=0; ;ix++)
  461. {
  462. if(long_options[ix].name == 0)
  463. break;
  464. len = strlen(long_options[ix].name);
  465. if(memcmp(long_options[ix].name,p,len)==0)
  466. {
  467. c = long_options[ix].val;
  468. optarg2 = NULL;
  469. if((long_options[ix].has_arg != 0) && (p[len]=='='))
  470. {
  471. optarg2 = &p[len+1];
  472. }
  473. break;
  474. }
  475. }
  476. }
  477. else
  478. if(strchr(arg_opts,c) != NULL)
  479. {
  480. opt_string="";
  481. if(optarg2[0]==0)
  482. {
  483. // the option's value is in the next argument
  484. optarg2 = argv[optind++];
  485. }
  486. }
  487. #else
  488. while(true)
  489. {
  490. 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
  491. long_options, &option_index);
  492. /* Detect the end of the options. */
  493. if (c == -1)
  494. break;
  495. optarg2 = optarg;
  496. #endif
  497. switch (c)
  498. {
  499. case 'b':
  500. // input character encoding, 8bit, 16bit, UTF8
  501. option_multibyte = espeakCHARS_8BIT;
  502. if((sscanf(optarg2,"%d",&value) == 1) && (value <= 4))
  503. option_multibyte= value;
  504. break;
  505. case 'h':
  506. init_path(argv[0],data_path);
  507. printf("\nspeak text-to-speech: %s Data at: %s\n%s",version_string,path_home,help_text);
  508. exit(0);
  509. case 'k':
  510. option_capitals = atoi(optarg2);
  511. break;
  512. case 'x':
  513. option_phonemes = 1;
  514. break;
  515. case 'X':
  516. option_phonemes = 2;
  517. break;
  518. case 'm':
  519. option_ssml = 1;
  520. break;
  521. case 'p':
  522. pitch_adjustment = atoi(optarg2);
  523. if(pitch_adjustment > 99) pitch_adjustment = 99;
  524. break;
  525. case 'q':
  526. quiet = 1;
  527. break;
  528. case 'f':
  529. strncpy0(filename,optarg2,sizeof(filename));
  530. break;
  531. case 'l':
  532. value = 0;
  533. value = atoi(optarg2);
  534. option_linelength = value;
  535. break;
  536. case 'a':
  537. amp = atoi(optarg2);
  538. break;
  539. case 's':
  540. speed = atoi(optarg2);
  541. break;
  542. case 'g':
  543. wordgap = atoi(optarg2);
  544. break;
  545. case 'v':
  546. strncpy0(voicename,optarg2,sizeof(voicename));
  547. break;
  548. case 'w':
  549. option_waveout = 1;
  550. strncpy0(wavefile,optarg2,sizeof(wavefile));
  551. break;
  552. case 'z':
  553. option_endpause = 0;
  554. break;
  555. case 0x100: // --stdin
  556. flag_stdin = 1;
  557. break;
  558. case 0x105: // --stdout
  559. option_waveout = 1;
  560. strcpy(wavefile,"stdout");
  561. break;
  562. case 0x101: // --compile-debug
  563. case 0x102: // --compile
  564. if(optarg2 != NULL)
  565. strncpy0(voicename,optarg2,sizeof(voicename));
  566. flag_compile = c;
  567. break;
  568. case 0x103: // --punct
  569. option_punctuation = 1;
  570. if(optarg2 != NULL)
  571. {
  572. ix = 0;
  573. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  574. option_punctlist[N_PUNCTLIST-1] = 0;
  575. option_punctuation = 2;
  576. }
  577. break;
  578. case 0x104: // --voices
  579. init_path(argv[0],data_path);
  580. DisplayVoices(stdout,optarg2);
  581. exit(0);
  582. case 0x106: // -- split
  583. if(optarg2 == NULL)
  584. samples_split = 30; // default 30 minutes
  585. else
  586. samples_split = atoi(optarg2);
  587. break;
  588. case 0x107: // --path
  589. data_path = optarg2;
  590. break;
  591. case 0x108: // --phonout
  592. if((f_trans = fopen(optarg2,"w")) == NULL)
  593. {
  594. fprintf(stderr,"Can't write to: %s\n",optarg2);
  595. f_trans = stderr;
  596. }
  597. break;
  598. case 0x109: // --pho
  599. option_mbrola_phonemes = 16;
  600. break;
  601. case 0x10a: // --ipa
  602. option_phonemes = 3;
  603. break;
  604. case 0x10b: // --version
  605. init_path(argv[0],data_path);
  606. printf("speak text-to-speech: %s Data at: %s\n",version_string,path_home);
  607. exit(0);
  608. default:
  609. exit(0);
  610. }
  611. }
  612. init_path(argv[0],data_path);
  613. initialise();
  614. if(flag_compile)
  615. {
  616. LoadVoice(voicename,5);
  617. #ifdef PLATFORM_DOS
  618. char path_dsource[sizeof(path_home)+20];
  619. strcpy(path_dsource,path_home);
  620. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  621. strcat(path_dsource,"dictsource\\");
  622. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  623. #else
  624. #ifdef PLATFORM_WINDOWS
  625. char path_dsource[sizeof(path_home)+20];
  626. strcpy(path_dsource,path_home);
  627. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  628. strcat(path_dsource,"dictsource\\");
  629. CompileDictionary(path_dsource,dictionary_name,NULL,NULL, flag_compile & 0x1);
  630. #else
  631. CompileDictionary(NULL,dictionary_name,NULL,NULL, flag_compile & 0x1);
  632. #endif
  633. #endif
  634. exit(0);
  635. }
  636. if(voicename[0] == 0)
  637. strcpy(voicename,"default");
  638. if(SetVoiceByName(voicename) != EE_OK)
  639. {
  640. memset(&voice_select,0,sizeof(voice_select));
  641. voice_select.languages = voicename;
  642. if(SetVoiceByProperties(&voice_select) != EE_OK)
  643. {
  644. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  645. exit(2);
  646. }
  647. }
  648. SetParameter(espeakRATE,speed,0);
  649. SetParameter(espeakVOLUME,amp,0);
  650. SetParameter(espeakCAPITALS,option_capitals,0);
  651. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  652. SetParameter(espeakWORDGAP,wordgap,0);
  653. if(pitch_adjustment != 50)
  654. {
  655. SetParameter(espeakPITCH,pitch_adjustment,0);
  656. }
  657. DoVoiceChange(voice);
  658. if(filename[0]==0)
  659. {
  660. if((optind < argc) && (flag_stdin == 0))
  661. {
  662. // there's a non-option parameter, and no -f or --stdin
  663. // use it as text
  664. p_text = argv[optind];
  665. }
  666. else
  667. {
  668. f_text = stdin;
  669. if(flag_stdin == 0)
  670. option_linelength = -1; // single input lines on stdin
  671. }
  672. }
  673. else
  674. {
  675. f_text = fopen(filename,"r");
  676. }
  677. if((f_text == NULL) && (p_text == NULL))
  678. {
  679. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  680. exit(1);
  681. }
  682. if(option_waveout || quiet)
  683. {
  684. if(quiet)
  685. {
  686. // no sound output
  687. OpenWaveFile(NULL,samplerate);
  688. option_waveout = 1;
  689. }
  690. else
  691. {
  692. // write sound output to a WAV file
  693. samples_split = (samplerate * samples_split) * 60;
  694. if(samples_split)
  695. {
  696. // don't open the wav file until we start generating speech
  697. char *extn;
  698. extn = strrchr(wavefile,'.');
  699. if((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4))
  700. {
  701. strcpy(filetype,extn);
  702. *extn = 0;
  703. }
  704. }
  705. else
  706. if(OpenWaveFile(wavefile,samplerate) != 0)
  707. {
  708. fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
  709. exit(3);
  710. }
  711. }
  712. InitText(0);
  713. SpeakNextClause(f_text,p_text,0);
  714. ix = 1;
  715. for(;;)
  716. {
  717. if(WavegenFile() != 0)
  718. {
  719. if(ix == 0)
  720. break; // finished, wavegen command queue is empty
  721. }
  722. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  723. {
  724. ix = SpeakNextClause(NULL,NULL,1);
  725. }
  726. }
  727. CloseWaveFile();
  728. }
  729. else
  730. {
  731. // Silence on ^C or SIGINT
  732. // signal(SIGINT,StopSpeak);
  733. // output sound using portaudio
  734. WavegenInitSound();
  735. InitText(0);
  736. SpeakNextClause(f_text,p_text,0);
  737. if(option_quiet)
  738. {
  739. while(SpeakNextClause(NULL,NULL,1) != 0);
  740. return(0);
  741. }
  742. #ifdef USE_PORTAUDIO
  743. speaking = 1;
  744. while(speaking)
  745. {
  746. // NOTE: if nanosleep() isn't recognised on your system, try replacing
  747. // this by sleep(1);
  748. #ifdef PLATFORM_WINDOWS
  749. Sleep(300); // 0.3s
  750. #else
  751. #ifdef USE_NANOSLEEP
  752. struct timespec period;
  753. struct timespec remaining;
  754. period.tv_sec = 0;
  755. period.tv_nsec = 300000000; // 0.3 sec
  756. nanosleep(&period,&remaining);
  757. #else
  758. sleep(1);
  759. #endif
  760. #endif
  761. if(SynthOnTimer() != 0)
  762. speaking = 0;
  763. }
  764. #else
  765. fprintf(stderr,"-w option must be used because the program was built without a sound interface\n");
  766. #endif // USE_PORTAUDIO
  767. }
  768. if((f_trans != stdout) && (f_trans != stderr))
  769. fclose(f_trans); // needed for WinCe
  770. return(0);
  771. }