gnumpi / esphome_audio

Custom audio components for ESPHome
Other
24 stars 11 forks source link

ESPHome - Audio Components

Target Version: ESPHome-2023.12.9

ESPHome-target ESPHome-latest

The purpose of these ESPHome components is to offer a wrapper framework that enables access to the elements from the Espressif Audio Development Framework (ADF) within the ESPHome environment.

Custom Components

i2s_audio

This customized version of i2s_audio offers several enhancements:

adf_pipeline

This custom component includes:

Configurations

General

external_components:
  - source:
      type: git
      url: https://github.com/gnumpi/esphome_audio
      ref: main
    components: [ adf_pipeline, i2s_audio ]

I2S-Settings:

For configuration examples not utilizing the adf_pipeline, please refer to the following YAML files:

ADF-Pipeline configurations:

Dedicated I2S-Port for Microphone and Speaker/Media Player:

Both input and output components have exclusive access to their assigned I2S-Port, allowing I2S settings to be configured independently of each other. ADF-Pipeline elements can remain initialized even when the pipeline is idle, with keep_pipeline_alive set to true.

Example config (see also: esp32-s3-N16R8-adf.yaml)

# define the i2s controller and their pins as before
i2s_audio:
  - id: i2s_in
    i2s_lrclk_pin: GPIO5
    i2s_bclk_pin: GPIO6
  - id: i2s_out
    i2s_lrclk_pin: GPIO46
    i2s_bclk_pin: GPIO9

adf_pipeline:
  - platform: i2s_audio
    type: audio_out
    id: adf_i2s_out
    i2s_audio_id: i2s_out
    i2s_dout_pin: GPIO10

  - platform: i2s_audio
    type: audio_in
    id: adf_i2s_in
    i2s_audio_id: i2s_in
    i2s_din_pin: GPIO4
    pdm: false
    channel: left
    sample_rate: 16000
    bits_per_sample: 32bit

microphone:
  - platform: adf_pipeline
    id: adf_microphone
    gain_log2: 3
    keep_pipeline_alive: true
    pipeline:
      - adf_i2s_in
      - self

media_player:
  - platform: adf_pipeline
    id: adf_media_player
    name: s3-dev_media_player
    keep_pipeline_alive: true
    internal: false
    pipeline:
      - self
      - adf_i2s_out

Shared I2S-Port with Exclusive Access

Either the I2SWriter or the I2SReader can be active at a time and must release the I2S-Port when not in use. Therefore, keep_pipeline_alive should be set to false. Since each component loads its own I2S driver, I2S settings can be configured independently for each component.

Example config (see also: m5stack-atom-echo-adf.yaml)

i2s_audio:
  - id: i2s_shared
    i2s_lrclk_pin: GPIO33
    i2s_bclk_pin: GPIO19
    access_mode: exclusive

adf_pipeline:
  - platform: i2s_audio
    type: audio_out
    id: adf_i2s_out
    i2s_audio_id: i2s_shared
    i2s_dout_pin: GPIO22
    fixed_settings: false

  - platform: i2s_audio
    type: audio_in
    id: adf_i2s_in
    i2s_audio_id: i2s_shared
    i2s_din_pin: GPIO23
    pdm: true
    bits_per_sample: 32bit
    channel: right
    fixed_settings: true

Shared I2S Port with Duplex Access

The I2S driver is installed only once and configured with shared settings that are used by both the I2S Reader and the I2S Writer. It's important to ensure that these settings are compatible for both components. To prevent interference from other components, such as the media_player, which might try to modify the I2S configuration, set fixed_settings to true.

For enhanced compatibility and to support dynamic audio configurations, integrate a resampler into the ADF-pipeline. This will help in adjusting audio sample rates or formats dynamically, facilitating smooth operation across different audio processing components.

Example config (see also: m5stack-core-s3-adf.yaml)

i2s_audio:
  - id: i2s_shared
    i2s_lrclk_pin: GPIO33
    i2s_bclk_pin: GPIO34
    i2s_mclk_pin: GPIO0
    access_mode: duplex

adf_pipeline:
  - platform: i2s_audio
    type: audio_out
    id: adf_i2s_out
    i2s_audio_id: i2s_shared
    i2s_dout_pin: GPIO13
    adf_alc: false
    dac:
      model: aw88298
      address: 0x36
      enable_pin:
        aw9523: aw9523_1
        port: 0
        pin: 2
        mode:
          output: true
    sample_rate: 16000
    bits_per_sample: 16bit
    fixed_settings: true

  - platform: i2s_audio
    type: audio_in
    id: adf_i2s_in
    i2s_audio_id: i2s_shared
    i2s_din_pin: GPIO14
    pdm: false
    adc:
      model: es7210
      address: 0x40
    bits_per_sample: 16bit
    fixed_settings: true

microphone:
  - platform: adf_pipeline
    id: adf_microphone
    keep_pipeline_alive: true
    pipeline:
      - adf_i2s_in
      - resampler
      - self

media_player:
  - platform: adf_pipeline
    id: adf_media_player
    name: s3-dev_media_player
    internal: false
    keep_pipeline_alive: true
    pipeline:
      - self
      - resampler
      - adf_i2s_out

Notes: