es8389_audio_codec.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "es8389_audio_codec.h"
  2. #include <esp_log.h>
  3. static const char TAG[] = "Es8389AudioCodec";
  4. Es8389AudioCodec::Es8389AudioCodec(void* i2c_master_handle, i2c_port_t i2c_port, int input_sample_rate, int output_sample_rate,
  5. gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
  6. gpio_num_t pa_pin, uint8_t es8389_addr, bool use_mclk) {
  7. duplex_ = true; // 是否双工
  8. input_reference_ = false; // 是否使用参考输入,实现回声消除
  9. input_channels_ = 1; // 输入通道数
  10. input_sample_rate_ = input_sample_rate;
  11. output_sample_rate_ = output_sample_rate;
  12. pa_pin_ = pa_pin;
  13. CreateDuplexChannels(mclk, bclk, ws, dout, din);
  14. // Do initialize of related interface: data_if, ctrl_if and gpio_if
  15. audio_codec_i2s_cfg_t i2s_cfg = {
  16. .port = I2S_NUM_0,
  17. .rx_handle = rx_handle_,
  18. .tx_handle = tx_handle_,
  19. };
  20. data_if_ = audio_codec_new_i2s_data(&i2s_cfg);
  21. assert(data_if_ != NULL);
  22. // Output
  23. audio_codec_i2c_cfg_t i2c_cfg = {
  24. .port = i2c_port,
  25. .addr = es8389_addr,
  26. .bus_handle = i2c_master_handle,
  27. };
  28. ctrl_if_ = audio_codec_new_i2c_ctrl(&i2c_cfg);
  29. assert(ctrl_if_ != NULL);
  30. gpio_if_ = audio_codec_new_gpio();
  31. assert(gpio_if_ != NULL);
  32. es8389_codec_cfg_t es8389_cfg = {};
  33. es8389_cfg.ctrl_if = ctrl_if_;
  34. es8389_cfg.gpio_if = gpio_if_;
  35. es8389_cfg.codec_mode = ESP_CODEC_DEV_WORK_MODE_BOTH;
  36. es8389_cfg.pa_pin = pa_pin;
  37. es8389_cfg.use_mclk = use_mclk;
  38. es8389_cfg.hw_gain.pa_voltage = 5.0;
  39. es8389_cfg.hw_gain.codec_dac_voltage = 3.3;
  40. codec_if_ = es8389_codec_new(&es8389_cfg);
  41. assert(codec_if_ != NULL);
  42. esp_codec_dev_cfg_t outdev_cfg = {
  43. .dev_type = ESP_CODEC_DEV_TYPE_OUT,
  44. .codec_if = codec_if_,
  45. .data_if = data_if_,
  46. };
  47. output_dev_ = esp_codec_dev_new(&outdev_cfg);
  48. assert(output_dev_ != NULL);
  49. esp_codec_dev_cfg_t indev_cfg = {
  50. .dev_type = ESP_CODEC_DEV_TYPE_IN,
  51. .codec_if = codec_if_,
  52. .data_if = data_if_,
  53. };
  54. input_dev_ = esp_codec_dev_new(&indev_cfg);
  55. assert(input_dev_ != NULL);
  56. esp_codec_set_disable_when_closed(output_dev_, false);
  57. esp_codec_set_disable_when_closed(input_dev_, false);
  58. ESP_LOGI(TAG, "Es8389AudioCodec initialized");
  59. }
  60. Es8389AudioCodec::~Es8389AudioCodec() {
  61. ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
  62. esp_codec_dev_delete(output_dev_);
  63. ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
  64. esp_codec_dev_delete(input_dev_);
  65. audio_codec_delete_codec_if(codec_if_);
  66. audio_codec_delete_ctrl_if(ctrl_if_);
  67. audio_codec_delete_gpio_if(gpio_if_);
  68. audio_codec_delete_data_if(data_if_);
  69. }
  70. void Es8389AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
  71. assert(input_sample_rate_ == output_sample_rate_);
  72. i2s_chan_config_t chan_cfg = {
  73. .id = I2S_NUM_0,
  74. .role = I2S_ROLE_MASTER,
  75. .dma_desc_num = 6,
  76. .dma_frame_num = 240,
  77. .auto_clear_after_cb = true,
  78. .auto_clear_before_cb = false,
  79. .intr_priority = 0,
  80. };
  81. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
  82. i2s_std_config_t std_cfg = {
  83. .clk_cfg = {
  84. .sample_rate_hz = (uint32_t)output_sample_rate_,
  85. .clk_src = I2S_CLK_SRC_DEFAULT,
  86. .mclk_multiple = I2S_MCLK_MULTIPLE_256,
  87. #ifdef I2S_HW_VERSION_2
  88. .ext_clk_freq_hz = 0,
  89. #endif
  90. },
  91. .slot_cfg = {
  92. .data_bit_width = I2S_DATA_BIT_WIDTH_16BIT,
  93. .slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
  94. .slot_mode = I2S_SLOT_MODE_STEREO,
  95. .slot_mask = I2S_STD_SLOT_BOTH,
  96. .ws_width = I2S_DATA_BIT_WIDTH_16BIT,
  97. .ws_pol = false,
  98. .bit_shift = true,
  99. .left_align = true,
  100. .big_endian = false,
  101. .bit_order_lsb = false
  102. },
  103. .gpio_cfg = {
  104. .mclk = mclk,
  105. .bclk = bclk,
  106. .ws = ws,
  107. .dout = dout,
  108. .din = din,
  109. .invert_flags = {
  110. .mclk_inv = false,
  111. .bclk_inv = false,
  112. .ws_inv = false
  113. }
  114. }
  115. };
  116. ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
  117. ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
  118. ESP_LOGI(TAG, "Duplex channels created");
  119. }
  120. void Es8389AudioCodec::SetOutputVolume(int volume) {
  121. ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, volume));
  122. AudioCodec::SetOutputVolume(volume);
  123. }
  124. void Es8389AudioCodec::EnableInput(bool enable) {
  125. if (enable == input_enabled_) {
  126. return;
  127. }
  128. if (enable) {
  129. esp_codec_dev_sample_info_t fs = {
  130. .bits_per_sample = 16,
  131. .channel = 1,
  132. .channel_mask = 0,
  133. .sample_rate = (uint32_t)input_sample_rate_,
  134. .mclk_multiple = 0,
  135. };
  136. ESP_ERROR_CHECK(esp_codec_dev_open(input_dev_, &fs));
  137. ESP_ERROR_CHECK(esp_codec_dev_set_in_gain(input_dev_, 40.0));
  138. } else {
  139. ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
  140. }
  141. AudioCodec::EnableInput(enable);
  142. }
  143. void Es8389AudioCodec::EnableOutput(bool enable) {
  144. if (enable == output_enabled_) {
  145. return;
  146. }
  147. if (enable) {
  148. // Play 16bit 1 channel
  149. esp_codec_dev_sample_info_t fs = {
  150. .bits_per_sample = 16,
  151. .channel = 1,
  152. .channel_mask = 0,
  153. .sample_rate = (uint32_t)output_sample_rate_,
  154. .mclk_multiple = 0,
  155. };
  156. ESP_ERROR_CHECK(esp_codec_dev_open(output_dev_, &fs));
  157. ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, output_volume_));
  158. if (pa_pin_ != GPIO_NUM_NC) {
  159. gpio_set_level(pa_pin_, 1);
  160. }
  161. } else {
  162. ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
  163. if (pa_pin_ != GPIO_NUM_NC) {
  164. gpio_set_level(pa_pin_, 0);
  165. }
  166. }
  167. AudioCodec::EnableOutput(enable);
  168. }
  169. int Es8389AudioCodec::Read(int16_t* dest, int samples) {
  170. if (input_enabled_) {
  171. ESP_ERROR_CHECK_WITHOUT_ABORT(esp_codec_dev_read(input_dev_, (void*)dest, samples * sizeof(int16_t)));
  172. }
  173. return samples;
  174. }
  175. int Es8389AudioCodec::Write(const int16_t* data, int samples) {
  176. if (output_enabled_) {
  177. ESP_ERROR_CHECK_WITHOUT_ABORT(esp_codec_dev_write(output_dev_, (void*)data, samples * sizeof(int16_t)));
  178. }
  179. return samples;
  180. }