ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
esphome::i2s_audio::I2SAudioSpeaker Class Reference

#include <i2s_audio_speaker.h>

Inheritance diagram for esphome::i2s_audio::I2SAudioSpeaker:
esphome::i2s_audio::I2SAudioOut esphome::speaker::Speaker esphome::Component esphome::i2s_audio::I2SAudioBase esphome::Parented< I2SAudioComponent >

Public Member Functions

float get_setup_priority () const override
 
void setup () override
 
void dump_config () override
 
void loop () override
 
void set_buffer_duration (uint32_t buffer_duration_ms)
 
void set_timeout (uint32_t ms)
 
void set_internal_dac_mode (i2s_dac_mode_t mode)
 
void set_dout_pin (uint8_t pin)
 
void set_i2s_comm_fmt (i2s_comm_format_t mode)
 
void set_dout_pin (uint8_t pin)
 
void set_i2s_comm_fmt (std::string mode)
 
void start () override
 
void stop () override
 
void finish () override
 
void set_pause_state (bool pause_state) override
 
bool get_pause_state () const override
 
void stop_ (bool wait_on_empty)
 Plays the provided audio data.
 
esp_err_t start_i2s_driver_ (audio::AudioStreamInfo &audio_stream_info)
 Starts the ESP32 I2S driver.
 
void stop_i2s_driver_ ()
 Stops the I2S driver and unlocks the I2S port.
 
- Public Member Functions inherited from esphome::i2s_audio::I2SAudioBase
void set_i2s_mode (i2s_mode_t mode)
 
void set_channel (i2s_channel_fmt_t channel)
 
void set_bits_per_sample (i2s_bits_per_sample_t bits_per_sample)
 
void set_bits_per_channel (i2s_bits_per_chan_t bits_per_channel)
 
void set_i2s_role (i2s_role_t role)
 
void set_slot_mode (i2s_slot_mode_t slot_mode)
 
void set_std_slot_mask (i2s_std_slot_mask_t std_slot_mask)
 
void set_slot_bit_width (i2s_slot_bit_width_t slot_bit_width)
 
void set_sample_rate (uint32_t sample_rate)
 
void set_use_apll (uint32_t use_apll)
 
void set_mclk_multiple (i2s_mclk_multiple_t mclk_multiple)
 
- Public Member Functions inherited from esphome::Parented< I2SAudioComponent >
 Parented ()
 
 Parented (I2SAudioComponent *parent)
 
I2SAudioComponent * get_parent () const
 Get the parent of this object.
 
void set_parent (I2SAudioComponent *parent)
 Set the parent of this object.
 
- Public Member Functions inherited from esphome::speaker::Speaker
virtual size_t play (const uint8_t *data, size_t length)=0
 Plays the provided audio data.
 
size_t play (const std::vector< uint8_t > &data)
 
virtual bool has_buffered_data () const =0
 
bool is_running () const
 
bool is_stopped () const
 
virtual void set_volume (float volume)
 
virtual float get_volume ()
 
virtual void set_mute_state (bool mute_state)
 
virtual bool get_mute_state ()
 
void set_audio_dac (audio_dac::AudioDac *audio_dac)
 
void set_audio_stream_info (const audio::AudioStreamInfo &audio_stream_info)
 
audio::AudioStreamInfoget_audio_stream_info ()
 
void add_audio_output_callback (std::function< void(uint32_t, int64_t)> &&callback)
 Callback function for sending the duration of the audio written to the speaker since the last callback.
 
- Public Member Functions inherited from esphome::Component
float get_actual_setup_priority () const
 
void set_setup_priority (float priority)
 
virtual float get_loop_priority () const
 priority of loop().
 
void call ()
 
virtual void on_shutdown ()
 
virtual void on_safe_shutdown ()
 
virtual bool teardown ()
 Called during teardown to allow component to gracefully finish operations.
 
virtual void on_powerdown ()
 Called after teardown is complete to power down hardware.
 
uint8_t get_component_state () const
 
