| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #include "Mic.h"
- #include <stdbool.h>
- #include "esp_err.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/semphr.h"
- #include "string.h"
- #include "driver/gpio.h"
- #include "esp_err.h"
- #include "esp_log.h"
- #include "esp_rom_sys.h"
- #include "esp_check.h"
- #include "esp_vfs_fat.h"
- #include "sdmmc_cmd.h"
- #include "driver/i2s_std.h"
- #include "driver/i2s_types.h"
- #include "driver/i2s_pdm.h"
- #include "driver/i2s_tdm.h"
- #include <sys/unistd.h>
- #include <sys/stat.h>
- #include "dirent.h"
- #include "driver/sdmmc_host.h"
- #include <errno.h>
- #if ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN))
- #include "driver/sdmmc_host.h"
- #endif /* ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN)) */
- #define ADC_I2S_CHANNEL 4
- static sdmmc_card_t *card;
- static const char *TAG = "board";
- static int s_play_sample_rate = 16000;
- static int s_play_channel_format = 1;
- static int s_bits_per_chan = 16;
- #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
- static i2s_chan_handle_t tx_handle = NULL; // I2S tx channel handler
- static i2s_chan_handle_t rx_handle = NULL; // I2S rx channel handler
- #endif
- static audio_codec_data_if_t *record_data_if = NULL;
- static audio_codec_ctrl_if_t *record_ctrl_if = NULL;
- static audio_codec_if_t *record_codec_if = NULL;
- static esp_codec_dev_handle_t record_dev = NULL;
- static audio_codec_data_if_t *play_data_if = NULL;
- static audio_codec_ctrl_if_t *play_ctrl_if = NULL;
- static audio_codec_gpio_if_t *play_gpio_if = NULL;
- static audio_codec_if_t *play_codec_if = NULL;
- static esp_codec_dev_handle_t play_dev = NULL;
- static i2c_master_bus_handle_t i2c_bus_handle = NULL;
- static uint32_t SDCard_Size = 0;
- esp_codec_dev_handle_t esp_ret_play_dev(void)
- {
- return play_dev;
- }
- i2c_master_bus_handle_t esp_ret_i2c_handle(void)
- {
- return i2c_bus_handle;
- }
- uint32_t Get_SD_Size(void)
- {
- return SDCard_Size;
- }
- esp_err_t i2c_master_init(void)
- {
- const i2c_master_bus_config_t bus_config = {
- .i2c_port = I2C_NUM,
- .sda_io_num = GPIO_I2C_SDA,
- .scl_io_num = GPIO_I2C_SCL,
- .clk_source = I2C_CLK_SRC_DEFAULT,
- };
- esp_err_t ret = i2c_new_master_bus(&bus_config, &i2c_bus_handle);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to initialize I2C bus: %s", esp_err_to_name(ret));
- return ret;
- }
- ESP_LOGI(TAG, "I2C bus initialized successfully");
- return ESP_OK; // 返回 ESP_OK 表示成功
- }
- esp_err_t bsp_codec_adc_init(int sample_rate)
- {
- esp_err_t ret_val = ESP_OK;
- // Do initialize of related interface: data_if, ctrl_if and gpio_if
- audio_codec_i2s_cfg_t i2s_cfg = {
- .port = I2S_NUM_1,
- #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
- .rx_handle = rx_handle,
- .tx_handle = NULL,
- #endif
- };
- //record_data_if = audio_codec_new_i2s_data(&i2s_cfg);
- audio_codec_i2c_cfg_t i2c_cfg = {.addr = ES7210_CODEC_DEFAULT_ADDR,.bus_handle = i2c_bus_handle};
- //record_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
- // New input codec interface
- es7210_codec_cfg_t es7210_cfg = {
- .ctrl_if = record_ctrl_if,
- .mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2 | ES7210_SEL_MIC3 | ES7210_SEL_MIC4,
- };
- //record_codec_if = es7210_codec_new(&es7210_cfg);
- // New input codec device
- esp_codec_dev_cfg_t dev_cfg = {
- .codec_if = record_codec_if,
- .data_if = record_data_if,
- .dev_type = ESP_CODEC_DEV_TYPE_IN,
- };
- record_dev = esp_codec_dev_new(&dev_cfg);
- esp_codec_dev_sample_info_t fs = {
- .sample_rate = 16000,
- .channel = 2,
- .bits_per_sample = 32,
- };
- esp_codec_dev_open(record_dev, &fs);
- // esp_codec_dev_set_in_gain(record_dev, RECORD_VOLUME);
- esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0), RECORD_VOLUME);
- esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1), RECORD_VOLUME);
- esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(2), RECORD_VOLUME);
- esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(3), RECORD_VOLUME);
- return ret_val;
- }
- esp_err_t esp_get_mic_level(int16_t *buffer, int buffer_len)
- {
- esp_err_t ret = ESP_OK;
- size_t bytes_read;
- int audio_chunksize = buffer_len / (sizeof(int16_t) * ADC_I2S_CHANNEL);
- ret = esp_codec_dev_read(record_dev, (void *)buffer, buffer_len);
-
- int64_t sum = 0;
- for(int i = 0; i < buffer_len/sizeof(int16_t); i++) {
- sum += abs(buffer[i]);
- }
- ESP_LOGI("MIC", "Level=%lld", sum/(buffer_len/sizeof(int16_t)));
-
- return ret;
- }
|