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

#include <audio_resampler.h>

Public Member Functions

 AudioResampler (size_t input_buffer_size, size_t output_buffer_size)
 Allocates the input and output transfer buffers. More...
 
esp_err_t add_source (std::weak_ptr< RingBuffer > &input_ring_buffer)
 Adds a source ring buffer for audio data. More...
 
esp_err_t add_sink (std::weak_ptr< RingBuffer > &output_ring_buffer)
 Adds a sink ring buffer for resampled audio. More...
 
esp_err_t add_sink (speaker::Speaker *speaker)
 Adds a sink speaker for decoded audio. More...
 
esp_err_t start (AudioStreamInfo &input_stream_info, AudioStreamInfo &output_stream_info, uint16_t number_of_taps, uint16_t number_of_filters)
 Sets up the class to resample. More...
 
AudioResamplerState resample (bool stop_gracefully, int32_t *ms_differential)
 Resamples audio from the ring buffer source and writes to the sink. More...
 
void set_pause_output_state (bool pause_state)
 Pauses sending resampled audio to the sink. More...
 

Protected Attributes

std::unique_ptr< AudioSourceTransferBufferinput_transfer_buffer_
 
std::unique_ptr< AudioSinkTransferBufferoutput_transfer_buffer_
 
size_t input_buffer_size_
 
size_t output_buffer_size_
 
uint32_t accumulated_frames_used_ {0}
 
uint32_t accumulated_frames_generated_ {0}
 
bool pause_output_ {false}
 
AudioStreamInfo input_stream_info_
 
AudioStreamInfo output_stream_info_
 
std::unique_ptr< esp_audio_libs::resampler::Resampler > resampler_
 

Detailed Description

Definition at line 28 of file audio_resampler.h.

Constructor & Destructor Documentation

◆ AudioResampler()

esphome::audio::AudioResampler::AudioResampler ( 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 12 of file audio_resampler.cpp.

Member Function Documentation

◆ add_sink() [1/2]

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

Adds a sink ring buffer for resampled 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 26 of file audio_resampler.cpp.

◆ add_sink() [2/2]

esp_err_t esphome::audio::AudioResampler::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 35 of file audio_resampler.cpp.

◆ add_source()

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

Adds a source ring buffer for audio 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 18 of file audio_resampler.cpp.

◆ resample()

AudioResamplerState esphome::audio::AudioResampler::resample ( bool  stop_gracefully,
int32_t *  ms_differential 
)

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

Parameters
stop_gracefullyIf true, it indicates the file decoder is finished. The resampler will resample all the remaining audio and then finish.
ms_differentialPointer to a (int32_t) variable that will store the difference, in milliseconds, between the duration of input audio used and the duration of output audio generated.
Returns
AudioResamplerState

Definition at line 88 of file audio_resampler.cpp.

◆ set_pause_output_state()

void esphome::audio::AudioResampler::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 78 of file audio_resampler.h.

◆ start()

esp_err_t esphome::audio::AudioResampler::start ( AudioStreamInfo input_stream_info,
AudioStreamInfo output_stream_info,
uint16_t  number_of_taps,
uint16_t  number_of_filters 
)

Sets up the class to resample.

Parameters
input_stream_infoThe incoming sample rate, bits per sample, and number of channels
output_stream_infoThe desired outgoing sample rate, bits per sample, and number of channels
number_of_tapsNumber of taps per FIR filter
number_of_filtersNumber of FIR filters
Returns
ESP_OK if it is able to convert the incoming stream, ESP_ERR_NO_MEM if the transfer buffers failed to allocate, ESP_ERR_NOT_SUPPORTED if the stream can't be converted.

Definition at line 44 of file audio_resampler.cpp.

Field Documentation

◆ accumulated_frames_generated_

uint32_t esphome::audio::AudioResampler::accumulated_frames_generated_ {0}
protected

Definition at line 88 of file audio_resampler.h.

◆ accumulated_frames_used_

uint32_t esphome::audio::AudioResampler::accumulated_frames_used_ {0}
protected

Definition at line 87 of file audio_resampler.h.

◆ input_buffer_size_

size_t esphome::audio::AudioResampler::input_buffer_size_
protected

Definition at line 84 of file audio_resampler.h.

◆ input_stream_info_

AudioStreamInfo esphome::audio::AudioResampler::input_stream_info_
protected

Definition at line 92 of file audio_resampler.h.

◆ input_transfer_buffer_

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

Definition at line 81 of file audio_resampler.h.

◆ output_buffer_size_

size_t esphome::audio::AudioResampler::output_buffer_size_
protected

Definition at line 85 of file audio_resampler.h.

◆ output_stream_info_

AudioStreamInfo esphome::audio::AudioResampler::output_stream_info_
protected

Definition at line 93 of file audio_resampler.h.

◆ output_transfer_buffer_

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

Definition at line 82 of file audio_resampler.h.

◆ pause_output_

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

Definition at line 90 of file audio_resampler.h.

◆ resampler_

std::unique_ptr<esp_audio_libs::resampler::Resampler> esphome::audio::AudioResampler::resampler_
protected

Definition at line 95 of file audio_resampler.h.


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