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

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