void reset_to_construction_state ()
 Reset this component back to the construction state to allow setup to run again.
 
bool is_in_loop_state () const
 Check if this component has completed setup and is in the loop state.
 
virtual void mark_failed ()
 Mark this component as failed.
 
void mark_failed (const char *message)
 
void disable_loop ()
 Disable this component's loop.
 
void enable_loop ()
 Enable this component's loop.
 
void enable_loop_soon_any_context ()
 Thread and ISR-safe version of enable_loop() that can be called from any context.
 
bool is_failed () const
 
bool is_ready () const
 
virtual bool can_proceed ()
 
bool status_has_warning () const
 
bool status_has_error () const
 
void status_set_warning (const char *message=nullptr)
 
void status_set_error (const char *message=nullptr)
 
void status_clear_warning ()
 
void status_clear_error ()
 
void status_momentary_warning (const std::string &name, uint32_t length=5000)
 
void status_momentary_error (const std::string &name, uint32_t length=5000)
 
bool has_overridden_loop () const
 
void set_component_source (const char *source)
 Set where this component was loaded from for some debug messages.
 
const char * get_component_source () const
 Get the integration where this component was declared as a string.
 
bool should_warn_of_blocking (uint32_t blocking_time)
 

Static Public Member Functions

static bool i2s_on_sent_cb (i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx)
 Callback function used to send playback timestamps the to the speaker task.
 

Data Fields

TaskHandle_t speaker_task_handle_ {nullptr}
 
EventGroupHandle_t event_group_ {nullptr}
 
QueueHandle_t i2s_event_queue_
 
std::weak_ptr< RingBufferaudio_ring_buffer_
 
uint32_t buffer_duration_ms_
 
optional< uint32_t > timeout_
 
bool pause_state_ {false}
 
int16_t q15_volume_factor_ {INT16_MAX}
 
audio::AudioStreamInfo current_stream_info_
 
i2s_dac_mode_t internal_dac_mode_ {I2S_DAC_CHANNEL_DISABLE}
 
uint8_t dout_pin_
 
i2s_comm_format_t i2s_comm_fmt_
 
gpio_num_t dout_pin_
 
std::string i2s_comm_fmt_
 
i2s_chan_handle_t tx_handle_
 

Additional Inherited Members

- Protected Member Functions inherited from esphome::Component
virtual void call_loop ()
 
virtual void call_setup ()
 
virtual void call_dump_config ()
 
void set_component_state_ (uint8_t state)
 Helper to set component state (clears state bits and sets new state)
 
void set_interval (const std::string &name, uint32_t interval, std::function< void()> &&f)
 Set an interval function with a unique name.
 
void set_interval (const char *name, uint32_t interval, std::function< void()> &&f)
 Set an interval function with a const char* name.
 
void set_interval (uint32_t interval, std::function< void()> &&f)
 
bool cancel_interval (const std::string &name)
 Cancel an interval function.
 
bool cancel_interval (const char *name)
 
void set_retry (const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> &&f, float backoff_increase_factor=1.0f)
 Set an retry function with a unique name.
 
void set_retry (uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> &&f, float backoff_increase_factor=1.0f)
 
bool cancel_retry (const std::string &name)
 Cancel a retry function.
 
void set_timeout (const std::string &name, uint32_t timeout, std::function< void()> &&f)
 Set a timeout function with a unique name.
 
void set_timeout (const char *name, uint32_t timeout, std::function< void()> &&f)
 Set a timeout function with a const char* name.
 
void set_timeout (uint32_t timeout, std::function< void()> &&f)
 
bool cancel_timeout (const std::string &name)
 Cancel a timeout function.
 
bool cancel_timeout (const char *name)
 
void defer (const std::string &name, std::function< void()> &&f)
 Defer a callback to the next loop() call.
 
void defer (const char *name, std::function< void()> &&f)
 Defer a callback to the next loop() call with a const char* name.
 
void defer (std::function< void()> &&f)
 Defer a callback to the next loop() call.
 
