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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. "--split=\"<minutes>\"\n"
  63. "\t Starts a new WAV file every <minutes>. Used with -w\n"
  64. "--voices=<langauge>\n"
  65. "\t List the available voices for the specified language.\n"
  66. "\t If <language> is omitted, then list all voices.\n"
  67. "-k <integer>\n"
  68. "\t Indicate capital letters with: 1=sound, 2=the word \"capitals\",\n"
  69. "\t higher values = a pitch increase (try -k20).\n";
  70. int samplerate;
  71. int quiet = 0;
  72. unsigned int samples_total = 0;
  73. unsigned int samples_split = 0;
  74. unsigned int wavefile_count = 0;
  75. FILE *f_wavfile = NULL;
  76. char filetype[5];
  77. char wavefile[200];
  78. int GetFileLength(const char *filename)
  79. {//====================================
  80. struct stat statbuf;
  81. if(stat(filename,&statbuf) != 0)
  82. return(0);
  83. if((statbuf.st_mode & S_IFMT) == S_IFDIR)
  84. return(-2); // a directory
  85. return(statbuf.st_size);
  86. } // end of GetFileLength
  87. void strncpy0(char *dest, const char *source, int size)
  88. {//====================================================
  89. if(source!=NULL)
  90. {
  91. strncpy(dest,source,size);
  92. dest[size-1] = 0;
  93. }
  94. }
  95. void DisplayVoices(FILE *f_out, char *language)
  96. {//============================================
  97. int ix;
  98. const char *p;
  99. int len;
  100. int count;
  101. int scores = 0;
  102. const espeak_VOICE *v;
  103. const char *lang_name;
  104. char age_buf[12];
  105. const espeak_VOICE **voices;
  106. espeak_VOICE voice_select;
  107. static char genders[4] = {' ','M','F',' '};
  108. if((language != NULL) && (language[0] != 0))
  109. {
  110. // display only voices for the specified language, in order of priority
  111. voice_select.languages = language;
  112. voice_select.age = 0;
  113. voice_select.gender = 0;
  114. voice_select.name = NULL;
  115. voices = espeak_ListVoices(&voice_select);
  116. scores = 1;
  117. }
  118. else
  119. {
  120. voices = espeak_ListVoices(NULL);
  121. }
  122. fprintf(f_out,"Pty Language Age/Gender VoiceName File Other Langs\n");
  123. for(ix=0; (v = voices[ix]) != NULL; ix++)
  124. {
  125. count = 0;
  126. p = v->languages;
  127. while(*p != 0)
  128. {
  129. len = strlen(p+1);
  130. lang_name = p+1;
  131. if(v->age == 0)
  132. strcpy(age_buf," ");
  133. else
  134. sprintf(age_buf,"%3d",v->age);
  135. if(count==0)
  136. {
  137. fprintf(f_out,"%2d %-12s%s%c %-17s %-11s ",
  138. p[0],lang_name,age_buf,genders[v->gender],v->name,v->identifier);
  139. }
  140. else
  141. {
  142. fprintf(f_out,"(%s %d)",lang_name,p[0]);
  143. }
  144. count++;
  145. p += len+2;
  146. }
  147. // if(scores)
  148. // fprintf(f_out,"%3d ",v->score);
  149. fputc('\n',f_out);
  150. }
  151. } // end of DisplayVoices
  152. static void Write4Bytes(FILE *f, int value)
  153. {//=================================
  154. // Write 4 bytes to a file, least significant first
  155. int ix;
  156. for(ix=0; ix<4; ix++)
  157. {
  158. fputc(value & 0xff,f);
  159. value = value >> 8;
  160. }
  161. }
  162. int OpenWavFile(char *path, int rate)
  163. //===================================
  164. {
  165. static unsigned char wave_hdr[44] = {
  166. 'R','I','F','F',0x24,0xf0,0xff,0x7f,'W','A','V','E','f','m','t',' ',
  167. 0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
  168. 2,0,0x10,0,'d','a','t','a', 0x00,0xf0,0xff,0x7f};
  169. if(path == NULL)
  170. return(2);
  171. if(path[0] == 0)
  172. return(0);
  173. if(strcmp(path,"stdout")==0)
  174. f_wavfile = stdout;
  175. else
  176. f_wavfile = fopen(path,"wb");
  177. if(f_wavfile != NULL)
  178. {
  179. fwrite(wave_hdr,1,24,f_wavfile);
  180. Write4Bytes(f_wavfile,rate);
  181. Write4Bytes(f_wavfile,rate * 2);
  182. fwrite(&wave_hdr[32],1,12,f_wavfile);
  183. return(0);
  184. }
  185. return(1);
  186. } // end of OpenWavFile
  187. static void CloseWavFile()
  188. //========================
  189. {
  190. unsigned int pos;
  191. if((f_wavfile==NULL) || (f_wavfile == stdout))
  192. return;
  193. fflush(f_wavfile);
  194. pos = ftell(f_wavfile);
  195. fseek(f_wavfile,4,SEEK_SET);
  196. Write4Bytes(f_wavfile,pos - 8);
  197. fseek(f_wavfile,40,SEEK_SET);
  198. Write4Bytes(f_wavfile,pos - 44);
  199. fclose(f_wavfile);
  200. f_wavfile = NULL;
  201. } // end of CloseWavFile
  202. static int SynthCallback(short *wav, int numsamples, espeak_EVENT *events)
  203. {//========================================================================
  204. char fname[210];
  205. if(quiet) return(0); // -q quiet mode
  206. if(wav == NULL)
  207. {
  208. CloseWavFile();
  209. return(0);
  210. }
  211. if(samples_split > 0)
  212. {
  213. // start a new WAV file when this limit is reached, at the next sentence boundary
  214. while(events->type != 0)
  215. {
  216. if((events->type == espeakEVENT_SENTENCE) && (samples_total > samples_split))
  217. {
  218. CloseWavFile();
  219. samples_total = 0;
  220. }
  221. events++;
  222. }
  223. }
  224. if(f_wavfile == NULL)
  225. {
  226. sprintf(fname,"%s_%.2d%s",wavefile,++wavefile_count,filetype);
  227. if(OpenWavFile(fname, samplerate) != 0)
  228. return(1);
  229. }
  230. if(numsamples > 0)
  231. {
  232. samples_total += numsamples;
  233. fwrite(wav,numsamples*2,1,f_wavfile);
  234. }
  235. return(0);
  236. }
  237. int main (int argc, char **argv)
  238. //==============================
  239. {
  240. static struct option long_options[] =
  241. {
  242. /* These options set a flag. */
  243. // {"verbose", no_argument, &verbose_flag, 1},
  244. // {"brief", no_argument, &verbose_flag, 0},
  245. /* These options don't set a flag.
  246. We distinguish them by their indices. */
  247. {"help", no_argument, 0, 'h'},
  248. {"stdin", no_argument, 0, 0x100},
  249. {"compile-debug", optional_argument, 0, 0x101},
  250. {"compile", optional_argument, 0, 0x102},
  251. {"punct", optional_argument, 0, 0x103},
  252. {"voices", optional_argument, 0, 0x104},
  253. {"stdout", no_argument, 0, 0x105},
  254. {"split", optional_argument, 0, 0x106},
  255. {0, 0, 0, 0}
  256. };
  257. static const char* err_load = "Failed to read ";
  258. FILE *f_text=NULL;
  259. char *p_text=NULL;
  260. int option_index = 0;
  261. int c;
  262. int ix;
  263. int flag_stdin = 0;
  264. int flag_compile = 0;
  265. int filesize = 0;
  266. int synth_flags = espeakCHARS_AUTO | espeakPHONEMES | espeakENDPAUSE;
  267. int volume = -1;
  268. int speed = -1;
  269. int pitch = -1;
  270. int wordgap = -1;
  271. int option_capitals = -1;
  272. int option_punctuation = -1;
  273. int option_phonemes = -1;
  274. int option_linelength = 0;
  275. int option_waveout = 0;
  276. char filename[120];
  277. char voicename[40];
  278. char voice_mbrola[20];
  279. char dictname[40];
  280. #define N_PUNCTLIST 100
  281. wchar_t option_punctlist[N_PUNCTLIST];
  282. voicename[0] = 0;
  283. voice_mbrola[0] = 0;
  284. dictname[0] = 0;
  285. wavefile[0] = 0;
  286. filename[0] = 0;
  287. option_punctlist[0] = 0;
  288. while(true)
  289. {
  290. c = getopt_long (argc, argv, "a:bf:g:hk:l:mp:qs:v:w:xXz",
  291. long_options, &option_index);
  292. /* Detect the end of the options. */
  293. if (c == -1)
  294. break;
  295. switch (c)
  296. {
  297. case 'b':
  298. synth_flags |= espeakCHARS_8BIT;
  299. break;
  300. case 'h':
  301. printf("\n");
  302. printf("eSpeak text-to-speech: %s\n%s",espeak_Info(NULL),help_text);
  303. exit(0);
  304. break;
  305. case 'k':
  306. option_capitals = atoi(optarg);
  307. break;
  308. case 'x':
  309. option_phonemes = 1;
  310. break;
  311. case 'X':
  312. option_phonemes = 2;
  313. break;
  314. case 'm':
  315. synth_flags |= espeakSSML;
  316. break;
  317. case 'p':
  318. pitch = atoi(optarg);
  319. break;
  320. case 'q':
  321. quiet = 1;
  322. break;
  323. case 'f':
  324. strncpy0(filename,optarg,sizeof(filename));
  325. break;
  326. case 'l':
  327. option_linelength = atoi(optarg);
  328. break;
  329. case 'a':
  330. volume = atoi(optarg);
  331. break;
  332. case 's':
  333. speed = atoi(optarg);
  334. break;
  335. case 'g':
  336. wordgap = atoi(optarg);
  337. break;
  338. case 'v':
  339. strncpy0(voicename,optarg,sizeof(voicename));
  340. break;
  341. case 'w':
  342. option_waveout = 1;
  343. strncpy0(wavefile,optarg,sizeof(filename));
  344. break;
  345. case 'z': // remove pause from the end of a sentence
  346. synth_flags &= ~espeakENDPAUSE;
  347. break;
  348. case 0x100: // --stdin
  349. flag_stdin = 1;
  350. break;
  351. case 0x105: // --stdout
  352. option_waveout = 1;
  353. strcpy(wavefile,"stdout");
  354. break;
  355. case 0x101: // --compile-debug
  356. case 0x102: // --compile
  357. strncpy0(voicename,optarg,sizeof(voicename));
  358. flag_compile = c;
  359. quiet = 1;
  360. break;
  361. case 0x103: // --punct
  362. option_punctuation = 1;
  363. if(optarg != NULL)
  364. {
  365. ix = 0;
  366. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg[ix]) != 0)) ix++;
  367. option_punctlist[N_PUNCTLIST-1] = 0;
  368. option_punctuation = 2;
  369. }
  370. break;
  371. case 0x104: // --voices
  372. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS,0,NULL,0);
  373. DisplayVoices(stdout,optarg);
  374. exit(0);
  375. case 0x106: // -- split
  376. if(optarg == NULL)
  377. samples_split = 30; // default 30 minutes
  378. else
  379. samples_split = atoi(optarg);
  380. break;
  381. default:
  382. exit(0);
  383. }
  384. }
  385. if(option_waveout || quiet)
  386. {
  387. // writing to a file (or no output), we can use synchronous mode
  388. samplerate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS,0,NULL,0);
  389. samples_split = (samplerate * samples_split) * 60;
  390. espeak_SetSynthCallback(SynthCallback);
  391. if(samples_split)
  392. {
  393. char *extn;
  394. extn = strrchr(wavefile,'.');
  395. if((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4))
  396. {
  397. strcpy(filetype,extn);
  398. *extn = 0;
  399. }
  400. }
  401. else
  402. if(option_waveout)
  403. {
  404. if(OpenWavFile(wavefile,samplerate) != 0)
  405. exit(4);
  406. }
  407. }
  408. else
  409. {
  410. // play the sound output
  411. samplerate = espeak_Initialize(AUDIO_OUTPUT_PLAYBACK,0,NULL,0);
  412. }
  413. if(voicename[0] == 0)
  414. strcpy(voicename,"default");
  415. if(espeak_SetVoiceByName(voicename) != EE_OK)
  416. {
  417. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  418. exit(2);
  419. }
  420. if(flag_compile)
  421. {
  422. // This must be done after the voice is set
  423. espeak_CompileDictionary("", stderr, flag_compile & 0x1);
  424. exit(0);
  425. }
  426. // set any non-default values of parameters. This must be done after espeak_Initialize()
  427. if(speed > 0)
  428. espeak_SetParameter(espeakRATE,speed,0);
  429. if(volume >= 0)
  430. espeak_SetParameter(espeakVOLUME,volume,0);
  431. if(pitch >= 0)
  432. espeak_SetParameter(espeakPITCH,pitch,0);
  433. if(option_capitals >= 0)
  434. espeak_SetParameter(espeakCAPITALS,option_capitals,0);
  435. if(option_punctuation >= 0)
  436. espeak_SetParameter(espeakPUNCTUATION,option_punctuation,0);
  437. if(wordgap >= 0)
  438. espeak_SetParameter(espeakWORDGAP,wordgap,0);
  439. if(option_linelength > 0)
  440. espeak_SetParameter(espeakLINELENGTH,option_linelength,0);
  441. if(option_punctuation == 2)
  442. espeak_SetPunctuationList(option_punctlist);
  443. if(option_phonemes >= 0)
  444. espeak_SetPhonemeTrace(option_phonemes,stderr);
  445. if(filename[0]==0)
  446. {
  447. if((optind < argc) && (flag_stdin == 0))
  448. {
  449. // there's a non-option parameter, and no -f or --stdin
  450. // use it as text
  451. p_text = argv[optind];
  452. }
  453. else
  454. {
  455. f_text = stdin;
  456. if(flag_stdin == 0)
  457. {
  458. flag_stdin = 2;
  459. }
  460. }
  461. }
  462. else
  463. {
  464. filesize = GetFileLength(filename);
  465. f_text = fopen(filename,"r");
  466. }
  467. if((f_text == NULL) && (p_text == NULL))
  468. {
  469. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  470. exit(1);
  471. }
  472. if(p_text != NULL)
  473. {
  474. int size;
  475. size = strlen(p_text);
  476. espeak_Synth(p_text,size+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  477. }
  478. else
  479. if(flag_stdin)
  480. {
  481. int max = 1000;
  482. p_text = (char *)malloc(max);
  483. if(flag_stdin == 2)
  484. {
  485. // line by line input on stdin
  486. while(fgets(p_text,max,stdin) != NULL)
  487. {
  488. p_text[max-1] = 0;
  489. espeak_Synth(p_text,max,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  490. }
  491. }
  492. else
  493. {
  494. // bulk input on stdin
  495. ix = 0;
  496. while(!feof(stdin))
  497. {
  498. p_text[ix++] = fgetc(stdin);
  499. if(ix >= (max-1))
  500. {
  501. max += 1000;
  502. p_text = (char *)realloc(p_text,max);
  503. }
  504. }
  505. if(ix > 0)
  506. {
  507. p_text[ix-1] = 0;
  508. espeak_Synth(p_text,ix+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  509. }
  510. }
  511. }
  512. else
  513. if(f_text != NULL)
  514. {
  515. if((p_text = (char *)malloc(filesize+1)) == NULL)
  516. {
  517. fprintf(stderr,"Failed to allocate memory %d bytes",filesize);
  518. exit(3);
  519. }
  520. fread(p_text,1,filesize,f_text);
  521. p_text[filesize]=0;
  522. espeak_Synth(p_text,filesize+1,0,POS_CHARACTER,0,synth_flags,NULL,NULL);
  523. fclose(f_text);
  524. }
  525. espeak_Synchronize();
  526. return(0);
  527. }