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

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