ESPHome  2025.2.0
audio_transfer_buffer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 #include "esphome/core/defines.h"
6 
7 #ifdef USE_SPEAKER
9 #endif
10 
11 #include "esp_err.h"
12 
13 #include <freertos/FreeRTOS.h>
14 
15 namespace esphome {
16 namespace 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:
64  bool allocate_buffer_(size_t buffer_size);
65 
67  void deallocate_buffer_();
68 
69  // A possible source or sink for the transfer buffer
70  std::shared_ptr<RingBuffer> ring_buffer_;
71 
72  uint8_t *buffer_{nullptr};
73  uint8_t *data_start_{nullptr};
74 
75  size_t buffer_size_{0};
76  size_t buffer_length_{0};
77 };
78 
80  /*
81  * @brief A class that implements a transfer buffer for audio sinks.
82  * Supports writing processed data in the transfer buffer to a ring buffer or a speaker component.
83  */
84  public:
88  static std::unique_ptr<AudioSinkTransferBuffer> create(size_t buffer_size);
89 
93  size_t transfer_data_to_sink(TickType_t ticks_to_wait);
94 
97  void set_sink(const std::weak_ptr<RingBuffer> &ring_buffer) { this->ring_buffer_ = ring_buffer.lock(); }
98 
99 #ifdef USE_SPEAKER
100  void set_sink(speaker::Speaker *speaker) { this->speaker_ = speaker; }
103 #endif
104 
105  void clear_buffered_data() override;
106 
107  bool has_buffered_data() const override;
108 
109  protected:
110 #ifdef USE_SPEAKER
111  speaker::Speaker *speaker_{nullptr};
112 #endif
113 };
114 
116  /*
117  * @brief A class that implements a transfer buffer for audio sources.
118  * Supports reading audio data from a ring buffer into the transfer buffer for processing.
119  */
120  public:
124  static std::unique_ptr<AudioSourceTransferBuffer> create(size_t buffer_size);
125 
129  size_t transfer_data_from_source(TickType_t ticks_to_wait);
130 
133  void set_source(const std::weak_ptr<RingBuffer> &ring_buffer) { this->ring_buffer_ = ring_buffer.lock(); };
134 };
135 
136 } // namespace audio
137 } // namespace esphome
138 
139 #endif
void deallocate_buffer_()
Deallocates the buffer and resets the class variables.
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...
size_t capacity() const
Returns the transfer buffers allocated bytes.
virtual bool has_buffered_data() const
Tests if there is any data in the tranfer buffer or the source/sink.
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&#39;s currently available bytes to read.
void set_sink(const std::weak_ptr< RingBuffer > &ring_buffer)
Adds a ring buffer as the transfer buffer&#39;s sink.
size_t free() const
Returns the transfer buffer&#39;s currrently free bytes available to write.
bool reallocate(size_t new_buffer_size)
void increase_buffer_length(size_t bytes)
Updates the internal state of the transfer buffer.
~AudioTransferBuffer()
Destructor that deallocates the transfer buffer.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
void set_source(const std::weak_ptr< RingBuffer > &ring_buffer)
Adds a ring buffer as the transfer buffer&#39;s source.
virtual void clear_buffered_data()
Clears data in the transfer buffer and, if possible, the source/sink.
std::shared_ptr< RingBuffer > ring_buffer_