ESPHome 2025.5.0
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
19 /*
20 * @brief Class that facilitates tranferring data between a buffer and an audio source or sink.
21 * The transfer buffer is a typical C array that temporarily holds data for processing in other audio components.
22 * Both sink and source transfer buffers can use a ring buffer as the sink/source.
23 * - The ring buffer is stored in a shared_ptr, so destroying the transfer buffer object will release ownership.
24 */
25 public:
28
30 uint8_t *get_buffer_start() const { return this->data_start_; }
31
33 uint8_t *get_buffer_end() const { return this->data_start_ + this->buffer_length_; }
34
37 void decrease_buffer_length(size_t bytes);
38
41 void increase_buffer_length(size_t bytes);
42
44 size_t available() const { return this->buffer_length_; }
45
47 size_t capacity() const { return this->buffer_size_; }
48
50 size_t free() const;
51
53 virtual void clear_buffered_data();
54
57 virtual bool has_buffered_data() const;
58
59 bool reallocate(size_t new_buffer_size);
60
61 protected:
65 bool allocate_buffer_(size_t buffer_size);
66
68 void deallocate_buffer_();
69
70 // A possible source or sink for the transfer buffer
71 std::shared_ptr<RingBuffer> ring_buffer_;
72
73 uint8_t *buffer_{nullptr};
74 uint8_t *data_start_{nullptr};
75
76 size_t buffer_size_{0};
77 size_t buffer_length_{0};
78};
79
81 /*
82 * @brief A class that implements a transfer buffer for audio sinks.
83 * Supports writing processed data in the transfer buffer to a ring buffer or a speaker component.
84 */
85 public:
89 static std::unique_ptr<AudioSinkTransferBuffer> create(size_t buffer_size);
90
96 size_t transfer_data_to_sink(TickType_t ticks_to_wait, bool post_shift = true);
97
100 void set_sink(const std::weak_ptr<RingBuffer> &ring_buffer) { this->ring_buffer_ = ring_buffer.lock(); }
101
102#ifdef USE_SPEAKER
105 void set_sink(speaker::Speaker *speaker) { this->speaker_ = speaker; }
106#endif
107
108 void clear_buffered_data() override;
109
110 bool has_buffered_data() const override;
111
112 protected:
113#ifdef USE_SPEAKER
115#endif
116};
117
119 /*
120 * @brief A class that implements a transfer buffer for audio sources.
121 * Supports reading audio data from a ring buffer into the transfer buffer for processing.
122 */
123 public:
127 static std::unique_ptr<AudioSourceTransferBuffer> create(size_t buffer_size);
128
134 size_t transfer_data_from_source(TickType_t ticks_to_wait, bool pre_shift = true);
135
138 void set_source(const std::weak_ptr<RingBuffer> &ring_buffer) { this->ring_buffer_ = ring_buffer.lock(); };
139};
140
141} // namespace audio
142} // namespace esphome
143
144#endif
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(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 transfer_data_from_source(TickType_t ticks_to_wait, bool pre_shift=true)
Reads any available data from the sink 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)
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 exisiting data can b...
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.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7