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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #ifdef 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. enum {
  55. MAX_NODE_COUNTER = 400,
  56. INACTIVITY_TIMEOUT = 50, // in ms, check that the stream is inactive
  57. MAX_INACTIVITY_CHECK = 2
  58. };
  59. void fifo_init()
  60. {
  61. // security
  62. pthread_mutex_init(&my_mutex, (const pthread_mutexattr_t *)NULL);
  63. init(0);
  64. assert(-1 != pthread_cond_init(&my_cond_command_is_running, NULL));
  65. assert(-1 != pthread_cond_init(&my_cond_start_is_required, NULL));
  66. assert(-1 != pthread_cond_init(&my_cond_stop_is_acknowledged, NULL));
  67. pthread_attr_t a_attrib;
  68. if (pthread_attr_init(&a_attrib)
  69. || pthread_attr_setdetachstate(&a_attrib, PTHREAD_CREATE_JOINABLE)
  70. || pthread_create(&my_thread,
  71. &a_attrib,
  72. say_thread,
  73. (void *)NULL)) {
  74. assert(0);
  75. }
  76. pthread_attr_destroy(&a_attrib);
  77. // leave once the thread is actually started
  78. assert(-1 != pthread_mutex_lock(&my_mutex));
  79. while (my_stop_is_acknowledged == false) {
  80. while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR)
  81. ;
  82. }
  83. my_stop_is_acknowledged = false;
  84. pthread_mutex_unlock(&my_mutex);
  85. }
  86. espeak_ng_STATUS fifo_add_command(t_espeak_command *the_command)
  87. {
  88. espeak_ng_STATUS status;
  89. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  90. return status;
  91. if ((status = push(the_command)) != ENS_OK) {
  92. pthread_mutex_unlock(&my_mutex);
  93. return status;
  94. }
  95. my_start_is_required = true;
  96. pthread_cond_signal(&my_cond_start_is_required);
  97. while (my_start_is_required && !my_command_is_running) {
  98. if((status = pthread_cond_wait(&my_cond_command_is_running, &my_mutex)) != ENS_OK && errno != EINTR) {
  99. pthread_mutex_unlock(&my_mutex);
  100. return status;
  101. }
  102. }
  103. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  104. return status;
  105. return ENS_OK;
  106. }
  107. espeak_ng_STATUS fifo_add_commands(t_espeak_command *command1, t_espeak_command *command2)
  108. {
  109. espeak_ng_STATUS status;
  110. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  111. return status;
  112. if (node_counter+1 >= MAX_NODE_COUNTER) {
  113. pthread_mutex_unlock(&my_mutex);
  114. return ENS_FIFO_BUFFER_FULL;
  115. }
  116. if ((status = push(command1)) != ENS_OK) {
  117. pthread_mutex_unlock(&my_mutex);
  118. return status;
  119. }
  120. if ((status = push(command2)) != ENS_OK) {
  121. pthread_mutex_unlock(&my_mutex);
  122. return status;
  123. }
  124. my_start_is_required = true;
  125. pthread_cond_signal(&my_cond_start_is_required);
  126. while (my_start_is_required && !my_command_is_running) {
  127. if((status = pthread_cond_wait(&my_cond_command_is_running, &my_mutex)) != ENS_OK && errno != EINTR) {
  128. pthread_mutex_unlock(&my_mutex);
  129. return status;
  130. }
  131. }
  132. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  133. return status;
  134. return ENS_OK;
  135. }
  136. espeak_ng_STATUS fifo_stop()
  137. {
  138. espeak_ng_STATUS status;
  139. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  140. return status;
  141. bool a_command_is_running = false;
  142. if (my_command_is_running) {
  143. a_command_is_running = true;
  144. my_stop_is_required = true;
  145. my_stop_is_acknowledged = false;
  146. }
  147. if (a_command_is_running) {
  148. while (my_stop_is_acknowledged == false) {
  149. while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR)
  150. continue; // Restart when interrupted by handler
  151. }
  152. }
  153. my_stop_is_required = false;
  154. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  155. return status;
  156. return ENS_OK;
  157. }
  158. int fifo_is_busy()
  159. {
  160. return my_command_is_running;
  161. }
  162. static int sleep_until_start_request_or_inactivity()
  163. {
  164. int a_start_is_required = false;
  165. // Wait for the start request (my_cond_start_is_required).
  166. // Besides this, if the audio stream is still busy,
  167. // check from time to time its end.
  168. // The end of the stream is confirmed by several checks
  169. // for filtering underflow.
  170. //
  171. int i = 0;
  172. int err = pthread_mutex_lock(&my_mutex);
  173. assert(err != -1);
  174. while ((i <= MAX_INACTIVITY_CHECK) && !a_start_is_required) {
  175. i++;
  176. struct timespec ts;
  177. struct timeval tv;
  178. clock_gettime2(&ts);
  179. add_time_in_ms(&ts, INACTIVITY_TIMEOUT);
  180. while ((err = pthread_cond_timedwait(&my_cond_start_is_required, &my_mutex, &ts)) == -1
  181. && errno == EINTR)
  182. continue;
  183. assert(gettimeofday(&tv, NULL) != -1);
  184. if (err == 0)
  185. a_start_is_required = true;
  186. }
  187. pthread_mutex_unlock(&my_mutex);
  188. return a_start_is_required;
  189. }
  190. static espeak_ng_STATUS close_stream()
  191. {
  192. espeak_ng_STATUS status = pthread_mutex_lock(&my_mutex);
  193. if (status != ENS_OK)
  194. return status;
  195. bool a_stop_is_required = my_stop_is_required;
  196. if (!a_stop_is_required)
  197. my_command_is_running = true;
  198. status = pthread_mutex_unlock(&my_mutex);
  199. if (!a_stop_is_required) {
  200. int a_status = pthread_mutex_lock(&my_mutex);
  201. if (status == ENS_OK)
  202. status = a_status;
  203. my_command_is_running = false;
  204. a_stop_is_required = my_stop_is_required;
  205. a_status = pthread_mutex_unlock(&my_mutex);
  206. if (status == ENS_OK)
  207. status = a_status;
  208. if (a_stop_is_required) {
  209. // cancel the audio early, to be more responsive when using eSpeak NG
  210. // for audio.
  211. cancel_audio();
  212. // acknowledge the stop request
  213. if((a_status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  214. return a_status;
  215. my_stop_is_acknowledged = true;
  216. a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged);
  217. if(a_status != ENS_OK)
  218. return a_status;
  219. a_status = pthread_mutex_unlock(&my_mutex);
  220. if (status == ENS_OK)
  221. status = a_status;
  222. }
  223. }
  224. return status;
  225. }
  226. static void *say_thread(void *p)
  227. {
  228. (void)p; // unused
  229. // announce that thread is started
  230. assert(-1 != pthread_mutex_lock(&my_mutex));
  231. my_stop_is_acknowledged = true;
  232. assert(-1 != pthread_cond_signal(&my_cond_stop_is_acknowledged));
  233. assert(-1 != pthread_mutex_unlock(&my_mutex));
  234. bool look_for_inactivity = false;
  235. while (!my_terminate_is_required) {
  236. bool a_start_is_required = false;
  237. if (look_for_inactivity) {
  238. a_start_is_required = sleep_until_start_request_or_inactivity();
  239. if (!a_start_is_required)
  240. close_stream();
  241. }
  242. look_for_inactivity = true;
  243. int a_status = pthread_mutex_lock(&my_mutex);
  244. assert(!a_status);
  245. if (!a_start_is_required) {
  246. while (my_start_is_required == false && my_terminate_is_required == false) {
  247. while ((pthread_cond_wait(&my_cond_start_is_required, &my_mutex) == -1) && errno == EINTR)
  248. continue; // Restart when interrupted by handler
  249. }
  250. }
  251. my_command_is_running = true;
  252. assert(-1 != pthread_cond_broadcast(&my_cond_command_is_running));
  253. assert(-1 != pthread_mutex_unlock(&my_mutex));
  254. while (my_command_is_running && !my_terminate_is_required) {
  255. int a_status = pthread_mutex_lock(&my_mutex);
  256. assert(!a_status);
  257. t_espeak_command *a_command = (t_espeak_command *)pop();
  258. if (a_command == NULL) {
  259. my_command_is_running = false;
  260. a_status = pthread_mutex_unlock(&my_mutex);
  261. } else {
  262. my_start_is_required = false;
  263. if (my_stop_is_required)
  264. my_command_is_running = false;
  265. a_status = pthread_mutex_unlock(&my_mutex);
  266. if (my_command_is_running)
  267. process_espeak_command(a_command);
  268. delete_espeak_command(a_command);
  269. }
  270. }
  271. if (my_stop_is_required || my_terminate_is_required) {
  272. // no mutex required since the stop command is synchronous
  273. // and waiting for my_cond_stop_is_acknowledged
  274. init(1);
  275. assert(-1 != pthread_mutex_lock(&my_mutex));
  276. my_start_is_required = false;
  277. // acknowledge the stop request
  278. my_stop_is_acknowledged = true;
  279. int a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged);
  280. assert(a_status != -1);
  281. pthread_mutex_unlock(&my_mutex);
  282. }
  283. // and wait for the next start
  284. }
  285. return NULL;
  286. }
  287. int fifo_is_command_enabled()
  288. {
  289. return 0 == my_stop_is_required;
  290. }
  291. typedef struct t_node {
  292. t_espeak_command *data;
  293. struct t_node *next;
  294. } node;
  295. static node *head = NULL;
  296. static node *tail = NULL;
  297. static espeak_ng_STATUS push(t_espeak_command *the_command)
  298. {
  299. assert((!head && !tail) || (head && tail));
  300. if (the_command == NULL)
  301. return EINVAL;
  302. if (node_counter >= MAX_NODE_COUNTER)
  303. return ENS_FIFO_BUFFER_FULL;
  304. node *n = (node *)malloc(sizeof(node));
  305. if (n == NULL)
  306. return ENOMEM;
  307. if (head == NULL) {
  308. head = n;
  309. tail = n;
  310. } else {
  311. tail->next = n;
  312. tail = n;
  313. }
  314. tail->next = NULL;
  315. tail->data = the_command;
  316. node_counter++;
  317. the_command->state = CS_PENDING;
  318. return ENS_OK;
  319. }
  320. static t_espeak_command *pop()
  321. {
  322. t_espeak_command *the_command = NULL;
  323. assert((!head && !tail) || (head && tail));
  324. if (head != NULL) {
  325. node *n = head;
  326. the_command = n->data;
  327. head = n->next;
  328. free(n);
  329. node_counter--;
  330. }
  331. if (head == NULL)
  332. tail = NULL;
  333. return the_command;
  334. }
  335. static void init(int process_parameters)
  336. {
  337. t_espeak_command *c = NULL;
  338. c = pop();
  339. while (c != NULL) {
  340. if (process_parameters && (c->type == ET_PARAMETER || c->type == ET_VOICE_NAME || c->type == ET_VOICE_SPEC))
  341. process_espeak_command(c);
  342. delete_espeak_command(c);
  343. c = pop();
  344. }
  345. node_counter = 0;
  346. }
  347. void fifo_terminate()
  348. {
  349. my_terminate_is_required = true;
  350. pthread_cond_signal(&my_cond_start_is_required);
  351. pthread_join(my_thread, NULL);
  352. my_terminate_is_required = false;
  353. pthread_mutex_destroy(&my_mutex);
  354. pthread_cond_destroy(&my_cond_start_is_required);
  355. pthread_cond_destroy(&my_cond_stop_is_acknowledged);
  356. init(0); // purge fifo
  357. }
  358. #endif