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 22KB

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