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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Copyright (C) 2006 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 <stdbool.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #ifndef NEED_GETOPT
  25. #include <getopt.h>
  26. #endif
  27. #include <time.h>
  28. #include <sys/stat.h>
  29. #include "speak_lib.h"
  30. #include "espeak_ng.h"
  31. extern void strncpy0(char *to, const char *from, int size);
  32. extern int utf8_in(int *c, const char *buf);
  33. extern int GetFileLength(const char *filename);
  34. // This version of the command-line speak program uses the
  35. // libespeak.so.1 library
  36. static const char *help_text =
  37. "\nespeak-ng [options] [\"<words>\"]\n\n"
  38. "-f <text file> Text file to speak\n"
  39. "--stdin Read text input from stdin instead of a file\n\n"
  40. "If neither -f nor --stdin, then <words> are spoken, or if none then text\n"
  41. "is spoken from stdin, each line separately.\n\n"
  42. "-a <integer>\n"
  43. "\t Amplitude, 0 to 200, default is 100\n"
  44. "-g <integer>\n"
  45. "\t Word gap. Pause between words, units of 10mS at the default speed\n"
  46. "-k <integer>\n"
  47. "\t Indicate capital letters with: 1=sound, 2=the word \"capitals\",\n"
  48. "\t higher values indicate a pitch increase (try -k20).\n"
  49. "-l <integer>\n"
  50. "\t Line length. If not zero (which is the default), consider\n"
  51. "\t lines less than this length as end-of-clause\n"
  52. "-p <integer>\n"
  53. "\t Pitch adjustment, 0 to 99, default is 50\n"
  54. "-s <integer>\n"
  55. "\t Speed in approximate words per minute. The default is 175\n"
  56. "-v <voice name>\n"
  57. "\t Use voice file of this name from espeak-data/voices\n"
  58. "-w <wave file name>\n"
  59. "\t Write speech to this WAV file, rather than speaking it directly\n"
  60. "-b\t Input text encoding, 1=UTF8, 2=8 bit, 4=16 bit \n"
  61. "-m\t Interpret SSML markup, and ignore other < > tags\n"
  62. "-q\t Quiet, don't produce any speech (may be useful with -x)\n"
  63. "-x\t Write phoneme mnemonics to stdout\n"
  64. "-X\t Write phonemes mnemonics and translation trace to stdout\n"
  65. "-z\t No final sentence pause at the end of the text\n"
  66. "--compile=<voice name>\n"
  67. "\t Compile pronunciation rules and dictionary from the current\n"
  68. "\t directory. <voice name> specifies the language\n"
  69. "--compile-debug=<voice name>\n"
  70. "\t Compile pronunciation rules and dictionary from the current\n"
  71. "\t directory, including line numbers for use with -X.\n"
  72. "\t <voice name> specifies the language\n"
  73. "--compile-mbrola=<voice name>\n"
  74. "\t Compile an MBROLA voice\n"
  75. "--compile-intonations\n"
  76. "\t Compile the intonation data\n"
  77. "--compile-phonemes\n"
  78. "\t Compile the phoneme data\n"
  79. "--ipa Write phonemes to stdout using International Phonetic Alphabet\n"
  80. "--path=\"<path>\"\n"
  81. "\t Specifies the directory containing the espeak-data directory\n"
  82. "--pho Write mbrola phoneme data (.pho) to stdout or to the file in --phonout\n"
  83. "--phonout=\"<filename>\"\n"
  84. "\t Write phoneme output from -x -X --ipa and --pho to this file\n"
  85. "--punct=\"<characters>\"\n"
  86. "\t Speak the names of punctuation characters during speaking. If\n"
  87. "\t =<characters> is omitted, all punctuation is spoken.\n"
  88. "--sep=<character>\n"
  89. "\t Separate phonemes (from -x --ipa) with <character>.\n"
  90. "\t Default is space, z means ZWJN character.\n"
  91. "--split=<minutes>\n"
  92. "\t Starts a new WAV file every <minutes>. Used with -w\n"
  93. "--stdout Write speech output to stdout\n"
  94. "--tie=<character>\n"
  95. "\t Use a tie character within multi-letter phoneme names.\n"
  96. "\t Default is U+361, z means ZWJ character.\n"
  97. "--version Shows version number and date, and location of espeak-data\n"
  98. "--voices=<language>\n"
  99. "\t List the available voices for the specified language.\n"
  100. "\t If <language> is omitted, then list all voices.\n";
  101. int samplerate;
  102. int quiet = 0;
  103. unsigned int samples_total = 0;
  104. unsigned int samples_split = 0;
  105. unsigned int samples_split_seconds = 0;
  106. unsigned int wavefile_count = 0;
  107. FILE *f_wavfile = NULL;
  108. char filetype[5];
  109. char wavefile[200];
  110. void DisplayVoices(FILE *f_out, char *language)
  111. {
  112. int ix;
  113. const char *p;
  114. int len;
  115. int count;
  116. int c;
  117. size_t j;
  118. const espeak_VOICE *v;
  119. const char *lang_name;
  120. char age_buf[12];
  121. char buf[80];
  122. const espeak_VOICE **voices;
  123. espeak_VOICE voice_select;
  124. static char genders[4] = { '-', 'M', 'F', '-' };
  125. if ((language != NULL) && (language[0] != 0)) {
  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. } else
  133. voices = espeak_ListVoices(NULL);
  134. fprintf(f_out, "Pty Language Age/Gender VoiceName File Other Languages\n");
  135. for (ix = 0; (v = voices[ix]) != NULL; ix++) {
  136. count = 0;
  137. p = v->languages;
  138. while (*p != 0) {
  139. len = strlen(p+1);
  140. lang_name = p+1;
  141. if (v->age == 0)
  142. strcpy(age_buf, " ");
  143. else
  144. sprintf(age_buf, "%3d", v->age);
  145. if (count == 0) {
  146. for (j = 0; j < sizeof(buf); j++) {
  147. // replace spaces in the name
  148. if ((c = v->name[j]) == ' ')
  149. c = '_';
  150. if ((buf[j] = c) == 0)
  151. break;
  152. }
  153. fprintf(f_out, "%2d %-12s%s%c %-20s %-13s ",
  154. p[0], lang_name, age_buf, genders[v->gender], buf, v->identifier);
  155. } else
  156. fprintf(f_out, "(%s %d)", lang_name, p[0]);
  157. count++;
  158. p += len+2;
  159. }
  160. fputc('\n', f_out);
  161. }
  162. }
  163. static void Write4Bytes(FILE *f, int value)
  164. {
  165. // Write 4 bytes to a file, least significant first
  166. int ix;
  167. for (ix = 0; ix < 4; ix++) {
  168. fputc(value & 0xff, f);
  169. value = value >> 8;
  170. }
  171. }
  172. int OpenWavFile(char *path, int rate)
  173. {
  174. static unsigned char wave_hdr[44] = {
  175. 'R', 'I', 'F', 'F', 0x24, 0xf0, 0xff, 0x7f, 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ',
  176. 0x10, 0, 0, 0, 1, 0, 1, 0, 9, 0x3d, 0, 0, 0x12, 0x7a, 0, 0,
  177. 2, 0, 0x10, 0, 'd', 'a', 't', 'a', 0x00, 0xf0, 0xff, 0x7f
  178. };
  179. if (path == NULL)
  180. return 2;
  181. while (isspace(*path)) path++;
  182. f_wavfile = NULL;
  183. if (path[0] != 0) {
  184. if (strcmp(path, "stdout") == 0)
  185. f_wavfile = stdout;
  186. else
  187. f_wavfile = fopen(path, "wb");
  188. }
  189. if (f_wavfile == NULL) {
  190. fprintf(stderr, "Can't write to: '%s'\n", path);
  191. return 1;
  192. }
  193. fwrite(wave_hdr, 1, 24, f_wavfile);
  194. Write4Bytes(f_wavfile, rate);
  195. Write4Bytes(f_wavfile, rate * 2);
  196. fwrite(&wave_hdr[32], 1, 12, f_wavfile);
  197. return 0;
  198. }
  199. static void CloseWavFile()
  200. {
  201. unsigned int pos;
  202. if ((f_wavfile == NULL) || (f_wavfile == stdout))
  203. return;
  204. fflush(f_wavfile);
  205. pos = ftell(f_wavfile);
  206. fseek(f_wavfile, 4, SEEK_SET);
  207. Write4Bytes(f_wavfile, pos - 8);
  208. fseek(f_wavfile, 40, SEEK_SET);
  209. Write4Bytes(f_wavfile, pos - 44);
  210. fclose(f_wavfile);
  211. f_wavfile = NULL;
  212. }
  213. static int SynthCallback(short *wav, int numsamples, espeak_EVENT *events)
  214. {
  215. char fname[210];
  216. if (quiet) return 0; // -q quiet mode
  217. if (wav == NULL) {
  218. CloseWavFile();
  219. return 0;
  220. }
  221. while (events->type != 0) {
  222. if (events->type == espeakEVENT_SAMPLERATE) {
  223. samplerate = events->id.number;
  224. samples_split = samples_split_seconds * samplerate;
  225. } else if (events->type == espeakEVENT_SENTENCE) {
  226. // start a new WAV file when the limit is reached, at this sentence boundary
  227. if ((samples_split > 0) && (samples_total > samples_split)) {
  228. CloseWavFile();
  229. samples_total = 0;
  230. wavefile_count++;
  231. }
  232. }
  233. events++;
  234. }
  235. if (f_wavfile == NULL) {
  236. if (samples_split > 0) {
  237. sprintf(fname, "%s_%.2d%s", wavefile, wavefile_count+1, filetype);
  238. if (OpenWavFile(fname, samplerate) != 0)
  239. return 1;
  240. } else if (OpenWavFile(wavefile, samplerate) != 0)
  241. return 1;
  242. }
  243. if (numsamples > 0) {
  244. samples_total += numsamples;
  245. fwrite(wav, numsamples*2, 1, f_wavfile);
  246. }
  247. return 0;
  248. }
  249. static void PrintVersion()
  250. {
  251. const char *version;
  252. const char *path_data;
  253. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 0, NULL, espeakINITIALIZE_DONT_EXIT);
  254. version = espeak_Info(&path_data);
  255. printf("eSpeak text-to-speech: %s Data at: %s\n", version, path_data);
  256. }
  257. #ifdef NEED_GETOPT
  258. struct option {
  259. char *name;
  260. int has_arg;
  261. int *flag;
  262. int val;
  263. };
  264. int optind;
  265. static int optional_argument;
  266. static const char *arg_opts = "abfgklpsvw"; // which options have arguments
  267. static char *opt_string = "";
  268. #define no_argument 0
  269. #define required_argument 1
  270. #define optional_argument 2
  271. #endif
  272. int main(int argc, char **argv)
  273. {
  274. static struct option long_options[] = {
  275. { "help", no_argument, 0, 'h' },
  276. { "stdin", no_argument, 0, 0x100 },
  277. { "compile-debug", optional_argument, 0, 0x101 },
  278. { "compile", optional_argument, 0, 0x102 },
  279. { "punct", optional_argument, 0, 0x103 },
  280. { "voices", optional_argument, 0, 0x104 },
  281. { "stdout", no_argument, 0, 0x105 },
  282. { "split", optional_argument, 0, 0x106 },
  283. { "path", required_argument, 0, 0x107 },
  284. { "phonout", required_argument, 0, 0x108 },
  285. { "pho", no_argument, 0, 0x109 },
  286. { "ipa", optional_argument, 0, 0x10a },
  287. { "version", no_argument, 0, 0x10b },
  288. { "sep", optional_argument, 0, 0x10c },
  289. { "tie", optional_argument, 0, 0x10d },
  290. { "compile-mbrola", optional_argument, 0, 0x10e },
  291. { "compile-intonations", no_argument, 0, 0x10f },
  292. { "compile-phonemes", no_argument, 0, 0x110 },
  293. { 0, 0, 0, 0 }
  294. };
  295. static const char *err_load = "Failed to read ";
  296. FILE *f_text = NULL;
  297. char *p_text = NULL;
  298. FILE *f_phonemes_out = stdout;
  299. char *data_path = NULL; // use default path for espeak-data
  300. int option_index = 0;
  301. int c;
  302. int ix;
  303. char *optarg2;
  304. int value;
  305. int flag_stdin = 0;
  306. int flag_compile = 0;
  307. int filesize = 0;
  308. int synth_flags = espeakCHARS_AUTO | espeakPHONEMES | espeakENDPAUSE;
  309. int volume = -1;
  310. int speed = -1;
  311. int pitch = -1;
  312. int wordgap = -1;
  313. int option_capitals = -1;
  314. int option_punctuation = -1;
  315. int phonemes_separator = 0;
  316. int phoneme_options = 0;
  317. int option_linelength = 0;
  318. int option_waveout = 0;
  319. espeak_VOICE voice_select;
  320. char filename[200];
  321. char voicename[40];
  322. #define N_PUNCTLIST 100
  323. wchar_t option_punctlist[N_PUNCTLIST];
  324. voicename[0] = 0;
  325. wavefile[0] = 0;
  326. filename[0] = 0;
  327. option_punctlist[0] = 0;
  328. #ifdef NEED_GETOPT
  329. optind = 1;
  330. opt_string = "";
  331. while (optind < argc) {
  332. int len;
  333. char *p;
  334. if ((c = *opt_string) == 0) {
  335. opt_string = argv[optind];
  336. if (opt_string[0] != '-')
  337. break;
  338. optind++;
  339. opt_string++;
  340. c = *opt_string;
  341. }
  342. opt_string++;
  343. p = optarg2 = opt_string;
  344. if (c == '-') {
  345. if (p[0] == 0)
  346. break; // -- means don't interpret further - as commands
  347. opt_string = "";
  348. for (ix = 0;; ix++) {
  349. if (long_options[ix].name == 0)
  350. break;
  351. len = strlen(long_options[ix].name);
  352. if (memcmp(long_options[ix].name, p, len) == 0) {
  353. c = long_options[ix].val;
  354. optarg2 = NULL;
  355. if ((long_options[ix].has_arg != 0) && (p[len] == '='))
  356. optarg2 = &p[len+1];
  357. break;
  358. }
  359. }
  360. } else if (strchr(arg_opts, c) != NULL) {
  361. opt_string = "";
  362. if (optarg2[0] == 0) {
  363. // the option's value is in the next argument
  364. optarg2 = argv[optind++];
  365. }
  366. }
  367. #else
  368. while (true) {
  369. c = getopt_long(argc, argv, "a:b:f:g:hk:l:mp:qs:v:w:xXz",
  370. long_options, &option_index);
  371. // Detect the end of the options.
  372. if (c == -1)
  373. break;
  374. optarg2 = optarg;
  375. #endif
  376. switch (c)
  377. {
  378. case 'b':
  379. // input character encoding, 8bit, 16bit, UTF8
  380. if ((sscanf(optarg2, "%d", &value) == 1) && (value <= 4))
  381. synth_flags |= value;
  382. else
  383. synth_flags |= espeakCHARS_8BIT;
  384. break;
  385. case 'h':
  386. printf("\n");
  387. PrintVersion();
  388. printf("%s", help_text);
  389. return 0;
  390. case 'k':
  391. option_capitals = atoi(optarg2);
  392. break;
  393. case 'x':
  394. phoneme_options |= espeakPHONEMES_SHOW;
  395. break;
  396. case 'X':
  397. phoneme_options |= espeakPHONEMES_TRACE;
  398. break;
  399. case 'm':
  400. synth_flags |= espeakSSML;
  401. break;
  402. case 'p':
  403. pitch = atoi(optarg2);
  404. break;
  405. case 'q':
  406. quiet = 1;
  407. break;
  408. case 'f':
  409. strncpy0(filename, optarg2, sizeof(filename));
  410. break;
  411. case 'l':
  412. option_linelength = atoi(optarg2);
  413. break;
  414. case 'a':
  415. volume = atoi(optarg2);
  416. break;
  417. case 's':
  418. speed = atoi(optarg2);
  419. break;
  420. case 'g':
  421. wordgap = atoi(optarg2);
  422. break;
  423. case 'v':
  424. strncpy0(voicename, optarg2, sizeof(voicename));
  425. break;
  426. case 'w':
  427. option_waveout = 1;
  428. strncpy0(wavefile, optarg2, sizeof(filename));
  429. break;
  430. case 'z': // remove pause from the end of a sentence
  431. synth_flags &= ~espeakENDPAUSE;
  432. break;
  433. case 0x100: // --stdin
  434. flag_stdin = 1;
  435. break;
  436. case 0x105: // --stdout
  437. option_waveout = 1;
  438. strcpy(wavefile, "stdout");
  439. break;
  440. case 0x101: // --compile-debug
  441. case 0x102: // --compile
  442. strncpy0(voicename, optarg2, sizeof(voicename));
  443. flag_compile = c;
  444. quiet = 1;
  445. break;
  446. case 0x103: // --punct
  447. option_punctuation = 1;
  448. if (optarg2 != NULL) {
  449. ix = 0;
  450. while ((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  451. option_punctlist[N_PUNCTLIST-1] = 0;
  452. option_punctuation = 2;
  453. }
  454. break;
  455. case 0x104: // --voices
  456. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 0, data_path, 0);
  457. DisplayVoices(stdout, optarg2);
  458. exit(0);
  459. case 0x106: // -- split
  460. if (optarg2 == NULL)
  461. samples_split_seconds = 30 * 60; // default 30 minutes
  462. else
  463. samples_split_seconds = atoi(optarg2) * 60;
  464. break;
  465. case 0x107: // --path
  466. data_path = optarg2;
  467. break;
  468. case 0x108: // --phonout
  469. if ((f_phonemes_out = fopen(optarg2, "w")) == NULL)
  470. fprintf(stderr, "Can't write to: %s\n", optarg2);
  471. break;
  472. case 0x109: // --pho
  473. phoneme_options |= espeakPHONEMES_MBROLA;
  474. break;
  475. case 0x10a: // --ipa
  476. phoneme_options |= espeakPHONEMES_IPA;
  477. if (optarg2 != NULL) {
  478. // deprecated and obsolete
  479. switch (atoi(optarg2))
  480. {
  481. case 1:
  482. phonemes_separator = '_';
  483. break;
  484. case 2:
  485. phonemes_separator = 0x0361;
  486. phoneme_options |= espeakPHONEMES_TIE;
  487. break;
  488. case 3:
  489. phonemes_separator = 0x200d; // ZWJ
  490. phoneme_options |= espeakPHONEMES_TIE;
  491. break;
  492. }
  493. }
  494. break;
  495. case 0x10b: // --version
  496. PrintVersion();
  497. exit(0);
  498. case 0x10c: // --sep
  499. phoneme_options |= espeakPHONEMES_SHOW;
  500. if (optarg2 == 0)
  501. phonemes_separator = ' ';
  502. else
  503. utf8_in(&phonemes_separator, optarg2);
  504. if (phonemes_separator == 'z')
  505. phonemes_separator = 0x200c; // ZWNJ
  506. break;
  507. case 0x10d: // --tie
  508. phoneme_options |= (espeakPHONEMES_SHOW | espeakPHONEMES_TIE);
  509. if (optarg2 == 0)
  510. phonemes_separator = 0x0361; // default: combining-double-inverted-breve
  511. else
  512. utf8_in(&phonemes_separator, optarg2);
  513. if (phonemes_separator == 'z')
  514. phonemes_separator = 0x200d; // ZWJ
  515. break;
  516. case 0x10e: // --compile-mbrola
  517. espeak_ng_InitializePath(data_path);
  518. return (espeak_ng_CompileMbrolaVoice(optarg2, stdout) == ENS_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
  519. case 0x10f: // --compile-intonations
  520. espeak_ng_InitializePath(data_path);
  521. return (espeak_ng_CompileIntonation(stdout) == ENS_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
  522. case 0x110: // --compile-phonemes
  523. espeak_ng_InitializePath(data_path);
  524. return (espeak_ng_CompilePhonemeData(22050, stdout) == ENS_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
  525. default:
  526. exit(0);
  527. }
  528. }
  529. espeak_ng_InitializePath(data_path);
  530. espeak_ng_STATUS result = espeak_ng_Initialize();
  531. if (result != ENS_OK) {
  532. if (result == ENE_READ_ERROR)
  533. fprintf(stderr, "Failed to load espeak-data\n");
  534. else
  535. fprintf(stderr, "Wrong version of espeak-data\n");
  536. exit(1);
  537. }
  538. if (option_waveout || quiet) {
  539. // writing to a file (or no output), we can use synchronous mode
  540. espeak_ng_InitializeOutput(ENOUTPUT_MODE_SYNCHRONOUS, 0, NULL);
  541. samplerate = espeak_ng_GetSampleRate();
  542. samples_split = samplerate * samples_split_seconds;
  543. espeak_SetSynthCallback(SynthCallback);
  544. if (samples_split) {
  545. char *extn;
  546. extn = strrchr(wavefile, '.');
  547. if ((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4)) {
  548. strcpy(filetype, extn);
  549. *extn = 0;
  550. }
  551. }
  552. } else {
  553. // play the sound output
  554. espeak_ng_InitializeOutput(ENOUTPUT_MODE_SPEAK_AUDIO, 0, NULL);
  555. samplerate = espeak_ng_GetSampleRate();
  556. }
  557. if (voicename[0] == 0)
  558. strcpy(voicename, "default");
  559. if (espeak_SetVoiceByName(voicename) != EE_OK) {
  560. memset(&voice_select, 0, sizeof(voice_select));
  561. voice_select.languages = voicename;
  562. if (espeak_SetVoiceByProperties(&voice_select) != EE_OK) {
  563. fprintf(stderr, "%svoice '%s'\n", err_load, voicename);
  564. exit(2);
  565. }
  566. }
  567. if (flag_compile) {
  568. // This must be done after the voice is set
  569. return (espeak_ng_CompileDictionary("", NULL, stderr, flag_compile & 0x1) == ENS_OK)
  570. ? EXIT_SUCCESS : EXIT_FAILURE;
  571. }
  572. // set any non-default values of parameters. This must be done after espeak_Initialize()
  573. if (speed > 0)
  574. espeak_SetParameter(espeakRATE, speed, 0);
  575. if (volume >= 0)
  576. espeak_SetParameter(espeakVOLUME, volume, 0);
  577. if (pitch >= 0)
  578. espeak_SetParameter(espeakPITCH, pitch, 0);
  579. if (option_capitals >= 0)
  580. espeak_SetParameter(espeakCAPITALS, option_capitals, 0);
  581. if (option_punctuation >= 0)
  582. espeak_SetParameter(espeakPUNCTUATION, option_punctuation, 0);
  583. if (wordgap >= 0)
  584. espeak_SetParameter(espeakWORDGAP, wordgap, 0);
  585. if (option_linelength > 0)
  586. espeak_SetParameter(espeakLINELENGTH, option_linelength, 0);
  587. if (option_punctuation == 2)
  588. espeak_SetPunctuationList(option_punctlist);
  589. espeak_SetPhonemeTrace(phoneme_options | (phonemes_separator << 8), f_phonemes_out);
  590. if (filename[0] == 0) {
  591. if ((optind < argc) && (flag_stdin == 0)) {
  592. // there's a non-option parameter, and no -f or --stdin
  593. // use it as text
  594. p_text = argv[optind];
  595. } else {
  596. f_text = stdin;
  597. if (flag_stdin == 0)
  598. flag_stdin = 2;
  599. }
  600. } else {
  601. filesize = GetFileLength(filename);
  602. f_text = fopen(filename, "r");
  603. }
  604. if ((f_text == NULL) && (p_text == NULL)) {
  605. fprintf(stderr, "%sfile '%s'\n", err_load, filename);
  606. exit(1);
  607. }
  608. if (p_text != NULL) {
  609. int size;
  610. size = strlen(p_text);
  611. espeak_Synth(p_text, size+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL);
  612. } else if (flag_stdin) {
  613. int max = 1000;
  614. p_text = (char *)malloc(max);
  615. if (flag_stdin == 2) {
  616. // line by line input on stdin
  617. while (fgets(p_text, max, stdin) != NULL) {
  618. p_text[max-1] = 0;
  619. espeak_Synth(p_text, max, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL);
  620. }
  621. } else {
  622. // bulk input on stdin
  623. ix = 0;
  624. while (!feof(stdin)) {
  625. p_text[ix++] = fgetc(stdin);
  626. if (ix >= (max-1)) {
  627. max += 1000;
  628. p_text = (char *)realloc(p_text, max);
  629. }
  630. }
  631. if (ix > 0) {
  632. p_text[ix-1] = 0;
  633. espeak_Synth(p_text, ix+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL);
  634. }
  635. }
  636. } else if (f_text != NULL) {
  637. if ((p_text = (char *)malloc(filesize+1)) == NULL) {
  638. fprintf(stderr, "Failed to allocate memory %d bytes", filesize);
  639. exit(3);
  640. }
  641. fread(p_text, 1, filesize, f_text);
  642. p_text[filesize] = 0;
  643. espeak_Synth(p_text, filesize+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL);
  644. fclose(f_text);
  645. }
  646. if (espeak_Synchronize() != EE_OK) {
  647. fprintf(stderr, "espeak_Synchronize() failed, maybe error when opening output device\n");
  648. exit(4);
  649. }
  650. if (f_phonemes_out != stdout)
  651. fclose(f_phonemes_out);
  652. return 0;
  653. }