bool cancel_defer (const std::string &name)
 Cancel a defer callback using the specified name, name must not be empty.
 
- Protected Attributes inherited from esphome::i2s_audio::I2SAudioBase
i2s_mode_t i2s_mode_ {}
 
i2s_channel_fmt_t channel_
 
i2s_bits_per_sample_t bits_per_sample_
 
i2s_bits_per_chan_t bits_per_channel_
 
i2s_role_t i2s_role_ {}
 
i2s_slot_mode_t slot_mode_
 
i2s_std_slot_mask_t std_slot_mask_
 
i2s_slot_bit_width_t slot_bit_width_
 
uint32_t sample_rate_
 
bool use_apll_
 
i2s_mclk_multiple_t mclk_multiple_
 
- Protected Attributes inherited from esphome::Parented< I2SAudioComponent >
I2SAudioComponent * parent_
 
- Protected Attributes inherited from esphome::speaker::Speaker
State state_ {STATE_STOPPED}
 
audio::AudioStreamInfo audio_stream_info_
 
float volume_ {1.0f}
 
bool mute_state_ {false}
 
audio_dac::AudioDacaudio_dac_ {nullptr}
 
CallbackManager< void(uint32_t, int64_t)> audio_output_callback_ {}
 
- Protected Attributes inherited from esphome::Component
const char * component_source_ {nullptr}
 
uint16_t warn_if_blocking_over_ {WARN_IF_BLOCKING_OVER_MS}
 Warn if blocked for this many ms (max 65.5s)
 
uint8_t component_state_ {0x00}
 State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION, 0x01=SETUP, 0x02=LOOP, 0x03=FAILED, 0x04=LOOP_DONE) Bit 3: STATUS_LED_WARNING Bit 4: STATUS_LED_ERROR Bits 5-7: Unused - reserved for future expansion.
 
volatile bool pending_enable_loop_ {false}
 ISR-safe flag for enable_loop_soon_any_context.
 

Detailed Description

Definition at line 22 of file i2s_audio_speaker.h.

Member Function Documentation

◆ dump_config()

void esphome::i2s_audio::I2SAudioSpeaker::dump_config ( )
overridevirtual

Reimplemented from esphome::Component.

◆ finish()

void esphome::i2s_audio::I2SAudioSpeaker::finish ( )
overridevirtual

Reimplemented from esphome::speaker::Speaker.

◆ get_pause_state()

bool esphome::i2s_audio::I2SAudioSpeaker::get_pause_state ( ) const
inlineoverridevirtual

Reimplemented from esphome::speaker::Speaker.

Definition at line 48 of file i2s_audio_speaker.h.

◆ get_setup_priority()

float esphome::i2s_audio::I2SAudioSpeaker::get_setup_priority ( ) const
inlineoverridevirtual

Reimplemented from esphome::Component.

Definition at line 24 of file i2s_audio_speaker.h.

◆ i2s_on_sent_cb()

static bool esphome::i2s_audio::I2SAudioSpeaker::i2s_on_sent_cb ( i2s_chan_handle_t handle,
i2s_event_data_t * event,
void * user_ctx )
static

Callback function used to send playback timestamps the to the speaker task.

Parameters
handle(i2s_chan_handle_t)
event(i2s_event_data_t)
user_ctx(void*) User context pointer that the callback accesses
Returns
True if a higher priority task was interrupted

◆ loop()

void esphome::i2s_audio::I2SAudioSpeaker::loop ( )
overridevirtual

Reimplemented from esphome::Component.

◆ set_buffer_duration()

void esphome::i2s_audio::I2SAudioSpeaker::set_buffer_duration ( uint32_t buffer_duration_ms)
inline

Definition at line 30 of file i2s_audio_speaker.h.

◆ set_dout_pin() [1/2]

void esphome::i2s_audio::I2SAudioSpeaker::set_dout_pin ( uint8_t pin)
inline

Definition at line 36 of file i2s_audio_speaker.h.

◆ set_dout_pin() [2/2]

