ESPHome 2025.9.0-dev
|
#include <i2s_audio_speaker.h>
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. | |
![]() | |
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) |
![]() | |
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. | |
![]() | |
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::AudioStreamInfo & | get_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. | |
![]() | |
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< RingBuffer > | audio_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 | |
![]() | |
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. | |
![]() | |
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_ |
![]() | |
I2SAudioComponent * | parent_ |
![]() | |
State | state_ {STATE_STOPPED} |
audio::AudioStreamInfo | audio_stream_info_ |
float | volume_ {1.0f} |
bool | mute_state_ {false} |
audio_dac::AudioDac * | audio_dac_ {nullptr} |
CallbackManager< void(uint32_t, int64_t)> | audio_output_callback_ {} |
![]() | |
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. | |
Definition at line 22 of file i2s_audio_speaker.h.
|
overridevirtual |
Reimplemented from esphome::Component.
|
overridevirtual |
Reimplemented from esphome::speaker::Speaker.
|
inlineoverridevirtual |
Reimplemented from esphome::speaker::Speaker.
Definition at line 48 of file i2s_audio_speaker.h.
|
inlineoverridevirtual |
Reimplemented from esphome::Component.
Definition at line 24 of file i2s_audio_speaker.h.
|
static |
Callback function used to send playback timestamps the to the speaker task.
handle | (i2s_chan_handle_t) |
event | (i2s_event_data_t) |
user_ctx | (void*) User context pointer that the callback accesses |
|
overridevirtual |
Reimplemented from esphome::Component.
|
inline |
Definition at line 30 of file i2s_audio_speaker.h.
|
inline |
Definition at line 36 of file i2s_audio_speaker.h.
|
inline |
Definition at line 39 of file i2s_audio_speaker.h.
|
inline |
Definition at line 37 of file i2s_audio_speaker.h.
|
inline |
Definition at line 40 of file i2s_audio_speaker.h.
|
inline |
Definition at line 34 of file i2s_audio_speaker.h.
|
inlineoverridevirtual |
Reimplemented from esphome::speaker::Speaker.
Definition at line 47 of file i2s_audio_speaker.h.
|
inline |
Definition at line 31 of file i2s_audio_speaker.h.
|
overridevirtual |
Reimplemented from esphome::Component.
|
overridevirtual |
Implements esphome::speaker::Speaker.
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.
audio_stream_info | Stream information for the I2S driver. |
|
overridevirtual |
Implements esphome::speaker::Speaker.
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.
data | Audio data in the format set by the parent speaker classes set_audio_stream_info method. / |
length | The length of the audio data in bytes. / |
ticks_to_wait | The FreeRTOS ticks to wait before writing as much data as possible to the ring buffer. / |
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. /
volume | between 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. /
mute_state | true 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_
. /
params | I2SAudioSpeaker component static void speaker_task(void *params); |
/ Sends a stop command to the speaker task via event_group_
.
wait_on_empty | If false, sends the COMMAND_STOP signal. If true, sends the COMMAND_STOP_GRACEFULLY signal. |
void esphome::i2s_audio::I2SAudioSpeaker::stop_i2s_driver_ | ( | ) |
Stops the I2S driver and unlocks the I2S port.
std::weak_ptr<RingBuffer> esphome::i2s_audio::I2SAudioSpeaker::audio_ring_buffer_ |
Definition at line 115 of file i2s_audio_speaker.h.
uint32_t esphome::i2s_audio::I2SAudioSpeaker::buffer_duration_ms_ |
Definition at line 117 of file i2s_audio_speaker.h.
audio::AudioStreamInfo esphome::i2s_audio::I2SAudioSpeaker::current_stream_info_ |
Definition at line 125 of file i2s_audio_speaker.h.
uint8_t esphome::i2s_audio::I2SAudioSpeaker::dout_pin_ |
Definition at line 131 of file i2s_audio_speaker.h.
gpio_num_t esphome::i2s_audio::I2SAudioSpeaker::dout_pin_ |
Definition at line 134 of file i2s_audio_speaker.h.
EventGroupHandle_t esphome::i2s_audio::I2SAudioSpeaker::event_group_ {nullptr} |
Definition at line 111 of file i2s_audio_speaker.h.
i2s_comm_format_t esphome::i2s_audio::I2SAudioSpeaker::i2s_comm_fmt_ |
Definition at line 132 of file i2s_audio_speaker.h.
std::string esphome::i2s_audio::I2SAudioSpeaker::i2s_comm_fmt_ |
Definition at line 135 of file i2s_audio_speaker.h.
QueueHandle_t esphome::i2s_audio::I2SAudioSpeaker::i2s_event_queue_ |
Definition at line 113 of file i2s_audio_speaker.h.
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.
bool esphome::i2s_audio::I2SAudioSpeaker::pause_state_ {false} |
Definition at line 121 of file i2s_audio_speaker.h.
int16_t esphome::i2s_audio::I2SAudioSpeaker::q15_volume_factor_ {INT16_MAX} |
Definition at line 123 of file i2s_audio_speaker.h.
TaskHandle_t esphome::i2s_audio::I2SAudioSpeaker::speaker_task_handle_ {nullptr} |
Definition at line 110 of file i2s_audio_speaker.h.
optional<uint32_t> esphome::i2s_audio::I2SAudioSpeaker::timeout_ |
Definition at line 119 of file i2s_audio_speaker.h.
i2s_chan_handle_t esphome::i2s_audio::I2SAudioSpeaker::tx_handle_ |
Definition at line 136 of file i2s_audio_speaker.h.