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.

event.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/time.h>
  28. #include <unistd.h>
  29. #include <espeak-ng/espeak_ng.h>
  30. #include <espeak-ng/speak_lib.h>
  31. #include "speech.h"
  32. #include "event.h"
  33. // my_mutex: protects my_thread_is_talking,
  34. static pthread_mutex_t my_mutex;
  35. static pthread_cond_t my_cond_start_is_required;
  36. static int my_start_is_required = 0;
  37. static pthread_cond_t my_cond_stop_is_required;
  38. static int my_stop_is_required = 0;
  39. static pthread_cond_t my_cond_stop_is_acknowledged;
  40. static int my_stop_is_acknowledged = 0;
  41. // my_thread: polls the audio duration and compares it to the duration of the first event.
  42. static pthread_t my_thread;
  43. static bool thread_inited;
  44. static t_espeak_callback *my_callback = NULL;
  45. static int my_event_is_running = 0;
  46. enum {
  47. MIN_TIMEOUT_IN_MS = 10,
  48. ACTIVITY_TIMEOUT = 50, // in ms, check that the stream is active
  49. MAX_ACTIVITY_CHECK = 6
  50. };
  51. typedef struct t_node {
  52. void *data;
  53. struct t_node *next;
  54. } node;
  55. static node *head = NULL;
  56. static node *tail = NULL;
  57. static int node_counter = 0;
  58. static espeak_ng_STATUS push(void *data);
  59. static void *pop();
  60. static void init();
  61. static void *polling_thread(void *);
  62. void event_set_callback(t_espeak_callback *SynthCallback)
  63. {
  64. my_callback = SynthCallback;
  65. }
  66. void event_init(void)
  67. {
  68. my_event_is_running = 0;
  69. // security
  70. pthread_mutex_init(&my_mutex, (const pthread_mutexattr_t *)NULL);
  71. init();
  72. assert(-1 != pthread_cond_init(&my_cond_start_is_required, NULL));
  73. assert(-1 != pthread_cond_init(&my_cond_stop_is_required, NULL));
  74. assert(-1 != pthread_cond_init(&my_cond_stop_is_acknowledged, NULL));
  75. pthread_attr_t a_attrib;
  76. if (pthread_attr_init(&a_attrib) == 0
  77. && pthread_attr_setdetachstate(&a_attrib, PTHREAD_CREATE_JOINABLE) == 0) {
  78. thread_inited = (0 == pthread_create(&my_thread,
  79. &a_attrib,
  80. polling_thread,
  81. (void *)NULL));
  82. }
  83. assert(thread_inited);
  84. pthread_attr_destroy(&a_attrib);
  85. }
  86. static espeak_EVENT *event_copy(espeak_EVENT *event)
  87. {
  88. if (event == NULL)
  89. return NULL;
  90. espeak_EVENT *a_event = (espeak_EVENT *)malloc(sizeof(espeak_EVENT));
  91. if (a_event) {
  92. memcpy(a_event, event, sizeof(espeak_EVENT));
  93. switch (event->type)
  94. {
  95. case espeakEVENT_MARK:
  96. case espeakEVENT_PLAY:
  97. if (event->id.name)
  98. a_event->id.name = strdup(event->id.name);
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. return a_event;
  105. }
  106. // Call the user supplied callback
  107. //
  108. // Note: the current sequence is:
  109. //
  110. // * First call with: event->type = espeakEVENT_SENTENCE
  111. // * 0, 1 or several calls: event->type = espeakEVENT_WORD
  112. // * Last call: event->type = espeakEVENT_MSG_TERMINATED
  113. //
  114. static void event_notify(espeak_EVENT *event)
  115. {
  116. static unsigned int a_old_uid = 0;
  117. espeak_EVENT events[2];
  118. memcpy(&events[0], event, sizeof(espeak_EVENT)); // the event parameter in the callback function should be an array of eventd
  119. memcpy(&events[1], event, sizeof(espeak_EVENT));
  120. events[1].type = espeakEVENT_LIST_TERMINATED; // ... terminated by an event type=0
  121. if (event && my_callback) {
  122. switch (event->type)
  123. {
  124. case espeakEVENT_SENTENCE:
  125. my_callback(NULL, 0, events);
  126. a_old_uid = event->unique_identifier;
  127. break;
  128. case espeakEVENT_MSG_TERMINATED:
  129. case espeakEVENT_MARK:
  130. case espeakEVENT_WORD:
  131. case espeakEVENT_END:
  132. case espeakEVENT_PHONEME:
  133. {
  134. if (a_old_uid != event->unique_identifier) {
  135. espeak_EVENT_TYPE a_new_type = events[0].type;
  136. events[0].type = espeakEVENT_SENTENCE;
  137. my_callback(NULL, 0, events);
  138. events[0].type = a_new_type;
  139. usleep(50000);
  140. }
  141. my_callback(NULL, 0, events);
  142. a_old_uid = event->unique_identifier;
  143. }
  144. break;
  145. case espeakEVENT_LIST_TERMINATED:
  146. case espeakEVENT_PLAY:
  147. default:
  148. break;
  149. }
  150. }
  151. }
  152. static int event_delete(espeak_EVENT *event)
  153. {
  154. if (event == NULL)
  155. return 0;
  156. switch (event->type)
  157. {
  158. case espeakEVENT_MSG_TERMINATED:
  159. event_notify(event);
  160. break;
  161. case espeakEVENT_MARK:
  162. case espeakEVENT_PLAY:
  163. if (event->id.name)
  164. free((void *)(event->id.name));
  165. break;
  166. default:
  167. break;
  168. }
  169. free(event);
  170. return 1;
  171. }
  172. espeak_ng_STATUS event_declare(espeak_EVENT *event)
  173. {
  174. if (!event)
  175. return EINVAL;
  176. espeak_ng_STATUS status;
  177. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) {
  178. my_start_is_required = 1;
  179. return status;
  180. }
  181. espeak_EVENT *a_event = event_copy(event);
  182. if ((status = push(a_event)) != ENS_OK) {
  183. event_delete(a_event);
  184. pthread_mutex_unlock(&my_mutex);
  185. } else {
  186. my_start_is_required = 1;
  187. pthread_cond_signal(&my_cond_start_is_required);
  188. status = pthread_mutex_unlock(&my_mutex);
  189. }
  190. return status;
  191. }
  192. espeak_ng_STATUS event_clear_all()
  193. {
  194. espeak_ng_STATUS status;
  195. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  196. return status;
  197. int a_event_is_running = 0;
  198. if (my_event_is_running) {
  199. my_stop_is_required = 1;
  200. pthread_cond_signal(&my_cond_stop_is_required);
  201. a_event_is_running = 1;
  202. } else
  203. init(); // clear pending events
  204. if (a_event_is_running) {
  205. while (my_stop_is_acknowledged == 0) {
  206. while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR)
  207. continue; // Restart when interrupted by handler
  208. }
  209. }
  210. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  211. return status;
  212. return ENS_OK;
  213. }
  214. static void *polling_thread(void *p)
  215. {
  216. (void)p; // unused
  217. while (1) {
  218. int a_stop_is_required = 0;
  219. int a_status = pthread_mutex_lock(&my_mutex);
  220. my_event_is_running = 0;
  221. while (my_start_is_required == 0) {
  222. while ((pthread_cond_wait(&my_cond_start_is_required, &my_mutex) == -1) && errno == EINTR)
  223. continue; // Restart when interrupted by handler
  224. }
  225. my_event_is_running = 1;
  226. a_stop_is_required = 0;
  227. my_start_is_required = 0;
  228. pthread_mutex_unlock(&my_mutex);
  229. // In this loop, my_event_is_running = 1
  230. while (head && (a_stop_is_required == 0)) {
  231. espeak_EVENT *event = (espeak_EVENT *)(head->data);
  232. assert(event);
  233. if (my_callback) {
  234. event_notify(event);
  235. // the user_data (and the type) are cleaned to be sure
  236. // that MSG_TERMINATED is called twice (at delete time too).
  237. event->type = espeakEVENT_LIST_TERMINATED;
  238. event->user_data = NULL;
  239. }
  240. a_status = pthread_mutex_lock(&my_mutex);
  241. event_delete((espeak_EVENT *)pop());
  242. a_stop_is_required = my_stop_is_required;
  243. if (a_stop_is_required > 0)
  244. my_stop_is_required = 0;
  245. a_status = pthread_mutex_unlock(&my_mutex);
  246. }
  247. a_status = pthread_mutex_lock(&my_mutex);
  248. my_event_is_running = 0;
  249. if (a_stop_is_required == 0) {
  250. a_stop_is_required = my_stop_is_required;
  251. if (a_stop_is_required > 0)
  252. my_stop_is_required = 0;
  253. }
  254. a_status = pthread_mutex_unlock(&my_mutex);
  255. if (a_stop_is_required > 0) {
  256. // no mutex required since the stop command is synchronous
  257. // and waiting for my_cond_stop_is_acknowledged
  258. init();
  259. // acknowledge the stop request
  260. espeak_ng_STATUS a_status = pthread_mutex_lock(&my_mutex);
  261. my_stop_is_acknowledged = 1;
  262. a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged);
  263. a_status = pthread_mutex_unlock(&my_mutex);
  264. }
  265. }
  266. return NULL;
  267. }
  268. enum { MAX_NODE_COUNTER = 1000 };
  269. static espeak_ng_STATUS push(void *the_data)
  270. {
  271. assert((!head && !tail) || (head && tail));
  272. if (the_data == NULL)
  273. return EINVAL;
  274. if (node_counter >= MAX_NODE_COUNTER)
  275. return ENS_EVENT_BUFFER_FULL;
  276. node *n = (node *)malloc(sizeof(node));
  277. if (n == NULL)
  278. return ENOMEM;
  279. if (head == NULL) {
  280. head = n;
  281. tail = n;
  282. } else {
  283. tail->next = n;
  284. tail = n;
  285. }
  286. tail->next = NULL;
  287. tail->data = the_data;
  288. node_counter++;
  289. return ENS_OK;
  290. }
  291. static void *pop()
  292. {
  293. void *the_data = NULL;
  294. assert((!head && !tail) || (head && tail));
  295. if (head != NULL) {
  296. node *n = head;
  297. the_data = n->data;
  298. head = n->next;
  299. free(n);
  300. node_counter--;
  301. }
  302. if (head == NULL)
  303. tail = NULL;
  304. return the_data;
  305. }
  306. static void init()
  307. {
  308. while (event_delete((espeak_EVENT *)pop()))
  309. ;
  310. node_counter = 0;
  311. }
  312. void event_terminate()
  313. {
  314. if (thread_inited) {
  315. pthread_cancel(my_thread);
  316. pthread_join(my_thread, NULL);
  317. pthread_mutex_destroy(&my_mutex);
  318. pthread_cond_destroy(&my_cond_start_is_required);
  319. pthread_cond_destroy(&my_cond_stop_is_required);
  320. pthread_cond_destroy(&my_cond_stop_is_acknowledged);
  321. init(); // purge event
  322. thread_inited = 0;
  323. }
  324. }
  325. enum { ONE_BILLION = 1000000000 };
  326. void clock_gettime2(struct timespec *ts)
  327. {
  328. struct timeval tv;
  329. if (!ts)
  330. return;
  331. assert(gettimeofday(&tv, NULL) != -1);
  332. ts->tv_sec = tv.tv_sec;
  333. ts->tv_nsec = tv.tv_usec*1000;
  334. }
  335. void add_time_in_ms(struct timespec *ts, int time_in_ms)
  336. {
  337. if (!ts)
  338. return;
  339. uint64_t t_ns = (uint64_t)ts->tv_nsec + 1000000 * (uint64_t)time_in_ms;
  340. while (t_ns >= ONE_BILLION) {
  341. ts->tv_sec += 1;
  342. t_ns -= ONE_BILLION;
  343. }
  344. ts->tv_nsec = (long int)t_ns;
  345. }