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

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