arduino-audio-tools
Loading...
Searching...
No Matches
BufferBlocking.h
Go to the documentation of this file.
1#pragma once
2#include "AudioToolsConfig.h"
8
9#ifndef AUDIO_TOOLS_RTOS_TICK_TYPES_DEFINED
10#define AUDIO_TOOLS_RTOS_TICK_TYPES_DEFINED
11using TickType_t = uint32_t;
12using BaseType_t = int;
13#ifndef portMAX_DELAY
14#define portMAX_DELAY UINT32_MAX
15#endif
16#ifndef pdMS_TO_TICKS
17#define pdMS_TO_TICKS(ms) (ms)
18#endif
19#ifndef pdTRUE
20#define pdTRUE 1
21#endif
22#ifndef pdFALSE
23#define pdFALSE 0
24#endif
25#endif
26
27namespace audio_tools {
28
44template <typename T>
45class BufferBlocking : public BaseBuffer<T> {
46 public:
47 BufferBlocking(size_t streamBufferSize, size_t xTriggerLevel = 1,
48 TickType_t writeMaxWait = portMAX_DELAY,
49 TickType_t readMaxWait = portMAX_DELAY,
50 Allocator &allocator = DefaultAllocator)
51 : buffer(streamBufferSize, allocator) {
52 (void)xTriggerLevel; // no coalescing support: every write is visible immediately
53 readWait = readMaxWait;
54 writeWait = writeMaxWait;
55 }
56
58 bool resize(size_t size) {
59 LockGuard guard(mutex);
60 return buffer.resize(size);
61 }
62
63 void setReadMaxWait(TickType_t ticks) { readWait = ticks; }
64
65 void setWriteMaxWait(TickType_t ticks) { writeWait = ticks; }
66
67 // no ISR context on the supported platforms: kept for BufferRTOS API parity
68 void setWriteFromISR(bool active) {}
69 void setReadFromISR(bool active) {}
70
71 // reads a single value
72 bool read(T &result) override { return readArray(&result, 1) == 1; }
73
74 // reads multiple values, blocking up to readWait ms until data is available
75 int readArray(T data[], int len) {
76 if (data == nullptr || len <= 0) return 0;
77 uint32_t start = millis();
78 int result = 0;
79 while (result == 0) {
80 {
81 LockGuard guard(mutex);
82 result = buffer.readArray(data, len);
83 }
84 if (result > 0 || !isTimeLeft(start, readWait)) break;
85 delay(1);
86 }
87 return result;
88 }
89
90 // writes multiple values, blocking up to writeWait ms until space is available
91 int writeArray(const T data[], int len) {
92 if (data == nullptr || len <= 0) return 0;
93 uint32_t start = millis();
94 int result = 0;
95 while (result == 0) {
96 {
97 LockGuard guard(mutex);
98 result = buffer.writeArray(data, len);
99 }
100 if (result > 0 || !isTimeLeft(start, writeWait)) break;
101 delay(1);
102 }
103 return result;
104 }
105
106 // peeks the actual entry from the buffer
107 bool peek(T &result) override {
108 LockGuard guard(mutex);
109 return buffer.peek(result);
110 }
111
112 // checks if the buffer is full
113 bool isFull() override {
114 LockGuard guard(mutex);
115 return buffer.isFull();
116 }
117
118 bool isEmpty() {
119 LockGuard guard(mutex);
120 return buffer.isEmpty();
121 }
122
123 // write add an entry to the buffer
124 bool write(T data) override { return writeArray(&data, 1) == 1; }
125
126 // clears the buffer
127 void reset() override {
128 LockGuard guard(mutex);
129 buffer.reset();
130 }
131
132 // provides the number of entries that are available to read
133 int available() override {
134 LockGuard guard(mutex);
135 return buffer.available();
136 }
137
138 // provides the number of entries that are available to write
139 int availableForWrite() override {
140 LockGuard guard(mutex);
141 return buffer.availableForWrite();
142 }
143
144 // returns the address of the start of the physical read buffer
145 T *address() override {
146 LOGE("address() not implemented");
147 return nullptr;
148 }
149
150 size_t size() { return buffer.size(); }
151
152 operator bool() { return size() > 0; }
153
154 protected:
159
160 bool isTimeLeft(uint32_t start, TickType_t maxWait) {
161 if (maxWait == portMAX_DELAY) return true;
162 return millis() - start < maxWait;
163 }
164};
165
169template <class T>
171
174template <class T>
176
179template <class T>
181
182} // namespace audio_tools
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define portMAX_DELAY
Definition BufferBlocking.h:14
int BaseType_t
Definition BufferBlocking.h:12
uint32_t TickType_t
Definition BufferBlocking.h:11
Memory allocateator which uses malloc.
Definition Allocator.h:25
Shared functionality of all buffers.
Definition Buffers.h:23
Buffer implementation which provides the same blocking, thread-safe semantics as BufferRTOS (ESP32/ST...
Definition BufferBlocking.h:45
size_t size()
Definition BufferBlocking.h:150
SpinLock mutex
Definition BufferBlocking.h:156
void setReadFromISR(bool active)
Definition BufferBlocking.h:69
bool peek(T &result) override
peeks the actual entry from the buffer
Definition BufferBlocking.h:107
bool read(T &result) override
reads a single value
Definition BufferBlocking.h:72
void setWriteMaxWait(TickType_t ticks)
Definition BufferBlocking.h:65
bool write(T data) override
write add an entry to the buffer
Definition BufferBlocking.h:124
int available() override
provides the number of entries that are available to read
Definition BufferBlocking.h:133
bool isTimeLeft(uint32_t start, TickType_t maxWait)
Definition BufferBlocking.h:160
TickType_t writeWait
Definition BufferBlocking.h:158
int availableForWrite() override
provides the number of entries that are available to write
Definition BufferBlocking.h:139
T * address() override
returns the address of the start of the physical read buffer
Definition BufferBlocking.h:145
TickType_t readWait
Definition BufferBlocking.h:157
bool isFull() override
checks if the buffer is full
Definition BufferBlocking.h:113
void setReadMaxWait(TickType_t ticks)
Definition BufferBlocking.h:63
int writeArray(const T data[], int len)
Fills the buffer data.
Definition BufferBlocking.h:91
BufferBlocking(size_t streamBufferSize, size_t xTriggerLevel=1, TickType_t writeMaxWait=UINT32_MAX, TickType_t readMaxWait=UINT32_MAX, Allocator &allocator=DefaultAllocator)
Definition BufferBlocking.h:47
void setWriteFromISR(bool active)
Definition BufferBlocking.h:68
bool resize(size_t size)
Re-Allocates the memory of the ring buffer.
Definition BufferBlocking.h:58
void reset() override
clears the buffer
Definition BufferBlocking.h:127
bool isEmpty()
Definition BufferBlocking.h:118
int readArray(T data[], int len)
reads multiple values
Definition BufferBlocking.h:75
RingBuffer< T > buffer
Definition BufferBlocking.h:155
Buffer implementation which is using a FreeRTOS StreamBuffer. The default allocator uses psram is ava...
Definition BufferRTOS.h:33
RAII implementaion using a Mutex: Only a few microcontrollers provide lock guards,...
Definition LockGuard.h:17
Implements a typed Ringbuffer.
Definition Buffers.h:363
virtual int readArray(T data[], int len) override
reads multiple values
Definition Buffers.h:412
virtual int writeArray(const T data[], int len) override
Fills the buffer data.
Definition Buffers.h:432
bool peek(T &result) override
peeks the actual entry from the buffer
Definition Buffers.h:383
virtual int availableForWrite() override
provides the number of entries that are available to write
Definition Buffers.h:478
virtual size_t size() override
Returns the maximum capacity of the buffer.
Definition Buffers.h:493
virtual void reset() override
clears the buffer
Definition Buffers.h:468
virtual bool isFull() override
checks if the buffer is full
Definition Buffers.h:451
virtual bool resize(size_t len)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:483
bool isEmpty()
Definition Buffers.h:453
virtual int available() override
provides the number of entries that are available to read
Definition Buffers.h:475
Busy-wait lock based on std::atomic - available on all platforms that support <atomic>.
Definition Mutex.h:28
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
static TAllocatorExt DefaultAllocator
Definition Allocator.h:208
void delay(uint32_t ms)
Definition Arduino.h:259
uint32_t millis()
Returns the milliseconds since the start.
Definition Arduino.h:260