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.

speak-ng.c 19KB

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