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

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