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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #ifndef PLATFORM_WINDOWS
  21. #include <unistd.h>
  22. #endif
  23. #include <assert.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <pthread.h>
  27. #include <semaphore.h>
  28. #include <sys/time.h>
  29. #include <errno.h>
  30. #include <stdbool.h>
  31. #include <espeak-ng/espeak_ng.h>
  32. #include <espeak/speak_lib.h>
  33. #include "speech.h"
  34. #include "event.h"
  35. #include "wave.h"
  36. // my_mutex: protects my_thread_is_talking,
  37. static pthread_mutex_t my_mutex;
  38. static sem_t my_sem_start_is_required;
  39. static sem_t my_sem_stop_is_required;
  40. static sem_t my_sem_stop_is_acknowledged;
  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 != sem_init(&my_sem_start_is_required, 0, 0));
  73. assert(-1 != sem_init(&my_sem_stop_is_required, 0, 0));
  74. assert(-1 != sem_init(&my_sem_stop_is_acknowledged, 0, 0));
  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. sem_post(&my_sem_start_is_required);
  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. status = pthread_mutex_unlock(&my_mutex);
  187. sem_post(&my_sem_start_is_required);
  188. return status;
  189. }
  190. espeak_ng_STATUS event_clear_all()
  191. {
  192. espeak_ng_STATUS status;
  193. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  194. return status;
  195. int a_event_is_running = 0;
  196. if (my_event_is_running) {
  197. sem_post(&my_sem_stop_is_required);
  198. a_event_is_running = 1;
  199. } else
  200. init(); // clear pending events
  201. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  202. return status;
  203. if (a_event_is_running) {
  204. while ((sem_wait(&my_sem_stop_is_acknowledged) == -1) && errno == EINTR)
  205. continue; // Restart when interrupted by handler
  206. }
  207. return ENS_OK;
  208. }
  209. static int sleep_until_timeout_or_stop_request(uint32_t time_in_ms)
  210. {
  211. int a_stop_is_required = 0;
  212. struct timespec ts;
  213. struct timeval tv;
  214. int err = 0;
  215. clock_gettime2(&ts);
  216. add_time_in_ms(&ts, time_in_ms);
  217. while ((err = sem_timedwait(&my_sem_stop_is_required, &ts)) == -1
  218. && errno == EINTR)
  219. continue; // Restart when interrupted by handler
  220. assert(gettimeofday(&tv, NULL) != -1);
  221. if (err == 0)
  222. a_stop_is_required = 1; // stop required
  223. return a_stop_is_required;
  224. }
  225. // Asked for the time interval required for reaching the sample.
  226. // If the stream is opened but the audio samples are not played,
  227. // a timeout is started.
  228. static int get_remaining_time(uint32_t sample, uint32_t *time_in_ms, int *stop_is_required)
  229. {
  230. int err = 0;
  231. *stop_is_required = 0;
  232. int i = 0;
  233. for (i = 0; i < MAX_ACTIVITY_CHECK && (*stop_is_required == 0); i++) {
  234. err = wave_get_remaining_time(sample, time_in_ms);
  235. if (err || // if err, stream not available: quit
  236. wave_is_busy(NULL) || // if wave is busy, time_in_ms is known: quit
  237. (*time_in_ms == 0)) { // if wave is not busy but remaining time == 0, event is reached: quit
  238. break;
  239. }
  240. // stream opened but not active
  241. //
  242. // Several possible states:
  243. // * the stream is opened but not yet started:
  244. //
  245. // wait for the start of stream
  246. //
  247. // * some samples have already been played,
  248. // ** the end of stream is reached
  249. // ** or there is an underrun
  250. //
  251. // wait for the close of stream
  252. *stop_is_required = sleep_until_timeout_or_stop_request(ACTIVITY_TIMEOUT);
  253. }
  254. return err;
  255. }
  256. static void *polling_thread(void *p)
  257. {
  258. (void)p; // unused
  259. while (1) {
  260. int a_stop_is_required = 0;
  261. int a_status = pthread_mutex_lock(&my_mutex);
  262. my_event_is_running = 0;
  263. pthread_mutex_unlock(&my_mutex);
  264. while ((sem_wait(&my_sem_start_is_required) == -1) && errno == EINTR)
  265. continue; // Restart when interrupted by handler
  266. a_status = pthread_mutex_lock(&my_mutex);
  267. my_event_is_running = 1;
  268. pthread_mutex_unlock(&my_mutex);
  269. a_stop_is_required = 0;
  270. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required); // NOTE: may set a_stop_is_required to -1
  271. if ((a_status == 0) && (a_stop_is_required > 0)) {
  272. while (0 == sem_trywait(&my_sem_stop_is_required))
  273. ;
  274. } else
  275. a_stop_is_required = 0;
  276. // In this loop, my_event_is_running = 1
  277. while (head && (a_stop_is_required <= 0)) {
  278. while (0 == sem_trywait(&my_sem_start_is_required))
  279. ;
  280. espeak_EVENT *event = (espeak_EVENT *)(head->data);
  281. assert(event);
  282. uint32_t time_in_ms = 0;
  283. int err = get_remaining_time((uint32_t)event->sample,
  284. &time_in_ms,
  285. &a_stop_is_required);
  286. if (a_stop_is_required > 0)
  287. break;
  288. else if (err != 0) {
  289. // No available time: the event is deleted.
  290. a_status = pthread_mutex_lock(&my_mutex);
  291. event_delete((espeak_EVENT *)pop());
  292. a_status = pthread_mutex_unlock(&my_mutex);
  293. } else if (time_in_ms == 0) { // the event is already reached.
  294. if (my_callback) {
  295. event_notify(event);
  296. // the user_data (and the type) are cleaned to be sure
  297. // that MSG_TERMINATED is called twice (at delete time too).
  298. event->type = espeakEVENT_LIST_TERMINATED;
  299. event->user_data = NULL;
  300. }
  301. a_status = pthread_mutex_lock(&my_mutex);
  302. event_delete((espeak_EVENT *)pop());
  303. a_status = pthread_mutex_unlock(&my_mutex);
  304. a_stop_is_required = 0;
  305. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
  306. if ((a_status == 0) && (a_stop_is_required > 0)) {
  307. while (0 == sem_trywait(&my_sem_stop_is_required))
  308. ;
  309. } else
  310. a_stop_is_required = 0;
  311. } else // The event will be notified soon: sleep until timeout or stop request
  312. a_stop_is_required = sleep_until_timeout_or_stop_request(time_in_ms);
  313. }
  314. a_status = pthread_mutex_lock(&my_mutex);
  315. my_event_is_running = 0;
  316. if (a_stop_is_required <= 0) {
  317. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
  318. if ((a_status == 0) && (a_stop_is_required > 0)) {
  319. while (0 == sem_trywait(&my_sem_stop_is_required))
  320. ;
  321. } else
  322. a_stop_is_required = 0;
  323. }
  324. a_status = pthread_mutex_unlock(&my_mutex);
  325. if (a_stop_is_required > 0) {
  326. // no mutex required since the stop command is synchronous
  327. // and waiting for my_sem_stop_is_acknowledged
  328. init();
  329. // acknowledge the stop request
  330. a_status = sem_post(&my_sem_stop_is_acknowledged);
  331. }
  332. }
  333. return NULL;
  334. }
  335. enum { MAX_NODE_COUNTER = 1000 };
  336. static espeak_ng_STATUS push(void *the_data)
  337. {
  338. assert((!head && !tail) || (head && tail));
  339. if (the_data == NULL)
  340. return EINVAL;
  341. if (node_counter >= MAX_NODE_COUNTER)
  342. return ENS_EVENT_BUFFER_FULL;
  343. node *n = (node *)malloc(sizeof(node));
  344. if (n == NULL)
  345. return ENOMEM;
  346. if (head == NULL) {
  347. head = n;
  348. tail = n;
  349. } else {
  350. tail->next = n;
  351. tail = n;
  352. }
  353. tail->next = NULL;
  354. tail->data = the_data;
  355. node_counter++;
  356. return ENS_OK;
  357. }
  358. static void *pop()
  359. {
  360. void *the_data = NULL;
  361. assert((!head && !tail) || (head && tail));
  362. if (head != NULL) {
  363. node *n = head;
  364. the_data = n->data;
  365. head = n->next;
  366. free(n);
  367. node_counter--;
  368. }
  369. if (head == NULL)
  370. tail = NULL;
  371. return the_data;
  372. }
  373. static void init()
  374. {
  375. while (event_delete((espeak_EVENT *)pop()))
  376. ;
  377. node_counter = 0;
  378. }
  379. void event_terminate()
  380. {
  381. if (thread_inited) {
  382. pthread_cancel(my_thread);
  383. pthread_join(my_thread, NULL);
  384. pthread_mutex_destroy(&my_mutex);
  385. sem_destroy(&my_sem_start_is_required);
  386. sem_destroy(&my_sem_stop_is_required);
  387. sem_destroy(&my_sem_stop_is_acknowledged);
  388. init(); // purge event
  389. thread_inited = 0;
  390. }
  391. }
  392. enum { ONE_BILLION = 1000000000 };
  393. void clock_gettime2(struct timespec *ts)
  394. {
  395. struct timeval tv;
  396. if (!ts)
  397. return;
  398. assert(gettimeofday(&tv, NULL) != -1);
  399. ts->tv_sec = tv.tv_sec;
  400. ts->tv_nsec = tv.tv_usec*1000;
  401. }
  402. void add_time_in_ms(struct timespec *ts, int time_in_ms)
  403. {
  404. if (!ts)
  405. return;
  406. uint64_t t_ns = (uint64_t)ts->tv_nsec + 1000000 * (uint64_t)time_in_ms;
  407. while (t_ns >= ONE_BILLION) {
  408. ts->tv_sec += 1;
  409. t_ns -= ONE_BILLION;
  410. }
  411. ts->tv_nsec = (long int)t_ns;
  412. }