arduino-audio-tools
Loading...
Searching...
No Matches
BufferRTOS.h
Go to the documentation of this file.
1#pragma once
2#include "AudioToolsConfig.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
32template <typename T>
33class BufferRTOS : public BaseBuffer<T> {
34 public:
50
52
54 bool resize(size_t size) {
55 bool result = true;
56 int req_size_bytes = (size + 1)*sizeof(T);
58 end();
60 result = setup();
61 }
62 return result;
63 }
64
66
68
69 void setWriteFromISR(bool active) { write_from_isr = active; }
70
71 void setReadFromISR(bool active) { read_from_isr = active; }
72
73 // reads a single value
74 bool read(T &result) override {
75 T data = 0;
76 return readArray(&data, 1)==1;
77 }
78
79 // reads multiple values
80 int readArray(T data[], int len) {
81 if (xStreamBuffer == nullptr || data == nullptr || len == 0) return 0;
82
83 if (read_from_isr) {
85 int result = xStreamBufferReceiveFromISR(xStreamBuffer, (void *)data,
86 sizeof(T) * len,
88#ifdef ESP32X
90#else
92#endif
93 return result / sizeof(T);
94 } else {
95 return xStreamBufferReceive(xStreamBuffer, (void *)data, sizeof(T) * len,
96 readWait) / sizeof(T);
97 }
98 }
99
100 int writeArray(const T data[], int len) {
101 LOGD("%s: %d", LOG_METHOD, len);
102 if (xStreamBuffer == nullptr || data == nullptr || len == 0) return 0;
103
104 if (write_from_isr) {
106 int result =
107 xStreamBufferSendFromISR(xStreamBuffer, (void *)data, sizeof(T) * len,
109#ifdef ESP32X
111#else
113#endif
114 return result / sizeof(T);
115 } else {
116 return xStreamBufferSend(xStreamBuffer, (void *)data, sizeof(T) * len,
117 writeWait) / sizeof(T);
118 }
119 }
120
121 // peeks the actual entry from the buffer
122 bool peek(T &result) override {
123 LOGE("peek not implemented");
124 return false;
125 }
126
127 // checks if the buffer is full
128 bool isFull() override {
129 if (xStreamBuffer == nullptr) return true;
131 }
132
133 bool isEmpty() {
134 if (xStreamBuffer == nullptr) return true;
136 }
137
138 // write add an entry to the buffer
139 bool write(T data) override {
140 int len = sizeof(T);
141 return writeArray(&data, len) == len;
142 }
143
144 // clears the buffer
145 void reset() override {
146 if (xStreamBuffer == nullptr) return;
148 }
149
150 // provides the number of entries that are available to read
151 int available() override {
152 if (xStreamBuffer == nullptr) return 0;
154 }
155
156 // provides the number of entries that are available to write
157 int availableForWrite() override {
158 if (xStreamBuffer == nullptr) return 0;
160 }
161
162 // returns the address of the start of the physical read buffer
163 T *address() override {
164 LOGE("address() not implemented");
165 return nullptr;
166 }
167
168 size_t size() { return current_size_bytes / sizeof(T); }
169
170 operator bool() { return xStreamBuffer != nullptr && size()>0;}
171
172 protected:
175 uint8_t *p_data = nullptr;
177 BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
180 bool read_from_isr = false;
181 bool write_from_isr = false;
183 size_t trigger_level = 0;
184
187 bool setup() {
188 if (current_size_bytes == 0) return true;
189
190 // allocate data if necessary
191 int size = (current_size_bytes + 1) * sizeof(T);
192 if (p_data == nullptr) {
194 // check allocation
195 if (p_data == nullptr) {
196 LOGE("allocate falied for %d bytes", size)
197 return false;
198 }
199 }
200
201
202 // create stream buffer if necessary
203 if (xStreamBuffer == nullptr) {
206 }
207 if (xStreamBuffer == nullptr) {
208 LOGE("xStreamBufferCreateStatic failed");
209 return false;
210 }
211 // make sure that the data is empty
212 reset();
213 return true;
214 }
215
217 void end() {
219 if (p_data != nullptr) p_allocator->free(p_data);
221 p_data = nullptr;
222 xStreamBuffer = nullptr;
223 }
224};
225// #endif // ESP_IDF_VERSION_MAJOR >= 4
226
229template <class T>
231
232} // namespace audio_tools
233
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define LOG_METHOD
Definition AudioToolsConfig.h:74
#define portMAX_DELAY
Definition QueueZephyr.h:14
#define pdFALSE
Definition QueueZephyr.h:23
int BaseType_t
Definition QueueZephyr.h:12
#define pdTRUE
Definition QueueZephyr.h:20
uint32_t TickType_t
Definition QueueZephyr.h:11
Memory allocateator which uses malloc.
Definition Allocator.h:24
virtual void free(void *memory)
frees memory
Definition Allocator.h:83
virtual void * allocate(size_t size)
Allocates memory.
Definition Allocator.h:71
Shared functionality of all buffers.
Definition Buffers.h:23
Buffer implementation which is using a FreeRTOS StreamBuffer. The default allocator uses psram is ava...
Definition BufferRTOS.h:33
size_t size()
Definition BufferRTOS.h:168
StaticStreamBuffer_t static_stream_buffer
Definition BufferRTOS.h:174
int writeWait
Definition BufferRTOS.h:179
size_t current_size_bytes
Definition BufferRTOS.h:182
Allocator * p_allocator
Definition BufferRTOS.h:176
void setReadFromISR(bool active)
Definition BufferRTOS.h:71
bool peek(T &result) override
peeks the actual entry from the buffer
Definition BufferRTOS.h:122
bool read(T &result) override
reads a single value
Definition BufferRTOS.h:74
void setWriteMaxWait(TickType_t ticks)
Definition BufferRTOS.h:67
bool write(T data) override
write add an entry to the buffer
Definition BufferRTOS.h:139
int available() override
provides the number of entries that are available to read
Definition BufferRTOS.h:151
StreamBufferHandle_t xStreamBuffer
Definition BufferRTOS.h:173
uint8_t * p_data
Definition BufferRTOS.h:175
int availableForWrite() override
provides the number of entries that are available to write
Definition BufferRTOS.h:157
T * address() override
returns the address of the start of the physical read buffer
Definition BufferRTOS.h:163
bool isFull() override
checks if the buffer is full
Definition BufferRTOS.h:128
int readWait
Definition BufferRTOS.h:178
BaseType_t xHigherPriorityTaskWoken
Definition BufferRTOS.h:177
bool read_from_isr
Definition BufferRTOS.h:180
size_t trigger_level
Definition BufferRTOS.h:183
void setReadMaxWait(TickType_t ticks)
Definition BufferRTOS.h:65
void end()
Release resurces: call resize to restart again.
Definition BufferRTOS.h:217
int writeArray(const T data[], int len)
Fills the buffer data.
Definition BufferRTOS.h:100
void setWriteFromISR(bool active)
Definition BufferRTOS.h:69
~BufferRTOS()
Definition BufferRTOS.h:51
bool resize(size_t size)
Re-Allocats the memory and the queue.
Definition BufferRTOS.h:54
void reset() override
clears the buffer
Definition BufferRTOS.h:145
bool isEmpty()
Definition BufferRTOS.h:133
bool setup()
Definition BufferRTOS.h:187
int readArray(T data[], int len)
reads multiple values
Definition BufferRTOS.h:80
bool write_from_isr
Definition BufferRTOS.h:181
BufferRTOS(size_t streamBufferSize, size_t xTriggerLevel=1, TickType_t writeMaxWait=portMAX_DELAY, TickType_t readMaxWait=portMAX_DELAY, Allocator &allocator=DefaultAllocator)
Definition BufferRTOS.h:35
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
static TAllocatorExt DefaultAllocator
Definition Allocator.h:207
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508