3 #ifndef ARDUINO_ARCH_RP2040
4 # error "Unsupported architecture"
7 #include "AudioConfig.h"
8 #include "AudioTools/CoreAudio/AudioLogger.h"
9 #include "AudioTools/CoreAudio/Buffers.h"
29 buffer_size = bufferSize *
sizeof(T);
30 buffer_size_req_bytes = buffer_size * bufferCount;
37 buffer_size_req_bytes = size;
38 if (buffer_size_total_bytes != buffer_size_req_bytes) {
39 LOGI(
"resize %d -> %d", buffer_size_total_bytes, buffer_size_req_bytes);
40 assert(buffer_size > 0);
41 if (is_blocking_write){
42 write_buffer.resize(buffer_size);
43 read_buffer.resize(buffer_size * 2);
46 if (buffer_size_total_bytes > 0) {
50 if (buffer_size_req_bytes > 0) {
51 int count = buffer_size_req_bytes / buffer_size;
52 LOGI(
"queue_init(size:%d, count:%d)", buffer_size, count);
53 queue_init(&queue, buffer_size, count);
55 buffer_size_total_bytes = buffer_size_req_bytes;
69 LOGE(
"peek not implmented");
75 LOGD(
"readArray: %d", len);
76 if (!is_blocking_write && read_buffer.size()==0){
78 read_buffer.resize(len + buffer_size);
81 if (buffer_size_total_bytes == 0)
return 0;
82 if (isEmpty() && read_buffer.isEmpty())
return 0;
84 while (read_buffer.availableForWrite() >= buffer_size) {
85 LOGD(
"reading %d %d ", buffer_size, read_buffer.availableForWrite());
87 if (queue_try_remove(&queue, tmp)){
88 LOGD(
"queue_try_remove -> success");
89 read_buffer.writeArray(tmp, buffer_size);
91 LOGD(
"queue_try_remove -> failed");
95 LOGD(
"read_buffer.available: %d, availableForWrite: %d ", read_buffer.available(), read_buffer.availableForWrite());
96 int result = read_buffer.readArray(data, len);
97 LOGD(
"=> readArray: %d -> %d", len, result);
102 LOGD(
"writeArray: %d", len);
105 resize(buffer_size_req_bytes);
107 if (is_blocking_write) {
108 result = writeBlocking(data, len);
110 result = writeNonBlocking(data, len);
118 if (buffer_size_total_bytes == 0)
return false;
119 return queue_is_full(&queue);
123 if (buffer_size_total_bytes == 0)
return true;
124 return queue_is_empty(&queue);
133 buffer_size_total_bytes = 0;
138 return buffer_size /
sizeof(T);
146 LOGE(
"address() not implemented");
150 size_t size() {
return buffer_size_req_bytes /
sizeof(T); }
154 is_blocking_write = flag;
159 int buffer_size_total_bytes = 0;
160 int buffer_size_req_bytes = 0;
164 bool is_blocking_write =
true;
166 int writeBlocking(
const T data[],
int len) {
167 LOGD(
"writeArray: %d", len);
169 if (len > buffer_size){
170 LOGE(
"write %d too big for buffer_size: %d", len, buffer_size);
175 for (
int j = 0; j < len; j++) {
176 write_buffer.write(data[j]);
177 if (write_buffer.isFull()) {
178 LOGD(
"queue_add_blocking");
179 queue_add_blocking(&queue, write_buffer.data());
180 write_buffer.reset();
186 int writeNonBlocking(
const T data[],
int len) {
187 if (len != buffer_size){
188 LOGE(
"write %d must be buffer_size: %d", len, buffer_size);
192 if (queue_try_add(&queue, write_buffer.data())){
200 using BufferRP2040 = BufferRP2040T<uint8_t>;