arduino-audio-tools
Loading...
Searching...
No Matches
BufferRP2040.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef ARDUINO_ARCH_RP2040
4# error "Unsupported architecture"
5#endif
6
7#include "AudioToolsConfig.h"
10
11namespace audio_tools {
12
13// #if ESP_IDF_VERSION_MAJOR >= 4
14
32template <typename T>
33class BufferRP2040T : public BaseBuffer<T> {
34 public:
35 BufferRP2040T(int bufferCount) : BaseBuffer<T>() {
36 buffer_size = 1;
37 buffer_size_bytes = sizeof(T);
39 }
40
41 BufferRP2040T(size_t bufferSize, int bufferCount) : BaseBuffer<T>() {
42 buffer_size = bufferSize;
43 buffer_size_bytes = bufferSize * sizeof(T);
45 }
46
48
50 bool resize(size_t size) {
51 int req_bytes = size * sizeof(T);
53 LOGI("resize %d -> %d", buffer_size_alloc_bytes / sizeof(T), size);
57 // create new queu
59 // release existing queue
62 }
63
64 int count = req_bytes / buffer_size_bytes;
65 LOGI("queue_init(size:%d, count:%d)", buffer_size_bytes, count);
68 }
69 }
70 return true;
71 }
72
73 // reads a single value
74 bool read(T& data) {
75 return readArray(&data, 1)==1;
76 }
77
78
79 // peeks the actual entry from the buffer
80 bool peek(T &result) override {
81 LOGE("peek not implemented");
82 return false;
83 }
84
85 // reads multiple values
86 int readArray(T data[], int len) override {
87 LOGD("readArray: %d", len);
88 // handle unalloc;ated queue
89 if (buffer_size_alloc_bytes == 0) return 0;
90
91 // blocking read
92 while (is_blocking_read && read_buffer.available() + available() < len)
93 delay(1);
94
95 // fill read buffer if necessary
97 LOGD("reading %d %d ", buffer_size, read_buffer.availableForWrite());
98 T tmp[buffer_size];
99 if (queue_try_remove(&queue, tmp)){
100 LOGD("queue_try_remove -> success");
102 } else {
103 LOGD("queue_try_remove -> failed");
104 break;
105 }
106 }
107 LOGD("read_buffer.available: %d, availableForWrite: %d ", read_buffer.available(), read_buffer.availableForWrite());
108 int result = read_buffer.readArray(data, len);
109 LOGD("=> readArray: %d -> %d", len, result);
110 return result;
111 }
112
113 int writeArray(const T data[], int len) override {
114 LOGD("writeArray: %d", len);
115 int result = 0;
116 // make sure that we have the data allocated
117 resize(buffer_size_req_bytes / sizeof(T));
118
119 if (is_blocking_write) {
120 result = writeBlocking(data, len);
121 } else {
122 result = writeNonBlocking(data, len);
123 }
124
125 return result;
126 }
127
128 // checks if the buffer is full
129 bool isFull() override {
130 if (buffer_size_alloc_bytes == 0) return false;
131 return queue_is_full(&queue);
132 }
133
134 bool isEmpty() {
135 if (buffer_size_alloc_bytes == 0) return true;
136 return queue_is_empty(&queue);
137 }
138
139 // write add an entry to the buffer
140 bool write(T data) override { return writeArray(&data, 1) == 1; }
141
142 // clears the buffer
143 void reset() override {
146 }
147
148 // provides the number of entries that are available to read
149 int available() override {
150 if (buffer_size_alloc_bytes == 0) return 0;
151 return (queue_get_level(&queue) * buffer_size);
152 }
153
154 // provides the number of entries that are available to write
155 int availableForWrite() override {
156 if (buffer_size_alloc_bytes == 0) return size();
157 return size() - available(); }
158
159 // returns the address of the start of the physical read buffer
160 T *address() override {
161 LOGE("address() not implemented");
162 return nullptr;
163 }
164
165 size_t size() { return buffer_size_alloc_bytes / sizeof(T); }
166
170 }
171
175 }
176
177
178 protected:
183 int buffer_size = 0;
186 bool is_blocking_write = true;
187 bool is_blocking_read = false;
188
189 int writeBlocking(const T data[], int len) {
190 LOGD("writeArray: %d", len);
191
192 if (len > buffer_size_bytes){
193 LOGE("write %d too big for buffer_size: %d", len, buffer_size_bytes);
194 return 0;
195 }
196
197 // fill the write buffer and when it is full flush it to the queue
198 for (int j = 0; j < len; j++) {
199 write_buffer.write(data[j]);
200 if (write_buffer.isFull()) {
201 LOGD("queue_add_blocking");
204 }
205 }
206 return len;
207 }
208
209 int writeNonBlocking(const T data[], int len) {
210 if (len != buffer_size_bytes){
211 LOGE("write %d must be buffer_size: %d", len, buffer_size_bytes);
212 return 0;
213 }
214
216 return len;
217 }
218 return 0;
219 }
220
221};
222
226
227} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define assert(T)
Definition avr.h:10
Shared functionality of all buffers.
Definition Buffers.h:23
virtual int readArray(T data[], int len)
reads multiple values
Definition Buffers.h:34
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:56
Buffer implementation which is based on a RP2040 queue. This class is intended to be used to exchange...
Definition BufferRP2040.h:33
size_t size()
Definition BufferRP2040.h:165
BufferRP2040T(int bufferCount)
Definition BufferRP2040.h:35
void setBlockingRead(bool flag)
When we use a blockingread, the we wait for the data to be available.
Definition BufferRP2040.h:173
void setBlockingWrite(bool flag)
When we use a non blocking write, the write size must be identical with the buffer size.
Definition BufferRP2040.h:168
queue_t queue
Definition BufferRP2040.h:179
~BufferRP2040T()
Definition BufferRP2040.h:47
bool is_blocking_read
Definition BufferRP2040.h:187
bool peek(T &result) override
peeks the actual entry from the buffer
Definition BufferRP2040.h:80
bool write(T data) override
write add an entry to the buffer
Definition BufferRP2040.h:140
int available() override
provides the number of entries that are available to read
Definition BufferRP2040.h:149
bool read(T &data)
reads a single value
Definition BufferRP2040.h:74
int buffer_size_bytes
Definition BufferRP2040.h:182
int writeBlocking(const T data[], int len)
Definition BufferRP2040.h:189
int availableForWrite() override
provides the number of entries that are available to write
Definition BufferRP2040.h:155
T * address() override
returns the address of the start of the physical read buffer
Definition BufferRP2040.h:160
bool isFull() override
checks if the buffer is full
Definition BufferRP2040.h:129
int writeNonBlocking(const T data[], int len)
Definition BufferRP2040.h:209
bool is_blocking_write
Definition BufferRP2040.h:186
int writeArray(const T data[], int len) override
Fills the buffer data.
Definition BufferRP2040.h:113
BufferRP2040T(size_t bufferSize, int bufferCount)
Definition BufferRP2040.h:41
bool resize(size_t size)
Re-Allocats the memory and the queue (size is in entries)
Definition BufferRP2040.h:50
void reset() override
clears the buffer
Definition BufferRP2040.h:143
int readArray(T data[], int len) override
reads multiple values
Definition BufferRP2040.h:86
int buffer_size
Definition BufferRP2040.h:183
bool isEmpty()
Definition BufferRP2040.h:134
SingleBuffer< T > write_buffer
Definition BufferRP2040.h:184
int buffer_size_req_bytes
Definition BufferRP2040.h:181
int buffer_size_alloc_bytes
Definition BufferRP2040.h:180
audio_tools::RingBuffer< T > read_buffer
Definition BufferRP2040.h:185
Implements a typed Ringbuffer.
Definition Buffers.h:353
virtual int availableForWrite() override
provides the number of entries that are available to write
Definition Buffers.h:425
virtual bool resize(size_t len)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:430
virtual int available() override
provides the number of entries that are available to read
Definition Buffers.h:422
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition Buffers.h:184
bool write(T sample) override
write add an entry to the buffer
Definition Buffers.h:218
bool isFull() override
checks if the buffer is full
Definition Buffers.h:252
T * data()
Provides address of actual data.
Definition Buffers.h:296
bool resize(size_t size)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:317
void reset() override
clears the buffer
Definition Buffers.h:298
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(uint32_t ms)
Definition Arduino.h:255
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508