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

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