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

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