BackgroundAudio 1.3.3
Loading...
Searching...
No Matches
BackgroundAudioBuffers.h
1/*
2 BackgroundAudio
3 Plays an audio file using IRQ driven decompression. Main loop() writes
4 data to the buffer but isn't blocked while playing
5
6 Copyright (c) 2024 Earle F. Philhower, III <earlephilhower@yahoo.com>
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#pragma once
23#include <Arduino.h>
24
30template <size_t bytes>
32public:
34 _len = 0;
35#if defined(ESP32)
36 _mtx = portMUX_INITIALIZER_UNLOCKED;
37#else
38 mutex_init(&_mtx);
39#endif
40 }
41
43 /* no op */
44 }
45
51 inline uint8_t *buffer() {
52 return _buff;
53 }
54
60 inline size_t available() {
61 return _len;
62 }
63
69 inline size_t availableForWrite() {
70 return count - _len;
71 }
72
78 inline constexpr size_t size() {
79 return count;
80 }
81
90 inline size_t write(const uint8_t *data, size_t cnt) {
91#if defined(ESP32)
92 taskENTER_CRITICAL(&_mtx);
93#else
94 noInterrupts();
95 mutex_enter_blocking(&_mtx);
96#endif
97 size_t maxWritable = count - _len;
98 size_t toWrite = std::min(cnt, maxWritable);
99 memcpy(_buff + _len, data, toWrite);
100 _len += toWrite;
101#if defined(ESP32)
102 taskEXIT_CRITICAL(&_mtx);
103#else
104 mutex_exit(&_mtx);
105 interrupts();
106#endif
107 return toWrite;
108 }
116 inline size_t write0(size_t cnt) {
117#if defined(ESP32)
118 taskENTER_CRITICAL(&_mtx);
119#else
120 noInterrupts();
121 mutex_enter_blocking(&_mtx);
122#endif
123 size_t maxWritable = count - _len;
124 size_t toWrite = std::min(cnt, maxWritable);
125 bzero(_buff + _len, toWrite);
126 _len += toWrite;
127#if defined(ESP32)
128 taskEXIT_CRITICAL(&_mtx);
129#else
130 mutex_exit(&_mtx);
131 interrupts();
132#endif
133 return toWrite;
134 }
135
148 inline void shiftUp(size_t cnt) {
149#if defined(ESP32)
150 taskENTER_CRITICAL(&_mtx);
151#else
152 noInterrupts();
153 mutex_enter_blocking(&_mtx);
154#endif
155 if (cnt <= _len) {
156 size_t toShift = _len - cnt;
157 memmove(_buff, _buff + cnt, toShift);
158 _len -= cnt;
159 } else {
160 _len = 0;
161 }
162#if defined(ESP32)
163 taskEXIT_CRITICAL(&_mtx);
164#else
165 mutex_exit(&_mtx);
166 interrupts();
167#endif
168 }
169
173 inline void flush() {
174 _len = 0;
175 }
176
177private:
178 static const size_t count = bytes;
179 uint8_t _buff[count];
180 size_t _len;
181#if defined(ESP32)
182 portMUX_TYPE _mtx;
183#else
184 mutex_t _mtx;
185#endif
186};
187
188
193public:
194 ROMDataBuffer() {
195 _buff = nullptr;
196 _len = 0;
197 _count = 0;
198#if defined(ESP32)
199 _mtx = portMUX_INITIALIZER_UNLOCKED;
200#else
201 mutex_init(&_mtx);
202#endif
203 }
204
206 /* no op */
207 }
208
214 inline const uint8_t *buffer() {
215 return _buff;
216 }
217
223 inline size_t available() {
224 return _len;
225 }
226
232 inline size_t availableForWrite() {
233 return 0;
234 }
235
241 inline constexpr size_t size() {
242 return _count;
243 }
244
259 inline size_t write(const uint8_t *data, size_t cnt) {
260 _buff = data;
261 _len = cnt;
262 _count = cnt;
263 return cnt;
264 }
265
273 inline size_t write0(size_t cnt) {
274 return 0;
275 }
276
277
292 inline void shiftUp(size_t cnt) {
293#if defined(ESP32)
294 taskENTER_CRITICAL(&_mtx);
295#else
296 noInterrupts();
297 mutex_enter_blocking(&_mtx);
298#endif
299 if (cnt <= _len) {
300 _buff += cnt;
301 _len -= cnt;
302 } else {
303 _len = 0;
304 }
305#if defined(ESP32)
306 taskEXIT_CRITICAL(&_mtx);
307#else
308 mutex_exit(&_mtx);
309 interrupts();
310#endif
311 }
312
316 inline void flush() {
317 _len = 0;
318 }
319
320private:
321 const uint8_t *_buff;
322 size_t _len;
323 size_t _count;
324#if defined(ESP32)
325 portMUX_TYPE _mtx;
326#else
327 mutex_t _mtx;
328#endif
329};
Special-purpose buffer which never shifts memory and only allows a single written block of data,...
Definition BackgroundAudioBuffers.h:192
void shiftUp(size_t cnt)
Invalidate a portion of buffer and shift remaining data up.
Definition BackgroundAudioBuffers.h:292
size_t availableForWrite()
Determine how much unused space is available in the buffer.
Definition BackgroundAudioBuffers.h:232
size_t write0(size_t cnt)
0-fill a portion of the buffer, but will fail because there is no buffer here
Definition BackgroundAudioBuffers.h:273
constexpr size_t size()
Get total size of the statically allocated buffer.
Definition BackgroundAudioBuffers.h:241
void flush()
Throw away (flush) the input buffer.
Definition BackgroundAudioBuffers.h:316
size_t write(const uint8_t *data, size_t cnt)
Copy a pointer to a block of memory into the buffer, replacing any existing data.
Definition BackgroundAudioBuffers.h:259
const uint8_t * buffer()
Get access to internal buffer pointer (avoiding memcpy)
Definition BackgroundAudioBuffers.h:214
size_t available()
Determine number of bytes that can be read.
Definition BackgroundAudioBuffers.h:223
Interrupt-safe, multicore-safe biftable buffer for libmad raw data.
Definition BackgroundAudioBuffers.h:31
uint8_t * buffer()
Get access to internal buffer pointer (avoiding memcpy)
Definition BackgroundAudioBuffers.h:51
void flush()
Throw out any data in the buffer.
Definition BackgroundAudioBuffers.h:173
constexpr size_t size()
Get total size of the statically allocated buffer.
Definition BackgroundAudioBuffers.h:78
size_t write(const uint8_t *data, size_t cnt)
Copy a block of memory into the buffer. Will not block.
Definition BackgroundAudioBuffers.h:90
void shiftUp(size_t cnt)
Invalidate a portion of buffer and shift remaining data up.
Definition BackgroundAudioBuffers.h:148
size_t availableForWrite()
Determine how much unused space is available in the buffer.
Definition BackgroundAudioBuffers.h:69
size_t write0(size_t cnt)
0-fill a portion of the buffer
Definition BackgroundAudioBuffers.h:116
size_t available()
Determine number of bytes that can be read.
Definition BackgroundAudioBuffers.h:60