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

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