linux_audio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #ifdef IRON_A2
  2. #include <alsa/asoundlib.h>
  3. #include <errno.h>
  4. #include <iron_audio.h>
  5. #include <poll.h>
  6. #include <pthread.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. // apt-get install libasound2-dev
  10. iron_a2_buffer_t a2_buffer;
  11. pthread_t threadid;
  12. bool audioRunning = false;
  13. snd_pcm_t *playback_handle;
  14. short buf[4096 * 4];
  15. static unsigned int samples_per_second = 44100;
  16. uint32_t iron_a2_samples_per_second(void) {
  17. return samples_per_second;
  18. }
  19. void copySample(void *buffer) {
  20. float left_value = *(float *)&a2_buffer.channels[0][a2_buffer.read_location];
  21. float right_value = *(float *)&a2_buffer.channels[1][a2_buffer.read_location];
  22. a2_buffer.read_location += 1;
  23. if (a2_buffer.read_location >= a2_buffer.data_size) {
  24. a2_buffer.read_location = 0;
  25. }
  26. ((int16_t *)buffer)[0] = (int16_t)(left_value * 32767);
  27. ((int16_t *)buffer)[1] = (int16_t)(right_value * 32767);
  28. }
  29. int playback_callback(snd_pcm_sframes_t nframes) {
  30. int err = 0;
  31. if (iron_a2_internal_callback(&a2_buffer, nframes)) {
  32. int ni = 0;
  33. while (ni < nframes) {
  34. int i = 0;
  35. for (; ni < nframes && i < 4096 * 2; ++i, ++ni) {
  36. copySample(&buf[i * 2]);
  37. }
  38. int err2;
  39. if ((err2 = snd_pcm_writei(playback_handle, buf, i)) < 0) {
  40. fprintf(stderr, "write failed (%s)\n", snd_strerror(err2));
  41. }
  42. err += err2;
  43. }
  44. }
  45. return err;
  46. }
  47. bool tryToRecover(snd_pcm_t *handle, int errorCode) {
  48. switch (-errorCode) {
  49. case EINTR:
  50. case EPIPE:
  51. case ESPIPE:
  52. case ESTRPIPE: {
  53. int recovered = snd_pcm_recover(playback_handle, errorCode, 1);
  54. if (recovered != 0) {
  55. fprintf(stderr, "unable to recover from ALSA error code=%i\n", errorCode);
  56. return false;
  57. }
  58. else {
  59. fprintf(stdout, "recovered from ALSA error code=%i\n", errorCode);
  60. return true;
  61. }
  62. }
  63. default:
  64. fprintf(stderr, "unhandled ALSA error code=%i\n", errorCode);
  65. return false;
  66. }
  67. }
  68. void *doAudio(void *arg) {
  69. snd_pcm_hw_params_t *hw_params;
  70. snd_pcm_sw_params_t *sw_params;
  71. snd_pcm_sframes_t frames_to_deliver;
  72. int err;
  73. if ((err = snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  74. fprintf(stderr, "cannot open audio device default (%s)\n", snd_strerror(err));
  75. return NULL;
  76. }
  77. if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
  78. fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror(err));
  79. return NULL;
  80. }
  81. if ((err = snd_pcm_hw_params_any(playback_handle, hw_params)) < 0) {
  82. fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror(err));
  83. return NULL;
  84. }
  85. if ((err = snd_pcm_hw_params_set_access(playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
  86. fprintf(stderr, "cannot set access type (%s)\n", snd_strerror(err));
  87. return NULL;
  88. }
  89. if ((err = snd_pcm_hw_params_set_format(playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
  90. fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err));
  91. return NULL;
  92. }
  93. int dir = 0;
  94. if ((err = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, &samples_per_second, &dir)) < 0) {
  95. fprintf(stderr, "cannot set sample rate (%s)\n", snd_strerror(err));
  96. return NULL;
  97. }
  98. if ((err = snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2)) < 0) {
  99. fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err));
  100. return NULL;
  101. }
  102. snd_pcm_uframes_t bufferSize = samples_per_second / 8;
  103. if (((err = snd_pcm_hw_params_set_buffer_size(playback_handle, hw_params, bufferSize)) < 0 &&
  104. (snd_pcm_hw_params_set_buffer_size_near(playback_handle, hw_params, &bufferSize)) < 0)) {
  105. fprintf(stderr, "cannot set buffer size (%s)\n", snd_strerror(err));
  106. return NULL;
  107. }
  108. if ((err = snd_pcm_hw_params(playback_handle, hw_params)) < 0) {
  109. fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err));
  110. return NULL;
  111. }
  112. snd_pcm_hw_params_free(hw_params);
  113. /* tell ALSA to wake us up whenever 4096 or more frames
  114. of playback data can be delivered. Also, tell
  115. ALSA that we'll start the device ourselves.
  116. */
  117. if ((err = snd_pcm_sw_params_malloc(&sw_params)) < 0) {
  118. fprintf(stderr, "cannot allocate software parameters structure (%s)\n", snd_strerror(err));
  119. return NULL;
  120. }
  121. if ((err = snd_pcm_sw_params_current(playback_handle, sw_params)) < 0) {
  122. fprintf(stderr, "cannot initialize software parameters structure (%s)\n", snd_strerror(err));
  123. return NULL;
  124. }
  125. if ((err = snd_pcm_sw_params_set_avail_min(playback_handle, sw_params, 4096)) < 0) {
  126. fprintf(stderr, "cannot set minimum available count (%s)\n", snd_strerror(err));
  127. return NULL;
  128. }
  129. if ((err = snd_pcm_sw_params_set_start_threshold(playback_handle, sw_params, 0U)) < 0) {
  130. fprintf(stderr, "cannot set start mode (%s)\n", snd_strerror(err));
  131. return NULL;
  132. }
  133. if ((err = snd_pcm_sw_params(playback_handle, sw_params)) < 0) {
  134. fprintf(stderr, "cannot set software parameters (%s)\n", snd_strerror(err));
  135. return NULL;
  136. }
  137. /* the interface will interrupt the kernel every 4096 frames, and ALSA
  138. will wake up this program very soon after that.
  139. */
  140. if ((err = snd_pcm_prepare(playback_handle)) < 0) {
  141. fprintf(stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror(err));
  142. return NULL;
  143. }
  144. while (audioRunning) {
  145. /* wait till the interface is ready for data, or 1 second
  146. has elapsed.
  147. */
  148. if ((err = snd_pcm_wait(playback_handle, 1000)) < 0) {
  149. fprintf(stderr, "poll failed (%s)\n", strerror(errno));
  150. break;
  151. }
  152. /* find out how much space is available for playback data */
  153. if ((frames_to_deliver = snd_pcm_avail_update(playback_handle)) < 0) {
  154. if (!tryToRecover(playback_handle, frames_to_deliver)) {
  155. // break;
  156. }
  157. }
  158. else {
  159. // frames_to_deliver = frames_to_deliver > 4096 ? 4096 : frames_to_deliver;
  160. /* deliver the data */
  161. int delivered = playback_callback(frames_to_deliver);
  162. if (delivered != frames_to_deliver) {
  163. fprintf(stderr, "playback callback failed (delivered %i / %ld frames)\n", delivered, frames_to_deliver);
  164. // break;
  165. }
  166. }
  167. }
  168. snd_pcm_close(playback_handle);
  169. return NULL;
  170. }
  171. static bool initialized = false;
  172. void iron_a2_init() {
  173. if (initialized) {
  174. return;
  175. }
  176. iron_a2_internal_init();
  177. initialized = true;
  178. a2_buffer.read_location = 0;
  179. a2_buffer.write_location = 0;
  180. a2_buffer.data_size = 128 * 1024;
  181. a2_buffer.channel_count = 2;
  182. a2_buffer.channels[0] = (float *)malloc(a2_buffer.data_size * sizeof(float));
  183. a2_buffer.channels[1] = (float *)malloc(a2_buffer.data_size * sizeof(float));
  184. audioRunning = true;
  185. pthread_create(&threadid, NULL, &doAudio, NULL);
  186. }
  187. void iron_a2_update() {}
  188. void iron_a2_shutdown() {
  189. audioRunning = false;
  190. }
  191. #endif