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

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