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

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