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.cpp 23KB

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