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

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