Mic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "Mic.h"
  2. #include <stdbool.h>
  3. #include "esp_err.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/semphr.h"
  6. #include "string.h"
  7. #include "driver/gpio.h"
  8. #include "esp_err.h"
  9. #include "esp_log.h"
  10. #include "esp_rom_sys.h"
  11. #include "esp_check.h"
  12. #include "esp_vfs_fat.h"
  13. #include "sdmmc_cmd.h"
  14. #include "driver/i2s_std.h"
  15. #include "driver/i2s_types.h"
  16. #include "driver/i2s_pdm.h"
  17. #include "driver/i2s_tdm.h"
  18. #include <sys/unistd.h>
  19. #include <sys/stat.h>
  20. #include "dirent.h"
  21. #include "driver/sdmmc_host.h"
  22. #include <errno.h>
  23. #if ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN))
  24. #include "driver/sdmmc_host.h"
  25. #endif /* ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN)) */
  26. #define ADC_I2S_CHANNEL 4
  27. static sdmmc_card_t *card;
  28. static const char *TAG = "board";
  29. static int s_play_sample_rate = 16000;
  30. static int s_play_channel_format = 1;
  31. static int s_bits_per_chan = 16;
  32. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  33. static i2s_chan_handle_t tx_handle = NULL; // I2S tx channel handler
  34. static i2s_chan_handle_t rx_handle = NULL; // I2S rx channel handler
  35. #endif
  36. static audio_codec_data_if_t *record_data_if = NULL;
  37. static audio_codec_ctrl_if_t *record_ctrl_if = NULL;
  38. static audio_codec_if_t *record_codec_if = NULL;
  39. static esp_codec_dev_handle_t record_dev = NULL;
  40. static audio_codec_data_if_t *play_data_if = NULL;
  41. static audio_codec_ctrl_if_t *play_ctrl_if = NULL;
  42. static audio_codec_gpio_if_t *play_gpio_if = NULL;
  43. static audio_codec_if_t *play_codec_if = NULL;
  44. static esp_codec_dev_handle_t play_dev = NULL;
  45. static i2c_master_bus_handle_t i2c_bus_handle = NULL;
  46. static uint32_t SDCard_Size = 0;
  47. esp_codec_dev_handle_t esp_ret_play_dev(void)
  48. {
  49. return play_dev;
  50. }
  51. i2c_master_bus_handle_t esp_ret_i2c_handle(void)
  52. {
  53. return i2c_bus_handle;
  54. }
  55. uint32_t Get_SD_Size(void)
  56. {
  57. return SDCard_Size;
  58. }
  59. esp_err_t i2c_master_init(void)
  60. {
  61. const i2c_master_bus_config_t bus_config = {
  62. .i2c_port = I2C_NUM,
  63. .sda_io_num = GPIO_I2C_SDA,
  64. .scl_io_num = GPIO_I2C_SCL,
  65. .clk_source = I2C_CLK_SRC_DEFAULT,
  66. };
  67. esp_err_t ret = i2c_new_master_bus(&bus_config, &i2c_bus_handle);
  68. if (ret != ESP_OK) {
  69. ESP_LOGE(TAG, "Failed to initialize I2C bus: %s", esp_err_to_name(ret));
  70. return ret;
  71. }
  72. ESP_LOGI(TAG, "I2C bus initialized successfully");
  73. return ESP_OK; // 返回 ESP_OK 表示成功
  74. }
  75. esp_err_t bsp_codec_adc_init(int sample_rate)
  76. {
  77. esp_err_t ret_val = ESP_OK;
  78. // Do initialize of related interface: data_if, ctrl_if and gpio_if
  79. audio_codec_i2s_cfg_t i2s_cfg = {
  80. .port = I2S_NUM_1,
  81. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  82. .rx_handle = rx_handle,
  83. .tx_handle = NULL,
  84. #endif
  85. };
  86. //record_data_if = audio_codec_new_i2s_data(&i2s_cfg);
  87. audio_codec_i2c_cfg_t i2c_cfg = {.addr = ES7210_CODEC_DEFAULT_ADDR,.bus_handle = i2c_bus_handle};
  88. //record_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
  89. // New input codec interface
  90. es7210_codec_cfg_t es7210_cfg = {
  91. .ctrl_if = record_ctrl_if,
  92. .mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2 | ES7210_SEL_MIC3 | ES7210_SEL_MIC4,
  93. };
  94. //record_codec_if = es7210_codec_new(&es7210_cfg);
  95. // New input codec device
  96. esp_codec_dev_cfg_t dev_cfg = {
  97. .codec_if = record_codec_if,
  98. .data_if = record_data_if,
  99. .dev_type = ESP_CODEC_DEV_TYPE_IN,
  100. };
  101. record_dev = esp_codec_dev_new(&dev_cfg);
  102. esp_codec_dev_sample_info_t fs = {
  103. .sample_rate = 16000,
  104. .channel = 2,
  105. .bits_per_sample = 32,
  106. };
  107. esp_codec_dev_open(record_dev, &fs);
  108. // esp_codec_dev_set_in_gain(record_dev, RECORD_VOLUME);
  109. esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0), RECORD_VOLUME);
  110. esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1), RECORD_VOLUME);
  111. esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(2), RECORD_VOLUME);
  112. esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(3), RECORD_VOLUME);
  113. return ret_val;
  114. }
  115. esp_err_t esp_get_mic_level(int16_t *buffer, int buffer_len)
  116. {
  117. esp_err_t ret = ESP_OK;
  118. size_t bytes_read;
  119. int audio_chunksize = buffer_len / (sizeof(int16_t) * ADC_I2S_CHANNEL);
  120. ret = esp_codec_dev_read(record_dev, (void *)buffer, buffer_len);
  121. int64_t sum = 0;
  122. for(int i = 0; i < buffer_len/sizeof(int16_t); i++) {
  123. sum += abs(buffer[i]);
  124. }
  125. ESP_LOGI("MIC", "Level=%lld", sum/(buffer_len/sizeof(int16_t)));
  126. return ret;
  127. }