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

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