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

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