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 AudioCodec

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

Overview

Concrete tool for I2S audio codec abstraction provided by the XiaoZhi firmware, defining the interface for reading microphone PCM data and writing speaker PCM data through the I2S DMA peripheral.

Description

AudioCodec is the abstract base class for all audio hardware interfaces in XiaoZhi. It wraps the ESP-IDF I2S driver and provides a simplified read/write API. Concrete implementations (e.g., NoAudioCodec, Es8311AudioCodec, Es8388AudioCodec) configure the I2S channels with board-specific pins from config.h and implement the pure virtual Read() and Write() methods.

Key properties:

  • Dual-mode: duplex_ flag indicates independent input/output paths (true for most codecs)
  • Reference channel: input_reference_ indicates a reference mic for AEC
  • Sample rates: input_sample_rate_ and output_sample_rate_ may differ (e.g., 16kHz in, 48kHz out)
  • Volume control: SetOutputVolume(int 0–100), default 70
  • Input gain: SetInputGain(float) for microphone sensitivity

Usage

Reference this implementation when:

  • Implementing a new audio codec driver for a board
  • Understanding microphone/speaker data format and rates
  • Configuring AEC reference channel
  • Tracing the I2S DMA data path

Code Reference

Source Location

  • Repository: XiaoZhi ESP32
  • Files: main/audio/audio_codec.h, main/audio/audio_codec.cc

Signature

class AudioCodec {
public:
    AudioCodec();
    virtual ~AudioCodec();

    virtual void SetOutputVolume(int volume);
    virtual void SetInputGain(float gain);
    virtual void EnableInput(bool enable);
    virtual void EnableOutput(bool enable);
    virtual void OutputData(std::vector<int16_t>& data);
    virtual bool InputData(std::vector<int16_t>& data);
    virtual void Start();

    // Accessors
    bool duplex() const;
    bool input_reference() const;
    int input_sample_rate() const;
    int output_sample_rate() const;
    int input_channels() const;
    int output_channels() const;
    int output_volume() const;
    float input_gain() const;
    bool input_enabled() const;
    bool output_enabled() const;

protected:
    i2s_chan_handle_t tx_handle_ = nullptr;
    i2s_chan_handle_t rx_handle_ = nullptr;
    bool duplex_ = false;
    bool input_reference_ = false;
    bool input_enabled_ = false;
    bool output_enabled_ = false;
    int input_sample_rate_ = 0;
    int output_sample_rate_ = 0;
    int input_channels_ = 1;
    int output_channels_ = 1;
    int output_volume_ = 70;
    float input_gain_ = 0.0;

    virtual int Read(int16_t* dest, int samples) = 0;
    virtual int Write(const int16_t* data, int samples) = 0;
};

Import

#include "audio_codec.h"

I/O Contract

Inputs

Name Type Required Description
data (InputData) std::vector<int16_t>& Buffer to fill with mic PCM; resized to samples at 16kHz
data (OutputData) std::vector<int16_t>& PCM buffer to write to speaker via I2S
enable (EnableInput/Output) bool Enable or disable I2S channel

Outputs

Name Type Description
InputData() True if mic data read successfully
OutputData() Writes PCM to I2S DMA TX buffer
Read() (protected) Number of samples actually read from I2S
Write() (protected) Number of samples actually written to I2S

Usage Examples

Reading Microphone Data

// Used by AudioService::ReadAudioData()
std::vector<int16_t> data;
if (codec_->InputData(data)) {
    // data contains 16kHz mono PCM samples
    // Feed to audio engine or encode queue
}

Writing Speaker Data

// Used by AudioService::AudioOutputTask()
// data contains decoded PCM resampled to output_sample_rate_
codec_->OutputData(data);  // writes to I2S DMA

Related Pages

Implements Principle

Requires Environment

Page Connections

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