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

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