tcircles3_audio_codec.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "tcircles3_audio_codec.h"
  2. #include <esp_log.h>
  3. #include <driver/i2c_master.h>
  4. #include <driver/i2s_tdm.h>
  5. #include "config.h"
  6. static const char TAG[] = "Tcircles3AudioCodec";
  7. Tcircles3AudioCodec::Tcircles3AudioCodec(int input_sample_rate, int output_sample_rate,
  8. gpio_num_t mic_bclk, gpio_num_t mic_ws, gpio_num_t mic_data,
  9. gpio_num_t spkr_bclk, gpio_num_t spkr_lrclk, gpio_num_t spkr_data,
  10. bool input_reference) {
  11. duplex_ = true; // 是否双工
  12. input_reference_ = input_reference; // 是否使用参考输入,实现回声消除
  13. input_channels_ = input_reference_ ? 2 : 1; // 输入通道数
  14. input_sample_rate_ = input_sample_rate;
  15. output_sample_rate_ = output_sample_rate;
  16. CreateVoiceHardware(mic_bclk, mic_ws, mic_data, spkr_bclk, spkr_lrclk, spkr_data);
  17. gpio_config_t config;
  18. config.pin_bit_mask = BIT64(AUDIO_SPKR_ENABLE);
  19. config.mode = GPIO_MODE_OUTPUT;
  20. config.pull_up_en = GPIO_PULLUP_DISABLE;
  21. config.pull_down_en = GPIO_PULLDOWN_ENABLE;
  22. config.intr_type = GPIO_INTR_DISABLE;
  23. #if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
  24. config.hys_ctrl_mode = GPIO_HYS_SOFT_ENABLE;
  25. #endif
  26. gpio_config(&config);
  27. gpio_set_level(AUDIO_SPKR_ENABLE, 0);
  28. ESP_LOGI(TAG, "Tcircles3AudioCodec initialized");
  29. }
  30. Tcircles3AudioCodec::~Tcircles3AudioCodec() {
  31. audio_codec_delete_codec_if(in_codec_if_);
  32. audio_codec_delete_ctrl_if(in_ctrl_if_);
  33. audio_codec_delete_codec_if(out_codec_if_);
  34. audio_codec_delete_ctrl_if(out_ctrl_if_);
  35. audio_codec_delete_gpio_if(gpio_if_);
  36. audio_codec_delete_data_if(data_if_);
  37. }
  38. void Tcircles3AudioCodec::CreateVoiceHardware(gpio_num_t mic_bclk, gpio_num_t mic_ws, gpio_num_t mic_data,
  39. gpio_num_t spkr_bclk, gpio_num_t spkr_lrclk, gpio_num_t spkr_data) {
  40. i2s_chan_config_t mic_chan_config = I2S_CHANNEL_DEFAULT_CONFIG(i2s_port_t(0), I2S_ROLE_MASTER);
  41. mic_chan_config.auto_clear = true; // Auto clear the legacy data in the DMA buffer
  42. i2s_chan_config_t spkr_chan_config = I2S_CHANNEL_DEFAULT_CONFIG(i2s_port_t(1), I2S_ROLE_MASTER);
  43. spkr_chan_config.auto_clear = true; // Auto clear the legacy data in the DMA buffer
  44. ESP_ERROR_CHECK(i2s_new_channel(&mic_chan_config, NULL, &rx_handle_));
  45. ESP_ERROR_CHECK(i2s_new_channel(&spkr_chan_config, &tx_handle_, NULL));
  46. i2s_std_config_t mic_config = {
  47. .clk_cfg = {
  48. .sample_rate_hz = (uint32_t)output_sample_rate_,
  49. .clk_src = I2S_CLK_SRC_DEFAULT,
  50. .mclk_multiple = I2S_MCLK_MULTIPLE_256,
  51. #ifdef I2S_HW_VERSION_2
  52. .ext_clk_freq_hz = 0,
  53. #endif
  54. },
  55. .slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
  56. .gpio_cfg ={
  57. .mclk = I2S_GPIO_UNUSED,
  58. .bclk = mic_bclk,
  59. .ws = mic_ws,
  60. .dout = I2S_GPIO_UNUSED,
  61. .din = mic_data,
  62. .invert_flags = {
  63. .mclk_inv = false,
  64. .bclk_inv = false,
  65. .ws_inv = false,
  66. }
  67. }
  68. };
  69. i2s_std_config_t spkr_config = {
  70. .clk_cfg ={
  71. .sample_rate_hz = static_cast<uint32_t>(11025),
  72. .clk_src = I2S_CLK_SRC_DEFAULT,
  73. .mclk_multiple = I2S_MCLK_MULTIPLE_256,
  74. #ifdef I2S_HW_VERSION_2
  75. .ext_clk_freq_hz = 0,
  76. #endif
  77. },
  78. .slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
  79. .gpio_cfg ={
  80. .mclk = I2S_GPIO_UNUSED,
  81. .bclk = spkr_bclk,
  82. .ws = spkr_lrclk,
  83. .dout = spkr_data,
  84. .din = I2S_GPIO_UNUSED,
  85. .invert_flags = {
  86. .mclk_inv = false,
  87. .bclk_inv = false,
  88. .ws_inv = false
  89. }
  90. }
  91. };
  92. ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &mic_config));
  93. ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &spkr_config));
  94. ESP_LOGI(TAG, "Voice hardware created");
  95. }
  96. void Tcircles3AudioCodec::SetOutputVolume(int volume) {
  97. volume_ = volume;
  98. AudioCodec::SetOutputVolume(volume);
  99. }
  100. void Tcircles3AudioCodec::EnableInput(bool enable) {
  101. AudioCodec::EnableInput(enable);
  102. }
  103. void Tcircles3AudioCodec::EnableOutput(bool enable) {
  104. if (enable){
  105. gpio_set_level(AUDIO_SPKR_ENABLE, 1);
  106. }else{
  107. gpio_set_level(AUDIO_SPKR_ENABLE, 0);
  108. }
  109. AudioCodec::EnableOutput(enable);
  110. }
  111. int Tcircles3AudioCodec::Read(int16_t *dest, int samples){
  112. if (input_enabled_){
  113. size_t bytes_read;
  114. i2s_channel_read(rx_handle_, dest, samples * sizeof(int16_t), &bytes_read, portMAX_DELAY);
  115. }
  116. return samples;
  117. }
  118. int Tcircles3AudioCodec::Write(const int16_t *data, int samples){
  119. if (output_enabled_){
  120. size_t bytes_read;
  121. auto output_data = (int16_t *)malloc(samples * sizeof(int16_t));
  122. for (size_t i = 0; i < samples; i++){
  123. output_data[i] = (float)data[i] * (float)(volume_ / 100.0);
  124. }
  125. i2s_channel_write(tx_handle_, output_data, samples * sizeof(int16_t), &bytes_read, portMAX_DELAY);
  126. free(output_data);
  127. }
  128. return samples;
  129. }