Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:78 Xiaozhi esp32 BoxAudioCodec

From Leeroopedia
Knowledge Sources
Domains Audio_Hardware, Embedded_Systems
Last Updated 2026-10-17 00:00 GMT

Overview

Concrete dual-chip audio codec driver for boards combining an ES8311 DAC for speaker output with an ES7210 ADC for multi-channel microphone input, provided by the XiaoZhi firmware using the esp_codec_dev abstraction.

Description

BoxAudioCodec is an AudioCodec subclass that manages two separate codec chips simultaneously:

  • Output: ES8311 configured in DAC mode (ESP_CODEC_DEV_WORK_MODE_DAC) with PA pin and hardware gain voltage settings.
  • Input: ES7210 configured for up to 4 microphone channels (ES7210_SEL_MIC1|MIC2|MIC3|MIC4), enabling multi-microphone arrays for AEC.

It creates a single duplex I2S channel shared between both chips. When input_reference is true, input_channels_ is set to 2 to support echo cancellation reference paths. It also supports a per-channel reference gain (reference_gain_channel_, reference_gain_) for fine-tuning AEC microphone sensitivity.

Usage

Reference this implementation when:

  • A board uses dual ES8311 + ES7210 codec chips
  • Implementing multi-microphone AEC with a reference channel
  • Understanding esp_codec_dev dual-device configuration

Code Reference

Source Location

Signature

class BoxAudioCodec : public AudioCodec {
public:
    BoxAudioCodec(void* i2c_master_handle, int input_sample_rate, int output_sample_rate,
                  gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
                  gpio_num_t pa_pin, uint8_t es8311_addr, uint8_t es7210_addr,
                  bool input_reference,
                  float input_gain = 30.0f,
                  int reference_gain_channel = -1,
                  float reference_gain = 0.0f);
    virtual ~BoxAudioCodec();
    virtual void SetOutputVolume(int volume) override;
    virtual void EnableInput(bool enable) override;
    virtual void EnableOutput(bool enable) override;
};

Import

#include "box_audio_codec.h"

I/O Contract

Inputs

Name Type Required Description
i2c_master_handle void* Yes I2C master driver handle
es8311_addr uint8_t Yes I2C address of ES8311 DAC
es7210_addr uint8_t Yes I2C address of ES7210 ADC
input_reference bool Yes Enable AEC reference channel (sets input_channels_=2)
input_gain float No Microphone gain (default 30.0)
reference_gain_channel int No Channel index for reference gain (default -1 = none)
reference_gain float No Reference channel gain (default 0.0)

Outputs

Name Type Description
Read() int 16-bit PCM samples from ES7210 ADC
Write() int 16-bit PCM samples to ES8311 DAC

Usage Examples

Dual Chip with AEC

#include "box_audio_codec.h"

auto codec = std::make_unique<BoxAudioCodec>(
    i2c_master_handle,
    16000, 16000,                          // input/output sample rates
    CONFIG_I2S_MCLK, CONFIG_I2S_BCLK, CONFIG_I2S_WS,
    CONFIG_I2S_DOUT, CONFIG_I2S_DIN,
    CONFIG_PA_PIN, 0x18, 0x40,            // PA, ES8311 addr, ES7210 addr
    true,                                  // input_reference for AEC
    30.0f,                                 // input_gain
    1, 10.0f                               // reference_gain_channel=1, gain=10
);
codec->Start();

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment