14#include <freertos/FreeRTOS.h>
98 static std::unique_ptr<AudioSinkTransferBuffer>
create(
size_t buffer_size);
109 void set_sink(
const std::weak_ptr<ring_buffer::RingBuffer> &ring_buffer) { this->
ring_buffer_ = ring_buffer.lock(); }
139 virtual const uint8_t *
data()
const = 0;
145 virtual size_t free()
const {
return 0; }
157 virtual size_t fill(TickType_t ticks_to_wait) {
return 0; }
160 ESPDEPRECATED(
"Use fill(ticks_to_wait) instead. Removed in 2027.5.0.",
"2026.11.0")
161 virtual
size_t fill(TickType_t ticks_to_wait,
bool pre_shift) {
return this->
fill(ticks_to_wait); }
165#pragma GCC diagnostic push
166#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
167class ESPDEPRECATED(
"Use RingBufferAudioSource instead. Removed in 2027.5.0.",
"2026.11.0") AudioSourceTransferBuffer
168 :
public AudioTransferBuffer,
169 public AudioReadableBuffer {
179 static std::unique_ptr<AudioSourceTransferBuffer> create(
size_t buffer_size);
186 size_t transfer_data_from_source(TickType_t ticks_to_wait,
bool pre_shift =
true);
190 void set_source(
const std::weak_ptr<ring_buffer::RingBuffer> &ring_buffer) {
191 this->ring_buffer_ = ring_buffer.lock();
195 const uint8_t *data()
const override {
return this->data_start_; }
196 size_t available()
const override {
return this->buffer_length_; }
197 size_t free()
const override;
198 void consume(
size_t bytes)
override { this->decrease_buffer_length(bytes); }
199 bool has_buffered_data()
const override;
200 size_t fill(TickType_t ticks_to_wait)
override {
return this->transfer_data_from_source(ticks_to_wait); }
201 size_t fill(TickType_t ticks_to_wait,
bool pre_shift)
override {
202 return this->transfer_data_from_source(ticks_to_wait, pre_shift);
205#pragma GCC diagnostic pop
219 void consume(
size_t bytes)
override;
249 static std::unique_ptr<RingBufferAudioSource>
create(std::shared_ptr<ring_buffer::RingBuffer> ring_buffer,
250 size_t max_fill_bytes, uint8_t alignment_bytes = 1);
257 void consume(
size_t bytes)
override;
260 size_t fill(TickType_t ticks_to_wait)
override;
285 uint8_t alignment_bytes)
Abstract interface for reading audio data from a buffer.
ESPDEPRECATED("Use fill(ticks_to_wait) instead. Removed in 2027.5.0.", "2026.11.0") virtual size_t fill(TickType_t ticks_to_wait
virtual const uint8_t * data() const =0
Returns a pointer to the start of readable data.
virtual size_t available() const =0
Returns the number of bytes available to read.
virtual size_t free() const
Returns the number of free bytes available to write. Defaults to 0 for read-only buffers.
virtual ~AudioReadableBuffer()=default
virtual bool has_buffered_data() const =0
Tests if there is any buffered data.
virtual void consume(size_t bytes)=0
Advances past consumed data.
virtual size_t fill(TickType_t ticks_to_wait)
Refills the buffer from its source.
Abstract interface for writing decoded audio data to a sink.
virtual size_t audio_sink_write(uint8_t *data, size_t length, TickType_t ticks_to_wait)=0
size_t transfer_data_to_sink(TickType_t ticks_to_wait, bool post_shift=true)
Writes any available data in the transfer buffer to the sink.
void set_sink(AudioSinkCallback *callback)
Adds a callback as the transfer buffer's sink.
void clear_buffered_data() override
AudioSinkCallback * sink_callback_
void set_sink(speaker::Speaker *speaker)
Adds a speaker as the transfer buffer's sink.
bool has_buffered_data() const override
speaker::Speaker * speaker_
static std::unique_ptr< AudioSinkTransferBuffer > create(size_t buffer_size)
Creates a new sink transfer buffer.
void set_sink(const std::weak_ptr< ring_buffer::RingBuffer > &ring_buffer)
Adds a ring buffer as the transfer buffer's sink.
size_t free() const
Returns the transfer buffer's currrently free bytes available to write.
virtual bool has_buffered_data() const
Tests if there is any data in the tranfer buffer or the source/sink.
virtual void clear_buffered_data()
Clears data in the transfer buffer and, if possible, the source/sink.
~AudioTransferBuffer()
Destructor that deallocates the transfer buffer.
uint8_t * get_buffer_end() const
Returns a pointer to the end of the transfer buffer where free() bytes of new data can be written.
void increase_buffer_length(size_t bytes)
Updates the internal state of the transfer buffer.
bool reallocate(size_t new_buffer_size)
Reallocates the transfer buffer, preserving any existing data.
bool allocate_buffer_(size_t buffer_size)
Allocates the transfer buffer in external memory, if available.
uint8_t * get_buffer_start() const
Returns a pointer to the start of the transfer buffer where available() bytes of existing data can be...
void decrease_buffer_length(size_t bytes)
Updates the internal state of the transfer buffer.
size_t available() const
Returns the transfer buffer's currently available bytes to read.
std::shared_ptr< ring_buffer::RingBuffer > ring_buffer_
size_t capacity() const
Returns the transfer buffers allocated bytes.
void deallocate_buffer_()
Deallocates the buffer and resets the class variables.
A lightweight read-only audio buffer for const data sources (e.g., flash memory).
size_t available() const override
bool has_buffered_data() const override
void set_data(const uint8_t *data, size_t length)
Sets the data pointer and length for the buffer.
const uint8_t * data_start_
const uint8_t * data() const override
void consume(size_t bytes) override
Zero-copy audio source that reads directly from a ring buffer's internal storage.
uint8_t item_trailing_length_
uint8_t splice_buffer_[MAX_ALIGNMENT_BYTES]
void release_item_()
Releases the currently held ring buffer item, first copying any trailing sub-frame bytes into the spl...
void consume(size_t bytes) override
~RingBufferAudioSource() override
size_t current_available_
bool has_buffered_data() const override
size_t expose_padded_partial(size_t frame_bytes)
Zero-pads held whole frames short of an alignment unit and exposes them, so a stream's tail is not st...
uint8_t * item_trailing_ptr_
static constexpr size_t MAX_ALIGNMENT_BYTES
Maximum supported alignment. Sized to cover 32-bit samples across up to 2 channels (8 bytes).
static std::unique_ptr< RingBufferAudioSource > create(std::shared_ptr< ring_buffer::RingBuffer > ring_buffer, size_t max_fill_bytes, uint8_t alignment_bytes=1)
Creates a new ring-buffer-backed audio source after validating its parameters.
size_t available() const override
RingBufferAudioSource(std::shared_ptr< ring_buffer::RingBuffer > ring_buffer, size_t max_fill_bytes, uint8_t alignment_bytes)
Constructs a new ring-buffer-backed audio source.
uint8_t * mutable_data()
Returns a mutable pointer to the currently exposed audio data.
void clear_buffered_data()
Discards all buffered audio: releases any held ring buffer item, clears the source's in-flight state,...
size_t fill(TickType_t ticks_to_wait) override
An unconsumed exposure stays in place and fill() returns 0 until it is fully consumed.
std::shared_ptr< ring_buffer::RingBuffer > ring_buffer_
const uint8_t * data() const override