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.

fifo.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (C) 2007, Gilles Casse <[email protected]>
  3. * Copyright (C) 2013-2016 Reece H. Dunn
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  17. */
  18. // This source file is only used for asynchronous modes
  19. #include "config.h"
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <pthread.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #include <unistd.h>
  30. #include <espeak-ng/espeak_ng.h>
  31. #include "speech.h"
  32. #include "espeak_command.h"
  33. #include "fifo.h"
  34. #include "event.h"
  35. #if USE_ASYNC
  36. // my_mutex: protects my_thread_is_talking,
  37. // my_stop_is_required, and the command fifo
  38. static pthread_mutex_t my_mutex;
  39. static bool my_command_is_running = false;
  40. static pthread_cond_t my_cond_command_is_running;
  41. static bool my_stop_is_required = false;
  42. static bool my_terminate_is_required = 0;
  43. // my_thread: reads commands from the fifo, and runs them.
  44. static pthread_t my_thread;
  45. static pthread_cond_t my_cond_start_is_required;
  46. static bool my_start_is_required = false;
  47. static pthread_cond_t my_cond_stop_is_acknowledged;
  48. static bool my_stop_is_acknowledged = false;
  49. static void *say_thread(void *);
  50. static espeak_ng_STATUS push(t_espeak_command *the_command);
  51. static t_espeak_command *pop(void);
  52. static void init(int process_parameters);
  53. static int node_counter = 0;
  54. static bool thread_inited = false;
  55. enum {
  56. MAX_NODE_COUNTER = 400,
  57. INACTIVITY_TIMEOUT = 50, // in ms, check that the stream is inactive
  58. MAX_INACTIVITY_CHECK = 2
  59. };
  60. void fifo_init(void)
  61. {
  62. // security
  63. pthread_mutex_init(&my_mutex, (const pthread_mutexattr_t *)NULL);
  64. init(0);
  65. int a_status;
  66. a_status = pthread_cond_init(&my_cond_command_is_running, NULL);
  67. assert(-1 != a_status);
  68. a_status = pthread_cond_init(&my_cond_start_is_required, NULL);
  69. assert(-1 != a_status);
  70. a_status = pthread_cond_init(&my_cond_stop_is_acknowledged, NULL);
  71. assert(-1 != a_status);
  72. pthread_attr_t a_attrib;
  73. if (pthread_attr_init(&a_attrib)
  74. || pthread_attr_setdetachstate(&a_attrib, PTHREAD_CREATE_JOINABLE)
  75. || pthread_create(&my_thread,
  76. &a_attrib,
  77. say_thread,
  78. (void *)NULL)) {
  79. assert(0);
  80. }
  81. thread_inited = true;
  82. pthread_attr_destroy(&a_attrib);
  83. // leave once the thread is actually started
  84. a_status = pthread_mutex_lock(&my_mutex);
  85. assert(-1 != a_status);
  86. (void)a_status;
  87. while (my_stop_is_acknowledged == false) {
  88. while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR)
  89. ;
  90. }
  91. my_stop_is_acknowledged = false;
  92. pthread_mutex_unlock(&my_mutex);
  93. }
  94. espeak_ng_STATUS fifo_add_command(t_espeak_command *the_command)
  95. {
  96. espeak_ng_STATUS status;
  97. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  98. return status;
  99. if ((status = push(the_command)) != ENS_OK) {
  100. pthread_mutex_unlock(&my_mutex);
  101. return status;
  102. }
  103. my_start_is_required = true;
  104. pthread_cond_signal(&my_cond_start_is_required);
  105. while (my_start_is_required && !my_command_is_running) {
  106. if((status = pthread_cond_wait(&my_cond_command_is_running, &my_mutex)) != ENS_OK && errno != EINTR) {
  107. pthread_mutex_unlock(&my_mutex);
  108. return status;
  109. }
  110. }
  111. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  112. return status;
  113. return ENS_OK;
  114. }
  115. espeak_ng_STATUS fifo_add_commands(t_espeak_command *command1, t_espeak_command *command2)
  116. {
  117. espeak_ng_STATUS status;
  118. if (!thread_inited) {
  119. return ENS_NOT_INITIALIZED;
  120. }
  121. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  122. return status;
  123. if (node_counter+1 >= MAX_NODE_COUNTER) {
  124. pthread_mutex_unlock(&my_mutex);
  125. return ENS_FIFO_BUFFER_FULL;
  126. }
  127. if ((status = push(command1)) != ENS_OK) {
  128. pthread_mutex_unlock(&my_mutex);
  129. return status;
  130. }
  131. if ((status = push(command2)) != ENS_OK) {
  132. pthread_mutex_unlock(&my_mutex);
  133. return status;
  134. }
  135. my_start_is_required = true;
  136. pthread_cond_signal(&my_cond_start_is_required);
  137. while (my_start_is_required && !my_command_is_running) {
  138. if((status = pthread_cond_wait(&my_cond_command_is_running, &my_mutex)) != ENS_OK && errno != EINTR) {
  139. pthread_mutex_unlock(&my_mutex);
  140. return status;
  141. }
  142. }
  143. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  144. return status;
  145. return ENS_OK;
  146. }
  147. espeak_ng_STATUS fifo_stop(void)
  148. {
  149. if (!thread_inited) return ENS_OK;
  150. espeak_ng_STATUS status;
  151. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  152. return status;
  153. bool a_command_is_running = false;
  154. if (my_command_is_running) {
  155. a_command_is_running = true;
  156. my_stop_is_required = true;
  157. my_stop_is_acknowledged = false;
  158. }
  159. if (a_command_is_running) {
  160. while (my_stop_is_acknowledged == false) {
  161. while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR)
  162. continue; // Restart when interrupted by handler
  163. }
  164. }
  165. my_stop_is_required = false;
  166. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  167. return status;
  168. return ENS_OK;
  169. }
  170. int fifo_is_busy(void)
  171. {
  172. if (!thread_inited) return false;
  173. pthread_mutex_lock(&my_mutex);
  174. bool running = my_command_is_running;
  175. pthread_mutex_unlock(&my_mutex);
  176. return running;
  177. }
  178. static int sleep_until_start_request_or_inactivity(void)
  179. {
  180. int a_start_is_required = false;
  181. // Wait for the start request (my_cond_start_is_required).
  182. // Besides this, if the audio stream is still busy,
  183. // check from time to time its end.
  184. // The end of the stream is confirmed by several checks
  185. // for filtering underflow.
  186. //
  187. int i = 0;
  188. int err = pthread_mutex_lock(&my_mutex);
  189. assert(err != -1);
  190. while ((i <= MAX_INACTIVITY_CHECK) && !a_start_is_required) {
  191. i++;
  192. struct timespec ts;
  193. clock_gettime2(&ts);
  194. add_time_in_ms(&ts, INACTIVITY_TIMEOUT);
  195. while ((err = pthread_cond_timedwait(&my_cond_start_is_required, &my_mutex, &ts)) == -1
  196. && errno == EINTR)
  197. continue;
  198. if (err == 0)
  199. a_start_is_required = true;
  200. }
  201. pthread_mutex_unlock(&my_mutex);
  202. return a_start_is_required;
  203. }
  204. static espeak_ng_STATUS close_stream(void)
  205. {
  206. espeak_ng_STATUS status = pthread_mutex_lock(&my_mutex);
  207. if (status != ENS_OK)
  208. return status;
  209. bool a_stop_is_required = my_stop_is_required;
  210. if (!a_stop_is_required)
  211. my_command_is_running = true;
  212. status = pthread_mutex_unlock(&my_mutex);
  213. if (!a_stop_is_required) {
  214. int a_status = pthread_mutex_lock(&my_mutex);
  215. if (status == ENS_OK)
  216. status = a_status;
  217. my_command_is_running = false;
  218. a_stop_is_required = my_stop_is_required;
  219. a_status = pthread_mutex_unlock(&my_mutex);
  220. if (status == ENS_OK)
  221. status = a_status;
  222. if (a_stop_is_required) {
  223. // cancel the audio early, to be more responsive when using eSpeak NG
  224. // for audio.
  225. cancel_audio();
  226. // acknowledge the stop request
  227. if((a_status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  228. return a_status;
  229. my_stop_is_acknowledged = true;
  230. a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged);
  231. if(a_status != ENS_OK)
  232. return a_status;
  233. a_status = pthread_mutex_unlock(&my_mutex);
  234. if (status == ENS_OK)
  235. status = a_status;
  236. }
  237. }
  238. return status;
  239. }
  240. static void *say_thread(void *p)
  241. {
  242. (void)p; // unused
  243. int a_status;
  244. // announce that thread is started
  245. a_status = pthread_mutex_lock(&my_mutex);
  246. assert(-1 != a_status);
  247. my_stop_is_acknowledged = true;
  248. a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged);
  249. assert(-1 != a_status);
  250. a_status = pthread_mutex_unlock(&my_mutex);
  251. assert(-1 != a_status);
  252. bool look_for_inactivity = false;
  253. while (!my_terminate_is_required) {
  254. bool a_start_is_required = false;
  255. if (look_for_inactivity) {
  256. a_start_is_required = sleep_until_start_request_or_inactivity();
  257. if (!a_start_is_required)
  258. close_stream();
  259. }
  260. look_for_inactivity = true;
  261. a_status = pthread_mutex_lock(&my_mutex);
  262. assert(!a_status);
  263. if (!a_start_is_required) {
  264. while (my_start_is_required == false && my_terminate_is_required == false) {
  265. while ((pthread_cond_wait(&my_cond_start_is_required, &my_mutex) == -1) && errno == EINTR)
  266. continue; // Restart when interrupted by handler
  267. }
  268. }
  269. my_command_is_running = true;
  270. a_status = pthread_cond_broadcast(&my_cond_command_is_running);
  271. assert(-1 != a_status);
  272. a_status = pthread_mutex_unlock(&my_mutex);
  273. assert(-1 != a_status);
  274. while (my_command_is_running && !my_terminate_is_required) {
  275. a_status = pthread_mutex_lock(&my_mutex);
  276. assert(!a_status);
  277. t_espeak_command *a_command = (t_espeak_command *)pop();
  278. if (a_command == NULL) {
  279. my_command_is_running = false;
  280. a_status = pthread_mutex_unlock(&my_mutex);
  281. } else {
  282. my_start_is_required = false;
  283. if (my_stop_is_required)
  284. my_command_is_running = false;
  285. a_status = pthread_mutex_unlock(&my_mutex);
  286. if (my_command_is_running)
  287. process_espeak_command(a_command);
  288. delete_espeak_command(a_command);
  289. }
  290. }
  291. if (my_stop_is_required || my_terminate_is_required) {
  292. // no mutex required since the stop command is synchronous
  293. // and waiting for my_cond_stop_is_acknowledged
  294. init(1);
  295. a_status = pthread_mutex_lock(&my_mutex);
  296. assert(-1 != a_status);
  297. my_start_is_required = false;
  298. // acknowledge the stop request
  299. my_stop_is_acknowledged = true;
  300. a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged);
  301. assert(a_status != -1);
  302. pthread_mutex_unlock(&my_mutex);
  303. }
  304. // and wait for the next start
  305. }
  306. (void)a_status;
  307. return NULL;
  308. }
  309. int fifo_is_command_enabled(void)
  310. {
  311. return 0 == my_stop_is_required;
  312. }
  313. typedef struct t_node {
  314. t_espeak_command *data;
  315. struct t_node *next;
  316. } node;
  317. static node *head = NULL;
  318. static node *tail = NULL;
  319. static espeak_ng_STATUS push(t_espeak_command *the_command)
  320. {
  321. assert((!head && !tail) || (head && tail));
  322. if (the_command == NULL)
  323. return EINVAL;
  324. if (node_counter >= MAX_NODE_COUNTER)
  325. return ENS_FIFO_BUFFER_FULL;
  326. node *n = (node *)malloc(sizeof(node));
  327. if (n == NULL)
  328. return ENOMEM;
  329. if (head == NULL) {
  330. head = n;
  331. tail = n;
  332. } else {
  333. tail->next = n;
  334. tail = n;
  335. }
  336. tail->next = NULL;
  337. tail->data = the_command;
  338. node_counter++;
  339. the_command->state = CS_PENDING;
  340. return ENS_OK;
  341. }
  342. static t_espeak_command *pop(void)
  343. {
  344. t_espeak_command *the_command = NULL;
  345. assert((!head && !tail) || (head && tail));
  346. if (head != NULL) {
  347. node *n = head;
  348. the_command = n->data;
  349. head = n->next;
  350. free(n);
  351. node_counter--;
  352. }
  353. if (head == NULL)
  354. tail = NULL;
  355. return the_command;
  356. }
  357. static void init(int process_parameters)
  358. {
  359. t_espeak_command *c = NULL;
  360. c = pop();
  361. while (c != NULL) {
  362. if (process_parameters && (c->type == ET_PARAMETER || c->type == ET_VOICE_NAME || c->type == ET_VOICE_SPEC))
  363. process_espeak_command(c);
  364. delete_espeak_command(c);
  365. c = pop();
  366. }
  367. node_counter = 0;
  368. }
  369. void fifo_terminate(void)
  370. {
  371. if (!thread_inited) return;
  372. pthread_mutex_lock(&my_mutex);
  373. my_terminate_is_required = true;
  374. pthread_mutex_unlock(&my_mutex);
  375. pthread_cond_signal(&my_cond_start_is_required);
  376. pthread_join(my_thread, NULL);
  377. my_terminate_is_required = false;
  378. thread_inited = false;
  379. pthread_mutex_destroy(&my_mutex);
  380. pthread_cond_destroy(&my_cond_start_is_required);
  381. pthread_cond_destroy(&my_cond_stop_is_acknowledged);
  382. init(0); // purge fifo
  383. }
  384. #endif