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.

espeak.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /***************************************************************************
  2. * Copyright (C) 2006 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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <getopt.h>
  23. #include <time.h>
  24. #include <sys/stat.h>
  25. #include "speak_lib.h"
  26. // This version of the command-line speak program uses the
  27. // libespeak.so.1 library
  28. static const char *help_text =
  29. "\nspeak [options] [\"<words>\"]\n\n"
  30. "-f <text file> Text file to speak\n"
  31. "--stdin Read text input from stdin instead of a file\n\n"
  32. "If neither -f nor --stdin, <words> are spoken, or if none then text is\n"
  33. "spoken from stdin, each line separately.\n\n"
  34. "-a <integer>\n"
  35. "\t Amplitude, 0 to 200, default is 100\n"
  36. "-g <integer>\n"
  37. "\t Word gap. Pause between words, units of 10mS at the default speed\n"
  38. "-l <integer>\n"
  39. "\t Line length. If not zero (which is the default), consider\n"
  40. "\t lines less than this length as end-of-clause\n"
  41. "-p <integer>\n"
  42. "\t Pitch adjustment, 0 to 99, default is 50\n"
  43. "-s <integer>\n"
  44. "\t Speed in words per minute, 80 to 370, default is 170\n"
  45. "-v <voice name>\n"
  46. "\t Use voice file of this name from espeak-data/voices\n"
  47. "-w <wave file name>\n"
  48. "\t Write output to this WAV file, rather than speaking it directly\n"
  49. "-b\t Input text is 8-bit encoding\n"
  50. "-m\t Interpret SSML markup, and ignore other < > tags\n"
  51. "-q\t Quiet, don't produce any speech (may be useful with -x)\n"
  52. "-x\t Write phoneme mnemonics to stdout\n"
  53. "-X\t Write phonemes mnemonics and translation trace to stdout\n"
  54. "-z\t No final sentence pause at the end of the text\n"
  55. "--stdout Write speech output to stdout\n"
  56. "--compile=<voice name>\n"
  57. "\t Compile the pronunciation rules and dictionary in the current\n"
  58. "\t directory. =<voice name> is optional and specifies which language\n"
  59. "--punct=\"<characters>\"\n"
  60. "\t Speak the names of punctuation characters during speaking. If\n"
  61. "\t =<characters> is omitted, all punctuation is spoken.\n"
  62. "--voices=<langauge>\n"
  63. "\t List the available voices for the specified language.\n"
  64. "\t If <language> is omitted, then list all voices.\n"
  65. "-k <integer>\n"
  66. "\t Indicate capital letters with: 1=sound, 2=the word \"capitals\",\n"
  67. "\t higher values = a pitch increase (try -k20).\n";
  68. int samplerate;
  69. FILE *f_wavfile = NULL;
  70. char wavefile[160];
  71. int GetFileLength(const char *filename)
  72. {//====================================
  73. struct stat statbuf;
  74. if(stat(filename,&statbuf) != 0)
  75. return(0);
  76. if((statbuf.st_mode & S_IFMT) == S_IFDIR)
  77. return(-2); // a directory
  78. return(statbuf.st_size);
  79. } // end of GetFileLength
  80. void strncpy0(char *dest, const char *source, int size)
  81. {//====================================================
  82. if(source!=NULL)
  83. {
  84. strncpy(dest,source,size);
  85. dest[size-1] = 0;
  86. }
  87. }
  88. void DisplayVoices(FILE *f_out, char *language)
  89. {//============================================
  90. int ix;
  91. const char *p;
  92. int len;
  93. int count;
  94. int scores = 0;
  95. const espeak_VOICE *v;
  96. const char *lang_name;
  97. char age_buf[12];
  98. const espeak_VOICE **voices;
  99. espeak_VOICE voice_select;
  100. static char genders[4] = {' ','M','F',' '};
  101. if((language != NULL) && (language[0] != 0))
  102. {
  103. // display only voices for the specified language, in order of priority
  104. voice_select.languages = language;
  105. voice_select.age = 0;
  106. voice_select.gender = 0;
  107. voice_select.name = NULL;
  108. voices = espeak_ListVoices(&voice_select);
  109. scores = 1;
  110. }
  111. else
  112. {
  113. voices = espeak_ListVoices(NULL);
  114. }
  115. fprintf(f_out,"Pty Language Age/Gender VoiceName File Other Langs\n");
  116. for(ix=0; (v = voices[ix]) != NULL; ix++)
  117. {
  118. count = 0;
  119. p = v->languages;
  120. while(*p != 0)
  121. {
  122. len = strlen(p+1);
  123. lang_name = p+1;
  124. if(v->age == 0)
  125. strcpy(age_buf," ");
  126. else
  127. sprintf(age_buf,"%3d",v->age);
  128. if(count==0)
  129. {
  130. fprintf(f_out,"%2d %-12s%s%c %-17s %-11s ",
  131. p[0],lang_name,age_buf,genders[v->gender],v->name,v->identifier);
  132. }
  133. else
  134. {
  135. fprintf(f_out,"(%s %d)",lang_name,p[0]);
  136. }
  137. count++;
  138. p += len+2;
  139. }
  140. // if(scores)
  141. // fprintf(f_out,"%3d ",v->score);
  142. fputc('\n',f_out);
  143. }
  144. } // end of DisplayVoices
  145. static void Write4Bytes(FILE *f, int value)
  146. {//=================================
  147. // Write 4 bytes to a file, least significant first
  148. int ix;
  149. for(ix=0; ix<4; ix++)
  150. {
  151. fputc(value & 0xff,f);
  152. value = value >> 8;
  153. }
  154. }
  155. int OpenWavFile(char *path, int rate)
  156. //===================================
  157. {
  158. static unsigned char wave_hdr[44] = {
  159. 'R','I','F','F',0,0,0,0,'W','A','V','E','f','m','t',' ',
  160. 0x10,0,0,0,1,0,1,0, 0,0,0,0, 0,0,0,0,
  161. 2,0,0x10,0,'d','a','t','a', 0,0,0,0 };
  162. if(path == NULL)
  163. return(2);
  164. if(path[0] == 0)
  165. return(0);
  166. if(strcmp(path,"stdout")==0)
  167. f_wavfile = stdout;
  168. else
  169. f_wavfile = fopen(path,"wb");
  170. if(f_wavfile != NULL)
  171. {
  172. fwrite(wave_hdr,1,24,f_wavfile);
  173. Write4Bytes(f_wavfile,rate);
  174. Write4Bytes(f_wavfile,rate * 2);
  175. fwrite(&wave_hdr[32],1,8,f_wavfile);
  176. return(0);
  177. }
  178. return(1);
  179. } // end of OpenWavFile
  180. static void CloseWavFile(int rate)
  181. //================================
  182. {
  183. unsigned int pos;
  184. if((f_wavfile==NULL) || (f_wavfile == stdout))
  185. return;
  186. fflush(f_wavfile);
  187. pos = ftell(f_wavfile);
  188. fseek(f_wavfile,4,SEEK_SET);
  189. Write4Bytes(f_wavfile,pos - 8);
  190. fseek(f_wavfile,40,SEEK_SET);
  191. Write4Bytes(f_wavfile,pos - 44);
  192. fclose(f_wavfile);
  193. f_wavfile = NULL;
  194. } // end of CloseWavFile
  195. static int SynthCallback(short *wav, int numsamples, espeak_EVENT *events)
  196. {//========================================================================
  197. if(f_wavfile == NULL) return(0); // -q quiet mode
  198. if(wav == NULL)
  199. {
  200. CloseWavFile(samplerate);
  201. return(0);
  202. }
  203. if(numsamples > 0)
  204. {
  205. fwrite(wav,numsamples*2,1,f_wavfile);
  206. }
  207. return(0);
  208. }
  209. int main (int argc, char **argv)
  210. //==============================
  211. {
  212. static struct option long_options[] =
  213. {
  214. /* These options set a flag. */
  215. // {"verbose", no_argument, &verbose_flag, 1},
  216. // {"brief", no_argument, &verbose_flag, 0},
  217. /* These options don't set a flag.
  218. We distinguish them by their indices. */
  219. {"help", no_argument, 0, 'h'},
  220. {"stdin", no_argument, 0, 0x100},
  221. {"compile-debug", optional_argument, 0, 0x101},
  222. {"compile", optional_argument, 0, 0x102},
  223. {"punct", optional_argument, 0, 0x103},
  224. {"voices", optional_argument, 0, 0x104},
  225. {"stdout", no_argument, 0, 0x105},
  226. {0, 0, 0, 0}
  227. };
  228. static const char* err_load = "Failed to read ";
  229. FILE *f_text=NULL;
  230. char *p_text=NULL;
  231. int option_index = 0;
  232. int c;
  233. int ix;
  234. int quiet = 0;
  235. int flag_stdin = 0;
  236. int flag_compile = 0;
  237. int filesize = 0;
  238. int synth_flags = espeakCHARS_AUTO | espeakPHONEMES | espeakENDPAUSE;
  239. int volume = -1;
  240. int speed = -1;
  241. int pitch = -1;
  242. int wordgap = -1;
  243. int option_capitals = -1;
  244. int option_punctuation = -1;
  245. int option_phonemes = -1;
  246. int option_linelength = 0;
  247. int option_waveout = 0;
  248. char filename[120];
  249. char voicename[40];
  250. char voice_mbrola[20];
  251. char dictname[40];
  252. #define N_PUNCTLIST 100
  253. wchar_t option_punctlist[N_PUNCTLIST];
  254. voicename[0] = 0;
  255. voice_mbrola[0] = 0;
  256. dictname[0] = 0;
  257. wavefile[0] = 0;
  258. filename[0] = 0;
  259. option_punctlist[0] = 0;
  260. while(true)
  261. {
  262. c = getopt_long (argc, argv, "a:bf:g:hk:l:mp:qs:v:w:xXz",
  263. long_options, &option_index);
  264. /* Detect the end of the options. */
  265. if (c == -1)
  266. break;
  267. switch (c)
  268. {
  269. case 'b':
  270. synth_flags |= espeakCHARS_8BIT;
  271. break;
  272. case 'h':
  273. printf("\n");
  274. printf("eSpeak text-to-speech: %s\n%s",espeak_Info(NULL),help_text);
  275. exit(0);
  276. break;
  277. case 'k':
  278. option_capitals = atoi(optarg);
  279. break;
  280. case 'x':
  281. option_phonemes = 1;
  282. break;
  283. case 'X':
  284. option_phonemes = 2;
  285. break;
  286. case 'm':
  287. synth_flags |= espeakSSML;
  288. break;
  289. case 'p':
  290. pitch = atoi(optarg);
  291. break;
  292. case 'q':
  293. quiet = 1;
  294. break;
  295. case 'f':
  296. strncpy0(filename,optarg,sizeof(filename));
  297. break;
  298. case 'l':
  299. option_linelength = atoi(optarg);
  300. break;
  301. case 'a':
  302. volume = atoi(optarg);
  303. break;
  304. case 's':
  305. speed = atoi(optarg);
  306. break;
  307. case 'g':
  308. wordgap = atoi(optarg);
  309. break;
  310. case 'v':
  311. strncpy0(voicename,optarg,sizeof(voicename));
  312. break;
  313. case 'w':
  314. option_waveout = 1;
  315. strncpy0(wavefile,optarg,sizeof(filename));
  316. break;
  317. case 'z': // remove pause from the end of a sentence
  318. synth_flags &= ~espeakENDPAUSE;
  319. break;
  320. case 0x100: // --stdin
  321. flag_stdin = 1;
  322. break;
  323. case 0x105: // --stdout
  324. option_waveout = 1;
  325. strcpy(wavefile,"stdout");
  326. break;
  327. case 0x101: // --compile-debug
  328. case 0x102: // --compile
  329. strncpy0(voicename,optarg,sizeof(voicename));
  330. flag_compile = c;
  331. quiet = 1;
  332. break;
  333. case 0x103: // --punct
  334. option_punctuation = 1;
  335. if(optarg != NULL)
  336. {
  337. ix = 0;
  338. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg[ix]) != 0)) ix++;
  339. option_punctlist[N_PUNCTLIST-1] = 0;
  340. option_punctuation = 2;
  341. }
  342. break;
  343. case 0x104: // --voices
  344. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS,0,NULL,0);
  345. DisplayVoices(stdout,optarg);
  346. exit(0);
  347. default:
  348. exit(0);
  349. }
  350. }
  351. if(option_waveout || quiet)
  352. {
  353. // writing to a file (or no output), we can use synchronous mode
  354. samplerate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS,0,NULL,0);
  355. espeak_SetSynthCallback(SynthCallback);
  356. if(option_waveout)
  357. {
  358. if(OpenWavFile(wavefile,samplerate) != 0)
  359. exit(4);
  360. }
  361. }
  362. else
  363. {
  364. // play the sound output
  365. samplerate = espeak_Initialize(AUDIO_OUTPUT_PLAYBACK,0,NULL,0);
  366. }
  367. if(voicename[0] == 0)
  368. strcpy(voicename,"default");
  369. if(espeak_SetVoiceByName(voicename) != EE_OK)
  370. {
  371. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  372. exit(2);
  373. }
  374. if(flag_compile)
  375. {
  376. // This must be done after the voice is set
  377. espeak_CompileDictionary("", stderr, flag_compile & 0x1);
  378. exit(0);
  379. }
  380. // set any non-default values of parameters. This must be done after espeak_Initialize()
  381. if(speed > 0)
  382. espeak_SetParameter(espeakRATE,speed,0);
  383. if(volume >= 0)
  384. espeak_SetParameter(espeakVOLUME,volume,0);
  385. if(pitch >= 0)
  386. espeak_SetParameter(espeakPITCH,pitch,0);
  387. if(option_capitals >= 0)
  388. espeak_SetParameter(espeakCAPITALS,option_capitals,0);
  389. if(option_punctuation >= 0)
  390. espeak_SetParameter(espeakPUNCTUATION,option_punctuation,0);
  391. if(wordgap >= 0)
  392. espeak_SetParameter(espeakWORDGAP,wordgap,0);
  393. if(option_linelength > 0)
  394. espeak_SetParameter(espeakLINELENGTH,option_linelength,0);
  395. if(option_punctuation == 2)
  396. espeak_SetPunctuationList(option_punctlist);
  397. if(option_phonemes >= 0)
  398. espeak_SetPhonemeTrace(option_phonemes,stderr);
  399. if(filename[0]==0)
  400. {
  401. if((optind < argc) && (flag_stdin == 0))
  402. {
  403. // there's a non-option parameter, and no -f or --stdin
  404. // use it as text
  405. p_text = argv[optind];
  406. }
  407. else
  408. {
  409. f_text = stdin;
  410. if(flag_stdin == 0)
  411. {
  412. flag_stdin = 2;
  413. }
  414. }
  415. }
  416. else
  417. {
  418. filesize = GetFileLength(filename);
  419. f_text = fopen(filename,"r");
  420. }
  421. if((f_text == NULL) && (p_text == NULL))
  422. {
  423. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  424. exit(1);
  425. }
  426. if(p_text != NULL)
  427. {
  428. int size;
  429. size = strlen(p_text);
  430. espeak_Synth(p_text,size+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  431. }
  432. else
  433. if(flag_stdin)
  434. {
  435. int max = 1000;
  436. p_text = (char *)malloc(max);
  437. if(flag_stdin == 2)
  438. {
  439. // line by line input on stdin
  440. while(fgets(p_text,max,stdin) != NULL)
  441. {
  442. p_text[max-1] = 0;
  443. espeak_Synth(p_text,max,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  444. }
  445. }
  446. else
  447. {
  448. // bulk input on stdin
  449. ix = 0;
  450. while(!feof(stdin))
  451. {
  452. p_text[ix++] = fgetc(stdin);
  453. if(ix >= (max-1))
  454. {
  455. max += 1000;
  456. p_text = (char *)realloc(p_text,max);
  457. }
  458. }
  459. if(ix > 0)
  460. {
  461. p_text[ix-1] = 0;
  462. espeak_Synth(p_text,ix+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  463. }
  464. }
  465. }
  466. else
  467. if(f_text != NULL)
  468. {
  469. if((p_text = (char *)malloc(filesize+1)) == NULL)
  470. {
  471. fprintf(stderr,"Failed to allocate memory %d bytes",filesize);
  472. exit(3);
  473. }
  474. fread(p_text,1,filesize,f_text);
  475. p_text[filesize]=0;
  476. espeak_Synth(p_text,filesize+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  477. fclose(f_text);
  478. }
  479. espeak_Synchronize();
  480. return(0);
  481. }