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

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