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.

wave_pulse.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * Copyright (C) 2007, Gilles Casse <[email protected]>
  3. * Copyright (C) 2015 Reece H. Dunn
  4. * eSpeak driver for PulseAudio
  5. * based on the XMMS PulseAudio Plugin
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the
  19. * Free Software Foundation, Inc.,
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. // TBD:
  23. // * ARCH_BIG
  24. // * uint64? a_timing_info.read_index
  25. // * prebuf,... size?
  26. // * 0.9.6: pb pulse_free using tlength=8820 (max size never returned -> tlength=10000 ok, but higher drain).
  27. //
  28. #include "speech.h"
  29. #ifdef USE_ASYNC
  30. // This source file is only used for asynchronious modes
  31. #include <stdbool.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <math.h>
  36. #include <assert.h>
  37. #include <sys/time.h>
  38. #include <time.h>
  39. #include <pulse/pulseaudio.h>
  40. #include <pthread.h>
  41. #ifndef PLATFORM_WINDOWS
  42. #include <unistd.h>
  43. #endif
  44. #include "wave.h"
  45. #include "debug.h"
  46. enum {ONE_BILLION=1000000000};
  47. enum {
  48. /* return value */
  49. PULSE_OK = 0,
  50. PULSE_ERROR = -1,
  51. PULSE_NO_CONNECTION = -2
  52. };
  53. #ifdef USE_PULSEAUDIO
  54. static t_wave_callback* my_callback_is_output_enabled=NULL;
  55. #define SAMPLE_RATE 22050
  56. #define ESPEAK_FORMAT PA_SAMPLE_S16LE
  57. #define ESPEAK_CHANNEL 1
  58. #define MAXLENGTH 132300
  59. #define TLENGTH 4410
  60. #define PREBUF 2200
  61. #define MINREQ 880
  62. #define FRAGSIZE 0
  63. #ifdef USE_PORTAUDIO
  64. // rename functions to be wrapped
  65. #define wave_init wave_pulse_init
  66. #define wave_open wave_pulse_open
  67. #define wave_write wave_pulse_write
  68. #define wave_close wave_pulse_close
  69. #define wave_is_busy wave_pulse_is_busy
  70. #define wave_terminate wave_pulse_terminate
  71. #define wave_get_read_position wave_pulse_get_read_position
  72. #define wave_get_write_position wave_pulse_get_write_position
  73. #define wave_flush wave_pulse_flush
  74. #define wave_set_callback_is_output_enabled wave_pulse_set_callback_is_output_enabled
  75. #define wave_test_get_write_buffer wave_pulse_test_get_write_buffer
  76. #define wave_get_remaining_time wave_pulse_get_remaining_time
  77. // check whether we can connect to PulseAudio
  78. #include <pulse/simple.h>
  79. int is_pulse_running()
  80. {
  81. pa_sample_spec ss;
  82. ss.format = ESPEAK_FORMAT;
  83. ss.rate = SAMPLE_RATE;
  84. ss.channels = ESPEAK_CHANNEL;
  85. pa_simple *s = pa_simple_new(NULL, "eSpeak", PA_STREAM_PLAYBACK, NULL, "is_pulse_running", &ss, NULL, NULL, NULL);
  86. if (s) {
  87. pa_simple_free(s);
  88. return 1;
  89. } else
  90. return 0;
  91. }
  92. #endif // USE_PORTAUDIO
  93. static pthread_mutex_t pulse_mutex;
  94. static pa_context *context = NULL;
  95. static pa_stream *stream = NULL;
  96. static pa_threaded_mainloop *mainloop = NULL;
  97. static int do_trigger = 0;
  98. static uint64_t written = 0;
  99. static int time_offset_msec = 0;
  100. static int just_flushed = 0;
  101. static int connected = 0;
  102. static int wave_samplerate;
  103. #define CHECK_DEAD_GOTO(label, warn) do { \
  104. if (!mainloop || \
  105. !context || pa_context_get_state(context) != PA_CONTEXT_READY || \
  106. !stream || pa_stream_get_state(stream) != PA_STREAM_READY) { \
  107. if (warn) \
  108. SHOW("Connection died: %s\n", context ? pa_strerror(pa_context_errno(context)) : "NULL"); \
  109. goto label; \
  110. } \
  111. } while(0);
  112. #define CHECK_CONNECTED(retval) \
  113. do { \
  114. if (!connected) return retval; \
  115. } while (0);
  116. #define CHECK_CONNECTED_NO_RETVAL(id) \
  117. do { \
  118. if (!connected) { SHOW("CHECK_CONNECTED_NO_RETVAL: !pulse_connected\n", ""); return; } \
  119. } while (0);
  120. static void subscribe_cb(struct pa_context *c, enum pa_subscription_event_type t, uint32_t index, void *userdata) {
  121. ENTER(__FUNCTION__);
  122. assert(c);
  123. if (!stream ||
  124. index != pa_stream_get_index(stream) ||
  125. (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
  126. t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW)))
  127. return;
  128. }
  129. static void context_state_cb(pa_context *c, void *userdata) {
  130. ENTER(__FUNCTION__);
  131. assert(c);
  132. switch (pa_context_get_state(c)) {
  133. case PA_CONTEXT_READY:
  134. case PA_CONTEXT_TERMINATED:
  135. case PA_CONTEXT_FAILED:
  136. pa_threaded_mainloop_signal(mainloop, 0);
  137. break;
  138. case PA_CONTEXT_UNCONNECTED:
  139. case PA_CONTEXT_CONNECTING:
  140. case PA_CONTEXT_AUTHORIZING:
  141. case PA_CONTEXT_SETTING_NAME:
  142. break;
  143. }
  144. }
  145. static void stream_state_cb(pa_stream *s, void * userdata) {
  146. ENTER(__FUNCTION__);
  147. assert(s);
  148. switch (pa_stream_get_state(s)) {
  149. case PA_STREAM_READY:
  150. case PA_STREAM_FAILED:
  151. case PA_STREAM_TERMINATED:
  152. pa_threaded_mainloop_signal(mainloop, 0);
  153. break;
  154. case PA_STREAM_UNCONNECTED:
  155. case PA_STREAM_CREATING:
  156. break;
  157. }
  158. }
  159. static void stream_success_cb(pa_stream *s, int success, void *userdata) {
  160. ENTER(__FUNCTION__);
  161. assert(s);
  162. if (userdata)
  163. *(int*) userdata = success;
  164. pa_threaded_mainloop_signal(mainloop, 0);
  165. }
  166. static void context_success_cb(pa_context *c, int success, void *userdata) {
  167. ENTER(__FUNCTION__);
  168. assert(c);
  169. if (userdata)
  170. *(int*) userdata = success;
  171. pa_threaded_mainloop_signal(mainloop, 0);
  172. }
  173. static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
  174. ENTER(__FUNCTION__);
  175. assert(s);
  176. pa_threaded_mainloop_signal(mainloop, 0);
  177. }
  178. static void stream_latency_update_cb(pa_stream *s, void *userdata) {
  179. assert(s);
  180. pa_threaded_mainloop_signal(mainloop, 0);
  181. }
  182. static int pulse_free(void) {
  183. ENTER(__FUNCTION__);
  184. size_t l = 0;
  185. pa_operation *o = NULL;
  186. CHECK_CONNECTED(0);
  187. SHOW("pulse_free: %s (call)\n", "pa_threaded_main_loop_lock");
  188. pa_threaded_mainloop_lock(mainloop);
  189. CHECK_DEAD_GOTO(fail, 1);
  190. if ((l = pa_stream_writable_size(stream)) == (size_t) -1) {
  191. SHOW("pa_stream_writable_size() failed: %s", pa_strerror(pa_context_errno(context)));
  192. l = 0;
  193. goto fail;
  194. }
  195. SHOW("pulse_free: %s (ret=%d)\n", "pa_stream_writable_size", l);
  196. /* If this function is called twice with no pulse_write() call in
  197. * between this means we should trigger the playback */
  198. if (do_trigger) {
  199. int success = 0;
  200. SHOW("pulse_free: %s (call)\n", "pa_stream_trigger");
  201. if (!(o = pa_stream_trigger(stream, stream_success_cb, &success))) {
  202. SHOW("pa_stream_trigger() failed: %s", pa_strerror(pa_context_errno(context)));
  203. goto fail;
  204. }
  205. SHOW("pulse_free: %s (call)\n", "pa_threaded_main_loop");
  206. while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
  207. CHECK_DEAD_GOTO(fail, 1);
  208. pa_threaded_mainloop_wait(mainloop);
  209. }
  210. SHOW("pulse_free: %s (ret)\n", "pa_threaded_main_loop");
  211. if (!success)
  212. SHOW("pa_stream_trigger() failed: %s", pa_strerror(pa_context_errno(context)));
  213. }
  214. fail:
  215. SHOW("pulse_free: %s (call)\n", "pa_operation_unref");
  216. if (o)
  217. pa_operation_unref(o);
  218. SHOW("pulse_free: %s (call)\n", "pa_threaded_main_loop_unlock");
  219. pa_threaded_mainloop_unlock(mainloop);
  220. do_trigger = !!l;
  221. SHOW("pulse_free: %d (ret)\n", (int)l);
  222. return (int) l;
  223. }
  224. static int pulse_playing(const pa_timing_info *the_timing_info) {
  225. ENTER(__FUNCTION__);
  226. int r = 0;
  227. const pa_timing_info *i;
  228. assert(the_timing_info);
  229. CHECK_CONNECTED(0);
  230. pa_threaded_mainloop_lock(mainloop);
  231. for (;; ) {
  232. CHECK_DEAD_GOTO(fail, 1);
  233. if ((i = pa_stream_get_timing_info(stream)))
  234. {
  235. break;
  236. }
  237. if (pa_context_errno(context) != PA_ERR_NODATA) {
  238. SHOW("pa_stream_get_timing_info() failed: %s", pa_strerror(pa_context_errno(context)));
  239. goto fail;
  240. }
  241. pa_threaded_mainloop_wait(mainloop);
  242. }
  243. r = i->playing;
  244. memcpy((void*)the_timing_info, (void*)i, sizeof(pa_timing_info));
  245. fail:
  246. pa_threaded_mainloop_unlock(mainloop);
  247. return r;
  248. }
  249. static void pulse_write(void* ptr, int length) {
  250. ENTER(__FUNCTION__);
  251. SHOW("pulse_write > length=%d\n", length);
  252. CHECK_CONNECTED_NO_RETVAL();
  253. pa_threaded_mainloop_lock(mainloop);
  254. CHECK_DEAD_GOTO(fail, 1);
  255. if (pa_stream_write(stream, ptr, length, NULL, PA_SEEK_RELATIVE, (pa_seek_mode_t)0) < 0) {
  256. SHOW("pa_stream_write() failed: %s", pa_strerror(pa_context_errno(context)));
  257. goto fail;
  258. }
  259. do_trigger = 0;
  260. written += length;
  261. fail:
  262. pa_threaded_mainloop_unlock(mainloop);
  263. }
  264. static int drain(void) {
  265. pa_operation *o = NULL;
  266. int success = 0;
  267. int ret = PULSE_ERROR;
  268. ENTER(__FUNCTION__);
  269. CHECK_CONNECTED(ret);
  270. pa_threaded_mainloop_lock(mainloop);
  271. CHECK_DEAD_GOTO(fail, 0);
  272. SHOW_TIME("pa_stream_drain (call)");
  273. if (!(o = pa_stream_drain(stream, stream_success_cb, &success))) {
  274. SHOW("pa_stream_drain() failed: %s\n", pa_strerror(pa_context_errno(context)));
  275. goto fail;
  276. }
  277. SHOW_TIME("pa_threaded_mainloop_wait (call)");
  278. while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
  279. CHECK_DEAD_GOTO(fail, 1);
  280. pa_threaded_mainloop_wait(mainloop);
  281. }
  282. SHOW_TIME("pa_threaded_mainloop_wait (ret)");
  283. if (!success) {
  284. SHOW("pa_stream_drain() failed: %s\n", pa_strerror(pa_context_errno(context)));
  285. }
  286. else {
  287. ret = PULSE_OK;
  288. }
  289. fail:
  290. SHOW_TIME("pa_operation_unref (call)");
  291. if (o)
  292. pa_operation_unref(o);
  293. pa_threaded_mainloop_unlock(mainloop);
  294. SHOW_TIME("drain (ret)");
  295. return ret;
  296. }
  297. static void pulse_close(void) {
  298. ENTER(__FUNCTION__);
  299. drain();
  300. connected = 0;
  301. if (mainloop)
  302. pa_threaded_mainloop_stop(mainloop);
  303. connected = 0;
  304. if (context) {
  305. SHOW_TIME("pa_context_disconnect (call)");
  306. pa_context_disconnect(context);
  307. pa_context_unref(context);
  308. context = NULL;
  309. }
  310. if (mainloop) {
  311. SHOW_TIME("pa_threaded_mainloop_free (call)");
  312. pa_threaded_mainloop_free(mainloop);
  313. mainloop = NULL;
  314. }
  315. SHOW_TIME("pulse_close (ret)");
  316. }
  317. static int pulse_open()
  318. {
  319. ENTER(__FUNCTION__);
  320. pa_sample_spec ss;
  321. pa_operation *o = NULL;
  322. int success;
  323. int ret = PULSE_ERROR;
  324. assert(!mainloop);
  325. assert(!context);
  326. assert(!stream);
  327. assert(!connected);
  328. pthread_mutex_init( &pulse_mutex, (const pthread_mutexattr_t *)NULL);
  329. ss.format = ESPEAK_FORMAT;
  330. ss.rate = wave_samplerate;
  331. ss.channels = ESPEAK_CHANNEL;
  332. if (!pa_sample_spec_valid(&ss))
  333. return false;
  334. SHOW_TIME("pa_threaded_mainloop_new (call)");
  335. if (!(mainloop = pa_threaded_mainloop_new())) {
  336. SHOW("Failed to allocate main loop\n","");
  337. goto fail;
  338. }
  339. pa_threaded_mainloop_lock(mainloop);
  340. SHOW_TIME("pa_context_new (call)");
  341. if (!(context = pa_context_new(pa_threaded_mainloop_get_api(mainloop), "eSpeak"))) {
  342. SHOW("Failed to allocate context\n","");
  343. goto unlock_and_fail;
  344. }
  345. pa_context_set_state_callback(context, context_state_cb, NULL);
  346. pa_context_set_subscribe_callback(context, subscribe_cb, NULL);
  347. SHOW_TIME("pa_context_connect (call)");
  348. if (pa_context_connect(context, NULL, (pa_context_flags_t)0, NULL) < 0) {
  349. SHOW("Failed to connect to server: %s", pa_strerror(pa_context_errno(context)));
  350. ret = PULSE_NO_CONNECTION;
  351. goto unlock_and_fail;
  352. }
  353. SHOW_TIME("pa_threaded_mainloop_start (call)");
  354. if (pa_threaded_mainloop_start(mainloop) < 0) {
  355. SHOW("Failed to start main loop","");
  356. goto unlock_and_fail;
  357. }
  358. /* Wait until the context is ready */
  359. SHOW_TIME("pa_threaded_mainloop_wait");
  360. pa_threaded_mainloop_wait(mainloop);
  361. if (pa_context_get_state(context) != PA_CONTEXT_READY) {
  362. SHOW("Failed to connect to server: %s", pa_strerror(pa_context_errno(context)));
  363. ret = PULSE_NO_CONNECTION;
  364. if (mainloop)
  365. pa_threaded_mainloop_stop(mainloop);
  366. goto unlock_and_fail;
  367. }
  368. SHOW_TIME("pa_stream_new");
  369. if (!(stream = pa_stream_new(context, "unknown", &ss, NULL))) {
  370. SHOW("Failed to create stream: %s", pa_strerror(pa_context_errno(context)));
  371. goto unlock_and_fail;
  372. }
  373. pa_stream_set_state_callback(stream, stream_state_cb, NULL);
  374. pa_stream_set_write_callback(stream, stream_request_cb, NULL);
  375. pa_stream_set_latency_update_callback(stream, stream_latency_update_cb, NULL);
  376. pa_buffer_attr a_attr;
  377. a_attr.maxlength = MAXLENGTH;
  378. a_attr.tlength = TLENGTH;
  379. a_attr.prebuf = PREBUF;
  380. a_attr.minreq = MINREQ;
  381. a_attr.fragsize = 0;
  382. SHOW_TIME("pa_connect_playback");
  383. if (pa_stream_connect_playback(stream, NULL, &a_attr, (pa_stream_flags_t)(PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE), NULL, NULL) < 0) {
  384. SHOW("Failed to connect stream: %s", pa_strerror(pa_context_errno(context)));
  385. goto unlock_and_fail;
  386. }
  387. /* Wait until the stream is ready */
  388. SHOW_TIME("pa_threaded_mainloop_wait");
  389. pa_threaded_mainloop_wait(mainloop);
  390. if (pa_stream_get_state(stream) != PA_STREAM_READY) {
  391. SHOW("Failed to connect stream: %s", pa_strerror(pa_context_errno(context)));
  392. goto unlock_and_fail;
  393. }
  394. /* Now subscribe to events */
  395. SHOW_TIME("pa_context_subscribe");
  396. if (!(o = pa_context_subscribe(context, PA_SUBSCRIPTION_MASK_SINK_INPUT, context_success_cb, &success))) {
  397. SHOW("pa_context_subscribe() failed: %s", pa_strerror(pa_context_errno(context)));
  398. goto unlock_and_fail;
  399. }
  400. success = 0;
  401. SHOW_TIME("pa_threaded_mainloop_wait");
  402. while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
  403. CHECK_DEAD_GOTO(fail, 1);
  404. pa_threaded_mainloop_wait(mainloop);
  405. }
  406. pa_operation_unref(o);
  407. if (!success) {
  408. SHOW("pa_context_subscribe() failed: %s", pa_strerror(pa_context_errno(context)));
  409. goto unlock_and_fail;
  410. }
  411. do_trigger = 0;
  412. written = 0;
  413. time_offset_msec = 0;
  414. just_flushed = 0;
  415. connected = 1;
  416. pa_threaded_mainloop_unlock(mainloop);
  417. SHOW_TIME("pulse_open (ret true)");
  418. return PULSE_OK;
  419. unlock_and_fail:
  420. if (o)
  421. pa_operation_unref(o);
  422. pa_threaded_mainloop_unlock(mainloop);
  423. fail:
  424. if (ret == PULSE_NO_CONNECTION) {
  425. if (context) {
  426. SHOW_TIME("pa_context_disconnect (call)");
  427. pa_context_disconnect(context);
  428. pa_context_unref(context);
  429. context = NULL;
  430. }
  431. if (mainloop) {
  432. SHOW_TIME("pa_threaded_mainloop_free (call)");
  433. pa_threaded_mainloop_free(mainloop);
  434. mainloop = NULL;
  435. }
  436. }
  437. else {
  438. pulse_close();
  439. }
  440. SHOW_TIME("pulse_open (ret false)");
  441. return ret;
  442. }
  443. void wave_flush(void* theHandler)
  444. {
  445. ENTER("wave_flush");
  446. }
  447. void wave_set_callback_is_output_enabled(t_wave_callback* cb)
  448. {
  449. my_callback_is_output_enabled = cb;
  450. }
  451. int wave_init(int srate)
  452. {
  453. ENTER("wave_init");
  454. stream = NULL;
  455. wave_samplerate = srate;
  456. return pulse_open() == PULSE_OK;
  457. }
  458. void* wave_open(const char* the_api)
  459. {
  460. ENTER("wave_open");
  461. return((void*)1);
  462. }
  463. size_t wave_write(void* theHandler, char* theMono16BitsWaveBuffer, size_t theSize)
  464. {
  465. ENTER("wave_write");
  466. size_t bytes_to_write = theSize;
  467. char* aBuffer=theMono16BitsWaveBuffer;
  468. assert(stream);
  469. size_t aTotalFreeMem=0;
  470. pthread_mutex_lock(&pulse_mutex);
  471. while (1)
  472. {
  473. if (my_callback_is_output_enabled
  474. && (0==my_callback_is_output_enabled()))
  475. {
  476. SHOW_TIME("wave_write > my_callback_is_output_enabled: no!");
  477. theSize=0;
  478. goto terminate;
  479. }
  480. aTotalFreeMem = pulse_free();
  481. if (aTotalFreeMem >= bytes_to_write)
  482. {
  483. SHOW("wave_write > aTotalFreeMem(%d) >= bytes_to_write(%d)\n", aTotalFreeMem, bytes_to_write);
  484. break;
  485. }
  486. // TBD: check if really helpful
  487. if (aTotalFreeMem >= MAXLENGTH*2)
  488. {
  489. aTotalFreeMem = MAXLENGTH*2;
  490. }
  491. SHOW("wave_write > wait: aTotalFreeMem(%d) < bytes_to_write(%d)\n", aTotalFreeMem, bytes_to_write);
  492. // 500: threshold for avoiding too many calls to pulse_write
  493. if (aTotalFreeMem>500)
  494. {
  495. pulse_write(aBuffer, aTotalFreeMem);
  496. bytes_to_write -= aTotalFreeMem;
  497. aBuffer += aTotalFreeMem;
  498. }
  499. usleep(10000);
  500. }
  501. pulse_write(aBuffer, bytes_to_write);
  502. terminate:
  503. pthread_mutex_unlock(&pulse_mutex);
  504. SHOW("wave_write: theSize=%d", theSize);
  505. SHOW_TIME("wave_write > LEAVE");
  506. return theSize;
  507. }
  508. int wave_close(void* theHandler)
  509. {
  510. SHOW_TIME("wave_close > ENTER");
  511. static int aStopStreamCount = 0;
  512. // Avoid race condition by making sure this function only
  513. // gets called once at a time
  514. aStopStreamCount++;
  515. if (aStopStreamCount != 1)
  516. {
  517. SHOW_TIME("wave_close > LEAVE (stopStreamCount)");
  518. return 0;
  519. }
  520. int a_status = pthread_mutex_lock(&pulse_mutex);
  521. if (a_status)
  522. {
  523. SHOW("Error: pulse_mutex lock=%d (%s)\n", a_status, __FUNCTION__);
  524. aStopStreamCount = 0; // last action
  525. return PULSE_ERROR;
  526. }
  527. drain();
  528. pthread_mutex_unlock(&pulse_mutex);
  529. SHOW_TIME("wave_close (ret)");
  530. aStopStreamCount = 0; // last action
  531. return PULSE_OK;
  532. }
  533. int wave_is_busy(void* theHandler)
  534. {
  535. SHOW_TIME("wave_is_busy");
  536. pa_timing_info a_timing_info;
  537. int active = pulse_playing(&a_timing_info);
  538. SHOW("wave_is_busy: %d\n",active);
  539. return active;
  540. }
  541. void wave_terminate()
  542. {
  543. ENTER("wave_terminate");
  544. int a_status;
  545. pthread_mutex_t* a_mutex = NULL;
  546. a_mutex = &pulse_mutex;
  547. a_status = pthread_mutex_lock(a_mutex);
  548. pulse_close();
  549. SHOW_TIME("unlock mutex");
  550. a_status = pthread_mutex_unlock(a_mutex);
  551. pthread_mutex_destroy(a_mutex);
  552. }
  553. uint32_t wave_get_read_position(void* theHandler)
  554. {
  555. pa_timing_info a_timing_info;
  556. pulse_playing(&a_timing_info);
  557. SHOW("wave_get_read_position > %lx\n", a_timing_info.read_index);
  558. return a_timing_info.read_index;
  559. }
  560. uint32_t wave_get_write_position(void* theHandler)
  561. {
  562. pa_timing_info a_timing_info;
  563. pulse_playing(&a_timing_info);
  564. SHOW("wave_get_read_position > %lx\n", a_timing_info.write_index);
  565. return a_timing_info.write_index;
  566. }
  567. int wave_get_remaining_time(uint32_t sample, uint32_t* time)
  568. {
  569. double a_time=0;
  570. if (!time || !stream)
  571. {
  572. SHOW("event get_remaining_time> %s\n","audio device not available");
  573. return -1;
  574. }
  575. pa_timing_info a_timing_info;
  576. pulse_playing(&a_timing_info);
  577. if (sample > a_timing_info.read_index)
  578. {
  579. // TBD: take in account time suplied by portaudio V18 API
  580. a_time = sample - a_timing_info.read_index;
  581. a_time = 0.5 + (a_time * 1000.0) / wave_samplerate;
  582. }
  583. else
  584. {
  585. a_time = 0;
  586. }
  587. SHOW("wave_get_remaining_time > sample=%d, time=%d\n", sample, (uint32_t)a_time);
  588. *time = (uint32_t)a_time;
  589. return 0;
  590. }
  591. void *wave_test_get_write_buffer()
  592. {
  593. return NULL;
  594. }
  595. #else
  596. int wave_init(return 1; ) {
  597. }
  598. void* wave_open(const char* the_api) {
  599. return (void *)1;
  600. }
  601. size_t wave_write(void* theHandler, char* theMono16BitsWaveBuffer, size_t theSize) {
  602. return theSize;
  603. }
  604. int wave_close(void* theHandler) {
  605. return 0;
  606. }
  607. int wave_is_busy(void* theHandler) {
  608. return 0;
  609. }
  610. void wave_terminate() {
  611. }
  612. uint32_t wave_get_read_position(void* theHandler) {
  613. return 0;
  614. }
  615. uint32_t wave_get_write_position(void* theHandler) {
  616. return 0;
  617. }
  618. void wave_flush(void* theHandler) {
  619. }
  620. typedef int (t_wave_callback)(void);
  621. void wave_set_callback_is_output_enabled(t_wave_callback* cb) {
  622. }
  623. extern void* wave_test_get_write_buffer() {
  624. return NULL;
  625. }
  626. int wave_get_remaining_time(uint32_t sample, uint32_t* time)
  627. {
  628. if (!time) return(-1);
  629. *time = (uint32_t)0;
  630. return 0;
  631. }
  632. #endif // of USE_PULSEAUDIO
  633. #ifndef USE_PORTAUDIO
  634. void clock_gettime2(struct timespec *ts)
  635. {
  636. struct timeval tv;
  637. if (!ts)
  638. {
  639. return;
  640. }
  641. assert (gettimeofday(&tv, NULL) != -1);
  642. ts->tv_sec = tv.tv_sec;
  643. ts->tv_nsec = tv.tv_usec*1000;
  644. }
  645. void add_time_in_ms(struct timespec *ts, int time_in_ms)
  646. {
  647. if (!ts)
  648. {
  649. return;
  650. }
  651. uint64_t t_ns = (uint64_t)ts->tv_nsec + 1000000 * (uint64_t)time_in_ms;
  652. while(t_ns >= ONE_BILLION)
  653. {
  654. SHOW("event > add_time_in_ms ns: %d sec %Lu nsec \n", ts->tv_sec, t_ns);
  655. ts->tv_sec += 1;
  656. t_ns -= ONE_BILLION;
  657. }
  658. ts->tv_nsec = (long int)t_ns;
  659. }
  660. #endif // ifndef USE_PORTAUDIO
  661. #endif // USE_ASYNC