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

#include <audio_reader.h>

Public Member Functions

 AudioReader (size_t buffer_size)
 Constructs an AudioReader object. More...
 
 ~AudioReader ()
 
esp_err_t add_sink (const std::weak_ptr< RingBuffer > &output_ring_buffer)
 Adds a sink ring buffer for audio data. More...
 
esp_err_t start (const std::string &uri, AudioFileType &file_type)
 Starts reading an audio file from an http source. More...
 
esp_err_t start (AudioFile *audio_file, AudioFileType &file_type)
 Starts reading an audio file from flash. More...
 
AudioReaderState read ()
 Reads new file data from the source and sends to the ring buffer sink. More...
 

Protected Member Functions

AudioReaderState file_read_ ()
 
AudioReaderState http_read_ ()
 
void cleanup_connection_ ()
 

Static Protected Member Functions

static esp_err_t http_event_handler (esp_http_client_event_t *evt)
 Monitors the http client events to attempt determining the file type from the Content-Type header. More...
 
static AudioFileType get_audio_type (const char *content_type)
 Determines the audio file type from the http header's Content-Type key. More...
 

Protected Attributes

std::shared_ptr< RingBufferfile_ring_buffer_
 
std::unique_ptr< AudioSinkTransferBufferoutput_transfer_buffer_
 
size_t buffer_size_
 
uint32_t no_data_read_count_
 
esp_http_client_handle_t client_ {nullptr}
 
AudioFilecurrent_audio_file_ {nullptr}
 
AudioFileType audio_file_type_ {AudioFileType::NONE}
 
const uint8_t * file_current_ {nullptr}
 

Detailed Description

Definition at line 23 of file audio_reader.h.

Constructor & Destructor Documentation

◆ AudioReader()

esphome::audio::AudioReader::AudioReader ( size_t  buffer_size)
inline

Constructs an AudioReader object.

The transfer buffer isn't allocated here, but only if necessary (an http source) in the start function.

Parameters
buffer_sizeTransfer buffer size in bytes.

Definition at line 33 of file audio_reader.h.

◆ ~AudioReader()

esphome::audio::AudioReader::~AudioReader ( )

Definition at line 53 of file audio_reader.cpp.

Member Function Documentation

◆ add_sink()

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

Adds a sink ring buffer for audio data.

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 successful, ESP_ERR_INVALID_STATE otherwise

Definition at line 55 of file audio_reader.cpp.

◆ cleanup_connection_()

void esphome::audio::AudioReader::cleanup_connection_ ( )
protected

Definition at line 297 of file audio_reader.cpp.

◆ file_read_()

AudioReaderState esphome::audio::AudioReader::file_read_ ( )
protected

Definition at line 246 of file audio_reader.cpp.

◆ get_audio_type()

AudioFileType esphome::audio::AudioReader::get_audio_type ( const char *  content_type)
staticprotected

Determines the audio file type from the http header's Content-Type key.

Parameters
content_typestring with the Content-Type key
Returns
AudioFileType of the url, if it can be determined. If not, return AudioFileType::NONE.

Definition at line 212 of file audio_reader.cpp.

◆ http_event_handler()

esp_err_t esphome::audio::AudioReader::http_event_handler ( esp_http_client_event_t *  evt)
staticprotected

Monitors the http client events to attempt determining the file type from the Content-Type header.

Definition at line 230 of file audio_reader.cpp.

◆ http_read_()

AudioReaderState esphome::audio::AudioReader::http_read_ ( )
protected

Definition at line 259 of file audio_reader.cpp.

◆ read()

AudioReaderState esphome::audio::AudioReader::read ( )

Reads new file data from the source and sends to the ring buffer sink.

Returns
AudioReaderState

Definition at line 202 of file audio_reader.cpp.

◆ start() [1/2]

esp_err_t esphome::audio::AudioReader::start ( const std::string &  uri,
AudioFileType file_type 
)

Starts reading an audio file from an http source.

The transfer buffer is allocated here.

Parameters
uriWeb url to the http file.
file_typeAudioFileType variable passed-by-reference indicating the type of file being read.
Returns
ESP_OK if successful, an ESP_ERR* code otherwise.

Definition at line 81 of file audio_reader.cpp.

◆ start() [2/2]

esp_err_t esphome::audio::AudioReader::start ( AudioFile audio_file,
AudioFileType file_type 
)

Starts reading an audio file from flash.

No transfer buffer is allocated.

Parameters
audio_fileAudioFile struct containing the file.
file_typeAudioFileType variable passed-by-reference indicating the type of file being read.
Returns
ESP_OK

Definition at line 70 of file audio_reader.cpp.

Field Documentation

◆ audio_file_type_

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

Definition at line 79 of file audio_reader.h.

◆ buffer_size_

size_t esphome::audio::AudioReader::buffer_size_
protected

Definition at line 73 of file audio_reader.h.

◆ client_

esp_http_client_handle_t esphome::audio::AudioReader::client_ {nullptr}
protected

Definition at line 76 of file audio_reader.h.

◆ current_audio_file_

AudioFile* esphome::audio::AudioReader::current_audio_file_ {nullptr}
protected

Definition at line 78 of file audio_reader.h.

◆ file_current_

const uint8_t* esphome::audio::AudioReader::file_current_ {nullptr}
protected

Definition at line 80 of file audio_reader.h.

◆ file_ring_buffer_

std::shared_ptr<RingBuffer> esphome::audio::AudioReader::file_ring_buffer_
protected

Definition at line 69 of file audio_reader.h.

◆ no_data_read_count_

uint32_t esphome::audio::AudioReader::no_data_read_count_
protected

Definition at line 74 of file audio_reader.h.

◆ output_transfer_buffer_

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

Definition at line 70 of file audio_reader.h.


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