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

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