eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wave_pulse.c 15KB

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