output_dac.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "squeezelite.h"
  2. #include "driver/i2s.h"
  3. #include <signal.h>
  4. #define I2S_NUM (0)
  5. #define I2S_BCK_IO (GPIO_NUM_26)
  6. #define I2S_WS_IO (GPIO_NUM_25)
  7. #define I2S_DO_IO (GPIO_NUM_22)
  8. #define I2S_DI_IO (-1)
  9. // buffer length is expressed in number of samples
  10. #define I2S_BUF_LEN 60
  11. static log_level loglevel;
  12. static bool running = true;
  13. extern struct outputstate output;
  14. extern struct buffer *outputbuf;
  15. #if REPACK && BYTES_PER_FRAMES == 4
  16. #error "REPACK is not compatible with BYTES_PER_FRAME=4"
  17. #endif
  18. #define LOCK mutex_lock(outputbuf->mutex)
  19. #define UNLOCK mutex_unlock(outputbuf->mutex)
  20. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  21. extern u8_t *silencebuf;
  22. static u8_t *optr;
  23. static int bytes_per_frame;
  24. static thread_type thread;
  25. static int _dac_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  26. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  27. static void *output_thread();
  28. void set_volume(unsigned left, unsigned right) {
  29. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  30. LOCK;
  31. output.gainL = left;
  32. output.gainR = right;
  33. UNLOCK;
  34. }
  35. void output_init_dac(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  36. loglevel = level;
  37. optr = malloc(FRAME_BLOCK * BYTES_PER_FRAME);
  38. if (!optr) {
  39. LOG_ERROR("unable to malloc buf");
  40. return;
  41. }
  42. LOG_INFO("init output DAC");
  43. memset(&output, 0, sizeof(output));
  44. #if BYTES_PER_FRAME == 4
  45. output.format = S16_LE;
  46. #else
  47. output.format = S32_LE;
  48. #endif
  49. output.start_frames = FRAME_BLOCK * 2;
  50. output.write_cb = &_dac_write_frames;
  51. output.rate_delay = rate_delay;
  52. if (params) {
  53. if (!strcmp(params, "32")) output.format = S32_LE;
  54. if (!strcmp(params, "24")) output.format = S24_3LE;
  55. if (!strcmp(params, "16")) output.format = S16_LE;
  56. }
  57. // ensure output rate is specified to avoid test open
  58. if (!rates[0]) {
  59. rates[0] = 44100;
  60. }
  61. output_init_common(level, device, output_buf_size, rates, idle);
  62. i2s_config_t i2s_config = {
  63. .mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
  64. .sample_rate = output.current_sample_rate,
  65. .bits_per_sample = BYTES_PER_FRAME * 8,
  66. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  67. .communication_format = I2S_COMM_FORMAT_I2S
  68. | I2S_COMM_FORMAT_I2S_MSB,
  69. .dma_buf_count = 6, //todo: tune this parameter. Expressed in numbrer of buffers
  70. .dma_buf_len = I2S_BUF_LEN, // todo: tune this parameter. Expressed in number of samples. Byte size depends on bit depth
  71. .use_apll = false,
  72. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 //Interrupt level 1
  73. };
  74. i2s_pin_config_t pin_config = { .bck_io_num = I2S_BCK_IO, .ws_io_num =
  75. I2S_WS_IO, .data_out_num = I2S_DO_IO, .data_in_num = I2S_DI_IO //Not used
  76. };
  77. i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
  78. i2s_set_pin(I2S_NUM, &pin_config);
  79. i2s_set_clk(I2S_NUM, output.current_sample_rate, i2s_config.bits_per_sample, 2);
  80. #if LINUX || OSX || FREEBSD || POSIX
  81. pthread_attr_t attr;
  82. pthread_attr_init(&attr);
  83. #ifdef PTHREAD_STACK_MIN
  84. pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
  85. #endif
  86. pthread_create(&thread, &attr, output_thread, NULL);
  87. pthread_attr_destroy(&attr);
  88. #endif
  89. #if WIN
  90. thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&output_thread, NULL, 0, NULL);
  91. #endif
  92. }
  93. void output_close_dac(void) {
  94. LOG_INFO("close output");
  95. LOCK;
  96. running = false;
  97. UNLOCK;
  98. free(optr);
  99. output_close_common();
  100. }
  101. static int _dac_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  102. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  103. u8_t *obuf;
  104. if (!silence) {
  105. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  106. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  107. }
  108. #if !REPACK
  109. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  110. _apply_gain(outputbuf, out_frames, gainL, gainR);
  111. }
  112. IF_DSD(
  113. if (output.outfmt == DOP) {
  114. update_dop((u32_t *) outputbuf->readp, out_frames, output.invert);
  115. } else if (output.outfmt != PCM && output.invert)
  116. dsd_invert((u32_t *) outputbuf->readp, out_frames);
  117. )
  118. memcpy(optr, outputbuf->readp, out_frames * BYTES_PER_FRAME);
  119. #else
  120. obuf = outputbuf->readp;
  121. #endif
  122. } else {
  123. obuf = silencebuf;
  124. #if !REPACK
  125. IF_DSD(
  126. if (output.outfmt != PCM) {
  127. obuf = silencebuf_dsd;
  128. update_dop((u32_t *) obuf, out_frames, false); // don't invert silence
  129. }
  130. )
  131. memcpy(optr, obuf, out_frames * BYTES_PER_FRAME);
  132. #endif
  133. }
  134. #if REPACK
  135. _scale_and_pack_frames(optr, (s32_t *)(void *)obuf, out_frames, gainL, gainR, output.format);
  136. #endif
  137. return (int)out_frames;
  138. }
  139. static void *output_thread() {
  140. // buffer to hold output data so we can block on writing outside of output lock, allocated on init
  141. u8_t *obuf = malloc(FRAME_BLOCK * BYTES_PER_FRAME);
  142. int frames = 0;
  143. size_t i2s_bytes_write = 0;
  144. #if REPACK
  145. LOCK;
  146. switch (output.format) {
  147. case S32_LE:
  148. bytes_per_frame = 4 * 2; break;
  149. case S24_3LE:
  150. bytes_per_frame = 3 * 2; break;
  151. case S16_LE:
  152. bytes_per_frame = 2 * 2; break;
  153. default:
  154. bytes_per_frame = 4 * 2; break;
  155. break;
  156. }
  157. UNLOCK;
  158. #else
  159. bytes_per_frame = BYTES_PER_FRAME;
  160. #endif
  161. while (running) {
  162. LOCK;
  163. if (output.state == OUTPUT_OFF) {
  164. UNLOCK;
  165. usleep(500000);
  166. continue;
  167. }
  168. // todo: call i2s_set_clock here if rate is changed
  169. output.device_frames = 0;
  170. output.updated = gettime_ms();
  171. output.frames_played_dmp = output.frames_played;
  172. optr = obuf + frames * bytes_per_frame;
  173. frames += _output_frames(FRAME_BLOCK);
  174. UNLOCK;
  175. if (frames) {
  176. i2s_write(I2S_NUM, optr,frames*BYTES_PER_FRAME, &i2s_bytes_write, 100);
  177. if(i2s_bytes_write!=frames*BYTES_PER_FRAME){
  178. LOG_WARN("Bytes available: %d, I2S wrote %d", frames*BYTES_PER_FRAME,i2s_bytes_write);
  179. }
  180. usleep((frames * 1000 * 1000) / output.current_sample_rate);
  181. frames = 0;
  182. } else {
  183. usleep((FRAME_BLOCK * 1000 * 1000) / output.current_sample_rate);
  184. }
  185. }
  186. return 0;
  187. }
  188. bool test_open(const char *device, unsigned rates[], bool userdef_rates) {
  189. unsigned _rates[] = { 96000, 88200, 48000, 44100, 32000, 0 };
  190. memcpy(rates, _rates, sizeof(_rates));
  191. return true;
  192. }