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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. bool quiet = false;
  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. static 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. static 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 = true;
  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. if (optarg2 != NULL && *optarg2) {
  400. strncpy0(voicename, optarg2, sizeof(voicename));
  401. flag_compile = c;
  402. quiet = true;
  403. break;
  404. } else {
  405. fprintf(stderr, "Voice name to '%s' not specified.\n", c == 0x101 ? "--compile-debug" : "--compile");
  406. exit(EXIT_FAILURE);
  407. }
  408. case 0x103: // --punct
  409. option_punctuation = 1;
  410. if (optarg2 != NULL) {
  411. ix = 0;
  412. while ((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++;
  413. option_punctlist[N_PUNCTLIST-1] = 0;
  414. option_punctuation = 2;
  415. }
  416. break;
  417. case 0x104: // --voices
  418. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 0, data_path, 0);
  419. DisplayVoices(stdout, optarg2);
  420. exit(0);
  421. case 0x106: // -- split
  422. if (optarg2 == NULL)
  423. samples_split_seconds = 30 * 60; // default 30 minutes
  424. else
  425. samples_split_seconds = atoi(optarg2) * 60;
  426. break;
  427. case 0x107: // --path
  428. data_path = optarg2;
  429. break;
  430. case 0x108: // --phonout
  431. if ((f_phonemes_out = fopen(optarg2, "w")) == NULL)
  432. fprintf(stderr, "Can't write to: %s\n", optarg2);
  433. break;
  434. case 0x109: // --pho
  435. phoneme_options |= espeakPHONEMES_MBROLA;
  436. break;
  437. case 0x10a: // --ipa
  438. phoneme_options |= espeakPHONEMES_IPA;
  439. if (optarg2 != NULL) {
  440. // deprecated and obsolete
  441. switch (atoi(optarg2))
  442. {
  443. case 1:
  444. phonemes_separator = '_';
  445. break;
  446. case 2:
  447. phonemes_separator = 0x0361;
  448. phoneme_options |= espeakPHONEMES_TIE;
  449. break;
  450. case 3:
  451. phonemes_separator = 0x200d; // ZWJ
  452. phoneme_options |= espeakPHONEMES_TIE;
  453. break;
  454. }
  455. }
  456. break;
  457. case 0x10b: // --version
  458. PrintVersion();
  459. exit(0);
  460. case 0x10c: // --sep
  461. phoneme_options |= espeakPHONEMES_SHOW;
  462. if (optarg2 == 0)
  463. phonemes_separator = ' ';
  464. else
  465. utf8_in(&phonemes_separator, optarg2);
  466. if (phonemes_separator == 'z')
  467. phonemes_separator = 0x200c; // ZWNJ
  468. break;
  469. case 0x10d: // --tie
  470. phoneme_options |= (espeakPHONEMES_SHOW | espeakPHONEMES_TIE);
  471. if (optarg2 == 0)
  472. phonemes_separator = 0x0361; // default: combining-double-inverted-breve
  473. else
  474. utf8_in(&phonemes_separator, optarg2);
  475. if (phonemes_separator == 'z')
  476. phonemes_separator = 0x200d; // ZWJ
  477. break;
  478. case 0x10e: // --compile-mbrola
  479. {
  480. espeak_ng_InitializePath(data_path);
  481. espeak_ng_ERROR_CONTEXT context = NULL;
  482. espeak_ng_STATUS result = espeak_ng_CompileMbrolaVoice(optarg2, stdout, &context);
  483. if (result != ENS_OK) {
  484. espeak_ng_PrintStatusCodeMessage(result, stderr, context);
  485. espeak_ng_ClearErrorContext(&context);
  486. return EXIT_FAILURE;
  487. }
  488. return EXIT_SUCCESS;
  489. }
  490. case 0x10f: // --compile-intonations
  491. {
  492. espeak_ng_InitializePath(data_path);
  493. espeak_ng_ERROR_CONTEXT context = NULL;
  494. espeak_ng_STATUS result = espeak_ng_CompileIntonation(stdout, &context);
  495. if (result != ENS_OK) {
  496. espeak_ng_PrintStatusCodeMessage(result, stderr, context);
  497. espeak_ng_ClearErrorContext(&context);
  498. return EXIT_FAILURE;
  499. }
  500. return EXIT_SUCCESS;
  501. }
  502. case 0x110: // --compile-phonemes
  503. {
  504. espeak_ng_InitializePath(data_path);
  505. espeak_ng_ERROR_CONTEXT context = NULL;
  506. espeak_ng_STATUS result;
  507. if (optarg2) {
  508. result = espeak_ng_CompilePhonemeDataPath(22050, optarg2, NULL, stdout, &context);
  509. } else {
  510. result = espeak_ng_CompilePhonemeData(22050, stdout, &context);
  511. }
  512. if (result != ENS_OK) {
  513. espeak_ng_PrintStatusCodeMessage(result, stderr, context);
  514. espeak_ng_ClearErrorContext(&context);
  515. return EXIT_FAILURE;
  516. }
  517. return EXIT_SUCCESS;
  518. }
  519. default:
  520. exit(0);
  521. }
  522. }
  523. espeak_ng_InitializePath(data_path);
  524. espeak_ng_ERROR_CONTEXT context = NULL;
  525. espeak_ng_STATUS result = espeak_ng_Initialize(&context);
  526. if (result != ENS_OK) {
  527. espeak_ng_PrintStatusCodeMessage(result, stderr, context);
  528. espeak_ng_ClearErrorContext(&context);
  529. exit(1);
  530. }
  531. if (option_waveout || quiet) {
  532. // writing to a file (or no output), we can use synchronous mode
  533. result = espeak_ng_InitializeOutput(ENOUTPUT_MODE_SYNCHRONOUS, 0, devicename[0] ? devicename : NULL);
  534. samplerate = espeak_ng_GetSampleRate();
  535. samples_split = samplerate * samples_split_seconds;
  536. espeak_SetSynthCallback(SynthCallback);
  537. if (samples_split) {
  538. char *extn;
  539. extn = strrchr(wavefile, '.');
  540. if ((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4)) {
  541. strcpy(filetype, extn);
  542. *extn = 0;
  543. }
  544. }
  545. } else {
  546. // play the sound output
  547. result = espeak_ng_InitializeOutput(PLAYBACK_MODE, 0, devicename[0] ? devicename : NULL);
  548. samplerate = espeak_ng_GetSampleRate();
  549. }
  550. if (result != ENS_OK) {
  551. espeak_ng_PrintStatusCodeMessage(result, stderr, NULL);
  552. exit(EXIT_FAILURE);
  553. }
  554. if (voicename[0] == 0)
  555. strcpy(voicename, ESPEAKNG_DEFAULT_VOICE);
  556. result = espeak_ng_SetVoiceByName(voicename);
  557. if (result != ENS_OK) {
  558. memset(&voice_select, 0, sizeof(voice_select));
  559. voice_select.languages = voicename;
  560. result = espeak_ng_SetVoiceByProperties(&voice_select);
  561. if (result != ENS_OK) {
  562. espeak_ng_PrintStatusCodeMessage(result, stderr, NULL);
  563. exit(EXIT_FAILURE);
  564. }
  565. }
  566. if (flag_compile) {
  567. // This must be done after the voice is set
  568. espeak_ng_ERROR_CONTEXT context = NULL;
  569. espeak_ng_STATUS result = espeak_ng_CompileDictionary("", NULL, stderr, flag_compile & 0x1, &context);
  570. if (result != ENS_OK) {
  571. espeak_ng_PrintStatusCodeMessage(result, stderr, context);
  572. espeak_ng_ClearErrorContext(&context);
  573. return EXIT_FAILURE;
  574. }
  575. return EXIT_SUCCESS;
  576. }
  577. // set any non-default values of parameters. This must be done after espeak_Initialize()
  578. if (speed > 0)
  579. espeak_SetParameter(espeakRATE, speed, 0);
  580. if (volume >= 0)
  581. espeak_SetParameter(espeakVOLUME, volume, 0);
  582. if (pitch >= 0)
  583. espeak_SetParameter(espeakPITCH, pitch, 0);
  584. if (option_capitals >= 0)
  585. espeak_SetParameter(espeakCAPITALS, option_capitals, 0);
  586. if (option_punctuation >= 0)
  587. espeak_SetParameter(espeakPUNCTUATION, option_punctuation, 0);
  588. if (wordgap >= 0)
  589. espeak_SetParameter(espeakWORDGAP, wordgap, 0);
  590. if (option_linelength > 0)
  591. espeak_SetParameter(espeakLINELENGTH, option_linelength, 0);
  592. if (option_punctuation == 2)
  593. espeak_SetPunctuationList(option_punctlist);
  594. espeak_SetPhonemeTrace(phoneme_options | (phonemes_separator << 8), f_phonemes_out);
  595. if (filename[0] == 0) {
  596. if ((optind < argc) && (flag_stdin == 0)) {
  597. // there's a non-option parameter, and no -f or --stdin
  598. // use it as text
  599. p_text = argv[optind];
  600. } else {
  601. f_text = stdin;
  602. if (flag_stdin == 0)
  603. flag_stdin = 2;
  604. }
  605. } else {
  606. struct stat st;
  607. if (stat(filename, &st) != 0) {
  608. fprintf(stderr, "Failed to stat() file '%s'\n", filename);
  609. exit(EXIT_FAILURE);
  610. }
  611. filesize = GetFileLength(filename);
  612. f_text = fopen(filename, "r");
  613. if (f_text == NULL) {
  614. fprintf(stderr, "Failed to read file '%s'\n", filename);
  615. exit(EXIT_FAILURE);
  616. }
  617. if (S_ISFIFO(st.st_mode)) {
  618. flag_stdin = 2;
  619. }
  620. }
  621. if (p_text != NULL) {
  622. int size;
  623. size = strlen(p_text);
  624. espeak_Synth(p_text, size+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL);
  625. } else if (flag_stdin) {
  626. size_t max = 1000;
  627. if ((p_text = (char *)malloc(max)) == NULL) {
  628. espeak_ng_PrintStatusCodeMessage(ENOMEM, stderr, NULL);
  629. exit(EXIT_FAILURE);
  630. }
  631. if (flag_stdin == 2) {
  632. // line by line input on stdin or from FIFO
  633. while (fgets(p_text, max, f_text) != NULL) {
  634. p_text[max-1] = 0;
  635. espeak_Synth(p_text, max, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL);
  636. // Allow subprocesses to use the audio data through pipes.
  637. fflush(stdout);
  638. }
  639. if (f_text != stdin) {
  640. fclose(f_text);
  641. }
  642. } else {
  643. // bulk input on stdin
  644. ix = 0;
  645. while (true) {
  646. if ((c = fgetc(stdin)) == EOF)
  647. break;
  648. p_text[ix++] = (char)c;
  649. if (ix >= (max-1)) {
  650. char *new_text = NULL;
  651. if (max <= SIZE_MAX - 1000) {
  652. max += 1000;
  653. new_text = (char *)realloc(p_text, max);
  654. }
  655. if (new_text == NULL) {
  656. free(p_text);
  657. espeak_ng_PrintStatusCodeMessage(ENOMEM, stderr, NULL);
  658. exit(EXIT_FAILURE);
  659. }
  660. p_text = new_text;
  661. }
  662. }
  663. if (ix > 0) {
  664. p_text[ix-1] = 0;
  665. espeak_Synth(p_text, ix+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL);
  666. }
  667. }
  668. free(p_text);
  669. } else if (f_text != NULL) {
  670. if ((p_text = (char *)malloc(filesize+1)) == NULL) {
  671. espeak_ng_PrintStatusCodeMessage(ENOMEM, stderr, NULL);
  672. exit(EXIT_FAILURE);
  673. }
  674. fread(p_text, 1, filesize, f_text);
  675. p_text[filesize] = 0;
  676. espeak_Synth(p_text, filesize+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL);
  677. fclose(f_text);
  678. free(p_text);
  679. }
  680. result = espeak_ng_Synchronize();
  681. if (result != ENS_OK) {
  682. espeak_ng_PrintStatusCodeMessage(result, stderr, NULL);
  683. exit(EXIT_FAILURE);
  684. }
  685. if (f_phonemes_out != stdout)
  686. fclose(f_phonemes_out);
  687. CloseWavFile();
  688. espeak_ng_Terminate();
  689. return 0;
  690. }