tdisplays3promvsrlora_audio_codec.cc 5.6 KB

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