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

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