/native/external/espeak/src/event.cpp

http://eyes-free.googlecode.com/ · C++ · 725 lines · 503 code · 123 blank · 99 comment · 98 complexity · 820d44bf865425221a59ab022d75d86b MD5 · raw file

  1. /***************************************************************************
  2. * Copyright (C) 2007, Gilles Casse <gcasse@oralux.org> *
  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. 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, to;
  326. struct timeval tv;
  327. int err=0;
  328. clock_gettime2( &ts);
  329. to.tv_sec = ts.tv_sec;
  330. to.tv_nsec = ts.tv_nsec;
  331. add_time_in_ms( &ts, time_in_ms);
  332. SHOW("polling_thread > sleep_until_timeout_or_stop_request > start sem_timedwait from %d.%09lu to %d.%09lu \n",
  333. to.tv_sec, to.tv_nsec,
  334. ts.tv_sec, ts.tv_nsec);
  335. while ((err = sem_timedwait(&my_sem_stop_is_required, &ts)) == -1
  336. && errno == EINTR)
  337. {
  338. continue; // Restart when interrupted by handler
  339. }
  340. assert (gettimeofday(&tv, NULL) != -1);
  341. SHOW("polling_thread > sleep_until_timeout_or_stop_request > stop sem_timedwait %d.%09lu \n",
  342. tv.tv_sec, tv.tv_usec*1000);
  343. if (err == 0)
  344. {
  345. SHOW("polling_thread > sleep_until_timeout_or_stop_request > %s\n","stop required!");
  346. a_stop_is_required=1; // stop required
  347. }
  348. return a_stop_is_required;
  349. }
  350. //>
  351. //<get_remaining_time
  352. // Asked for the time interval required for reaching the sample.
  353. // If the stream is opened but the audio samples are not played,
  354. // a timeout is started.
  355. static int get_remaining_time(uint32_t sample, uint32_t* time_in_ms, int* stop_is_required)
  356. {
  357. ENTER("get_remaining_time");
  358. int err = 0;
  359. *stop_is_required = 0;
  360. int i=0;
  361. for (i=0; i < MAX_ACTIVITY_CHECK && (*stop_is_required == 0); i++)
  362. {
  363. err = wave_get_remaining_time( sample, time_in_ms);
  364. if (err || wave_is_busy(NULL) || (*time_in_ms == 0))
  365. { // if err, stream not available: quit
  366. // if wave is busy, time_in_ms is known: quit
  367. // if wave is not busy but remaining time == 0, event is reached: quit
  368. break;
  369. }
  370. // stream opened but not active
  371. //
  372. // Several possible states:
  373. // * the stream is opened but not yet started:
  374. //
  375. // wait for the start of stream
  376. //
  377. // * some samples have already been played,
  378. // ** the end of stream is reached
  379. // ** or there is an underrun
  380. //
  381. // wait for the close of stream
  382. *stop_is_required = sleep_until_timeout_or_stop_request( ACTIVITY_TIMEOUT);
  383. }
  384. return err;
  385. }
  386. //>
  387. //<polling_thread
  388. static void* polling_thread(void*)
  389. {
  390. ENTER("polling_thread");
  391. while(1)
  392. {
  393. int a_stop_is_required=0;
  394. SHOW_TIME("polling_thread > locking\n");
  395. int a_status = pthread_mutex_lock(&my_mutex);
  396. SHOW_TIME("polling_thread > locked (my_event_is_running = 0)\n");
  397. my_event_is_running = 0;
  398. pthread_mutex_unlock(&my_mutex);
  399. SHOW_TIME("polling_thread > unlocked\n");
  400. SHOW_TIME("polling_thread > wait for my_sem_start_is_required\n");
  401. while ((sem_wait(&my_sem_start_is_required) == -1) && errno == EINTR)
  402. {
  403. continue; // Restart when interrupted by handler
  404. }
  405. SHOW_TIME("polling_thread > get my_sem_start_is_required\n");
  406. a_status = pthread_mutex_lock(&my_mutex);
  407. SHOW_TIME("polling_thread > locked (my_event_is_running = 1)\n");
  408. my_event_is_running = 1;
  409. pthread_mutex_unlock(&my_mutex);
  410. SHOW_TIME("polling_thread > unlocked\n");
  411. a_stop_is_required=0;
  412. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
  413. if ((a_status==0) && a_stop_is_required)
  414. {
  415. SHOW("polling_thread > stop required (%d)\n", __LINE__);
  416. while(0 == sem_trywait(&my_sem_stop_is_required))
  417. {
  418. };
  419. }
  420. else
  421. {
  422. a_stop_is_required=0;
  423. }
  424. // In this loop, my_event_is_running = 1
  425. while (head && !a_stop_is_required)
  426. {
  427. SHOW_TIME("polling_thread > check head\n");
  428. while(0 == sem_trywait(&my_sem_start_is_required))
  429. {
  430. };
  431. espeak_EVENT* event = (espeak_EVENT*)(head->data);
  432. assert(event);
  433. uint32_t time_in_ms = 0;
  434. int err = get_remaining_time((uint32_t)event->sample,
  435. &time_in_ms,
  436. &a_stop_is_required);
  437. if (a_stop_is_required)
  438. {
  439. break;
  440. }
  441. else if (err != 0)
  442. {
  443. // No available time: the event is deleted.
  444. SHOW("polling_thread > %s\n","audio device down");
  445. a_status = pthread_mutex_lock(&my_mutex);
  446. SHOW_TIME("polling_thread > locked\n");
  447. event_delete( (espeak_EVENT*)pop());
  448. a_status = pthread_mutex_unlock(&my_mutex);
  449. SHOW_TIME("polling_thread > unlocked\n");
  450. }
  451. else if (time_in_ms==0)
  452. { // the event is already reached.
  453. if (my_callback)
  454. {
  455. event_notify(event);
  456. // the user_data (and the type) are cleaned to be sure
  457. // that MSG_TERMINATED is called twice (at delete time too).
  458. event->type=espeakEVENT_LIST_TERMINATED;
  459. event->user_data=NULL;
  460. }
  461. a_status = pthread_mutex_lock(&my_mutex);
  462. SHOW_TIME("polling_thread > locked\n");
  463. event_delete( (espeak_EVENT*)pop());
  464. a_status = pthread_mutex_unlock(&my_mutex);
  465. SHOW_TIME("polling_thread > unlocked\n");
  466. a_stop_is_required=0;
  467. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
  468. if ((a_status==0) && a_stop_is_required)
  469. {
  470. SHOW("polling_thread > stop required (%d)\n", __LINE__);
  471. while(0 == sem_trywait(&my_sem_stop_is_required))
  472. {
  473. };
  474. }
  475. else
  476. {
  477. a_stop_is_required=0;
  478. }
  479. }
  480. else
  481. { // The event will be notified soon: sleep until timeout or stop request
  482. a_stop_is_required = sleep_until_timeout_or_stop_request(time_in_ms);
  483. }
  484. }
  485. a_status = pthread_mutex_lock(&my_mutex);
  486. SHOW_TIME("polling_thread > locked\n");
  487. SHOW_TIME("polling_thread > my_event_is_running = 0\n");
  488. my_event_is_running = 0;
  489. if(!a_stop_is_required)
  490. {
  491. a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
  492. if ((a_status==0) && a_stop_is_required)
  493. {
  494. SHOW("polling_thread > stop required (%d)\n", __LINE__);
  495. while(0 == sem_trywait(&my_sem_stop_is_required))
  496. {
  497. };
  498. }
  499. else
  500. {
  501. a_stop_is_required=0;
  502. }
  503. }
  504. a_status = pthread_mutex_unlock(&my_mutex);
  505. SHOW_TIME("polling_thread > unlocked\n");
  506. if (a_stop_is_required)
  507. {
  508. SHOW("polling_thread > %s\n","stop required!");
  509. // no mutex required since the stop command is synchronous
  510. // and waiting for my_sem_stop_is_acknowledged
  511. init();
  512. // acknowledge the stop request
  513. SHOW_TIME("polling_thread > post my_sem_stop_is_acknowledged\n");
  514. a_status = sem_post(&my_sem_stop_is_acknowledged);
  515. }
  516. }
  517. return NULL;
  518. }
  519. //>
  520. //<push, pop, init
  521. enum {MAX_NODE_COUNTER=1000};
  522. // return 1 if ok, 0 otherwise
  523. static espeak_ERROR push(void* the_data)
  524. {
  525. ENTER("event > push");
  526. assert((!head && !tail) || (head && tail));
  527. if (the_data == NULL)
  528. {
  529. SHOW("event > push > event=0x%x\n", NULL);
  530. return EE_INTERNAL_ERROR;
  531. }
  532. if (node_counter >= MAX_NODE_COUNTER)
  533. {
  534. SHOW("event > push > %s\n", "EE_BUFFER_FULL");
  535. return EE_BUFFER_FULL;
  536. }
  537. node *n = (node *)malloc(sizeof(node));
  538. if (n == NULL)
  539. {
  540. return EE_INTERNAL_ERROR;
  541. }
  542. if (head == NULL)
  543. {
  544. head = n;
  545. tail = n;
  546. }
  547. else
  548. {
  549. tail->next = n;
  550. tail = n;
  551. }
  552. tail->next = NULL;
  553. tail->data = the_data;
  554. node_counter++;
  555. SHOW("event > push > counter=%d (uid=%d)\n",node_counter,((espeak_EVENT*)the_data)->unique_identifier);
  556. return EE_OK;
  557. }
  558. static void* pop()
  559. {
  560. ENTER("event > pop");
  561. void* the_data = NULL;
  562. assert((!head && !tail) || (head && tail));
  563. if (head != NULL)
  564. {
  565. node* n = head;
  566. the_data = n->data;
  567. head = n->next;
  568. free(n);
  569. node_counter--;
  570. SHOW("event > pop > event=0x%x (counter=%d, uid=%d)\n",the_data, node_counter,((espeak_EVENT*)the_data)->unique_identifier);
  571. }
  572. if(head == NULL)
  573. {
  574. tail = NULL;
  575. }
  576. return the_data;
  577. }
  578. static void init()
  579. {
  580. ENTER("event > init");
  581. while (event_delete( (espeak_EVENT*)pop() ))
  582. {}
  583. node_counter = 0;
  584. }
  585. //>
  586. //<event_terminate
  587. void event_terminate()
  588. {
  589. ENTER("event_terminate");
  590. if (my_thread)
  591. {
  592. pthread_cancel(my_thread);
  593. pthread_join(my_thread,NULL);
  594. pthread_mutex_destroy(&my_mutex);
  595. sem_destroy(&my_sem_start_is_required);
  596. sem_destroy(&my_sem_stop_is_required);
  597. sem_destroy(&my_sem_stop_is_acknowledged);
  598. init(); // purge event
  599. }
  600. }
  601. #endif
  602. //>