output_dac.c.tes 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "squeezelite.h"
  2. #include "driver/i2s.h"
  3. static log_level loglevel;
  4. static bool running = true;
  5. extern struct outputstate output;
  6. extern struct buffer *outputbuf;
  7. extern struct buffer *streambuf;
  8. #define LOCK mutex_lock(outputbuf->mutex)
  9. #define UNLOCK mutex_unlock(outputbuf->mutex)
  10. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  11. extern u8_t *silencebuf;
  12. extern u8_t *buf;
  13. #define I2S_NUM (0)
  14. #define WAVE_FREQ_HZ (100)
  15. #define PI (3.14159265)
  16. #define I2S_BCK_IO (GPIO_NUM_26)
  17. #define I2S_WS_IO (GPIO_NUM_25)
  18. #define I2S_DO_IO (GPIO_NUM_22)
  19. #define I2S_DI_IO (-1)
  20. // buffer length is expressed in number of samples
  21. #define I2S_BUF_LEN 60
  22. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL,
  23. s32_t gainR, s32_t cross_gain_in, s32_t cross_gain_out,
  24. ISAMPLE_T **cross_ptr);
  25. void set_volume(unsigned left, unsigned right) {
  26. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  27. LOCK;
  28. output.gainL = left;
  29. output.gainR = right;
  30. // TODO
  31. output.gainL = FIXED_ONE;
  32. output.gainR = FIXED_ONE;
  33. UNLOCK;
  34. }
  35. static void *output_thread(void *arg) {
  36. bool start = true;
  37. bool output_off = (output.state == OUTPUT_OFF);
  38. bool probe_device = (arg != NULL);
  39. int err;
  40. while (running) {
  41. // todo: implement output off logic?
  42. // todo: call i2s_set_clock here if rate is changed
  43. LOCK;
  44. output.device_frames = 0;
  45. output.updated = gettime_ms();
  46. output.frames_played_dmp = output.frames_played;
  47. _output_frames(I2S_BUF_LEN*2); // fill at least one DMA buffer with stereo signal
  48. UNLOCK;
  49. }
  50. return 0;
  51. }
  52. static pthread_t thread;
  53. void output_init_dac(log_level level, char *device, unsigned output_buf_size,
  54. char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  55. loglevel = level;
  56. LOG_INFO("init output DAC");
  57. memset(&output, 0, sizeof(output));
  58. output.start_frames = 0; //CONFIG_ //FRAME_BLOCK * 2;
  59. output.write_cb = &_write_frames;
  60. output.rate_delay = rate_delay;
  61. // ensure output rate is specified to avoid test open
  62. if (!rates[0]) {
  63. rates[0] = 44100;
  64. }
  65. device = "DAC";
  66. output_init_common(level, device, output_buf_size, rates, idle);
  67. i2s_config_t i2s_config = {
  68. .mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
  69. .sample_rate = output.current_sample_rate,
  70. .bits_per_sample = BYTES_PER_FRAME * 8,
  71. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
  72. .communication_format = I2S_COMM_FORMAT_I2S
  73. | I2S_COMM_FORMAT_I2S_MSB,
  74. .dma_buf_count = 6, //todo: tune this parameter. Expressed in numbrer of buffers
  75. .dma_buf_len = I2S_BUF_LEN, // todo: tune this parameter. Expressed in number of samples. Byte size depends on bit depth
  76. .use_apll = false,
  77. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 //Interrupt level 1
  78. };
  79. i2s_pin_config_t pin_config = { .bck_io_num = I2S_BCK_IO, .ws_io_num =
  80. I2S_WS_IO, .data_out_num = I2S_DO_IO, .data_in_num = I2S_DI_IO //Not used
  81. };
  82. i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
  83. i2s_set_pin(I2S_NUM, &pin_config);
  84. i2s_set_clk(I2S_NUM, output.current_sample_rate, i2s_config.bits_per_sample, 2);
  85. #if LINUX || OSX || FREEBSD || POSIX
  86. pthread_attr_t attr;
  87. pthread_attr_init(&attr);
  88. #ifdef PTHREAD_STACK_MIN
  89. pthread_attr_setstacksize(&attr,
  90. PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
  91. #endif
  92. pthread_create(&thread, &attr, output_thread, NULL);
  93. pthread_attr_destroy(&attr);
  94. #endif
  95. #if WIN
  96. thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&output_thread, NULL, 0, NULL);
  97. #endif
  98. }
  99. void output_close_dac(void) {
  100. LOG_INFO("close output");
  101. LOCK;
  102. running = false;
  103. UNLOCK;
  104. output_close_common();
  105. }
  106. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL,
  107. s32_t gainR, s32_t cross_gain_in, s32_t cross_gain_out,
  108. ISAMPLE_T **cross_ptr) {
  109. u8_t *obuf;
  110. size_t i2s_bytes_write = 0;
  111. if (!silence) {
  112. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  113. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  114. }
  115. obuf = outputbuf->readp;
  116. } else {
  117. obuf = silencebuf;
  118. }
  119. //_scale_and_pack_frames(buf + buffill * bytes_per_frame, (s32_t *)(void *)obuf, out_frames, gainL, gainR, output.format);
  120. // buffill += out_frames;
  121. i2s_write(I2S_NUM, obuf, out_frames *BYTES_PER_FRAME, &i2s_bytes_write, 100);
  122. return (int)i2s_bytes_write * BYTES_PER_FRAME;
  123. }