ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
audio_transfer_buffer.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
6#include "esphome/core/helpers.h" // for ESPDEPRECATED
7
8#ifdef USE_SPEAKER
10#endif
11
12#include "esp_err.h"
13
14#include <freertos/FreeRTOS.h>
15
16namespace esphome::audio {
17
20 public:
21 virtual size_t audio_sink_write(uint8_t *data, size_t length, TickType_t ticks_to_wait) = 0;
22};
23
25 /*
26 * @brief Class that facilitates tranferring data between a buffer and an audio source or sink.
27 * The transfer buffer is a typical C array that temporarily holds data for processing in other audio components.
28 * Both sink and source transfer buffers can use a ring buffer as the sink/source.
29 * - The ring buffer is stored in a shared_ptr, so destroying the transfer buffer object will release ownership.
30 */
31 public:
34
36 uint8_t *get_buffer_start() const { return this->data_start_; }
37
39 uint8_t *get_buffer_end() const { return this->data_start_ + this->buffer_length_; }
40
43 void decrease_buffer_length(size_t bytes);
44
47 void increase_buffer_length(size_t bytes);
48
50 size_t available() const { return this->buffer_length_; }
51
53 size_t capacity() const { return this->buffer_size_; }
54
56 size_t free() const;
57
59 virtual void clear_buffered_data();
60
63 virtual bool has_buffered_data() const;
64
68 bool reallocate(size_t new_buffer_size);
69
70 protected:
74 bool allocate_buffer_(size_t buffer_size);
75
77 void deallocate_buffer_();
78
79 // A possible source or sink for the transfer buffer
80 std::shared_ptr<ring_buffer::RingBuffer> ring_buffer_;
81
82 uint8_t *buffer_{nullptr};
83 uint8_t *data_start_{nullptr};
84
85 size_t buffer_size_{0};
86 size_t buffer_length_{0};
87};
88
90 /*
91 * @brief A class that implements a transfer buffer for audio sinks.
92 * Supports writing processed data in the transfer buffer to a ring buffer or a speaker component.
93 */
94 public:
98 static std::unique_ptr<AudioSinkTransferBuffer> create(size_t buffer_size);
99
105 size_t transfer_data_to_sink(TickType_t ticks_to_wait, bool post_shift = true);
106
109 void set_sink(const std::weak_ptr<ring_buffer::RingBuffer> &ring_buffer) { this->ring_buffer_ = ring_buffer.lock(); }
110
111#ifdef USE_SPEAKER
114 void set_sink(speaker::Speaker *speaker) { this->speaker_ = speaker; }
115#endif
116
119 void set_sink(AudioSinkCallback *callback) { this->sink_callback_ = callback; }
120
121 void clear_buffered_data() override;
122
123 bool has_buffered_data() const override;
124
125 protected:
126#ifdef USE_SPEAKER
128#endif
130};
131
135 public:
136 virtual ~AudioReadableBuffer() = default;
137
139 virtual const uint8_t *data() const = 0;
140
142 virtual size_t available() const = 0;
143
145 virtual size_t free() const { return 0; }
146
149 virtual void consume(size_t bytes) = 0;
150
152 virtual bool has_buffered_data() const = 0;
153
157 virtual size_t fill(TickType_t ticks_to_wait) { return 0; }
158
159 // Remove before 2027.5.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); }
162};
163
164// Remove before 2027.5.0
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 {
170 /*
171 * @brief A class that implements a transfer buffer for audio sources.
172 * Supports reading audio data from a ring buffer into the transfer buffer for processing.
173 * Implements AudioReadableBuffer for use by consumers that only need read access.
174 */
175 public:
179 static std::unique_ptr<AudioSourceTransferBuffer> create(size_t buffer_size);
180
186 size_t transfer_data_from_source(TickType_t ticks_to_wait, bool pre_shift = true);
187
190 void set_source(const std::weak_ptr<ring_buffer::RingBuffer> &ring_buffer) {
191 this->ring_buffer_ = ring_buffer.lock();
192 };
193
194 // AudioReadableBuffer interface
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);
203 }
204};
205#pragma GCC diagnostic pop
206
210 public:
214 void set_data(const uint8_t *data, size_t length);
215
216 // AudioReadableBuffer interface
217 const uint8_t *data() const override { return this->data_start_; }
218 size_t available() const override { return this->length_; }
219 void consume(size_t bytes) override;
220 bool has_buffered_data() const override { return this->length_ > 0; }
221
222 protected:
223 const uint8_t *data_start_{nullptr};
224 size_t length_{0};
225};
226
238 public:
240 static constexpr size_t MAX_ALIGNMENT_BYTES = 8;
241
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);
251
252 ~RingBufferAudioSource() override;
253
254 // AudioReadableBuffer interface
255 const uint8_t *data() const override { return this->current_data_; }
256 size_t available() const override { return this->current_available_; }
257 void consume(size_t bytes) override;
258 bool has_buffered_data() const override;
260 size_t fill(TickType_t ticks_to_wait) override;
261 // Keeps the deprecated fill(ticks_to_wait, pre_shift) overload visible. Remove before 2027.5.0.
263
266 void clear_buffered_data();
267
272 size_t expose_padded_partial(size_t frame_bytes);
273
279 uint8_t *mutable_data() { return this->current_data_; }
280
281 protected:
284 explicit RingBufferAudioSource(std::shared_ptr<ring_buffer::RingBuffer> ring_buffer, size_t max_fill_bytes,
285 uint8_t alignment_bytes)
286 : ring_buffer_(std::move(ring_buffer)), max_fill_bytes_(max_fill_bytes), alignment_bytes_(alignment_bytes) {}
287
290 void release_item_();
291
292 std::shared_ptr<ring_buffer::RingBuffer> ring_buffer_;
294
295 void *acquired_item_{nullptr};
296 uint8_t *current_data_{nullptr};
297
298 // Sub-frame trailing bytes inside the held item that will be copied to splice_buffer_ on release.
299 uint8_t *item_trailing_ptr_{nullptr};
300
301 // After the currently-exposed splice frame is consumed, fill() will promote this region (the aligned
302 // remainder of the new chunk) to the exposed region. queued_length_ == 0 when nothing is queued.
303 uint8_t *queued_data_{nullptr};
304
305 // Splice buffer holds the start of a partial frame whose remainder lives at the head of the next
306 // chunk. While splice_length_ > 0, the buffer is incomplete and waiting for completion bytes.
308
310 size_t queued_length_{0};
311
312 // item_trailing_length_ and splice_length_ are bounded by MAX_ALIGNMENT_BYTES.
315 uint8_t splice_length_{0};
316};
317
318} // namespace esphome::audio
319
320#endif
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 set_sink(speaker::Speaker *speaker)
Adds a speaker as the transfer buffer's sink.
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).
void set_data(const uint8_t *data, size_t length)
Sets the data pointer and length for the buffer.
const uint8_t * data() const override
Zero-copy audio source that reads directly from a ring buffer's internal storage.
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...
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...
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.
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
STL namespace.
uint16_t length
Definition tt21100.cpp:0