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

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