ESPHome  2025.2.2
Public Member Functions | Protected Attributes
esphome::speaker::Speaker Class Referenceabstract

#include <speaker.h>

Inheritance diagram for esphome::speaker::Speaker:
esphome::i2s_audio::I2SAudioSpeaker esphome::mixer_speaker::SourceSpeaker esphome::resampler::ResamplerSpeaker

Public Member Functions

virtual size_t play (const uint8_t *data, size_t length, TickType_t ticks_to_wait)
 Plays the provided audio data. More...
 
virtual size_t play (const uint8_t *data, size_t length)=0
 Plays the provided audio data. More...
 
size_t play (const std::vector< uint8_t > &data)
 
virtual void start ()=0
 
virtual void stop ()=0
 
virtual void finish ()
 
virtual void set_pause_state (bool pause_state)
 
virtual bool get_pause_state () const
 
virtual bool has_buffered_data () const =0
 
bool is_running () const
 
bool is_stopped () const
 
virtual void set_volume (float volume)
 
float get_volume ()
 
virtual void set_mute_state (bool mute_state)
 
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, uint32_t, uint32_t, uint32_t)> &&callback)
 Callback function for sending the duration of the audio written to the speaker since the last callback. More...
 

Protected Attributes

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, uint32_t, uint32_t, uint32_t)> audio_output_callback_ {}
 

Detailed Description

Definition at line 29 of file speaker.h.

Member Function Documentation

◆ add_audio_output_callback()

void esphome::speaker::Speaker::add_audio_output_callback ( std::function< void(uint32_t, uint32_t, uint32_t, uint32_t)> &&  callback)
inline

Callback function for sending the duration of the audio written to the speaker since the last callback.

Parameters:

  • Duration in milliseconds. Never rounded and should always be less than or equal to the actual duration.
  • Remainder duration in microseconds. Rounded duration after subtracting the previous parameter from the actual duration.
  • Duration of remaining, unwritten audio buffered in the speaker in milliseconds.
  • System time in microseconds when the last write was completed.

Definition at line 112 of file speaker.h.

◆ finish()

virtual void esphome::speaker::Speaker::finish ( )
inlinevirtual

◆ get_audio_stream_info()

audio::AudioStreamInfo& esphome::speaker::Speaker::get_audio_stream_info ( )
inline

Definition at line 103 of file speaker.h.

◆ get_mute_state()

bool esphome::speaker::Speaker::get_mute_state ( )
inline

Definition at line 93 of file speaker.h.

◆ get_pause_state()

virtual bool esphome::speaker::Speaker::get_pause_state ( ) const
inlinevirtual

◆ get_volume()

float esphome::speaker::Speaker::get_volume ( )
inline

Definition at line 79 of file speaker.h.

◆ has_buffered_data()

virtual bool esphome::speaker::Speaker::has_buffered_data ( ) const
pure virtual

◆ is_running()

bool esphome::speaker::Speaker::is_running ( ) const
inline

Definition at line 66 of file speaker.h.

◆ is_stopped()

bool esphome::speaker::Speaker::is_stopped ( ) const
inline

Definition at line 67 of file speaker.h.

◆ play() [1/3]

virtual size_t esphome::speaker::Speaker::play ( const uint8_t *  data,
size_t  length,
TickType_t  ticks_to_wait 
)
inlinevirtual

Plays the provided audio data.

If the speaker component doesn't implement this method, it falls back to the play method without this parameter.

Parameters
dataAudio data in the format specified by 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 speaker's internal buffer.

Reimplemented in esphome::i2s_audio::I2SAudioSpeaker, esphome::mixer_speaker::SourceSpeaker, and esphome::resampler::ResamplerSpeaker.

Definition at line 38 of file speaker.h.

◆ play() [2/3]

virtual size_t esphome::speaker::Speaker::play ( const uint8_t *  data,
size_t  length 
)
pure virtual

Plays the provided audio data.

If the audio stream is not the default defined in "esphome/core/audio.h" and the speaker component implements it, then this should be called after calling set_audio_stream_info.

Parameters
dataAudio data in the format specified by set_audio_stream_info method.
lengthThe length of the audio data in bytes.
Returns
The number of bytes that were actually written to the speaker's internal buffer.

Implemented in esphome::i2s_audio::I2SAudioSpeaker, esphome::mixer_speaker::SourceSpeaker, and esphome::resampler::ResamplerSpeaker.

◆ play() [3/3]

size_t esphome::speaker::Speaker::play ( const std::vector< uint8_t > &  data)
inline

Definition at line 51 of file speaker.h.

◆ set_audio_dac()

void esphome::speaker::Speaker::set_audio_dac ( audio_dac::AudioDac audio_dac)
inline

Definition at line 96 of file speaker.h.

◆ set_audio_stream_info()

void esphome::speaker::Speaker::set_audio_stream_info ( const audio::AudioStreamInfo audio_stream_info)
inline

Definition at line 99 of file speaker.h.

◆ set_mute_state()

virtual void esphome::speaker::Speaker::set_mute_state ( bool  mute_state)
inlinevirtual

◆ set_pause_state()

virtual void esphome::speaker::Speaker::set_pause_state ( bool  pause_state)
inlinevirtual

◆ set_volume()

virtual void esphome::speaker::Speaker::set_volume ( float  volume)
inlinevirtual

◆ start()

virtual void esphome::speaker::Speaker::start ( )
pure virtual

◆ stop()

virtual void esphome::speaker::Speaker::stop ( )
pure virtual

Field Documentation

◆ audio_dac_

audio_dac::AudioDac* esphome::speaker::Speaker::audio_dac_ {nullptr}
protected

Definition at line 123 of file speaker.h.

◆ audio_output_callback_

CallbackManager<void(uint32_t, uint32_t, uint32_t, uint32_t)> esphome::speaker::Speaker::audio_output_callback_ {}
protected

Definition at line 126 of file speaker.h.

◆ audio_stream_info_

audio::AudioStreamInfo esphome::speaker::Speaker::audio_stream_info_
protected

Definition at line 118 of file speaker.h.

◆ mute_state_

bool esphome::speaker::Speaker::mute_state_ {false}
protected

Definition at line 120 of file speaker.h.

◆ state_

State esphome::speaker::Speaker::state_ {STATE_STOPPED}
protected

Definition at line 117 of file speaker.h.

◆ volume_

float esphome::speaker::Speaker::volume_ {1.0f}
protected

Definition at line 119 of file speaker.h.


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