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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /***************************************************************************
  2. * Copyright (C) 2007, Gilles Casse <[email protected]> *
  3. * Copyright (C) 2013-2015 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, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. // This source file is only used for asynchronious modes
  21. //<includes
  22. #ifndef PLATFORM_WINDOWS
  23. #include <unistd.h>
  24. #endif
  25. #include <assert.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <pthread.h>
  29. #include <semaphore.h>
  30. #include <sys/time.h>
  31. #include <errno.h>
  32. #include <stdbool.h>
  33. #include "speech.h"
  34. #include "speak_lib.h"
  35. #include "event.h"
  36. #include "wave.h"
  37. #include "debug.h"
  38. //>
  39. //<decls and function prototypes
  40. // my_mutex: protects my_thread_is_talking,
  41. static pthread_mutex_t my_mutex;
  42. static sem_t my_sem_start_is_required;
  43. static sem_t my_sem_stop_is_required;
  44. static sem_t my_sem_stop_is_acknowledged;
  45. // my_thread: polls the audio duration and compares it to the duration of the first event.
  46. static pthread_t my_thread;
  47. static bool thread_inited;
  48. static t_espeak_callback* my_callback = NULL;
  49. static int my_event_is_running=0;
  50. enum {MIN_TIMEOUT_IN_MS=10,
  51. ACTIVITY_TIMEOUT=50, // in ms, check that the stream is active
  52. MAX_ACTIVITY_CHECK=6
  53. };
  54. typedef struct t_node
  55. {
  56. void* data;
  57. struct t_node *next;
  58. } node;
  59. static node* head=NULL;
  60. static node* tail=NULL;
  61. static int node_counter=0;
  62. static espeak_ERROR push(void* data);
  63. static void* pop();
  64. static void init();
  65. static void* polling_thread(void*);
  66. //>
  67. //<event_init
  68. void event_set_callback(t_espeak_callback* SynthCallback)
  69. {
  70. my_callback = SynthCallback;
  71. }
  72. void event_init(void)
  73. {
  74. ENTER("event_init");
  75. my_event_is_running=0;
  76. // security
  77. pthread_mutex_init( &my_mutex, (const pthread_mutexattr_t *)NULL);
  78. init();
  79. assert(-1 != sem_init(&my_sem_start_is_required, 0, 0));
  80. assert(-1 != sem_init(&my_sem_stop_is_required, 0, 0));
  81. assert(-1 != sem_init(&my_sem_stop_is_acknowledged, 0, 0));
  82. pthread_attr_t a_attrib;
  83. if (pthread_attr_init (&a_attrib) == 0
  84. && pthread_attr_setdetachstate(&a_attrib, PTHREAD_CREATE_JOINABLE) == 0)
  85. {
  86. thread_inited = (0 == pthread_create(&my_thread,
  87. &a_attrib,
  88. polling_thread,
  89. (void*)NULL));
  90. }
  91. assert(thread_inited);
  92. pthread_attr_destroy(&a_attrib);
  93. }
  94. //>
  95. //<event_display
  96. static void event_display(espeak_EVENT* event)
  97. {
  98. ENTER("event_display");
  99. #ifdef DEBUG_ENABLED
  100. if (event==NULL)
  101. {
  102. SHOW("event_display > event=%s\n","NULL");
  103. }
  104. else
  105. {
  106. static const char* label[] = {
  107. "LIST_TERMINATED",
  108. "WORD",
  109. "SENTENCE",
  110. "MARK",
  111. "PLAY",
  112. "END",
  113. "MSG_TERMINATED",
  114. "PHONEME",
  115. "SAMPLERATE",
  116. "??"
  117. };
  118. SHOW("event_display > event=0x%x\n",event);
  119. SHOW("event_display > type=%s\n",label[event->type]);
  120. SHOW("event_display > uid=%d\n",event->unique_identifier);
  121. SHOW("event_display > text_position=%d\n",event->text_position);
  122. SHOW("event_display > length=%d\n",event->length);
  123. SHOW("event_display > audio_position=%d\n",event->audio_position);
  124. SHOW("event_display > sample=%d\n",event->sample);
  125. SHOW("event_display > user_data=0x%x\n",event->user_data);
  126. }
  127. #endif
  128. }
  129. //>
  130. //<event_copy
  131. static espeak_EVENT* event_copy (espeak_EVENT* event)
  132. {
  133. ENTER("event_copy");
  134. if (event==NULL)
  135. {
  136. return NULL;
  137. }
  138. espeak_EVENT* a_event=(espeak_EVENT*)malloc(sizeof(espeak_EVENT));
  139. if (a_event)
  140. {
  141. memcpy(a_event, event, sizeof(espeak_EVENT));
  142. switch(event->type)
  143. {
  144. case espeakEVENT_MARK:
  145. case espeakEVENT_PLAY:
  146. if (event->id.name)
  147. {
  148. a_event->id.name = strdup(event->id.name);
  149. }
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. event_display(a_event);
  156. return a_event;
  157. }
  158. //>
  159. //<event_notify
  160. // Call the user supplied callback
  161. //
  162. // Note: the current sequence is:
  163. //
  164. // * First call with: event->type = espeakEVENT_SENTENCE
  165. // * 0, 1 or several calls: event->type = espeakEVENT_WORD
  166. // * Last call: event->type = espeakEVENT_MSG_TERMINATED
  167. //
  168. static void event_notify(espeak_EVENT* event)
  169. {
  170. ENTER("event_notify");
  171. static unsigned int a_old_uid = 0;
  172. espeak_EVENT events[2];
  173. memcpy(&events[0],event,sizeof(espeak_EVENT)); // the event parameter in the callback function should be an array of eventd
  174. memcpy(&events[1],event,sizeof(espeak_EVENT));
  175. events[1].type = espeakEVENT_LIST_TERMINATED; // ... terminated by an event type=0
  176. if (event && my_callback)
  177. {
  178. event_display(event);
  179. switch(event->type)
  180. {
  181. case espeakEVENT_SENTENCE:
  182. my_callback(NULL, 0, events);
  183. a_old_uid = event->unique_identifier;
  184. break;
  185. case espeakEVENT_MSG_TERMINATED:
  186. case espeakEVENT_MARK:
  187. case espeakEVENT_WORD:
  188. case espeakEVENT_END:
  189. case espeakEVENT_PHONEME:
  190. {
  191. // jonsd - I'm not sure what this is for. gilles says it's for when Gnome Speech reads a file of blank lines
  192. if (a_old_uid != event->unique_identifier)
  193. {
  194. espeak_EVENT_TYPE a_new_type = events[0].type;
  195. events[0].type = espeakEVENT_SENTENCE;
  196. my_callback(NULL, 0, events);
  197. events[0].type = a_new_type;
  198. usleep(50000);
  199. }
  200. my_callback(NULL, 0, events);
  201. a_old_uid = event->unique_identifier;
  202. }
  203. break;
  204. default:
  205. case espeakEVENT_LIST_TERMINATED:
  206. case espeakEVENT_PLAY:
  207. break;
  208. }
  209. }
  210. }
  211. //>
  212. //<event_delete
  213. static int event_delete(espeak_EVENT* event)
  214. {
  215. ENTER("event_delete");
  216. event_display(event);
  217. if(event==NULL)
  218. {
  219. return 0;
  220. }
  221. switch(event->type)
  222. {
  223. case espeakEVENT_MSG_TERMINATED:
  224. event_notify(event);
  225. break;
  226. case espeakEVENT_MARK:
  227. case espeakEVENT_PLAY:
  228. if(event->id.name)
  229. {
  230. free((void*)(event->id.name));
  231. }
  232. break;
  233. default:
  234. break;
  235. }
  236. free(event);
  237. return 1;
  238. }
  239. //>
  240. //<event_declare
  241. espeak_ERROR event_declare (espeak_EVENT* event)
  242. {
  243. ENTER("event_declare");
  244. event_display(event);
  245. if (!event)
  246. {
  247. return EE_INTERNAL_ERROR;
  248. }
  249. int a_status = pthread_mutex_lock(&my_mutex);
  250. espeak_ERROR a_error = EE_OK;
  251. if (!a_status)
  252. {
  253. SHOW_TIME("event_declare > locked\n");
  254. espeak_EVENT* a_event = event_copy(event);
  255. a_error = push(a_event);
  256. if (a_error != EE_OK)
  257. {
  258. event_delete(a_event);
  259. }
  260. SHOW_TIME("event_declare > unlocking\n");
  261. a_status = pthread_mutex_unlock(&my_mutex);
  262. }
  263. // TBD: remove the comment
  264. // reminder: code in comment.
  265. // This wait can lead to an underrun
  266. //
  267. // if (!a_status && !my_event_is_running && (a_error == EE_OK))
  268. // {
  269. // // quit when command is actually started
  270. // // (for possible forthcoming 'end of command' checks)
  271. SHOW_TIME("event_declare > post my_sem_start_is_required\n");
  272. sem_post(&my_sem_start_is_required);
  273. // int val=1;
  274. // while (val)
  275. // {
  276. // usleep(50000); // TBD: event?
  277. // sem_getvalue(&my_sem_start_is_required, &val);
  278. // }
  279. // }
  280. if (a_status != 0)
  281. {
  282. a_error = EE_INTERNAL_ERROR;
  283. }
  284. return a_error;
  285. }
  286. //>
  287. //<event_clear_all
  288. espeak_ERROR event_clear_all ()
  289. {
  290. ENTER("event_clear_all");
  291. int a_status = pthread_mutex_lock(&my_mutex);
  292. int a_event_is_running = 0;
  293. SHOW_TIME("event_stop > locked\n");
  294. if (a_status != 0)
  295. {
  296. return EE_INTERNAL_ERROR;
  297. }
  298. if (my_event_is_running)
  299. {
  300. SHOW_TIME("event_stop > post my_sem_stop_is_required\n");
  301. sem_post(&my_sem_stop_is_required);
  302. a_event_is_running = 1;
  303. }
  304. else
  305. {
  306. init(); // clear pending events
  307. }
  308. SHOW_TIME("event_stop > unlocking\n");
  309. a_status = pthread_mutex_unlock(&my_mutex);
  310. if (a_status != 0)
  311. {
  312. return EE_INTERNAL_ERROR;
  313. }
  314. if (a_event_is_running)
  315. {
  316. SHOW_TIME("event_stop > wait for my_sem_stop_is_acknowledged\n");
  317. while ((sem_wait(&my_sem_stop_is_acknowledged) == -1) && errno == EINTR)
  318. {
  319. continue; // Restart when interrupted by handler
  320. }
  321. SHOW_TIME("event_stop > get my_sem_stop_is_acknowledged\n");
  322. }
  323. SHOW_TIME("LEAVE event_stop\n");
  324. return EE_OK;
  325. }
  326. //>
  327. //<sleep_until_timeout_or_stop_request
  328. static int sleep_until_timeout_or_stop_request(uint32_t time_in_ms)
  329. {
  330. ENTER("sleep_until_timeout_or_stop_request");
  331. int a_stop_is_required=0;
  332. struct timespec ts;
  333. struct timeval tv;
  334. int err=0;
  335. clock_gettime2( &ts);
  336. #ifdef DEBUG_ENABLED
  337. struct timespec to;
  338. to.tv_sec = ts.tv_sec;
  339. to.tv_nsec = ts.tv_nsec;
  340. #endif
  341. add_time_in_ms( &ts, time_in_ms);
  342. SHOW("polling_thread > sleep_until_timeout_or_stop_request > start sem_timedwait from %d.%09lu to %d.%09lu \n",
  343. to.tv_sec, to.tv_nsec,
  344. ts.tv_sec, ts.tv_nsec);
  345. while ((err = sem_timedwait(&my_sem_stop_is_required, &ts)) == -1
  346. && errno == EINTR)
  347. {
  348. continue; // Restart when interrupted by handler
  349. }
  350. assert (gettimeofday(&tv, NULL) != -1);
  351. SHOW("polling_thread > sleep_until_timeout_or_stop_request > stop sem_timedwait %d.%09lu \n",
  352. tv.tv_sec, tv.tv_usec*1000);
  353. if (err == 0)
  354. {
  355. SHOW("polling_thread > sleep_until_timeout_or_stop_request > %s\n","stop required!");
  356. a_stop_is_required=1; // stop required
  357. }
  358. return a_stop_is_required;
  359. }
  360. //>
  361. //<get_remaining_time
  362. // Asked for the time interval required for reaching the sample.
  363. // If the stream is opened but the audio samples are not played,
  364. // a timeout is started.
  365. static int get_remaining_time(uint32_t sample, uint32_t* time_in_ms, int* stop_is_required)
  366. {
  367. ENTER("get_remaining_time");
  368. int err = 0;
  369. *stop_is_required = 0;
  370. int i=0;
  371. for (i=0; i < MAX_ACTIVITY_CHECK && (*stop_is_required == 0); i++)
  372. {
  373. err = wave_get_remaining_time( sample, time_in_ms);
  374. if (err || wave_is_busy(NULL) || (*time_in_ms == 0))
  375. { // if err, stream not available: quit
  376. // if wave is busy, time_in_ms is known: quit
  377. // if wave is not busy but remaining time == 0, event is reached: quit
  378. break;
  379. }
  380. // stream opened but not active
  381. //
  382. // Several possible states:
  383. // * the stream is opened but not yet started:
  384. //
  385. // wait for the start of stream
  386. //
  387. // * some samples have already been played,
  388. // ** the end of stream is reached
  389. // ** or there is an underrun
  390. //
  391. // wait for the close of stream
  392. *stop_is_required = sleep_until_timeout_or_stop_request( ACTIVITY_TIMEOUT);
  393. }
  394. return err;
  395. }
  396. //>
  397. //<polling_thread
  398. static void* polling_thread(void*p)
  399. {
  400. ENTER("polling_thread");
  401. while(1)
  402. {
  403. int a_stop_is_required=0;
  404. SHOW_TIME("polling_thread > locking\n");
  405. int a_status = pthread_mutex_lock(&my_mutex);
  406. SHOW_TIME("polling_thread > locked (my_event_is_running = 0)\n");
  407. my_event_is_running = 0;
  408. pthread_mutex_unlock(&my_mutex);
  409. SHOW_TIME("polling_thread > unlocked\n");
  410. SHOW_TIME("polling_thread > wait for my_sem_start_is_required\n");
  411. while ((sem_wait(&my_sem_start_is_required) == -1) && errno == EINTR)
  412. {
  413. continue; // Restart when interrupted by handler
  414. }
  415. SHOW_TIME("polling_thread > get my_sem_start_is_required\n");
  416. a_status = pthread_mutex_lock(&my_mutex);
  417. SHOW_TIME("polling_thread > locked (my_event_is_running = 1)\n");
  418. my_event_is_running = 1;
  419. pthread_mutex_unlock(&my_mutex);
  420. SHOW_TIME("polling_thread > unlocked\n");
  421. a_stop_is_required=0;
  422. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required); // NOTE: may set a_stop_is_required to -1
  423. if ((a_status==0) && (a_stop_is_required > 0))
  424. {
  425. SHOW("polling_thread > stop required (%d)\n", __LINE__);
  426. while(0 == sem_trywait(&my_sem_stop_is_required))
  427. {
  428. };
  429. }
  430. else
  431. {
  432. a_stop_is_required=0;
  433. }
  434. // In this loop, my_event_is_running = 1
  435. while (head && (a_stop_is_required <= 0))
  436. {
  437. SHOW_TIME("polling_thread > check head\n");
  438. while(0 == sem_trywait(&my_sem_start_is_required))
  439. {
  440. };
  441. espeak_EVENT* event = (espeak_EVENT*)(head->data);
  442. assert(event);
  443. uint32_t time_in_ms = 0;
  444. int err = get_remaining_time((uint32_t)event->sample,
  445. &time_in_ms,
  446. &a_stop_is_required);
  447. if (a_stop_is_required > 0)
  448. {
  449. break;
  450. }
  451. else if (err != 0)
  452. {
  453. // No available time: the event is deleted.
  454. SHOW("polling_thread > %s\n","audio device down");
  455. a_status = pthread_mutex_lock(&my_mutex);
  456. SHOW_TIME("polling_thread > locked\n");
  457. event_delete( (espeak_EVENT*)pop());
  458. a_status = pthread_mutex_unlock(&my_mutex);
  459. SHOW_TIME("polling_thread > unlocked\n");
  460. }
  461. else if (time_in_ms==0)
  462. { // the event is already reached.
  463. if (my_callback)
  464. {
  465. event_notify(event);
  466. // the user_data (and the type) are cleaned to be sure
  467. // that MSG_TERMINATED is called twice (at delete time too).
  468. event->type=espeakEVENT_LIST_TERMINATED;
  469. event->user_data=NULL;
  470. }
  471. a_status = pthread_mutex_lock(&my_mutex);
  472. SHOW_TIME("polling_thread > locked\n");
  473. event_delete( (espeak_EVENT*)pop());
  474. a_status = pthread_mutex_unlock(&my_mutex);
  475. SHOW_TIME("polling_thread > unlocked\n");
  476. a_stop_is_required=0;
  477. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
  478. if ((a_status==0) && (a_stop_is_required > 0))
  479. {
  480. SHOW("polling_thread > stop required (%d)\n", __LINE__);
  481. while(0 == sem_trywait(&my_sem_stop_is_required))
  482. {
  483. };
  484. }
  485. else
  486. {
  487. a_stop_is_required=0;
  488. }
  489. }
  490. else
  491. { // The event will be notified soon: sleep until timeout or stop request
  492. a_stop_is_required = sleep_until_timeout_or_stop_request(time_in_ms);
  493. }
  494. }
  495. a_status = pthread_mutex_lock(&my_mutex);
  496. SHOW_TIME("polling_thread > locked\n");
  497. SHOW_TIME("polling_thread > my_event_is_running = 0\n");
  498. my_event_is_running = 0;
  499. if(a_stop_is_required <= 0)
  500. {
  501. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
  502. if ((a_status==0) && (a_stop_is_required > 0))
  503. {
  504. SHOW("polling_thread > stop required (%d)\n", __LINE__);
  505. while(0 == sem_trywait(&my_sem_stop_is_required))
  506. {
  507. };
  508. }
  509. else
  510. {
  511. a_stop_is_required=0;
  512. }
  513. }
  514. a_status = pthread_mutex_unlock(&my_mutex);
  515. SHOW_TIME("polling_thread > unlocked\n");
  516. if (a_stop_is_required > 0)
  517. {
  518. SHOW("polling_thread > %s\n","stop required!");
  519. // no mutex required since the stop command is synchronous
  520. // and waiting for my_sem_stop_is_acknowledged
  521. init();
  522. // acknowledge the stop request
  523. SHOW_TIME("polling_thread > post my_sem_stop_is_acknowledged\n");
  524. a_status = sem_post(&my_sem_stop_is_acknowledged);
  525. }
  526. }
  527. return NULL;
  528. }
  529. //>
  530. //<push, pop, init
  531. enum {MAX_NODE_COUNTER=1000};
  532. // return 1 if ok, 0 otherwise
  533. static espeak_ERROR push(void* the_data)
  534. {
  535. ENTER("event > push");
  536. assert((!head && !tail) || (head && tail));
  537. if (the_data == NULL)
  538. {
  539. SHOW("event > push > event=0x%x\n", NULL);
  540. return EE_INTERNAL_ERROR;
  541. }
  542. if (node_counter >= MAX_NODE_COUNTER)
  543. {
  544. SHOW("event > push > %s\n", "EE_BUFFER_FULL");
  545. return EE_BUFFER_FULL;
  546. }
  547. node *n = (node *)malloc(sizeof(node));
  548. if (n == NULL)
  549. {
  550. return EE_INTERNAL_ERROR;
  551. }
  552. if (head == NULL)
  553. {
  554. head = n;
  555. tail = n;
  556. }
  557. else
  558. {
  559. tail->next = n;
  560. tail = n;
  561. }
  562. tail->next = NULL;
  563. tail->data = the_data;
  564. node_counter++;
  565. SHOW("event > push > counter=%d (uid=%d)\n",node_counter,((espeak_EVENT*)the_data)->unique_identifier);
  566. return EE_OK;
  567. }
  568. static void* pop()
  569. {
  570. ENTER("event > pop");
  571. void* the_data = NULL;
  572. assert((!head && !tail) || (head && tail));
  573. if (head != NULL)
  574. {
  575. node* n = head;
  576. the_data = n->data;
  577. head = n->next;
  578. free(n);
  579. node_counter--;
  580. SHOW("event > pop > event=0x%x (counter=%d, uid=%d)\n",the_data, node_counter,((espeak_EVENT*)the_data)->unique_identifier);
  581. }
  582. if(head == NULL)
  583. {
  584. tail = NULL;
  585. }
  586. return the_data;
  587. }
  588. static void init()
  589. {
  590. ENTER("event > init");
  591. while (event_delete( (espeak_EVENT*)pop() ))
  592. {}
  593. node_counter = 0;
  594. }
  595. //>
  596. //<event_terminate
  597. void event_terminate()
  598. {
  599. ENTER("event_terminate");
  600. if (thread_inited)
  601. {
  602. pthread_cancel(my_thread);
  603. pthread_join(my_thread,NULL);
  604. pthread_mutex_destroy(&my_mutex);
  605. sem_destroy(&my_sem_start_is_required);
  606. sem_destroy(&my_sem_stop_is_required);
  607. sem_destroy(&my_sem_stop_is_acknowledged);
  608. init(); // purge event
  609. thread_inited = 0;
  610. }
  611. }
  612. //>