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

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