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.

mbrowrap.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * mbrowrap -- A wrapper library around the mbrola binary
  3. * providing a subset of the API from the Windows mbrola DLL.
  4. *
  5. * Copyright (C) 2005 to 2013 by Jonathan Duddington
  6. * Copyright (C) 2010 by Nicolas Pitre <[email protected]>
  7. * Copyright (C) 2013-2016 Reece H. Dunn
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include "config.h"
  20. #if defined(_WIN32) || defined(_WIN64)
  21. #include <windows.h>
  22. #endif
  23. #include "mbrowrap.h"
  24. int (WINAPI *init_MBR)(char *voice_path);
  25. void (WINAPI *close_MBR)(void);
  26. void (WINAPI *reset_MBR)(void);
  27. int (WINAPI *read_MBR)(short *buffer, int nb_samples);
  28. int (WINAPI *write_MBR)(char *data);
  29. int (WINAPI *flush_MBR)(void);
  30. int (WINAPI *getFreq_MBR)(void);
  31. void (WINAPI *setVolumeRatio_MBR)(float value);
  32. char * (WINAPI *lastErrorStr_MBR)(char *buffer, int bufsize);
  33. void (WINAPI *setNoError_MBR)(int no_error);
  34. #if defined(_WIN32) || defined(_WIN64)
  35. HINSTANCE hinstDllMBR = NULL;
  36. BOOL load_MBR()
  37. {
  38. if (hinstDllMBR != NULL)
  39. return TRUE; // already loaded
  40. if ((hinstDllMBR = LoadLibraryA("mbrola.dll")) == 0)
  41. return FALSE;
  42. init_MBR = (void *)GetProcAddress(hinstDllMBR, "init_MBR");
  43. write_MBR = (void *)GetProcAddress(hinstDllMBR, "write_MBR");
  44. flush_MBR = (void *)GetProcAddress(hinstDllMBR, "flush_MBR");
  45. read_MBR = (void *)GetProcAddress(hinstDllMBR, "read_MBR");
  46. close_MBR = (void *)GetProcAddress(hinstDllMBR, "close_MBR");
  47. reset_MBR = (void *)GetProcAddress(hinstDllMBR, "reset_MBR");
  48. lastErrorStr_MBR = (void *)GetProcAddress(hinstDllMBR, "lastErrorStr_MBR");
  49. setNoError_MBR = (void *)GetProcAddress(hinstDllMBR, "setNoError_MBR");
  50. setVolumeRatio_MBR = (void *)GetProcAddress(hinstDllMBR, "setVolumeRatio_MBR");
  51. return TRUE;
  52. }
  53. void unload_MBR()
  54. {
  55. if (hinstDllMBR) {
  56. FreeLibrary(hinstDllMBR);
  57. hinstDllMBR = NULL;
  58. }
  59. }
  60. #else
  61. #include <errno.h>
  62. #include <fcntl.h>
  63. #include <poll.h>
  64. #include <signal.h>
  65. #include <stdarg.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include <sys/types.h>
  70. #include <sys/wait.h>
  71. #include <unistd.h>
  72. #include <espeak-ng/espeak_ng.h>
  73. #include "speech.h"
  74. /*
  75. * mbrola instance parameters
  76. */
  77. enum mbr_state {
  78. MBR_INACTIVE = 0,
  79. MBR_IDLE,
  80. MBR_NEWDATA,
  81. MBR_AUDIO,
  82. MBR_WEDGED
  83. };
  84. static enum mbr_state mbr_state;
  85. static char *mbr_voice_path;
  86. static int mbr_cmd_fd, mbr_audio_fd, mbr_error_fd, mbr_proc_stat;
  87. static pid_t mbr_pid;
  88. static int mbr_samplerate;
  89. static float mbr_volume = 1.0;
  90. static char mbr_errorbuf[160];
  91. struct datablock {
  92. struct datablock *next;
  93. int done;
  94. int size;
  95. char buffer[1]; // 1 or more, dynamically allocated
  96. };
  97. static struct datablock *mbr_pending_data_head, *mbr_pending_data_tail;
  98. /*
  99. * Private support code.
  100. */
  101. static void err(const char *errmsg, ...)
  102. {
  103. va_list params;
  104. va_start(params, errmsg);
  105. vsnprintf(mbr_errorbuf, sizeof(mbr_errorbuf), errmsg, params);
  106. va_end(params);
  107. fprintf(stderr, "mbrowrap error: %s\n", mbr_errorbuf);
  108. }
  109. static int create_pipes(int p1[2], int p2[2], int p3[2])
  110. {
  111. int error;
  112. if (pipe(p1) != -1) {
  113. if (pipe(p2) != -1) {
  114. if (pipe(p3) != -1)
  115. return 0;
  116. else
  117. error = errno;
  118. close(p2[0]);
  119. close(p2[1]);
  120. } else
  121. error = errno;
  122. close(p1[0]);
  123. close(p1[1]);
  124. } else
  125. error = errno;
  126. err("pipe(): %s", strerror(error));
  127. return -1;
  128. }
  129. static void close_pipes(int p1[2], int p2[2], int p3[2])
  130. {
  131. close(p1[0]);
  132. close(p1[1]);
  133. close(p2[0]);
  134. close(p2[1]);
  135. close(p3[0]);
  136. close(p3[1]);
  137. }
  138. static int start_mbrola(const char *voice_path)
  139. {
  140. int error, p_stdin[2], p_stdout[2], p_stderr[2];
  141. ssize_t written;
  142. char charbuf[20];
  143. if (mbr_state != MBR_INACTIVE) {
  144. err("mbrola init request when already initialized");
  145. return -1;
  146. }
  147. error = create_pipes(p_stdin, p_stdout, p_stderr);
  148. if (error)
  149. return -1;
  150. mbr_pid = fork();
  151. if (mbr_pid == -1) {
  152. error = errno;
  153. close_pipes(p_stdin, p_stdout, p_stderr);
  154. err("fork(): %s", strerror(error));
  155. return -1;
  156. }
  157. if (mbr_pid == 0) {
  158. int i;
  159. if (dup2(p_stdin[0], 0) == -1 ||
  160. dup2(p_stdout[1], 1) == -1 ||
  161. dup2(p_stderr[1], 2) == -1) {
  162. snprintf(mbr_errorbuf, sizeof(mbr_errorbuf),
  163. "dup2(): %s\n", strerror(errno));
  164. written = write(p_stderr[1], mbr_errorbuf, strlen(mbr_errorbuf));
  165. (void)written; // suppress 'variable not used' warning
  166. _exit(1);
  167. }
  168. for (i = p_stderr[1]; i > 2; i--)
  169. close(i);
  170. signal(SIGHUP, SIG_IGN);
  171. signal(SIGINT, SIG_IGN);
  172. signal(SIGQUIT, SIG_IGN);
  173. signal(SIGTERM, SIG_IGN);
  174. snprintf(charbuf, sizeof(charbuf), "%g", mbr_volume);
  175. execlp("mbrola", "mbrola", "-e", "-v", charbuf,
  176. voice_path, "-", "-.wav", (char *)NULL);
  177. /* if execution reaches this point then the exec() failed */
  178. snprintf(mbr_errorbuf, sizeof(mbr_errorbuf),
  179. "mbrola: %s\n", strerror(errno));
  180. written = write(2, mbr_errorbuf, strlen(mbr_errorbuf));
  181. (void)written; // suppress 'variable not used' warning
  182. _exit(1);
  183. }
  184. snprintf(charbuf, sizeof(charbuf), "/proc/%d/stat", mbr_pid);
  185. mbr_proc_stat = open(charbuf, O_RDONLY);
  186. if (mbr_proc_stat == -1) {
  187. error = errno;
  188. close_pipes(p_stdin, p_stdout, p_stderr);
  189. waitpid(mbr_pid, NULL, 0);
  190. mbr_pid = 0;
  191. err("/proc is unaccessible: %s", strerror(error));
  192. return -1;
  193. }
  194. signal(SIGPIPE, SIG_IGN);
  195. if (fcntl(p_stdin[1], F_SETFL, O_NONBLOCK) == -1 ||
  196. fcntl(p_stdout[0], F_SETFL, O_NONBLOCK) == -1 ||
  197. fcntl(p_stderr[0], F_SETFL, O_NONBLOCK) == -1) {
  198. error = errno;
  199. close_pipes(p_stdin, p_stdout, p_stderr);
  200. waitpid(mbr_pid, NULL, 0);
  201. mbr_pid = 0;
  202. err("fcntl(): %s", strerror(error));
  203. return -1;
  204. }
  205. mbr_cmd_fd = p_stdin[1];
  206. mbr_audio_fd = p_stdout[0];
  207. mbr_error_fd = p_stderr[0];
  208. close(p_stdin[0]);
  209. close(p_stdout[1]);
  210. close(p_stderr[1]);
  211. mbr_state = MBR_IDLE;
  212. return 0;
  213. }
  214. static void stop_mbrola(void)
  215. {
  216. if (mbr_state == MBR_INACTIVE)
  217. return;
  218. close(mbr_proc_stat);
  219. close(mbr_cmd_fd);
  220. close(mbr_audio_fd);
  221. close(mbr_error_fd);
  222. if (mbr_pid) {
  223. kill(mbr_pid, SIGTERM);
  224. waitpid(mbr_pid, NULL, 0);
  225. mbr_pid = 0;
  226. }
  227. mbr_state = MBR_INACTIVE;
  228. }
  229. static void free_pending_data(void)
  230. {
  231. struct datablock *p, *head = mbr_pending_data_head;
  232. while (head) {
  233. p = head;
  234. head = head->next;
  235. free(p);
  236. }
  237. mbr_pending_data_head = NULL;
  238. mbr_pending_data_tail = NULL;
  239. }
  240. static int mbrola_died(void)
  241. {
  242. pid_t pid;
  243. int status, len;
  244. const char *msg;
  245. char msgbuf[80];
  246. pid = waitpid(mbr_pid, &status, WNOHANG);
  247. if (!pid)
  248. msg = "mbrola closed stderr and did not exit";
  249. else if (pid != mbr_pid)
  250. msg = "waitpid() is confused";
  251. else {
  252. mbr_pid = 0;
  253. if (WIFSIGNALED(status)) {
  254. int sig = WTERMSIG(status);
  255. snprintf(msgbuf, sizeof(msgbuf),
  256. "mbrola died by signal %d", sig);
  257. msg = msgbuf;
  258. } else if (WIFEXITED(status)) {
  259. int exst = WEXITSTATUS(status);
  260. snprintf(msgbuf, sizeof(msgbuf),
  261. "mbrola exited with status %d", exst);
  262. msg = msgbuf;
  263. } else
  264. msg = "mbrola died and wait status is weird";
  265. }
  266. fprintf(stderr, "mbrowrap error: %s\n", msg);
  267. len = strlen(mbr_errorbuf);
  268. if (!len)
  269. snprintf(mbr_errorbuf, sizeof(mbr_errorbuf), "%s", msg);
  270. else
  271. snprintf(mbr_errorbuf + len, sizeof(mbr_errorbuf) - len,
  272. ", (%s)", msg);
  273. return -1;
  274. }
  275. static int mbrola_has_errors(void)
  276. {
  277. int result;
  278. char buffer[256];
  279. char *buf_ptr, *lf;
  280. buf_ptr = buffer;
  281. for (;;) {
  282. result = read(mbr_error_fd, buf_ptr,
  283. sizeof(buffer) - (buf_ptr - buffer) - 1);
  284. if (result == -1) {
  285. if (errno == EAGAIN)
  286. return 0;
  287. err("read(error): %s", strerror(errno));
  288. return -1;
  289. }
  290. if (result == 0) {
  291. // EOF on stderr, assume mbrola died.
  292. return mbrola_died();
  293. }
  294. buf_ptr[result] = 0;
  295. for (; (lf = strchr(buf_ptr, '\n')); buf_ptr = lf + 1) {
  296. // inhibit the reset signal messages
  297. if (strncmp(buf_ptr, "Got a reset signal", 18) == 0 ||
  298. strncmp(buf_ptr, "Input Flush Signal", 18) == 0)
  299. continue;
  300. *lf = 0;
  301. fprintf(stderr, "mbrola: %s\n", buf_ptr);
  302. // is this the last line?
  303. if (lf == &buf_ptr[result - 1]) {
  304. snprintf(mbr_errorbuf, sizeof(mbr_errorbuf),
  305. "%s", buf_ptr);
  306. // don't consider this fatal at this point
  307. return 0;
  308. }
  309. }
  310. memmove(buffer, buf_ptr, result);
  311. buf_ptr = buffer + result;
  312. }
  313. }
  314. static int send_to_mbrola(const char *cmd)
  315. {
  316. ssize_t result;
  317. int len;
  318. if (!mbr_pid)
  319. return -1;
  320. len = strlen(cmd);
  321. result = write(mbr_cmd_fd, cmd, len);
  322. if (result == -1) {
  323. int error = errno;
  324. if (error == EPIPE && mbrola_has_errors())
  325. return -1;
  326. else if (error == EAGAIN)
  327. result = 0;
  328. else {
  329. err("write(): %s", strerror(error));
  330. return -1;
  331. }
  332. }
  333. if (result != len) {
  334. struct datablock *data;
  335. data = (struct datablock *)malloc(sizeof(*data) + len - result);
  336. if (data) {
  337. data->next = NULL;
  338. data->done = 0;
  339. data->size = len - result;
  340. memcpy(data->buffer, cmd + result, len - result);
  341. result = len;
  342. if (!mbr_pending_data_head)
  343. mbr_pending_data_head = data;
  344. else
  345. mbr_pending_data_tail->next = data;
  346. mbr_pending_data_tail = data;
  347. }
  348. }
  349. return result;
  350. }
  351. static int mbrola_is_idle(void)
  352. {
  353. char *p;
  354. char buffer[20]; // looking for "12345 (mbrola) S" so 20 is plenty
  355. // look in /proc to determine if mbrola is still running or sleeping
  356. if (lseek(mbr_proc_stat, 0, SEEK_SET) != 0)
  357. return 0;
  358. if (read(mbr_proc_stat, buffer, sizeof(buffer)) != sizeof(buffer))
  359. return 0;
  360. p = (char *)memchr(buffer, ')', sizeof(buffer));
  361. if (!p || (unsigned)(p - buffer) >= sizeof(buffer) - 2)
  362. return 0;
  363. return p[1] == ' ' && p[2] == 'S';
  364. }
  365. static ssize_t receive_from_mbrola(void *buffer, size_t bufsize)
  366. {
  367. int result, wait = 1;
  368. size_t cursize = 0;
  369. if (!mbr_pid)
  370. return -1;
  371. do {
  372. struct pollfd pollfd[3];
  373. nfds_t nfds = 0;
  374. int idle;
  375. pollfd[0].fd = mbr_audio_fd;
  376. pollfd[0].events = POLLIN;
  377. nfds++;
  378. pollfd[1].fd = mbr_error_fd;
  379. pollfd[1].events = POLLIN;
  380. nfds++;
  381. if (mbr_pending_data_head) {
  382. pollfd[2].fd = mbr_cmd_fd;
  383. pollfd[2].events = POLLOUT;
  384. nfds++;
  385. }
  386. idle = mbrola_is_idle();
  387. result = poll(pollfd, nfds, idle ? 0 : wait);
  388. if (result == -1) {
  389. err("poll(): %s", strerror(errno));
  390. return -1;
  391. }
  392. if (result == 0) {
  393. if (idle) {
  394. mbr_state = MBR_IDLE;
  395. break;
  396. } else {
  397. if (wait >= 5000 * (4-1)/4) {
  398. mbr_state = MBR_WEDGED;
  399. err("mbrola process is stalled");
  400. break;
  401. } else {
  402. wait *= 4;
  403. continue;
  404. }
  405. }
  406. }
  407. wait = 1;
  408. if (pollfd[1].revents && mbrola_has_errors())
  409. return -1;
  410. if (mbr_pending_data_head && pollfd[2].revents) {
  411. struct datablock *head = mbr_pending_data_head;
  412. char *data = head->buffer + head->done;
  413. int left = head->size - head->done;
  414. result = write(mbr_cmd_fd, data, left);
  415. if (result == -1) {
  416. int error = errno;
  417. if (error == EPIPE && mbrola_has_errors())
  418. return -1;
  419. err("write(): %s", strerror(error));
  420. return -1;
  421. }
  422. if (result != left)
  423. head->done += result;
  424. else {
  425. mbr_pending_data_head = head->next;
  426. free(head);
  427. if (!mbr_pending_data_head)
  428. mbr_pending_data_tail = NULL;
  429. else
  430. continue;
  431. }
  432. }
  433. if (pollfd[0].revents) {
  434. char *curpos = (char *)buffer + cursize;
  435. size_t space = bufsize - cursize;
  436. ssize_t obtained = read(mbr_audio_fd, curpos, space);
  437. if (obtained == -1) {
  438. err("read(): %s", strerror(errno));
  439. return -1;
  440. }
  441. cursize += obtained;
  442. mbr_state = MBR_AUDIO;
  443. }
  444. } while (cursize < bufsize);
  445. return cursize;
  446. }
  447. /*
  448. * API functions.
  449. */
  450. int init_mbrola(char *voice_path)
  451. {
  452. int error, result;
  453. unsigned char wavhdr[45];
  454. error = start_mbrola(voice_path);
  455. if (error)
  456. return -1;
  457. result = send_to_mbrola("#\n");
  458. if (result != 2) {
  459. stop_mbrola();
  460. return -1;
  461. }
  462. // we should actually be getting only 44 bytes
  463. result = receive_from_mbrola(wavhdr, 45);
  464. if (result != 44) {
  465. if (result >= 0)
  466. err("unable to get .wav header from mbrola");
  467. stop_mbrola();
  468. return -1;
  469. }
  470. // parse wavhdr to get mbrola voice samplerate
  471. if (memcmp(wavhdr, "RIFF", 4) != 0 ||
  472. memcmp(wavhdr+8, "WAVEfmt ", 8) != 0) {
  473. err("mbrola did not return a .wav header");
  474. stop_mbrola();
  475. return -1;
  476. }
  477. mbr_samplerate = wavhdr[24] + (wavhdr[25]<<8) +
  478. (wavhdr[26]<<16) + (wavhdr[27]<<24);
  479. // remember the voice path for setVolumeRatio_MBR()
  480. if (mbr_voice_path != voice_path) {
  481. free(mbr_voice_path);
  482. mbr_voice_path = strdup(voice_path);
  483. }
  484. return 0;
  485. }
  486. void close_mbrola(void)
  487. {
  488. stop_mbrola();
  489. free_pending_data();
  490. free(mbr_voice_path);
  491. mbr_voice_path = NULL;
  492. mbr_volume = 1.0;
  493. }
  494. void reset_mbrola(void)
  495. {
  496. int result, success = 1;
  497. char dummybuf[4096];
  498. if (mbr_state == MBR_IDLE)
  499. return;
  500. if (!mbr_pid)
  501. return;
  502. if (kill(mbr_pid, SIGUSR1) == -1)
  503. success = 0;
  504. free_pending_data();
  505. result = write(mbr_cmd_fd, "\n#\n", 3);
  506. if (result != 3)
  507. success = 0;
  508. do {
  509. result = read(mbr_audio_fd, dummybuf, sizeof(dummybuf));
  510. } while (result > 0);
  511. if (result != -1 || errno != EAGAIN)
  512. success = 0;
  513. if (!mbrola_has_errors() && success)
  514. mbr_state = MBR_IDLE;
  515. }
  516. int read_mbrola(short *buffer, int nb_samples)
  517. {
  518. int result = receive_from_mbrola(buffer, nb_samples * 2);
  519. if (result > 0)
  520. result /= 2;
  521. return result;
  522. }
  523. int write_mbrola(char *data)
  524. {
  525. mbr_state = MBR_NEWDATA;
  526. return send_to_mbrola(data);
  527. }
  528. int flush_mbrola(void)
  529. {
  530. return send_to_mbrola("\n#\n") == 3;
  531. }
  532. int getFreq_mbrola(void)
  533. {
  534. return mbr_samplerate;
  535. }
  536. void setVolumeRatio_mbrola(float value)
  537. {
  538. if (value == mbr_volume)
  539. return;
  540. mbr_volume = value;
  541. if (mbr_state != MBR_IDLE)
  542. return;
  543. /*
  544. * We have no choice but to kill and restart mbrola with
  545. * the new argument here.
  546. */
  547. stop_mbrola();
  548. init_MBR(mbr_voice_path);
  549. }
  550. char *lastErrorStr_mbrola(char *buffer, int bufsize)
  551. {
  552. if (mbr_pid)
  553. mbrola_has_errors();
  554. snprintf(buffer, bufsize, "%s", mbr_errorbuf);
  555. return buffer;
  556. }
  557. void setNoError_mbrola(int no_error)
  558. {
  559. (void)no_error; // unused
  560. }
  561. BOOL load_MBR(void)
  562. {
  563. init_MBR = init_mbrola;
  564. close_MBR = close_mbrola;
  565. reset_MBR = reset_mbrola;
  566. read_MBR = read_mbrola;
  567. write_MBR = write_mbrola;
  568. flush_MBR = flush_mbrola;
  569. getFreq_MBR = getFreq_mbrola;
  570. setVolumeRatio_MBR = setVolumeRatio_mbrola;
  571. lastErrorStr_MBR = lastErrorStr_mbrola;
  572. setNoError_MBR = setNoError_mbrola;
  573. return 1;
  574. }
  575. void unload_MBR(void)
  576. {
  577. }
  578. #endif