ADC Microphone

[中文]

Using ADC to capture analog microphone data eliminates the need for an external audio codec chip, making it suitable for applications that do not require high sampling rates and are cost-sensitive. The code structure follows the engineering model of esp_codec_dev.

Features

  • Supports multi-channel ADC audio sampling.

  • Supports up to 12-bit ADC sampling resolution.

Note

The maximum resolution for multi-channel sampling is determined by the ADC’s maximum sampling rate (83.3K for ESP32-S3) divided by the number of sampling channels.

Reference Circuit

External Configuration for ADC Continuous

You can initialize the ADC continuous mode externally and pass in an adc_continuous_handle_t.

adc_continuous_handle_t handle;
adc_continuous_handle_cfg_t adc_config = {
    .max_store_buf_size = adc_cfg->max_store_buf_size,
    .conv_frame_size = adc_cfg->conv_frame_size,
    .flags.flush_pool = true,
};

adc_continuous_new_handle(&adc_config, &handle);

Using with ADC Oneshot Mode

  • If ADC oneshot and ADC continuous use different ADC units, there will be no conflicts.

  • If they use the same ADC unit, make sure to call esp_codec_dev_close() to close adc_mic before calling adc_oneshot_read.

adc_continuous_start(handle);
adc_continuous_read();
adc_continuous_stop();
adc_oneshot_read();

Example Code

audio_codec_adc_cfg_t cfg = DEFAULT_AUDIO_CODEC_ADC_MONO_CFG(ADC_CHANNEL_0, 16000);
const audio_codec_data_if_t *adc_if = audio_codec_new_adc_data(&cfg);

esp_codec_dev_cfg_t codec_dev_cfg = {
    .dev_type = ESP_CODEC_DEV_TYPE_IN,
    .data_if = adc_if,
};
esp_codec_dev_handle_t dev = esp_codec_dev_new(&codec_dev_cfg);
esp_codec_dev_sample_info_t fs = {
    .sample_rate = 16000,
    .channel = 1,
    .bits_per_sample = 16,
};
esp_codec_dev_open(dev, &fs);
uint16_t *audio_buffer = malloc(16000 * sizeof(uint16_t));
assert(audio_buffer);
while (1) {
    int ret = esp_codec_dev_read(dev, audio_buffer, sizeof(uint16_t) * 16000);
    ESP_LOGI(TAG, "esp_codec_dev_read ret: %d\n", ret);
}

API Reference

Header File

Functions

const audio_codec_data_if_t *audio_codec_new_adc_data(audio_codec_adc_cfg_t *adc_cfg)

Initialize codec adc.

Parameters

adc_cfg – pointer of configuration struct

Returns

const audio_codec_data_if_t* adc data interface.

Structures

struct audio_codec_adc_cfg_t

codec adc configuration

Public Members

adc_continuous_handle_t *handle

Type of adc continuous mode driver handle, if NULL will create new one internal, else will use the handle

size_t max_store_buf_size

Max length of the conversion results that driver can store, in bytes.

size_t conv_frame_size

Conversion frame size, in bytes. This should be in multiples of SOC_ADC_DIGI_DATA_BYTES_PER_CONV

adc_unit_t unit_id

ADC unit

uint8_t *adc_channel_list

Channel of ADC

uint8_t adc_channel_num

Number of channels

adc_atten_t atten

It should correspond to the actual range, with 0dB attenuation having the least ripple.

uint32_t sample_rate_hz

Sample rate of ADC

Macros

DEFAULT_AUDIO_CODEC_ADC_MONO_CFG(_channel, _sample)
DEFAULT_AUDIO_CODEC_ADC_STEREO_CFG(_channel_l, _channel_r, _sample)