void esphome::i2s_audio::I2SAudioSpeaker::set_dout_pin ( uint8_t pin)
inline

Definition at line 39 of file i2s_audio_speaker.h.

◆ set_i2s_comm_fmt() [1/2]

void esphome::i2s_audio::I2SAudioSpeaker::set_i2s_comm_fmt ( i2s_comm_format_t mode)
inline

Definition at line 37 of file i2s_audio_speaker.h.

◆ set_i2s_comm_fmt() [2/2]

void esphome::i2s_audio::I2SAudioSpeaker::set_i2s_comm_fmt ( std::string mode)
inline

Definition at line 40 of file i2s_audio_speaker.h.

◆ set_internal_dac_mode()

void esphome::i2s_audio::I2SAudioSpeaker::set_internal_dac_mode ( i2s_dac_mode_t mode)
inline

Definition at line 34 of file i2s_audio_speaker.h.

◆ set_pause_state()

void esphome::i2s_audio::I2SAudioSpeaker::set_pause_state ( bool pause_state)
inlineoverridevirtual

Reimplemented from esphome::speaker::Speaker.

Definition at line 47 of file i2s_audio_speaker.h.

◆ set_timeout()

void esphome::i2s_audio::I2SAudioSpeaker::set_timeout ( uint32_t ms)
inline

Definition at line 31 of file i2s_audio_speaker.h.

◆ setup()

void esphome::i2s_audio::I2SAudioSpeaker::setup ( )
overridevirtual

Reimplemented from esphome::Component.

◆ start()

void esphome::i2s_audio::I2SAudioSpeaker::start ( )
overridevirtual

◆ start_i2s_driver_()

esp_err_t esphome::i2s_audio::I2SAudioSpeaker::start_i2s_driver_ ( audio::AudioStreamInfo & audio_stream_info)

Starts the ESP32 I2S driver.

Attempts to lock the I2S port, starts the I2S driver using the passed in stream information, and sets the data out pin. If it fails, it will unlock the I2S port and uninstalls the driver, if necessary.

Parameters
audio_stream_infoStream information for the I2S driver.
Returns
ESP_ERR_NOT_ALLOWED if the I2S port can't play the incoming audio stream. ESP_ERR_INVALID_STATE if the I2S port is already locked. ESP_ERR_INVALID_ARG if installing the driver or setting the data outpin fails due to a parameter error. ESP_ERR_NO_MEM if the driver fails to install due to a memory allocation error. ESP_FAIL if setting the data out pin fails due to an IO error ESP_OK if successful

◆ stop()

void esphome::i2s_audio::I2SAudioSpeaker::stop ( )
overridevirtual

◆ stop_()

void esphome::i2s_audio::I2SAudioSpeaker::stop_ ( bool wait_on_empty)

Plays the provided audio data.

Starts the speaker task, if necessary. Writes the audio data to the ring buffer.

Parameters
dataAudio data in the format set by the parent speaker classes set_audio_stream_info method. /
lengthThe length of the audio data in bytes. /
ticks_to_waitThe FreeRTOS ticks to wait before writing as much data as possible to the ring buffer. /
Returns
The number of bytes that were actually written to the ring buffer. size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) override; size_t play(const uint8_t *data, size_t length) override { return play(data, length, 0); }

bool has_buffered_data() const override;

/ Sets the volume of the speaker. Uses the speaker's configured audio dac component. If unavailble, it is / implemented as a software volume control. Overrides the default setter to convert the floating point volume to a / Q15 fixed-point factor. /

Parameters
volumebetween 0.0 and 1.0 void set_volume(float volume) override;

/ Mutes or unmute the speaker. Uses the speaker's configured audio dac component. If unavailble, it is / implemented as a software volume control. Overrides the default setter to convert the floating point volume to a / Q15 fixed-point factor. /

Parameters
mute_statetrue for muting, false for unmuting void set_mute_state(bool mute_state) override;

