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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /***************************************************************************
  2. * Copyright (C) 2005 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 "StdAfx.h"
  20. #include "speech.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifndef PLATFORM_DOS
  25. #ifdef PLATFORM_WINDOWS
  26. #include <windows.h>
  27. #include <winreg.h>
  28. #else
  29. #include <unistd.h>
  30. #endif
  31. #endif
  32. #ifndef NEED_GETOPT
  33. #include <getopt.h>
  34. #endif
  35. #include <time.h>
  36. #include <signal.h>
  37. #include <locale.h>
  38. #include <sys/stat.h>
  39. #include "speak_lib.h"
  40. #include "phoneme.h"
  41. #include "synthesize.h"
  42. #include "voice.h"
  43. #include "translate.h"
  44. char path_home[N_PATH_HOME]; // this is the espeak-data directory
  45. char wavefile[120];
  46. int (* uri_callback)(int, const char *, const char *) = NULL;
  47. int (* phoneme_callback)(const char *) = NULL;
  48. static const char *help_text =
  49. "\nspeak [options] [\"<words>\"]\n\n"
  50. "-f <text file> Text file to speak\n"
  51. "--stdin Read text input from stdin instead of a file\n\n"
  52. "If neither -f nor --stdin, <words> are spoken, or if none then text is\n"
  53. "spoken from stdin, each line separately.\n\n"
  54. "-a <integer>\n"
  55. "\t Amplitude, 0 to 200, default is 100\n"
  56. "-g <integer>\n"
  57. "\t Word gap. Pause between words, units of 10mS at the default speed\n"
  58. "-l <integer>\n"
  59. "\t Line length. If not zero (which is the default), consider\n"
  60. "\t lines less than this length as end-of-clause\n"
  61. "-p <integer>\n"
  62. "\t Pitch adjustment, 0 to 99, default is 50\n"
  63. "-s <integer>\n"
  64. "\t Speed in words per minute 80 to 370, default is 170\n"
  65. "-v <voice name>\n"
  66. "\t Use voice file of this name from espeak-data/voices\n"
  67. "-w <wave file name>\n"
  68. "\t Write output to this WAV file, rather than speaking it directly\n"
  69. "-b\t Input text is 8-bit encoding\n"
  70. "-m\t Interpret SSML markup, and ignore other < > tags\n"
  71. "-q\t Quiet, don't produce any speech (may be useful with -x)\n"
  72. "-x\t Write phoneme mnemonics to stdout\n"
  73. "-X\t Write phonemes mnemonics and translation trace to stdout\n"
  74. "--stdout Write speech output to stdout\n"
  75. "--compile=<voice name>\n"
  76. "\t Compile the pronunciation rules and dictionary in the current\n"
  77. "\t directory. =<voice name> is optional and specifies which language\n"
  78. "--punct=\"<characters>\"\n"
  79. "\t Speak the names of punctuation characters during speaking. If\n"
  80. "\t =<characters> is omitted, all punctuation is spoken.\n"
  81. "--voices=<langauge>\n"
  82. "\t List the available voices for the specified language.\n"
  83. "\t If <language> is omitted, then list all voices.\n"
  84. "-k <integer>\n"
  85. "\t Indicate capital letters with: 1=sound, 2=the word \"capitals\",\n"
  86. "\t higher values = a pitch increase (try -k20).\n";
  87. void DisplayVoices(FILE *f_out, char *language);
  88. USHORT voice_pcnt[N_PEAKS+1][3];
  89. int GetFileLength(const char *filename)
  90. {//====================================
  91. struct stat statbuf;
  92. if(stat(filename,&statbuf) != 0)
  93. return(0);
  94. if((statbuf.st_mode & S_IFMT) == S_IFDIR)
  95. // if(S_ISDIR(statbuf.st_mode))
  96. return(-2); // a directory
  97. return(statbuf.st_size);
  98. } // end of GetFileLength
  99. char *Alloc(int size)
  100. {//==================
  101. char *p;
  102. if((p = (char *)malloc(size)) == NULL)
  103. fprintf(stderr,"Can't allocate memory\n");
  104. return(p);
  105. }
  106. void Free(void *ptr)
  107. {//=================
  108. if(ptr != NULL)
  109. free(ptr);
  110. }
  111. void DisplayVoices(FILE *f_out, char *language)
  112. {//============================================
  113. int ix;
  114. const char *p;
  115. int len;
  116. int count;
  117. int scores = 0;
  118. const espeak_VOICE *v;
  119. const char *lang_name;
  120. char age_buf[12];
  121. const espeak_VOICE **voices;
  122. espeak_VOICE voice_select;
  123. static char genders[4] = {' ','M','F',' '};
  124. if((language != NULL) && (language[0] != 0))
  125. {
  126. // display only voices for the specified language, in order of priority
  127. voice_select.languages = language;
  128. voice_select.age = 0;
  129. voice_select.gender = 0;
  130. voice_select.name = NULL;
  131. voices = espeak_ListVoices(&voice_select);
  132. scores = 1;
  133. }
  134. else
  135. {
  136. voices = espeak_ListVoices(NULL);
  137. }
  138. fprintf(f_out,"Pty Language Age/Gender VoiceName File Other Langs\n");
  139. for(ix=0; (v = voices[ix]) != NULL; ix++)
  140. {
  141. count = 0;
  142. p = v->languages;
  143. while(*p != 0)
  144. {
  145. len = strlen(p+1);
  146. lang_name = p+1;
  147. if(v->age == 0)
  148. strcpy(age_buf," ");
  149. else
  150. sprintf(age_buf,"%3d",v->age);
  151. if(count==0)
  152. {
  153. fprintf(f_out,"%2d %-12s%s%c %-17s %-11s ",
  154. p[0],lang_name,age_buf,genders[v->gender],v->name,v->identifier);
  155. }
  156. else
  157. {
  158. fprintf(f_out,"(%s %d)",lang_name,p[0]);
  159. }
  160. count++;
  161. p += len+2;
  162. }
  163. // if(scores)
  164. // fprintf(f_out,"%3d ",v->score);
  165. fputc('\n',f_out);
  166. }
  167. } // end of DisplayVoices
  168. static void PitchAdjust(int pitch_adjustment)
  169. {//==========================================
  170. int ix, factor;
  171. voice->pitch_base = (voice->pitch_base * pitch_adjust_tab[pitch_adjustment])/128;
  172. // adjust formants to give better results for a different voice pitch
  173. factor = 256 + (25 * (pitch_adjustment - 50))/50;
  174. for(ix=0; ix<=5; ix++)
  175. {
  176. voice->freq[ix] = (voice->freq2[ix] * factor)/256;
  177. }
  178. } // end of PitchAdjustment
  179. void MarkerEvent(int type, unsigned int char_position, int value, unsigned char *out_ptr)
  180. {//======================================================================================
  181. // Do nothing in the command-line version.
  182. } // end of MarkerEvent
  183. static void init_path(char *argv0)
  184. {//===============================
  185. #ifdef PLATFORM_WINDOWS
  186. HKEY RegKey;
  187. unsigned long size;
  188. unsigned long var_type;
  189. char *p;
  190. char *env;
  191. unsigned char buf[sizeof(path_home)-12];
  192. if(((env = getenv("ESPEAK_DATA_PATH")) != NULL) && ((strlen(env)+12) < sizeof(path_home)))
  193. {
  194. sprintf(path_home,"%s\\espeak-data",env);
  195. if(GetFileLength(path_home) == -2)
  196. return; // an espeak-data directory exists in the directory specified by environment variable
  197. }
  198. strcpy(path_home,argv0);
  199. if((p = strrchr(path_home,'\\')) != NULL)
  200. {
  201. strcpy(&p[1],"espeak-data");
  202. if(GetFileLength(path_home) == -2)
  203. return; // an espeak-data directory exists in the same directory as the espeak program
  204. }
  205. // otherwise, look in the Windows Registry
  206. buf[0] = 0;
  207. RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Speech\\Voices\\Tokens\\eSpeak", 0, KEY_READ, &RegKey);
  208. size = sizeof(buf);
  209. var_type = REG_SZ;
  210. RegQueryValueEx(RegKey, "path", 0, &var_type, buf, &size);
  211. sprintf(path_home,"%s\\espeak-data",buf);
  212. #else
  213. #ifdef PLATFORM_DOS
  214. strcpy(path_home,PATH_ESPEAK_DATA);
  215. #else
  216. char *env;
  217. if((env = getenv("ESPEAK_DATA_PATH")) != NULL)
  218. {
  219. snprintf(path_home,sizeof(path_home),"%s/espeak-data",env);
  220. if(GetFileLength(path_home) == -2)
  221. return; // an espeak-data directory exists
  222. }
  223. snprintf(path_home,sizeof(path_home),"%s/espeak-data",getenv("HOME"));
  224. if(access(path_home,R_OK) != 0)
  225. {
  226. strcpy(path_home,PATH_ESPEAK_DATA);
  227. }
  228. #endif
  229. #endif
  230. }
  231. static int initialise(void)
  232. {//========================
  233. int param;
  234. int result;
  235. // It seems that the wctype functions don't work until the locale has been set
  236. // to something other than the default "C". Then, not only Latin1 but also the
  237. // other characters give the correct results with iswalpha() etc.
  238. #ifdef PLATFORM_RISCOS
  239. setlocale(LC_CTYPE,"ISO8859-1");
  240. #else
  241. if(setlocale(LC_CTYPE,"en_US.UTF-8") == NULL)
  242. {
  243. if(setlocale(LC_CTYPE,"UTF-8") == NULL)
  244. setlocale(LC_CTYPE,"");
  245. }
  246. #endif
  247. WavegenInit(22050,0); // 22050
  248. if((result = LoadPhData()) != 1)
  249. {
  250. if(result == -1)
  251. fprintf(stderr,"Failed to load espeak-data\n");
  252. else
  253. fprintf(stderr,"Wrong version of espeak-data 0x%x (expects 0x%x)\n",result,version_phdata);
  254. }
  255. #ifndef PLATFORM_WINDOWS
  256. LoadConfig(); // causes problem on Windows, don't know why
  257. #endif
  258. SetVoiceStack(NULL);
  259. SynthesizeInit();
  260. for(param=0; param<N_SPEECH_PARAM; param++)
  261. param_stack[0].parameter[param] = param_defaults[param];
  262. return(0);
  263. }
  264. static void StopSpeak(int unused)
  265. {//==============================
  266. signal(SIGINT,SIG_IGN);
  267. // DEBUG
  268. // printf("\n*** Interrupting speech output (use Ctrl-D to actually quit).\n");
  269. fflush(stdout);
  270. SpeakNextClause(NULL,NULL,5);
  271. signal(SIGINT,StopSpeak);
  272. } // end of StopSpeak()
  273. #ifdef NEED_GETOPT
  274. struct option {
  275. char *name;
  276. int has_arg;
  277. int *flag;
  278. int val;
  279. };
  280. static int optind;
  281. static int optional_argument;
  282. static const char *arg_opts = "afklpsvw"; // which options have arguments
  283. static char *opt_string="";
  284. #define no_argument 0
  285. #define optional_argument 2
  286. #endif
  287. int main (int argc, char **argv)
  288. //==============================
  289. {
  290. static struct option long_options[] =
  291. {
  292. /* These options set a flag. */
  293. // {"verbose", no_argument, &verbose_flag, 1},
  294. // {"brief", no_argument, &verbose_flag, 0},
  295. /* These options don't set a flag.
  296. We distinguish them by their indices. */
  297. {"help", no_argument, 0, 'h'},
  298. {"stdin", no_argument, 0, 0x100},
  299. {"stdout", no_argument, 0, 0x101},
  300. {"compile", optional_argument, 0, 0x102},
  301. {"punct", optional_argument, 0, 0x103},
  302. {"voices", optional_argument, 0, 0x104},
  303. {0, 0, 0, 0}
  304. };
  305. static const char *err_load = "Failed to read ";
  306. FILE *f_text=NULL;
  307. const char *p_text=NULL;
  308. int option_index = 0;
  309. int c;
  310. int value;
  311. int speed=170;
  312. int ix;
  313. char *optarg2;
  314. int amp = 100; // default
  315. int wordgap = 0;
  316. int speaking = 0;
  317. int quiet = 0;
  318. int flag_stdin = 0;
  319. int flag_compile = 0;
  320. int pitch_adjustment = 50;
  321. char filename[120];
  322. char voicename[40];
  323. char dictname[40];
  324. voicename[0] = 0;
  325. mbrola_name[0] = 0;
  326. dictname[0] = 0;
  327. wavefile[0] = 0;
  328. filename[0] = 0;
  329. option_linelength = 0;
  330. option_phonemes = 0;
  331. option_waveout = 0;
  332. option_wordgap = 0;
  333. option_multibyte = espeakCHARS_AUTO; // auto
  334. f_trans = stdout;
  335. init_path(argv[0]);
  336. #ifdef NEED_GETOPT
  337. optind = 1;
  338. while(optind < argc)
  339. {
  340. int len;
  341. char *p;
  342. if((c = *opt_string) == 0)
  343. {
  344. opt_string = argv[optind];
  345. if(opt_string[0] != '-')
  346. break;
  347. optind++;
  348. opt_string++;
  349. c = *opt_string;
  350. }
  351. opt_string++;
  352. p = optarg2 = opt_string;
  353. if(c == '-')
  354. {
  355. opt_string="";
  356. for(ix=0; ;ix++)
  357. {
  358. if(long_options[ix].name == 0)
  359. break;
  360. len = strlen(long_options[ix].name);
  361. if(memcmp(long_options[ix].name,p,len)==0)
  362. {
  363. c = long_options[ix].val;
  364. optarg2 = NULL;
  365. if((long_options[ix].has_arg != 0) && (p[len]=='='))
  366. {
  367. optarg2 = &p[len+1];
  368. }
  369. break;
  370. }
  371. }
  372. }
  373. else
  374. if(strchr(arg_opts,c) != NULL)
  375. {
  376. opt_string="";
  377. if(optarg2[0]==0)
  378. {
  379. // the option's value is in the next argument
  380. optarg2 = argv[optind++];
  381. }
  382. }
  383. #else
  384. while(true)
  385. {
  386. c = getopt_long (argc, argv, "a:bf:g:hk:l:p:qs:v:w:xXm",
  387. long_options, &option_index);
  388. /* Detect the end of the options. */
  389. if (c == -1)
  390. break;
  391. optarg2 = optarg;
  392. #endif
  393. switch (c)
  394. {
  395. case 'b':
  396. option_multibyte = espeakCHARS_8BIT;
  397. break;
  398. case 'h':
  399. printf("\nspeak text-to-speech: %s\n%s",version_string,help_text);
  400. exit(0);
  401. break;
  402. case 'k':
  403. option_capitals = atoi(optarg2);
  404. break;
  405. case 'x':
  406. option_phonemes = 1;
  407. break;
  408. case 'X':
  409. option_phonemes = 2;
  410. break;
  411. case 'm':
  412. option_ssml = 1;
  413. break;
  414. case 'p':
  415. pitch_adjustment = atoi(optarg2);
  416. if(pitch_adjustment > 99) pitch_adjustment = 99;
  417. break;
  418. case 'q':
  419. quiet = 1;
  420. break;
  421. case 'f':
  422. strncpy0(filename,optarg2,sizeof(filename));
  423. break;
  424. case 'l':
  425. value = 0;
  426. value = atoi(optarg2);
  427. option_linelength = value;
  428. break;
  429. case 'a':
  430. amp = atoi(optarg2);
  431. break;
  432. case 's':
  433. speed = atoi(optarg2);
  434. break;
  435. case 'g':
  436. wordgap = atoi(optarg2);
  437. break;
  438. case 'v':
  439. strncpy0(voicename,optarg2,sizeof(voicename));
  440. break;
  441. case 'w':
  442. option_waveout = 1;
  443. strncpy0(wavefile,optarg2,sizeof(wavefile));
  444. break;
  445. case 0x100: // --stdin
  446. flag_stdin = 1;
  447. break;
  448. case 0x101: // --stdout
  449. option_waveout = 1;
  450. strcpy(wavefile,"stdout");
  451. break;
  452. case 0x102: // --compile
  453. if(optarg2 != NULL)
  454. strncpy0(voicename,optarg2,sizeof(voicename));
  455. flag_compile = 1;
  456. break;
  457. case 0x103: // --punct
  458. option_punctuation = 1;
  459. if(optarg2 != NULL)
  460. {
  461. ix = 0;
  462. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  463. option_punctlist[N_PUNCTLIST-1] = 0;
  464. option_punctuation = 2;
  465. }
  466. break;
  467. case 0x104: // --voices
  468. DisplayVoices(stdout,optarg2);
  469. exit(0);
  470. default:
  471. exit(0);
  472. }
  473. }
  474. initialise();
  475. if(flag_compile)
  476. {
  477. LoadVoice(voicename,5);
  478. #ifdef PLATFORM_DOS
  479. char path_dsource[sizeof(path_home)+20];
  480. strcpy(path_dsource,path_home);
  481. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  482. strcat(path_dsource,"dictsource\\");
  483. CompileDictionary(path_dsource,dictionary_name,NULL,NULL);
  484. #else
  485. #ifdef PLATFORM_WINDOWS
  486. char path_dsource[sizeof(path_home)+20];
  487. strcpy(path_dsource,path_home);
  488. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  489. strcat(path_dsource,"dictsource\\");
  490. CompileDictionary(path_dsource,dictionary_name,NULL,NULL);
  491. #else
  492. CompileDictionary(NULL,dictionary_name,NULL,NULL);
  493. #endif
  494. #endif
  495. exit(0);
  496. }
  497. if(voicename[0] == 0)
  498. strcpy(voicename,"default");
  499. if(SetVoiceByName(voicename) != EE_OK)
  500. {
  501. fprintf(stderr,"%svoice '%s'\n",err_load,voicename);
  502. exit(2);
  503. }
  504. SetParameter(espeakRATE,speed,0);
  505. SetParameter(espeakVOLUME,amp,0);
  506. SetParameter(espeakCAPITALS,option_capitals,0);
  507. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  508. SetParameter(espeakWORDGAP,wordgap,0);
  509. if(pitch_adjustment != 50)
  510. {
  511. PitchAdjust(pitch_adjustment);
  512. }
  513. DoVoiceChange(voice);
  514. if(filename[0]==0)
  515. {
  516. if((optind < argc) && (flag_stdin == 0))
  517. {
  518. // there's a non-option parameter, and no -f or --stdin
  519. // use it as text
  520. p_text = argv[optind];
  521. }
  522. else
  523. {
  524. f_text = stdin;
  525. if(flag_stdin == 0)
  526. option_linelength = -1; // single input lines on stdin
  527. }
  528. }
  529. else
  530. {
  531. f_text = fopen(filename,"r");
  532. }
  533. if((f_text == NULL) && (p_text == NULL))
  534. {
  535. fprintf(stderr,"%sfile '%s'\n",err_load,filename);
  536. exit(1);
  537. }
  538. if(option_waveout || quiet)
  539. {
  540. if(quiet)
  541. {
  542. // no sound output
  543. OpenWaveFile(NULL,samplerate);
  544. option_waveout = 1;
  545. }
  546. else
  547. {
  548. // write sound output to a WAV file
  549. if(OpenWaveFile(wavefile,samplerate) != 0)
  550. {
  551. fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
  552. exit(3);
  553. }
  554. }
  555. InitText(0);
  556. SpeakNextClause(f_text,p_text,0);
  557. for(;;)
  558. {
  559. if(WavegenFile() != 0)
  560. {
  561. break; // finished, wavegen command queue is empty
  562. }
  563. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  564. SpeakNextClause(NULL,NULL,1);
  565. }
  566. CloseWaveFile(samplerate);
  567. }
  568. else
  569. {
  570. // Silence on ^C or SIGINT
  571. // signal(SIGINT,StopSpeak);
  572. // output sound using portaudio
  573. WavegenInitSound();
  574. InitText(0);
  575. SpeakNextClause(f_text,p_text,0);
  576. if(option_quiet)
  577. {
  578. while(SpeakNextClause(NULL,NULL,1) != 0);
  579. return(0);
  580. }
  581. #ifdef USE_PORTAUDIO
  582. speaking = 1;
  583. while(speaking)
  584. {
  585. // NOTE: if nanosleep() isn't recognised on your system, try replacing
  586. // this by sleep(1);
  587. #ifdef PLATFORM_WINDOWS
  588. Sleep(300); // 0.3s
  589. #else
  590. #ifdef USE_NANOSLEEP
  591. struct timespec period;
  592. struct timespec remaining;
  593. period.tv_sec = 0;
  594. period.tv_nsec = 300000000; // 0.3 sec
  595. nanosleep(&period,&remaining);
  596. #else
  597. sleep(1);
  598. #endif
  599. #endif
  600. if(SynthOnTimer() != 0)
  601. speaking = 0;
  602. }
  603. #else
  604. fprintf(stderr,"-w option must be used because the program was built without a sound interface\n");
  605. #endif // USE_PORTAUDIO
  606. }
  607. return(0);
  608. }