ESPHome 2026.3.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
7#ifdef USE_SPEAKER
9#endif
10
11#include "esp_err.h"
12
13#include <freertos/FreeRTOS.h>
14
15namespace esphome {
16namespace 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<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<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
158 virtual size_t fill(TickType_t ticks_to_wait, bool pre_shift) { return 0; }
159 size_t fill(TickType_t ticks_to_wait) { return this->fill(ticks_to_wait, true); }
160};
161
163 /*
164 * @brief A class that implements a transfer buffer for audio sources.
165 * Supports reading audio data from a ring buffer into the transfer buffer for processing.
166 * Implements AudioReadableBuffer for use by consumers that only need read access.
167 */
168 public:
172 static std::unique_ptr<AudioSourceTransferBuffer> create(size_t buffer_size);
173
179 size_t transfer_data_from_source(TickType_t ticks_to_wait, bool pre_shift = true);
180
183 void set_source(const std::weak_ptr<RingBuffer> &ring_buffer) { this->ring_buffer_ = ring_buffer.lock(); };
184
185 // AudioReadableBuffer interface
186 const uint8_t *data() const override { return this->data_start_; }
187 size_t available() const override { return this->buffer_length_; }
188 size_t free() const override;
189 void consume(size_t bytes) override { this->decrease_buffer_length(bytes); }
190 bool has_buffered_data() const override;
191 size_t fill(TickType_t ticks_to_wait, bool pre_shift) override {
192 return this->transfer_data_from_source(ticks_to_wait, pre_shift);
193 }
194};
195
199 public:
203 void set_data(const uint8_t *data, size_t length);
204
205 // AudioReadableBuffer interface
206 const uint8_t *data() const override { return this->data_start_; }
207 size_t available() const override { return this->length_; }
208 void consume(size_t bytes) override;
209 bool has_buffered_data() const override { return this->length_ > 0; }
210
211 protected:
212 const uint8_t *data_start_{nullptr};
213 size_t length_{0};
214};
215
216} // namespace audio
217} // namespace esphome
218
219#endif
Abstract interface for reading audio data from a buffer.
virtual const uint8_t * data() const =0
Returns a pointer to the start of readable data.
virtual size_t fill(TickType_t ticks_to_wait, bool pre_shift)
Refills the buffer from its source.
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
size_t fill(TickType_t ticks_to_wait)
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.
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(const std::weak_ptr< RingBuffer > &ring_buffer)
Adds a ring buffer as the transfer buffer's 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.
size_t fill(TickType_t ticks_to_wait, bool pre_shift) override
size_t transfer_data_from_source(TickType_t ticks_to_wait, bool pre_shift=true)
Reads any available data from the source into the transfer buffer.
static std::unique_ptr< AudioSourceTransferBuffer > create(size_t buffer_size)
Creates a new source transfer buffer.
void set_source(const std::weak_ptr< RingBuffer > &ring_buffer)
Adds a ring buffer as the transfer buffer's source.
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< 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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t length
Definition tt21100.cpp:0