protected: / Function for the FreeRTOS task handling audio output. / Allocates space for the buffers, reads audio from the ring buffer and writes audio to the I2S port. Stops / immmiately after receiving the COMMAND_STOP signal and stops only after the ring buffer is empty after receiving / the COMMAND_STOP_GRACEFULLY signal. Stops if the ring buffer hasn't read data for more than timeout_ milliseconds. / When stopping, it deallocates the buffers. It communicates its state and any errors via event_group_. /

Parameters
paramsI2SAudioSpeaker component static void speaker_task(void *params);

/ Sends a stop command to the speaker task via event_group_.

Parameters
wait_on_emptyIf false, sends the COMMAND_STOP signal. If true, sends the COMMAND_STOP_GRACEFULLY signal.

◆ stop_i2s_driver_()

void esphome::i2s_audio::I2SAudioSpeaker::stop_i2s_driver_ ( )

Stops the I2S driver and unlocks the I2S port.

Field Documentation

◆ audio_ring_buffer_

std::weak_ptr<RingBuffer> esphome::i2s_audio::I2SAudioSpeaker::audio_ring_buffer_

Definition at line 115 of file i2s_audio_speaker.h.

◆ buffer_duration_ms_

uint32_t esphome::i2s_audio::I2SAudioSpeaker::buffer_duration_ms_

Definition at line 117 of file i2s_audio_speaker.h.

◆ current_stream_info_

audio::AudioStreamInfo esphome::i2s_audio::I2SAudioSpeaker::current_stream_info_

Definition at line 125 of file i2s_audio_speaker.h.

◆ dout_pin_ [1/2]

uint8_t esphome::i2s_audio::I2SAudioSpeaker::dout_pin_

Definition at line 131 of file i2s_audio_speaker.h.

◆ dout_pin_ [2/2]

gpio_num_t esphome::i2s_audio::I2SAudioSpeaker::dout_pin_

Definition at line 134 of file i2s_audio_speaker.h.

◆ event_group_

EventGroupHandle_t esphome::i2s_audio::I2SAudioSpeaker::event_group_ {nullptr}

Definition at line 111 of file i2s_audio_speaker.h.

◆ i2s_comm_fmt_ [1/2]

i2s_comm_format_t esphome::i2s_audio::I2SAudioSpeaker::i2s_comm_fmt_

Definition at line 132 of file i2s_audio_speaker.h.

◆ i2s_comm_fmt_ [2/2]

std::string esphome::i2s_audio::I2SAudioSpeaker::i2s_comm_fmt_

Definition at line 135 of file i2s_audio_speaker.h.

◆ i2s_event_queue_

QueueHandle_t esphome::i2s_audio::I2SAudioSpeaker::i2s_event_queue_

Definition at line 113 of file i2s_audio_speaker.h.

◆ internal_dac_mode_

i2s_dac_mode_t esphome::i2s_audio::I2SAudioSpeaker::internal_dac_mode_ {I2S_DAC_CHANNEL_DISABLE}

Definition at line 129 of file i2s_audio_speaker.h.

◆ pause_state_

bool esphome::i2s_audio::I2SAudioSpeaker::pause_state_ {false}

Definition at line 121 of file i2s_audio_speaker.h.

◆ q15_volume_factor_

int16_t esphome::i2s_audio::I2SAudioSpeaker::q15_volume_factor_ {INT16_MAX}

Definition at line 123 of file i2s_audio_speaker.h.

◆ speaker_task_handle_

TaskHandle_t esphome::i2s_audio::I2SAudioSpeaker::speaker_task_handle_ {nullptr}

Definition at line 110 of file i2s_audio_speaker.h.

◆ timeout_

optional<uint32_t> esphome::i2s_audio::I2SAudioSpeaker::timeout_

Definition at line 119 of file i2s_audio_speaker.h.

◆ tx_handle_

i2s_chan_handle_t esphome::i2s_audio::I2SAudioSpeaker::tx_handle_

Definition at line 136 of file i2s_audio_speaker.h.


The documentation for this class was generated from the following file: