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