2
0

output_dac.c.tes 4.3 KB

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