arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
BufferRTOS.h
1#pragma once
2#include "AudioToolsConfig.h"
3#include "AudioTools/CoreAudio/Buffers.h"
4#include "AudioTools/CoreAudio/AudioLogger.h"
5#include "AudioTools/CoreAudio/AudioBasic/Collections/Allocator.h"
6
7#ifdef ESP32
8# include <freertos/stream_buffer.h>
9# include "freertos/FreeRTOS.h"
10#else
11# include "FreeRTOS.h"
12# include "stream_buffer.h"
13#endif
14
15namespace audio_tools {
16
17// #if ESP_IDF_VERSION_MAJOR >= 4
18
28template <typename T>
29class BufferRTOS : public BaseBuffer<T> {
30 public:
31 BufferRTOS(size_t streamBufferSize, size_t xTriggerLevel = 1,
32 TickType_t writeMaxWait = portMAX_DELAY,
33 TickType_t readMaxWait = portMAX_DELAY,
34 Allocator &allocator = DefaultAllocator)
35 : BaseBuffer<T>() {
36 readWait = readMaxWait;
37 writeWait = writeMaxWait;
38 current_size_bytes = (streamBufferSize+1) * sizeof(T);
39 trigger_level = xTriggerLevel;
40 p_allocator = &allocator;
41
42 if (streamBufferSize > 0) {
43 setup();
44 }
45 }
46
47 ~BufferRTOS() { end(); }
48
50 bool resize(size_t size) {
51 bool result = true;
52 int req_size_bytes = (size + 1)*sizeof(T);
53 if (current_size_bytes != req_size_bytes) {
54 end();
55 current_size_bytes = req_size_bytes;
56 result = setup();
57 }
58 return result;
59 }
60
61 void setReadMaxWait(TickType_t ticks) { readWait = ticks; }
62
63 void setWriteMaxWait(TickType_t ticks) { writeWait = ticks; }
64
65 void setWriteFromISR(bool active) { write_from_isr = active; }
66
67 void setReadFromISR(bool active) { read_from_isr = active; }
68
69 // reads a single value
70 bool read(T &result) override {
71 T data = 0;
72 return readArray(&data, 1)==1;
73 }
74
75 // reads multiple values
76 int readArray(T data[], int len) {
77 if (read_from_isr) {
78 xHigherPriorityTaskWoken = pdFALSE;
79 int result = xStreamBufferReceiveFromISR(xStreamBuffer, (void *)data,
80 sizeof(T) * len,
81 &xHigherPriorityTaskWoken);
82#ifdef ESP32X
83 portYIELD_FROM_ISR();
84#else
85 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
86#endif
87 return result / sizeof(T);
88 } else {
89 return xStreamBufferReceive(xStreamBuffer, (void *)data, sizeof(T) * len,
90 readWait) / sizeof(T);
91 }
92 }
93
94 int writeArray(const T data[], int len) {
95 LOGD("%s: %d", LOG_METHOD, len);
96 if (write_from_isr) {
97 xHigherPriorityTaskWoken = pdFALSE;
98 int result =
99 xStreamBufferSendFromISR(xStreamBuffer, (void *)data, sizeof(T) * len,
100 &xHigherPriorityTaskWoken);
101#ifdef ESP32X
102 portYIELD_FROM_ISR();
103#else
104 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
105#endif
106 return result / sizeof(T);
107 } else {
108 return xStreamBufferSend(xStreamBuffer, (void *)data, sizeof(T) * len,
109 writeWait) / sizeof(T);
110 }
111 }
112
113 // peeks the actual entry from the buffer
114 bool peek(T &result) override {
115 LOGE("peek not implemented");
116 return false;
117 }
118
119 // checks if the buffer is full
120 bool isFull() override {
121 return xStreamBufferIsFull(xStreamBuffer) == pdTRUE;
122 }
123
124 bool isEmpty() { return xStreamBufferIsEmpty(xStreamBuffer) == pdTRUE; }
125
126 // write add an entry to the buffer
127 bool write(T data) override {
128 int len = sizeof(T);
129 return writeArray(&data, len) == len;
130 }
131
132 // clears the buffer
133 void reset() override { xStreamBufferReset(xStreamBuffer); }
134
135 // provides the number of entries that are available to read
136 int available() override {
137 return xStreamBufferBytesAvailable(xStreamBuffer) / sizeof(T);
138 }
139
140 // provides the number of entries that are available to write
141 int availableForWrite() override {
142 return xStreamBufferSpacesAvailable(xStreamBuffer) / sizeof(T);
143 }
144
145 // returns the address of the start of the physical read buffer
146 T *address() override {
147 LOGE("address() not implemented");
148 return nullptr;
149 }
150
151 size_t size() { return current_size_bytes / sizeof(T); }
152
153 operator bool() { return xStreamBuffer != nullptr && size()>0;}
154
155 protected:
156 StreamBufferHandle_t xStreamBuffer = nullptr;
157 StaticStreamBuffer_t static_stream_buffer;
158 uint8_t *p_data = nullptr;
159 Allocator *p_allocator = nullptr;
160 BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
161 int readWait = portMAX_DELAY;
162 int writeWait = portMAX_DELAY;
163 bool read_from_isr = false;
164 bool write_from_isr = false;
165 size_t current_size_bytes = 0;
166 size_t trigger_level = 0;
167
170 bool setup() {
171 if (current_size_bytes == 0) return true;
172
173 // allocate data if necessary
174 int size = (current_size_bytes + 1) * sizeof(T);
175 if (p_data == nullptr) {
176 p_data = (uint8_t *)p_allocator->allocate(size);
177 // check allocation
178 if (p_data == nullptr) {
179 LOGE("allocate falied for %d bytes", size)
180 return false;
181 }
182 }
183
184
185 // create stream buffer if necessary
186 if (xStreamBuffer == nullptr) {
187 xStreamBuffer = xStreamBufferCreateStatic(current_size_bytes, trigger_level,
188 p_data, &static_stream_buffer);
189 }
190 if (xStreamBuffer == nullptr) {
191 LOGE("xStreamBufferCreateStatic failed");
192 return false;
193 }
194 // make sure that the data is empty
195 reset();
196 return true;
197 }
198
200 void end() {
201 if (xStreamBuffer != nullptr) vStreamBufferDelete(xStreamBuffer);
202 p_allocator->free(p_data);
203 current_size_bytes = 0;
204 p_data = nullptr;
205 xStreamBuffer = nullptr;
206 }
207};
208// #endif // ESP_IDF_VERSION_MAJOR >= 4
209
210template <class T>
211using SynchronizedBufferRTOS = BufferRTOS<T>;
212
213} // namespace audio_tools
214
Memory allocateator which uses malloc.
Definition Allocator.h:23
Shared functionality of all buffers.
Definition Buffers.h:26
Buffer implementation which is using a FreeRTOS StreamBuffer. The default allocator uses psram is ava...
Definition BufferRTOS.h:29
bool peek(T &result) override
peeks the actual entry from the buffer
Definition BufferRTOS.h:114
bool read(T &result) override
reads a single value
Definition BufferRTOS.h:70
bool write(T data) override
write add an entry to the buffer
Definition BufferRTOS.h:127
int available() override
provides the number of entries that are available to read
Definition BufferRTOS.h:136
int availableForWrite() override
provides the number of entries that are available to write
Definition BufferRTOS.h:141
T * address() override
returns the address of the start of the physical read buffer
Definition BufferRTOS.h:146
bool isFull() override
checks if the buffer is full
Definition BufferRTOS.h:120
void end()
Release resurces: call resize to restart again.
Definition BufferRTOS.h:200
int writeArray(const T data[], int len)
Fills the buffer data.
Definition BufferRTOS.h:94
bool resize(size_t size)
Re-Allocats the memory and the queue.
Definition BufferRTOS.h:50
void reset() override
clears the buffer
Definition BufferRTOS.h:133
bool setup()
Definition BufferRTOS.h:170
int readArray(T data[], int len)
reads multiple values
Definition BufferRTOS.h:76
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10