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

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