ESPHome  2025.2.0
Public Member Functions | Protected Member Functions | Protected Attributes
esphome::audio::AudioDecoder Class Reference

#include <audio_decoder.h>

Public Member Functions

 AudioDecoder (size_t input_buffer_size, size_t output_buffer_size)
 Allocates the input and output transfer buffers. More...
 
 ~AudioDecoder ()
 Deallocates the MP3 decoder (the flac and wav decoders are deallocated automatically) More...
 
esp_err_t add_source (std::weak_ptr< RingBuffer > &input_ring_buffer)
 Adds a source ring buffer for raw file data. More...
 
esp_err_t add_sink (std::weak_ptr< RingBuffer > &output_ring_buffer)
 Adds a sink ring buffer for decoded audio. More...
 
esp_err_t add_sink (speaker::Speaker *speaker)
 Adds a sink speaker for decoded audio. More...
 
esp_err_t start (AudioFileType audio_file_type)
 Sets up decoding the file. More...
 
AudioDecoderState decode (bool stop_gracefully)
 Decodes audio from the ring buffer source and writes to the sink. More...
 
const optional< audio::AudioStreamInfo > & get_audio_stream_info () const
 Gets the audio stream information, if it has been decoded from the files header. More...
 
uint32_t get_playback_ms () const
 Returns the duration of audio (in milliseconds) decoded and sent to the sink. More...
 
void set_pause_output_state (bool pause_state)
 Pauses sending resampled audio to the sink. More...
 

Protected Member Functions

FileDecoderState decode_flac_ ()
 
FileDecoderState decode_mp3_ ()
 
FileDecoderState decode_wav_ ()
 

Protected Attributes

std::unique_ptr< esp_audio_libs::wav_decoder::WAVDecoder > wav_decoder_
 
std::unique_ptr< esp_audio_libs::flac::FLACDecoder > flac_decoder_
 
esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_
 
std::unique_ptr< AudioSourceTransferBufferinput_transfer_buffer_
 
std::unique_ptr< AudioSinkTransferBufferoutput_transfer_buffer_
 
AudioFileType audio_file_type_ {AudioFileType::NONE}
 
optional< AudioStreamInfoaudio_stream_info_ {}
 
size_t free_buffer_required_ {0}
 
size_t wav_bytes_left_ {0}
 
uint32_t potentially_failed_count_ {0}
 
bool end_of_file_ {false}
 
bool wav_has_known_end_ {false}
 
bool pause_output_ {false}
 
uint32_t accumulated_frames_written_ {0}
 
uint32_t playback_ms_ {0}
 

Detailed Description

Definition at line 45 of file audio_decoder.h.

Constructor & Destructor Documentation

◆ AudioDecoder()

esphome::audio::AudioDecoder::AudioDecoder ( size_t  input_buffer_size,
size_t  output_buffer_size 
)

Allocates the input and output transfer buffers.

Parameters
input_buffer_sizeSize of the input transfer buffer in bytes.
output_buffer_sizeSize of the output transfer buffer in bytes.

Definition at line 15 of file audio_decoder.cpp.

◆ ~AudioDecoder()

esphome::audio::AudioDecoder::~AudioDecoder ( )

Deallocates the MP3 decoder (the flac and wav decoders are deallocated automatically)

Definition at line 20 of file audio_decoder.cpp.

Member Function Documentation

◆ add_sink() [1/2]

esp_err_t esphome::audio::AudioDecoder::add_sink ( std::weak_ptr< RingBuffer > &  output_ring_buffer)

Adds a sink ring buffer for decoded audio.

Takes ownership of the ring buffer in a shared_ptr.

Parameters
output_ring_bufferweak_ptr of a shared_ptr of the sink ring buffer to transfer ownership
Returns
ESP_OK if successsful, ESP_ERR_NO_MEM if the transfer buffer wasn't allocated

Definition at line 36 of file audio_decoder.cpp.

◆ add_sink() [2/2]

esp_err_t esphome::audio::AudioDecoder::add_sink ( speaker::Speaker speaker)

Adds a sink speaker for decoded audio.

Parameters
speakerpointer to speaker component
Returns
ESP_OK if successsful, ESP_ERR_NO_MEM if the transfer buffer wasn't allocated

Definition at line 45 of file audio_decoder.cpp.

◆ add_source()

esp_err_t esphome::audio::AudioDecoder::add_source ( std::weak_ptr< RingBuffer > &  input_ring_buffer)

Adds a source ring buffer for raw file data.

Takes ownership of the ring buffer in a shared_ptr.

Parameters
input_ring_bufferweak_ptr of a shared_ptr of the sink ring buffer to transfer ownership
Returns
ESP_OK if successsful, ESP_ERR_NO_MEM if the transfer buffer wasn't allocated

Definition at line 28 of file audio_decoder.cpp.

◆ decode()

AudioDecoderState esphome::audio::AudioDecoder::decode ( bool  stop_gracefully)

Decodes audio from the ring buffer source and writes to the sink.

