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-ng.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * Copyright (C) 2005 to 2013 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "speech.h"
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #ifndef PLATFORM_DOS
  27. #ifdef PLATFORM_WINDOWS
  28. #include <fcntl.h>
  29. #include <io.h>
  30. #include <windows.h>
  31. #include <winreg.h>
  32. #else
  33. #include <unistd.h>
  34. #endif
  35. #endif
  36. #ifndef NEED_GETOPT
  37. #include <getopt.h>
  38. #endif
  39. #include <time.h>
  40. #include <signal.h>
  41. #include <locale.h>
  42. #include <sys/stat.h>
  43. #include "speak_lib.h"
  44. #include "espeak_ng.h"
  45. #include "phoneme.h"
  46. #include "synthesize.h"
  47. #include "voice.h"
  48. #include "translate.h"
  49. extern void Write4Bytes(FILE *f, int value);
  50. char path_home[N_PATH_HOME]; // this is the espeak-data directory
  51. char filetype[5];
  52. char wavefile[200];
  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-ng [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. "--compile-debug=<voice name>\n"
  93. "\t Compile pronunciation rules and dictionary from the current\n"
  94. "\t directory, including line numbers for use with -X.\n"
  95. "\t <voice name> specifies the language\n"
  96. "--ipa Write phonemes to stdout using International Phonetic Alphabet\n"
  97. "--path=\"<path>\"\n"
  98. "\t Specifies the directory containing the espeak-data directory\n"
  99. "--pho Write mbrola phoneme data (.pho) to stdout or to the file in --phonout\n"
  100. "--phonout=\"<filename>\"\n"
  101. "\t Write phoneme output from -x -X --ipa and --pho to this file\n"
  102. "--punct=\"<characters>\"\n"
  103. "\t Speak the names of punctuation characters during speaking. If\n"
  104. "\t =<characters> is omitted, all punctuation is spoken.\n"
  105. "--sep=<character>\n"
  106. "\t Separate phonemes (from -x --ipa) with <character>.\n"
  107. "\t Default is space, z means ZWJN character.\n"
  108. "--split=<minutes>\n"
  109. "\t Starts a new WAV file every <minutes>. Used with -w\n"
  110. "--stdout Write speech output to stdout\n"
  111. "--tie=<character>\n"
  112. "\t Use a tie character within multi-letter phoneme names.\n"
  113. "\t Default is U+361, z means ZWJ character.\n"
  114. "--version Shows version number and date, and location of espeak-data\n"
  115. "--voices=<language>\n"
  116. "\t List the available voices for the specified language.\n"
  117. "\t If <language> is omitted, then list all voices.\n";
  118. void DisplayVoices(FILE *f_out, char *language);
  119. USHORT voice_pcnt[N_PEAKS+1][3];
  120. void DisplayVoices(FILE *f_out, char *language)
  121. {
  122. int ix;
  123. const char *p;
  124. int len;
  125. int count;
  126. int c;
  127. int j;
  128. const espeak_VOICE *v;
  129. const char *lang_name;
  130. char age_buf[12];
  131. char buf[80];
  132. const espeak_VOICE **voices;
  133. espeak_VOICE voice_select;
  134. static char genders[4] = { '-', 'M', 'F', '-' };
  135. if ((language != NULL) && (language[0] != 0)) {
  136. // display only voices for the specified language, in order of priority
  137. voice_select.languages = language;
  138. voice_select.age = 0;
  139. voice_select.gender = 0;
  140. voice_select.name = NULL;
  141. voices = espeak_ListVoices(&voice_select);
  142. } else
  143. voices = espeak_ListVoices(NULL);
  144. fprintf(f_out, "Pty Language Age/Gender VoiceName File Other Languages\n");
  145. for (ix = 0; (v = voices[ix]) != NULL; ix++) {
  146. count = 0;
  147. p = v->languages;
  148. while (*p != 0) {
  149. len = strlen(p+1);
  150. lang_name = p+1;
  151. if (v->age == 0)
  152. strcpy(age_buf, " ");
  153. else
  154. sprintf(age_buf, "%3d", v->age);
  155. if (count == 0) {
  156. for (j = 0; j < sizeof(buf); j++) {
  157. // replace spaces in the name
  158. if ((c = v->name[j]) == ' ')
  159. c = '_';
  160. if ((buf[j] = c) == 0)
  161. break;
  162. }
  163. fprintf(f_out, "%2d %-12s%s%c %-20s %-13s ",
  164. p[0], lang_name, age_buf, genders[v->gender], buf, v->identifier);
  165. } else
  166. fprintf(f_out, "(%s %d)", lang_name, p[0]);
  167. count++;
  168. p += len+2;
  169. }
  170. fputc('\n', f_out);
  171. }
  172. }
  173. static int OpenWaveFile(const char *path, int rate)
  174. {
  175. // Set the length of 0x7ffff000 for --stdout
  176. // This will be changed to the correct length for -w (write to file)
  177. static unsigned char wave_hdr[44] = {
  178. 'R', 'I', 'F', 'F', 0x24, 0xf0, 0xff, 0x7f, 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ',
  179. 0x10, 0, 0, 0, 1, 0, 1, 0, 9, 0x3d, 0, 0, 0x12, 0x7a, 0, 0,
  180. 2, 0, 0x10, 0, 'd', 'a', 't', 'a', 0x00, 0xf0, 0xff, 0x7f
  181. };
  182. if (path == NULL)
  183. return 2;
  184. while (isspace(*path)) path++;
  185. f_wave = NULL;
  186. if (path[0] != 0) {
  187. if (strcmp(path, "stdout") == 0) {
  188. #ifdef PLATFORM_WINDOWS
  189. // prevent Windows adding 0x0d before 0x0a bytes
  190. _setmode(_fileno(stdout), _O_BINARY);
  191. #endif
  192. f_wave = stdout;
  193. } else
  194. f_wave = fopen(path, "wb");
  195. }
  196. if (f_wave != NULL) {
  197. fwrite(wave_hdr, 1, 24, f_wave);
  198. Write4Bytes(f_wave, rate);
  199. Write4Bytes(f_wave, rate * 2);
  200. fwrite(&wave_hdr[32], 1, 12, f_wave);
  201. return 0;
  202. }
  203. return 1;
  204. }
  205. static void CloseWaveFile()
  206. {
  207. unsigned int pos;
  208. if ((f_wave == NULL) || (f_wave == stdout))
  209. return;
  210. fflush(f_wave);
  211. pos = ftell(f_wave);
  212. fseek(f_wave, 4, SEEK_SET);
  213. Write4Bytes(f_wave, pos - 8);
  214. fseek(f_wave, 40, SEEK_SET);
  215. Write4Bytes(f_wave, pos - 44);
  216. fclose(f_wave);
  217. f_wave = NULL;
  218. }
  219. static int WavegenFile(void)
  220. {
  221. int finished;
  222. unsigned char wav_outbuf[1024];
  223. char fname[210];
  224. out_ptr = out_start = wav_outbuf;
  225. out_end = wav_outbuf + sizeof(wav_outbuf);
  226. finished = WavegenFill();
  227. if (quiet)
  228. return finished;
  229. if (f_wave == NULL) {
  230. sprintf(fname, "%s_%.2d%s", wavefile, ++wavefile_count, filetype);
  231. if (OpenWaveFile(fname, samplerate) != 0)
  232. return 1;
  233. }
  234. if (end_of_sentence) {
  235. end_of_sentence = 0;
  236. if ((samples_split > 0 ) && (samples_total > samples_split)) {
  237. CloseWaveFile();
  238. samples_total = 0;
  239. }
  240. }
  241. if (f_wave != NULL) {
  242. samples_total += (out_ptr - wav_outbuf)/2;
  243. fwrite(wav_outbuf, 1, out_ptr - wav_outbuf, f_wave);
  244. }
  245. return finished;
  246. }
  247. static void init_path(char *argv0, char *path_specified)
  248. {
  249. if (path_specified) {
  250. sprintf(path_home, "%s/espeak-data", path_specified);
  251. return;
  252. }
  253. #ifdef PLATFORM_WINDOWS
  254. HKEY RegKey;
  255. unsigned long size;
  256. unsigned long var_type;
  257. char *p;
  258. char *env;
  259. unsigned char buf[sizeof(path_home)-12];
  260. if (((env = getenv("ESPEAK_DATA_PATH")) != NULL) && ((strlen(env)+12) < sizeof(path_home))) {
  261. sprintf(path_home, "%s\\espeak-data", env);
  262. if (GetFileLength(path_home) == -2)
  263. return; // an espeak-data directory exists in the directory specified by environment variable
  264. }
  265. strcpy(path_home, argv0);
  266. if ((p = strrchr(path_home, '\\')) != NULL) {
  267. strcpy(&p[1], "espeak-data");
  268. if (GetFileLength(path_home) == -2)
  269. return; // an espeak-data directory exists in the same directory as the espeak program
  270. }
  271. // otherwise, look in the Windows Registry
  272. buf[0] = 0;
  273. RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Speech\\Voices\\Tokens\\eSpeak", 0, KEY_READ, &RegKey);
  274. size = sizeof(buf);
  275. var_type = REG_SZ;
  276. RegQueryValueEx(RegKey, "path", 0, &var_type, buf, &size);
  277. sprintf(path_home, "%s\\espeak-data", buf);
  278. #else
  279. #ifdef PLATFORM_DOS
  280. strcpy(path_home, PATH_ESPEAK_DATA);
  281. #else
  282. char *env;
  283. if ((env = getenv("ESPEAK_DATA_PATH")) != NULL) {
  284. snprintf(path_home, sizeof(path_home), "%s/espeak-data", env);
  285. if (GetFileLength(path_home) == -2)
  286. return; // an espeak-data directory exists
  287. }
  288. snprintf(path_home, sizeof(path_home), "%s/espeak-data", getenv("HOME"));
  289. if (access(path_home, R_OK) != 0)
  290. strcpy(path_home, PATH_ESPEAK_DATA);
  291. #endif
  292. #endif
  293. }
  294. static int initialise(void)
  295. {
  296. int param;
  297. int result;
  298. int srate = 22050; // default sample rate
  299. // It seems that the wctype functions don't work until the locale has been set
  300. // to something other than the default "C". Then, not only Latin1 but also the
  301. // other characters give the correct results with iswalpha() etc.
  302. if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL) {
  303. if (setlocale(LC_CTYPE, "UTF-8") == NULL)
  304. setlocale(LC_CTYPE, "");
  305. }
  306. if ((result = LoadPhData(&srate)) != 1) {
  307. if (result == -1) {
  308. fprintf(stderr, "Failed to load espeak-data\n");
  309. exit(1);
  310. } else
  311. fprintf(stderr, "Wrong version of espeak-data 0x%x (expects 0x%x) at %s\n", result, version_phdata, path_home);
  312. }
  313. WavegenInit(srate, 0);
  314. LoadConfig();
  315. SetVoiceStack(NULL, "");
  316. SynthesizeInit();
  317. for (param = 0; param < N_SPEECH_PARAM; param++)
  318. param_stack[0].parameter[param] = param_defaults[param];
  319. return 0;
  320. }
  321. #ifdef NEED_GETOPT
  322. struct option {
  323. char *name;
  324. int has_arg;
  325. int *flag;
  326. int val;
  327. };
  328. int optind;
  329. static int optional_argument;
  330. static const char *arg_opts = "abfgklpsvw"; // which options have arguments
  331. static char *opt_string = "";
  332. #define no_argument 0
  333. #define required_argument 1
  334. #define optional_argument 2
  335. #endif
  336. int main(int argc, char **argv)
  337. {
  338. static struct option long_options[] = {
  339. { "help", no_argument, 0, 'h' },
  340. { "stdin", no_argument, 0, 0x100 },
  341. { "compile-debug", optional_argument, 0, 0x101 },
  342. { "compile", optional_argument, 0, 0x102 },
  343. { "punct", optional_argument, 0, 0x103 },
  344. { "voices", optional_argument, 0, 0x104 },
  345. { "stdout", no_argument, 0, 0x105 },
  346. { "split", optional_argument, 0, 0x106 },
  347. { "path", required_argument, 0, 0x107 },
  348. { "phonout", required_argument, 0, 0x108 },
  349. { "pho", no_argument, 0, 0x109 },
  350. { "ipa", optional_argument, 0, 0x10a },
  351. { "version", no_argument, 0, 0x10b },
  352. { "sep", optional_argument, 0, 0x10c },
  353. { "tie", optional_argument, 0, 0x10d },
  354. { "compile-mbrola", optional_argument, 0, 0x10e },
  355. { "compile-intonations", no_argument, 0, 0x10f },
  356. { "compile-phonemes", no_argument, 0, 0x110 },
  357. { 0, 0, 0, 0 }
  358. };
  359. static const char *err_load = "Failed to read ";
  360. FILE *f_text = NULL;
  361. const char *p_text = NULL;
  362. char *data_path = NULL; // use default path for espeak-data
  363. int option_index = 0;
  364. int c;
  365. int value;
  366. int speed = 175;
  367. int ix;
  368. char *optarg2;
  369. int amp = 100; // default
  370. int wordgap = 0;
  371. int flag_stdin = 0;
  372. int flag_compile = 0;
  373. int pitch_adjustment = 50;
  374. int phoneme_options = 0;
  375. int phonemes_separator = 0;
  376. espeak_VOICE voice_select;
  377. char filename[200];
  378. char voicename[40];
  379. int speaking = 0;
  380. voicename[0] = 0;
  381. mbrola_name[0] = 0;
  382. wavefile[0] = 0;
  383. filename[0] = 0;
  384. option_linelength = 0;
  385. option_phonemes = 0;
  386. option_waveout = 0;
  387. option_wordgap = 0;
  388. option_endpause = 1;
  389. option_phoneme_input = 1;
  390. option_multibyte = espeakCHARS_AUTO;
  391. f_trans = stdout;
  392. #ifdef NEED_GETOPT
  393. optind = 1;
  394. opt_string = "";
  395. while (optind < argc) {
  396. int len;
  397. char *p;
  398. if ((c = *opt_string) == 0) {
  399. opt_string = argv[optind];
  400. if (opt_string[0] != '-')
  401. break;
  402. optind++;
  403. opt_string++;
  404. c = *opt_string;
  405. }
  406. opt_string++;
  407. p = optarg2 = opt_string;
  408. if (c == '-') {
  409. if (p[0] == 0)
  410. break; // -- means don't interpret further - as commands
  411. opt_string = "";
  412. for (ix = 0;; ix++) {
  413. if (long_options[ix].name == 0)
  414. break;
  415. len = strlen(long_options[ix].name);
  416. if (memcmp(long_options[ix].name, p, len) == 0) {
  417. c = long_options[ix].val;
  418. optarg2 = NULL;
  419. if ((long_options[ix].has_arg != 0) && (p[len] == '='))
  420. optarg2 = &p[len+1];
  421. break;
  422. }
  423. }
  424. } else if (strchr(arg_opts, c) != NULL) {
  425. opt_string = "";
  426. if (optarg2[0] == 0) {
  427. // the option's value is in the next argument
  428. optarg2 = argv[optind++];
  429. }
  430. }
  431. #else
  432. while (true) {
  433. 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
  434. long_options, &option_index);
  435. // Detect the end of the options.
  436. if (c == -1)
  437. break;
  438. optarg2 = optarg;
  439. #endif
  440. switch (c)
  441. {
  442. case 'b':
  443. // input character encoding, 8bit, 16bit, UTF8
  444. option_multibyte = espeakCHARS_8BIT;
  445. if ((sscanf(optarg2, "%d", &value) == 1) && (value <= 4))
  446. option_multibyte = value;
  447. break;
  448. case 'h':
  449. init_path(argv[0], data_path);
  450. printf("\nspeak text-to-speech: %s Data at: %s\n%s", version_string, path_home, help_text);
  451. exit(0);
  452. case 'k':
  453. option_capitals = atoi(optarg2);
  454. break;
  455. case 'x':
  456. phoneme_options |= espeakPHONEMES_SHOW;
  457. break;
  458. case 'X':
  459. phoneme_options |= espeakPHONEMES_TRACE;
  460. break;
  461. case 'm':
  462. option_ssml = 1;
  463. break;
  464. case 'p':
  465. pitch_adjustment = atoi(optarg2);
  466. if (pitch_adjustment > 99) pitch_adjustment = 99;
  467. break;
  468. case 'q':
  469. quiet = 1;
  470. break;
  471. case 'f':
  472. strncpy0(filename, optarg2, sizeof(filename));
  473. break;
  474. case 'l':
  475. value = 0;
  476. value = atoi(optarg2);
  477. option_linelength = value;
  478. break;
  479. case 'a':
  480. amp = atoi(optarg2);
  481. break;
  482. case 's':
  483. speed = atoi(optarg2);
  484. break;
  485. case 'g':
  486. wordgap = atoi(optarg2);
  487. break;
  488. case 'v':
  489. strncpy0(voicename, optarg2, sizeof(voicename));
  490. break;
  491. case 'w':
  492. option_waveout = 1;
  493. strncpy0(wavefile, optarg2, sizeof(wavefile));
  494. break;
  495. case 'z':
  496. option_endpause = 0;
  497. break;
  498. case 0x100: // --stdin
  499. flag_stdin = 1;
  500. break;
  501. case 0x105: // --stdout
  502. option_waveout = 1;
  503. strcpy(wavefile, "stdout");
  504. break;
  505. case 0x101: // --compile-debug
  506. case 0x102: // --compile
  507. if (optarg2 != NULL)
  508. strncpy0(voicename, optarg2, sizeof(voicename));
  509. flag_compile = c;
  510. break;
  511. case 0x103: // --punct
  512. option_punctuation = 1;
  513. if (optarg2 != NULL) {
  514. ix = 0;
  515. while ((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  516. option_punctlist[N_PUNCTLIST-1] = 0;
  517. option_punctuation = 2;
  518. }
  519. break;
  520. case 0x104: // --voices
  521. init_path(argv[0], data_path);
  522. DisplayVoices(stdout, optarg2);
  523. exit(0);
  524. case 0x106: // -- split
  525. if (optarg2 == NULL)
  526. samples_split = 30; // default 30 minutes
  527. else
  528. samples_split = atoi(optarg2);
  529. break;
  530. case 0x107: // --path
  531. data_path = optarg2;
  532. break;
  533. case 0x108: // --phonout
  534. if ((f_trans = fopen(optarg2, "w")) == NULL) {
  535. fprintf(stderr, "Can't write to: %s\n", optarg2);
  536. f_trans = stderr;
  537. }
  538. break;
  539. case 0x109: // --pho
  540. phoneme_options |= espeakPHONEMES_MBROLA;
  541. break;
  542. case 0x10a: // --ipa
  543. phoneme_options |= espeakPHONEMES_IPA;
  544. if (optarg2 != NULL) {
  545. // deprecated and obsolete
  546. switch (atoi(optarg2))
  547. {
  548. case 1:
  549. phonemes_separator = '_';
  550. break;
  551. case 2:
  552. phonemes_separator = 0x0361;
  553. phoneme_options |= espeakPHONEMES_TIE;
  554. break;
  555. case 3:
  556. phonemes_separator = 0x200d; // ZWJ
  557. phoneme_options |= espeakPHONEMES_TIE;
  558. break;
  559. }
  560. }
  561. break;
  562. case 0x10b: // --version
  563. init_path(argv[0], data_path);
  564. printf("speak text-to-speech: %s Data at: %s\n", version_string, path_home);
  565. exit(0);
  566. case 0x10c: // --sep
  567. phoneme_options |= espeakPHONEMES_SHOW;
  568. if (optarg2 == 0)
  569. phonemes_separator = ' ';
  570. else
  571. utf8_in(&phonemes_separator, optarg2);
  572. if (phonemes_separator == 'z')
  573. phonemes_separator = 0x200c; // ZWNJ
  574. break;
  575. case 0x10d: // --tie
  576. phoneme_options |= (espeakPHONEMES_SHOW | espeakPHONEMES_TIE);
  577. if (optarg2 == 0)
  578. phonemes_separator = 0x0361; // default: combining-double-inverted-breve
  579. else
  580. utf8_in(&phonemes_separator, optarg2);
  581. if (phonemes_separator == 'z')
  582. phonemes_separator = 0x200d; // ZWJ
  583. break;
  584. case 0x10e: // --compile-mbrola
  585. samplerate = espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, data_path, 0);
  586. return (espeak_ng_CompileMbrolaVoice(optarg2, stdout) == ENS_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
  587. case 0x10f: // --compile-intonations
  588. samplerate = espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, data_path, espeakINITIALIZE_PATH_ONLY);
  589. return (espeak_ng_CompileIntonation(stdout) == ENS_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
  590. case 0x110: // --compile-phonemes
  591. samplerate = espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, data_path, espeakINITIALIZE_PATH_ONLY);
  592. return (espeak_ng_CompilePhonemeData(22050, stdout) == ENS_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
  593. default:
  594. exit(0);
  595. }
  596. }
  597. init_path(argv[0], data_path);
  598. initialise();
  599. if (voicename[0] == 0)
  600. strcpy(voicename, "default");
  601. if (espeak_SetVoiceByName(voicename) != EE_OK) {
  602. memset(&voice_select, 0, sizeof(voice_select));
  603. voice_select.languages = voicename;
  604. if (espeak_SetVoiceByProperties(&voice_select) != EE_OK) {
  605. fprintf(stderr, "%svoice '%s'\n", err_load, voicename);
  606. exit(2);
  607. }
  608. }
  609. if (flag_compile) {
  610. #if defined(PLATFORM_DOS) || defined(PLATFORM_WINDOWS)
  611. char path_dsource[sizeof(path_home)+20];
  612. strcpy(path_dsource, path_home);
  613. path_dsource[strlen(path_home)-11] = 0; // remove "espeak-data" from the end
  614. strcat(path_dsource, "dictsource\\");
  615. espeak_ng_STATUS status = espeak_ng_CompileDictionary(path_dsource, dictionary_name, NULL, flag_compile & 0x1);
  616. #else
  617. espeak_ng_STATUS status = espeak_ng_CompileDictionary(NULL, dictionary_name, NULL, flag_compile & 0x1);
  618. #endif
  619. return (status == ENS_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
  620. }
  621. SetParameter(espeakRATE, speed, 0);
  622. SetParameter(espeakVOLUME, amp, 0);
  623. SetParameter(espeakCAPITALS, option_capitals, 0);
  624. SetParameter(espeakPUNCTUATION, option_punctuation, 0);
  625. SetParameter(espeakWORDGAP, wordgap, 0);
  626. option_phonemes = phoneme_options | (phonemes_separator << 8);
  627. if (pitch_adjustment != 50)
  628. SetParameter(espeakPITCH, pitch_adjustment, 0);
  629. DoVoiceChange(voice);
  630. if (filename[0] == 0) {
  631. if ((optind < argc) && (flag_stdin == 0)) {
  632. // there's a non-option parameter, and no -f or --stdin
  633. // use it as text
  634. p_text = argv[optind];
  635. } else {
  636. f_text = stdin;
  637. if (flag_stdin == 0)
  638. option_linelength = -1; // single input lines on stdin
  639. }
  640. } else
  641. f_text = fopen(filename, "r");
  642. if ((f_text == NULL) && (p_text == NULL)) {
  643. fprintf(stderr, "%sfile '%s'\n", err_load, filename);
  644. exit(1);
  645. }
  646. if (option_waveout || quiet) {
  647. if (quiet) {
  648. // no sound output
  649. OpenWaveFile(NULL, samplerate);
  650. option_waveout = 1;
  651. } else {
  652. // write sound output to a WAV file
  653. samples_split = (samplerate * samples_split) * 60;
  654. if (samples_split) {
  655. // don't open the wav file until we start generating speech
  656. char *extn;
  657. extn = strrchr(wavefile, '.');
  658. if ((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4)) {
  659. strcpy(filetype, extn);
  660. *extn = 0;
  661. }
  662. } else if (OpenWaveFile(wavefile, samplerate) != 0) {
  663. fprintf(stderr, "Can't write to output file '%s'\n'", wavefile);
  664. exit(3);
  665. }
  666. }
  667. InitText(0);
  668. SpeakNextClause(f_text, p_text, 0);
  669. ix = 1;
  670. for (;;) {
  671. if (WavegenFile() != 0) {
  672. if (ix == 0)
  673. break; // finished, wavegen command queue is empty
  674. }
  675. if (Generate(phoneme_list, &n_phoneme_list, 1) == 0)
  676. ix = SpeakNextClause(NULL, NULL, 1);
  677. }
  678. CloseWaveFile();
  679. } else {
  680. WavegenInitSound();
  681. InitText(0);
  682. SpeakNextClause(f_text, p_text, 0);
  683. if (option_quiet) {
  684. while (SpeakNextClause(NULL, NULL, 1) != 0) ;
  685. return 0;
  686. }
  687. speaking = 1;
  688. while (speaking) {
  689. // NOTE: if nanosleep() isn't recognised on your system, try replacing
  690. // this by sleep(1);
  691. #ifdef PLATFORM_WINDOWS
  692. Sleep(300); // 0.3s
  693. #else
  694. #ifdef USE_NANOSLEEP
  695. struct timespec period;
  696. struct timespec remaining;
  697. period.tv_sec = 0;
  698. period.tv_nsec = 300000000; // 0.3 sec
  699. nanosleep(&period, &remaining);
  700. #else
  701. sleep(1);
  702. #endif
  703. #endif
  704. if (SynthOnTimer() != 0)
  705. speaking = 0;
  706. }
  707. }
  708. if ((f_trans != stdout) && (f_trans != stderr))
  709. fclose(f_trans);
  710. return 0;
  711. }