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.

fifo.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /***************************************************************************
  2. * Copyright (C) 2007, Gilles Casse <[email protected]> *
  3. * Copyright (C) 2013 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 <wchar.h>
  31. #include <errno.h>
  32. #include <sys/time.h>
  33. #include <time.h>
  34. #include "speech.h"
  35. #include "fifo.h"
  36. #include "wave.h"
  37. #include "debug.h"
  38. //>
  39. //<decls and function prototypes
  40. // my_mutex: protects my_thread_is_talking,
  41. // my_stop_is_required, and the command fifo
  42. static pthread_mutex_t my_mutex;
  43. static int my_command_is_running = 0;
  44. static int my_stop_is_required = 0;
  45. // + fifo
  46. //
  47. // my_thread: reads commands from the fifo, and runs them.
  48. static pthread_t my_thread;
  49. static sem_t my_sem_start_is_required;
  50. static sem_t my_sem_stop_is_acknowledged;
  51. static void* say_thread(void*);
  52. static espeak_ERROR push(t_espeak_command* the_command);
  53. static t_espeak_command* pop();
  54. static void init(int process_parameters);
  55. static int node_counter=0;
  56. enum {MAX_NODE_COUNTER=400,
  57. INACTIVITY_TIMEOUT=50, // in ms, check that the stream is inactive
  58. MAX_INACTIVITY_CHECK=2
  59. };
  60. //>
  61. //<fifo_init
  62. void fifo_init()
  63. {
  64. ENTER("fifo_init");
  65. // security
  66. pthread_mutex_init( &my_mutex, (const pthread_mutexattr_t *)NULL);
  67. init(0);
  68. assert(-1 != sem_init(&my_sem_start_is_required, 0, 0));
  69. assert(-1 != sem_init(&my_sem_stop_is_acknowledged, 0, 0));
  70. pthread_attr_t a_attrib;
  71. if (pthread_attr_init (& a_attrib)
  72. || pthread_attr_setdetachstate(&a_attrib, PTHREAD_CREATE_JOINABLE)
  73. || pthread_create( &my_thread,
  74. & a_attrib,
  75. say_thread,
  76. (void*)NULL))
  77. {
  78. assert(0);
  79. }
  80. pthread_attr_destroy(&a_attrib);
  81. // leave once the thread is actually started
  82. SHOW_TIME("fifo > wait for my_sem_stop_is_acknowledged\n");
  83. while ((sem_wait(&my_sem_stop_is_acknowledged) == -1) && errno == EINTR)
  84. {
  85. continue; // Restart when interrupted by handler
  86. }
  87. SHOW_TIME("fifo > get my_sem_stop_is_acknowledged\n");
  88. }
  89. //>
  90. //<fifo_add_command
  91. espeak_ERROR fifo_add_command (t_espeak_command* the_command)
  92. {
  93. ENTER("fifo_add_command");
  94. int a_status = pthread_mutex_lock(&my_mutex);
  95. espeak_ERROR a_error = EE_OK;
  96. if (!a_status)
  97. {
  98. SHOW_TIME("fifo_add_command > locked\n");
  99. a_error = push(the_command);
  100. SHOW_TIME("fifo_add_command > unlocking\n");
  101. a_status = pthread_mutex_unlock(&my_mutex);
  102. }
  103. if (!a_status && !my_command_is_running && (a_error == EE_OK))
  104. {
  105. // quit when command is actually started
  106. // (for possible forthcoming 'end of command' checks)
  107. SHOW_TIME("fifo_add_command > post my_sem_start_is_required\n");
  108. sem_post(&my_sem_start_is_required);
  109. int val=1;
  110. while (val > 0)
  111. {
  112. usleep(50000); // TBD: event?
  113. sem_getvalue(&my_sem_start_is_required, &val);
  114. }
  115. }
  116. if (a_status != 0)
  117. {
  118. a_error = EE_INTERNAL_ERROR;
  119. }
  120. SHOW_TIME("LEAVE fifo_add_command");
  121. return a_error;
  122. }
  123. //>
  124. //<fifo_add_commands
  125. espeak_ERROR fifo_add_commands (t_espeak_command* command1, t_espeak_command* command2)
  126. {
  127. ENTER("fifo_add_command");
  128. int a_status = pthread_mutex_lock(&my_mutex);
  129. espeak_ERROR a_error = EE_OK;
  130. if (!a_status)
  131. {
  132. SHOW_TIME("fifo_add_commands > locked\n");
  133. if (node_counter+1 >= MAX_NODE_COUNTER)
  134. {
  135. SHOW("push > %s\n", "EE_BUFFER_FULL");
  136. a_error = EE_BUFFER_FULL;
  137. }
  138. else
  139. {
  140. push(command1);
  141. push(command2);
  142. }
  143. SHOW_TIME("fifo_add_command > unlocking\n");
  144. a_status = pthread_mutex_unlock(&my_mutex);
  145. }
  146. if (!a_status && !my_command_is_running && (a_error == EE_OK))
  147. {
  148. // quit when one command is actually started
  149. // (for possible forthcoming 'end of command' checks)
  150. SHOW_TIME("fifo_add_command > post my_sem_start_is_required\n");
  151. sem_post(&my_sem_start_is_required);
  152. int val=1;
  153. while (val > 0)
  154. {
  155. usleep(50000); // TBD: event?
  156. sem_getvalue(&my_sem_start_is_required, &val);
  157. }
  158. }
  159. if (a_status != 0)
  160. {
  161. a_error = EE_INTERNAL_ERROR;
  162. }
  163. SHOW_TIME("LEAVE fifo_add_commands");
  164. return a_error;
  165. }
  166. //>
  167. //<fifo_stop
  168. espeak_ERROR fifo_stop ()
  169. {
  170. ENTER("fifo_stop");
  171. int a_command_is_running = 0;
  172. int a_status = pthread_mutex_lock(&my_mutex);
  173. SHOW_TIME("fifo_stop > locked\n");
  174. if (a_status != 0)
  175. {
  176. return EE_INTERNAL_ERROR;
  177. }
  178. if (my_command_is_running)
  179. {
  180. a_command_is_running = 1;
  181. my_stop_is_required = 1;
  182. SHOW_TIME("fifo_stop > my_stop_is_required = 1\n");
  183. }
  184. SHOW_TIME("fifo_stop > unlocking\n");
  185. a_status = pthread_mutex_unlock(&my_mutex);
  186. if (a_status != 0)
  187. {
  188. return EE_INTERNAL_ERROR;
  189. }
  190. if (a_command_is_running)
  191. {
  192. SHOW_TIME("fifo_stop > wait for my_sem_stop_is_acknowledged\n");
  193. while ((sem_wait(&my_sem_stop_is_acknowledged) == -1) && errno == EINTR)
  194. {
  195. continue; // Restart when interrupted by handler
  196. }
  197. SHOW_TIME("fifo_stop > get my_sem_stop_is_acknowledged\n");
  198. }
  199. SHOW_TIME("fifo_stop > my_stop_is_required = 0\n");
  200. my_stop_is_required = 0;
  201. SHOW_TIME("LEAVE fifo_stop\n");
  202. return EE_OK;
  203. }
  204. //>
  205. //<fifo_is_speaking
  206. int fifo_is_busy ()
  207. {
  208. // ENTER("isSpeaking");
  209. // int aResult = (int) (my_command_is_running || WaveIsPlaying());
  210. SHOW("fifo_is_busy > aResult = %d\n",my_command_is_running);
  211. return my_command_is_running;
  212. }
  213. // int pause ()
  214. // {
  215. // ENTER("pause");
  216. // // TBD
  217. // // if (espeakPause (espeakHandle, 1))
  218. // return true;
  219. // }
  220. // int resume ()
  221. // {
  222. // ENTER("resume");
  223. // // TBD
  224. // // if (espeakPause (espeakHandle, 0))
  225. // return true;
  226. // }
  227. //>
  228. //<sleep_until_start_request_or_inactivity
  229. static int sleep_until_start_request_or_inactivity()
  230. {
  231. SHOW_TIME("fifo > sleep_until_start_request_or_inactivity > ENTER");
  232. int a_start_is_required=0;
  233. // Wait for the start request (my_sem_start_is_required).
  234. // Besides this, if the audio stream is still busy,
  235. // check from time to time its end.
  236. // The end of the stream is confirmed by several checks
  237. // for filtering underflow.
  238. //
  239. int i=0;
  240. while((i<= MAX_INACTIVITY_CHECK) && !a_start_is_required)
  241. {
  242. if (wave_is_busy( NULL) )
  243. {
  244. i = 0;
  245. }
  246. else
  247. {
  248. i++;
  249. }
  250. int err=0;
  251. struct timespec ts;
  252. struct timeval tv;
  253. clock_gettime2( &ts);
  254. #ifdef DEBUG_ENABLED
  255. struct timespec to;
  256. to.tv_sec = ts.tv_sec;
  257. to.tv_nsec = ts.tv_nsec;
  258. #endif
  259. add_time_in_ms( &ts, INACTIVITY_TIMEOUT);
  260. SHOW("fifo > sleep_until_start_request_or_inactivity > start sem_timedwait (start_is_required) from %d.%09lu to %d.%09lu \n",
  261. to.tv_sec, to.tv_nsec,
  262. ts.tv_sec, ts.tv_nsec);
  263. while ((err = sem_timedwait(&my_sem_start_is_required, &ts)) == -1
  264. && errno == EINTR)
  265. {
  266. continue;
  267. }
  268. assert (gettimeofday(&tv, NULL) != -1);
  269. SHOW("fifo > sleep_until_start_request_or_inactivity > stop sem_timedwait (start_is_required, err=%d) %d.%09lu \n", err,
  270. tv.tv_sec, tv.tv_usec*1000);
  271. if (err==0)
  272. {
  273. a_start_is_required = 1;
  274. }
  275. }
  276. SHOW_TIME("fifo > sleep_until_start_request_or_inactivity > LEAVE");
  277. return a_start_is_required;
  278. }
  279. //>
  280. //<close_stream
  281. static void close_stream()
  282. {
  283. SHOW_TIME("fifo > close_stream > ENTER\n");
  284. // Warning: a wave_close can be already required by
  285. // an external command (espeak_Cancel + fifo_stop), if so:
  286. // my_stop_is_required = 1;
  287. int a_status = pthread_mutex_lock(&my_mutex);
  288. assert (!a_status);
  289. int a_stop_is_required = my_stop_is_required;
  290. if (!a_stop_is_required)
  291. {
  292. my_command_is_running = 1;
  293. }
  294. a_status = pthread_mutex_unlock(&my_mutex);
  295. if (!a_stop_is_required)
  296. {
  297. wave_close(NULL);
  298. int a_status = pthread_mutex_lock(&my_mutex);
  299. assert (!a_status);
  300. my_command_is_running = 0;
  301. a_stop_is_required = my_stop_is_required;
  302. a_status = pthread_mutex_unlock(&my_mutex);
  303. if (a_stop_is_required)
  304. {
  305. // acknowledge the stop request
  306. SHOW_TIME("fifo > close_stream > post my_sem_stop_is_acknowledged\n");
  307. int a_status = sem_post(&my_sem_stop_is_acknowledged);
  308. assert( a_status != -1);
  309. }
  310. }
  311. SHOW_TIME("fifo > close_stream > LEAVE\n");
  312. }
  313. //>
  314. //<say_thread
  315. static void* say_thread(void*)
  316. {
  317. ENTER("say_thread");
  318. SHOW_TIME("say_thread > post my_sem_stop_is_acknowledged\n");
  319. // announce that thread is started
  320. sem_post(&my_sem_stop_is_acknowledged);
  321. int look_for_inactivity=0;
  322. while(1)
  323. {
  324. SHOW_TIME("say_thread > wait for my_sem_start_is_required\n");
  325. int a_start_is_required = 0;
  326. if (look_for_inactivity)
  327. {
  328. a_start_is_required = sleep_until_start_request_or_inactivity();
  329. if (!a_start_is_required)
  330. {
  331. close_stream();
  332. }
  333. }
  334. look_for_inactivity = 1;
  335. if (!a_start_is_required)
  336. {
  337. while ((sem_wait(&my_sem_start_is_required) == -1) && errno == EINTR)
  338. {
  339. continue; // Restart when interrupted by handler
  340. }
  341. }
  342. SHOW_TIME("say_thread > get my_sem_start_is_required\n");
  343. SHOW_TIME("say_thread > my_command_is_running = 1\n");
  344. my_command_is_running = 1;
  345. while( my_command_is_running)
  346. {
  347. SHOW_TIME("say_thread > locking\n");
  348. int a_status = pthread_mutex_lock(&my_mutex);
  349. assert (!a_status);
  350. t_espeak_command* a_command = (t_espeak_command*)pop();
  351. if (a_command == NULL)
  352. {
  353. SHOW_TIME("say_thread > text empty (talking=0) \n");
  354. a_status = pthread_mutex_unlock(&my_mutex);
  355. SHOW_TIME("say_thread > unlocked\n");
  356. SHOW_TIME("say_thread > my_command_is_running = 0\n");
  357. my_command_is_running = 0;
  358. }
  359. else
  360. {
  361. display_espeak_command(a_command);
  362. // purge start semaphore
  363. SHOW_TIME("say_thread > purge my_sem_start_is_required\n");
  364. while(0 == sem_trywait(&my_sem_start_is_required))
  365. {
  366. };
  367. if (my_stop_is_required)
  368. {
  369. SHOW_TIME("say_thread > my_command_is_running = 0\n");
  370. my_command_is_running = 0;
  371. }
  372. SHOW_TIME("say_thread > unlocking\n");
  373. a_status = pthread_mutex_unlock(&my_mutex);
  374. if (my_command_is_running)
  375. {
  376. process_espeak_command(a_command);
  377. }
  378. delete_espeak_command(a_command);
  379. }
  380. }
  381. if (my_stop_is_required)
  382. {
  383. // no mutex required since the stop command is synchronous
  384. // and waiting for my_sem_stop_is_acknowledged
  385. init(1);
  386. // purge start semaphore
  387. SHOW_TIME("say_thread > purge my_sem_start_is_required\n");
  388. while(0==sem_trywait(&my_sem_start_is_required))
  389. {
  390. };
  391. // acknowledge the stop request
  392. SHOW_TIME("say_thread > post my_sem_stop_is_acknowledged\n");
  393. int a_status = sem_post(&my_sem_stop_is_acknowledged);
  394. assert( a_status != -1);
  395. }
  396. // and wait for the next start
  397. SHOW_TIME("say_thread > wait for my_sem_start_is_required\n");
  398. }
  399. return NULL;
  400. }
  401. int fifo_is_command_enabled()
  402. {
  403. SHOW("ENTER fifo_is_command_enabled=%d\n",(int)(0 == my_stop_is_required));
  404. return (0 == my_stop_is_required);
  405. }
  406. //>
  407. //<fifo
  408. typedef struct t_node
  409. {
  410. t_espeak_command* data;
  411. t_node *next;
  412. } node;
  413. static node* head=NULL;
  414. static node* tail=NULL;
  415. // return 1 if ok, 0 otherwise
  416. static espeak_ERROR push(t_espeak_command* the_command)
  417. {
  418. ENTER("fifo > push");
  419. assert((!head && !tail) || (head && tail));
  420. if (the_command == NULL)
  421. {
  422. SHOW("push > command=0x%x\n", NULL);
  423. return EE_INTERNAL_ERROR;
  424. }
  425. if (node_counter >= MAX_NODE_COUNTER)
  426. {
  427. SHOW("push > %s\n", "EE_BUFFER_FULL");
  428. return EE_BUFFER_FULL;
  429. }
  430. node *n = (node *)malloc(sizeof(node));
  431. if (n == NULL)
  432. {
  433. return EE_INTERNAL_ERROR;
  434. }
  435. if (head == NULL)
  436. {
  437. head = n;
  438. tail = n;
  439. }
  440. else
  441. {
  442. tail->next = n;
  443. tail = n;
  444. }
  445. tail->next = NULL;
  446. tail->data = the_command;
  447. node_counter++;
  448. SHOW("push > counter=%d\n",node_counter);
  449. the_command->state = CS_PENDING;
  450. display_espeak_command(the_command);
  451. return EE_OK;
  452. }
  453. static t_espeak_command* pop()
  454. {
  455. ENTER("fifo > pop");
  456. t_espeak_command* the_command = NULL;
  457. assert((!head && !tail) || (head && tail));
  458. if (head != NULL)
  459. {
  460. node* n = head;
  461. the_command = n->data;
  462. head = n->next;
  463. free(n);
  464. node_counter--;
  465. SHOW("pop > command=0x%x (counter=%d)\n",the_command, node_counter);
  466. }
  467. if(head == NULL)
  468. {
  469. tail = NULL;
  470. }
  471. display_espeak_command(the_command);
  472. return the_command;
  473. }
  474. static void init(int process_parameters)
  475. {
  476. // Changed by Tyler Spivey 30.Nov.2011
  477. t_espeak_command *c = NULL;
  478. ENTER("fifo > init");
  479. c = pop();
  480. while (c != NULL) {
  481. if (process_parameters && (c->type == ET_PARAMETER || c->type == ET_VOICE_NAME || c->type == ET_VOICE_SPEC))
  482. {
  483. process_espeak_command(c);
  484. }
  485. delete_espeak_command(c);
  486. c = pop();
  487. }
  488. node_counter = 0;
  489. }
  490. //>
  491. //<fifo_init
  492. void fifo_terminate()
  493. {
  494. ENTER("fifo_terminate");
  495. pthread_cancel(my_thread);
  496. pthread_join(my_thread,NULL);
  497. pthread_mutex_destroy(&my_mutex);
  498. sem_destroy(&my_sem_start_is_required);
  499. sem_destroy(&my_sem_stop_is_acknowledged);
  500. init(0); // purge fifo
  501. }
  502. //>