Parameters
stop_gracefullyIf true, it indicates the file source is finished. The decoder will decode all the reamining data and then finish.
Returns
AudioDecoderState

Definition at line 92 of file audio_decoder.cpp.

◆ decode_flac_()

FileDecoderState esphome::audio::AudioDecoder::decode_flac_ ( )
protected

Definition at line 192 of file audio_decoder.cpp.

◆ decode_mp3_()

FileDecoderState esphome::audio::AudioDecoder::decode_mp3_ ( )
protected

Definition at line 257 of file audio_decoder.cpp.

◆ decode_wav_()

FileDecoderState esphome::audio::AudioDecoder::decode_wav_ ( )
protected

Definition at line 310 of file audio_decoder.cpp.

◆ get_audio_stream_info()

const optional<audio::AudioStreamInfo>& esphome::audio::AudioDecoder::get_audio_stream_info ( ) const
inline

Gets the audio stream information, if it has been decoded from the files header.

Returns
optional<AudioStreamInfo> with the audio information. If not available yet, returns no value.

Definition at line 92 of file audio_decoder.h.

◆ get_playback_ms()

uint32_t esphome::audio::AudioDecoder::get_playback_ms ( ) const
inline

Returns the duration of audio (in milliseconds) decoded and sent to the sink.

Returns
Duration of decoded audio in milliseconds

Definition at line 96 of file audio_decoder.h.

◆ set_pause_output_state()

void esphome::audio::AudioDecoder::set_pause_output_state ( bool  pause_state)
inline

Pauses sending resampled audio to the sink.

If paused, it will continue to process internal buffers.

Parameters
pause_stateIf true, audio data is not sent to the sink.

Definition at line 100 of file audio_decoder.h.

◆ start()

esp_err_t esphome::audio::AudioDecoder::start ( AudioFileType  audio_file_type)

Sets up decoding the file.

Parameters
audio_file_typeAudioFileType of the file
Returns
ESP_OK if successful, ESP_ERR_NO_MEM if the transfer buffers fail to allocate, or ESP_ERR_NOT_SUPPORTED if the format isn't supported.

Definition at line 54 of file audio_decoder.cpp.

Field Documentation

◆ accumulated_frames_written_

uint32_t esphome::audio::AudioDecoder::accumulated_frames_written_ {0}
protected

Definition at line 129 of file audio_decoder.h.

◆ audio_file_type_

AudioFileType esphome::audio::AudioDecoder::audio_file_type_ {AudioFileType::NONE}
protected

Definition at line 117 of file audio_decoder.h.

◆ audio_stream_info_

optional<AudioStreamInfo> esphome::audio::AudioDecoder::audio_stream_info_ {}
protected

Definition at line 118 of file audio_decoder.h.

◆ end_of_file_

bool esphome::audio::AudioDecoder::end_of_file_ {false}
protected

Definition at line 124 of file audio_decoder.h.

◆ flac_decoder_

std::unique_ptr<esp_audio_libs::flac::FLACDecoder> esphome::audio::AudioDecoder::flac_decoder_
protected

Definition at line 106 of file audio_decoder.h.

◆ free_buffer_required_

size_t esphome::audio::AudioDecoder::free_buffer_required_ {0}
protected

Definition at line 120 of file audio_decoder.h.

◆ input_transfer_buffer_

std::unique_ptr<AudioSourceTransferBuffer> esphome::audio::AudioDecoder::input_transfer_buffer_
protected

Definition at line 114 of file audio_decoder.h.

◆ mp3_decoder_

esp_audio_libs::helix_decoder::HMP3Decoder esphome::audio::AudioDecoder::mp3_decoder_
protected

Definition at line 110 of file audio_decoder.h.

◆ output_transfer_buffer_

std::unique_ptr<AudioSinkTransferBuffer> esphome::audio::AudioDecoder::output_transfer_buffer_
protected

Definition at line 115 of file audio_decoder.h.

◆ pause_output_

bool esphome::audio::AudioDecoder::pause_output_ {false}
protected

Definition at line 127 of file audio_decoder.h.

◆ playback_ms_

uint32_t esphome::audio::AudioDecoder::playback_ms_ {0}
protected

Definition at line 130 of file audio_decoder.h.

◆ potentially_failed_count_

uint32_t esphome::audio::AudioDecoder::potentially_failed_count_ {0}
protected

Definition at line 123 of file audio_decoder.h.

◆ wav_bytes_left_

size_t esphome::audio::AudioDecoder::wav_bytes_left_ {0}
protected

Definition at line 121 of file audio_decoder.h.

◆ wav_decoder_

std::unique_ptr<esp_audio_libs::wav_decoder::WAVDecoder> esphome::audio::AudioDecoder::wav_decoder_
protected

Definition at line 103 of file audio_decoder.h.

◆ wav_has_known_end_

bool esphome::audio::AudioDecoder::wav_has_known_end_ {false}
protected

Definition at line 125 of file audio_decoder.h.


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