|
ESPHome 2026.11.0-dev
|
Zero-copy audio source that reads directly from a ring buffer's internal storage. More...
#include <audio_transfer_buffer.h>
Public Member Functions | |
| ~RingBufferAudioSource () override | |
| const uint8_t * | data () const override |
| size_t | available () const override |
| void | consume (size_t bytes) override |
| bool | has_buffered_data () const override |
| size_t | fill (TickType_t ticks_to_wait) override |
| An unconsumed exposure stays in place and fill() returns 0 until it is fully consumed. | |
| void | clear_buffered_data () |
| Discards all buffered audio: releases any held ring buffer item, clears the source's in-flight state, and resets the underlying ring buffer. | |
| 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 stranded. | |
| uint8_t * | mutable_data () |
| Returns a mutable pointer to the currently exposed audio data. | |
Public Member Functions inherited from esphome::audio::AudioReadableBuffer | |
| virtual | ~AudioReadableBuffer ()=default |
| virtual size_t | free () const |
| Returns the number of free bytes available to write. Defaults to 0 for read-only buffers. | |
| ESPDEPRECATED ("Use fill(ticks_to_wait) instead. Removed in 2027.5.0.", "2026.11.0") virtual size_t fill(TickType_t ticks_to_wait | |
Static Public Member Functions | |
| 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. | |
Static Public Attributes | |
| static constexpr size_t | MAX_ALIGNMENT_BYTES = 8 |
| Maximum supported alignment. Sized to cover 32-bit samples across up to 2 channels (8 bytes). | |
Protected Member Functions | |
| 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. | |
| void | release_item_ () |
| Releases the currently held ring buffer item, first copying any trailing sub-frame bytes into the splice buffer so they can be stitched with the next chunk. | |
Protected Attributes | |
| std::shared_ptr< ring_buffer::RingBuffer > | ring_buffer_ |
| size_t | max_fill_bytes_ |
| void * | acquired_item_ {nullptr} |
| uint8_t * | current_data_ {nullptr} |
| uint8_t * | item_trailing_ptr_ {nullptr} |
| uint8_t * | queued_data_ {nullptr} |
| uint8_t | splice_buffer_ [MAX_ALIGNMENT_BYTES] |
| size_t | current_available_ {0} |
| size_t | queued_length_ {0} |
| uint8_t | alignment_bytes_ |
| uint8_t | item_trailing_length_ {0} |
| uint8_t | splice_length_ {0} |
Additional Inherited Members | |
Data Fields inherited from esphome::audio::AudioReadableBuffer | |
| bool | pre_shift { return this->fill(ticks_to_wait) |
Zero-copy audio source that reads directly from a ring buffer's internal storage.
Optionally enforces a minimum read alignment (e.g. one audio frame). When alignment_bytes > 1, the source transparently stitches frames that straddle the ring buffer's wrap boundary by buffering the trailing partial frame from one chunk and joining it with the head of the next chunk in a small internal splice buffer, so callers always see frame-aligned data.
Not thread-safe. The underlying ring_buffer::RingBuffer supports one producer and one consumer running concurrently, but a given RingBufferAudioSource (its acquired item, splice buffer, and queued region) must be used by only one thread, and that thread is the ring buffer's consumer.
Definition at line 237 of file audio_transfer_buffer.h.
|
override |
Definition at line 223 of file audio_transfer_buffer.cpp.
|
inlineexplicitprotected |
Constructs a new ring-buffer-backed audio source.
Use create() instead, which validates arguments before construction.
Definition at line 284 of file audio_transfer_buffer.h.
|
inlineoverridevirtual |
Implements esphome::audio::AudioReadableBuffer.
Definition at line 256 of file audio_transfer_buffer.h.
| void esphome::audio::RingBufferAudioSource::clear_buffered_data | ( | ) |
Discards all buffered audio: releases any held ring buffer item, clears the source's in-flight state, and resets the underlying ring buffer.
Must be invoked from the ring buffer's consumer thread.
Definition at line 259 of file audio_transfer_buffer.cpp.
|
overridevirtual |
Implements esphome::audio::AudioReadableBuffer.
Definition at line 246 of file audio_transfer_buffer.cpp.
|
static |
Creates a new ring-buffer-backed audio source after validating its parameters.
| ring_buffer | The ring buffer to read from. Must be non-null. |
| max_fill_bytes | Soft cap on bytes acquired per fill() call. Must be > 0. |
| alignment_bytes | Minimum exposed-region alignment in bytes (defaults to 1, i.e. byte-aligned). Pass bytes_per_frame to make every exposed region a whole number of frames. Must be in [1, MAX_ALIGNMENT_BYTES]. |
Definition at line 214 of file audio_transfer_buffer.cpp.
|
inlineoverridevirtual |
Implements esphome::audio::AudioReadableBuffer.
Definition at line 255 of file audio_transfer_buffer.h.
| size_t esphome::audio::RingBufferAudioSource::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 stranded.
A held partial frame is left to wait for its remaining bytes.
| frame_bytes | Bytes per audio frame |
Definition at line 275 of file audio_transfer_buffer.cpp.
|
overridevirtual |
An unconsumed exposure stays in place and fill() returns 0 until it is fully consumed.
Reimplemented from esphome::audio::AudioReadableBuffer.
Definition at line 300 of file audio_transfer_buffer.cpp.
|
overridevirtual |
Implements esphome::audio::AudioReadableBuffer.
Definition at line 292 of file audio_transfer_buffer.cpp.
|
inline |
Returns a mutable pointer to the currently exposed audio data.
The pointer may reference the ring buffer's internal storage or, when exposing a stitched frame across a wrap boundary, an internal splice buffer. In either case mutations are safe but data should be discarded after use, since the underlying storage will be reused on the next fill(). Use only when the caller is the sole consumer of this source.
Definition at line 279 of file audio_transfer_buffer.h.
|
protected |
Releases the currently held ring buffer item, first copying any trailing sub-frame bytes into the splice buffer so they can be stitched with the next chunk.
Definition at line 230 of file audio_transfer_buffer.cpp.
|
protected |
Definition at line 295 of file audio_transfer_buffer.h.
|
protected |
Definition at line 313 of file audio_transfer_buffer.h.
|
protected |
Definition at line 309 of file audio_transfer_buffer.h.
|
protected |
Definition at line 296 of file audio_transfer_buffer.h.
|
protected |
Definition at line 314 of file audio_transfer_buffer.h.
|
protected |
Definition at line 299 of file audio_transfer_buffer.h.
|
staticconstexpr |
Maximum supported alignment. Sized to cover 32-bit samples across up to 2 channels (8 bytes).
Definition at line 240 of file audio_transfer_buffer.h.
|
protected |
Definition at line 293 of file audio_transfer_buffer.h.
|
protected |
Definition at line 303 of file audio_transfer_buffer.h.
|
protected |
Definition at line 310 of file audio_transfer_buffer.h.
|
protected |
Definition at line 292 of file audio_transfer_buffer.h.
|
protected |
Definition at line 307 of file audio_transfer_buffer.h.
|
protected |
Definition at line 315 of file audio_transfer_buffer.h.