-
Notifications
You must be signed in to change notification settings - Fork 439
/
AudioFileSourceBuffer.cpp
206 lines (175 loc) · 5.22 KB
/
AudioFileSourceBuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
AudioFileSourceBuffer
Double-buffered file source using system RAM
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "AudioFileSourceBuffer.h"
#pragma GCC optimize ("O3")
AudioFileSourceBuffer::AudioFileSourceBuffer(AudioFileSource *source, uint32_t buffSizeBytes)
{
buffSize = buffSizeBytes;
buffer = (uint8_t*)malloc(sizeof(uint8_t) * buffSize);
if (!buffer) Serial.printf_P(PSTR("Unable to allocate AudioFileSourceBuffer::buffer[]\n"));
deallocateBuffer = true;
writePtr = 0;
readPtr = 0;
src = source;
length = 0;
filled = false;
}
AudioFileSourceBuffer::AudioFileSourceBuffer(AudioFileSource *source, void *inBuff, uint32_t buffSizeBytes)
{
buffSize = buffSizeBytes;
buffer = (uint8_t*)inBuff;
deallocateBuffer = false;
writePtr = 0;
readPtr = 0;
src = source;
length = 0;
filled = false;
}
AudioFileSourceBuffer::~AudioFileSourceBuffer()
{
if (deallocateBuffer) free(buffer);
buffer = NULL;
}
bool AudioFileSourceBuffer::seek(int32_t pos, int dir)
{
// Invalidate
readPtr = 0;
writePtr = 0;
length = 0;
return src->seek(pos, dir);
}
bool AudioFileSourceBuffer::close()
{
if (deallocateBuffer) free(buffer);
buffer = NULL;
return src->close();
}
bool AudioFileSourceBuffer::isOpen()
{
return src->isOpen();
}
uint32_t AudioFileSourceBuffer::getSize()
{
return src->getSize();
}
uint32_t AudioFileSourceBuffer::getPos()
{
return src->getPos();
}
uint32_t AudioFileSourceBuffer::getFillLevel()
{
return length;
}
uint32_t AudioFileSourceBuffer::read(void *data, uint32_t len)
{
if (!buffer) return src->read(data, len);
uint32_t bytes = 0;
if (!filled) {
// Fill up completely before returning any data at all
cb.st(STATUS_FILLING, PSTR("Refilling buffer"));
length = src->read(buffer, buffSize);
writePtr = length % buffSize;
filled = true;
}
// Pull from buffer until we've got none left or we've satisfied the request
uint8_t *ptr = reinterpret_cast<uint8_t*>(data);
uint32_t toReadFromBuffer = (len < length) ? len : length;
if ( (toReadFromBuffer > 0) && (readPtr >= writePtr) ) {
uint32_t toReadToEnd = (toReadFromBuffer < (uint32_t)(buffSize - readPtr)) ? toReadFromBuffer : (buffSize - readPtr);
memcpy(ptr, &buffer[readPtr], toReadToEnd);
readPtr = (readPtr + toReadToEnd) % buffSize;
len -= toReadToEnd;
length -= toReadToEnd;
ptr += toReadToEnd;
bytes += toReadToEnd;
toReadFromBuffer -= toReadToEnd;
}
if (toReadFromBuffer > 0) { // We know RP < WP at this point
memcpy(ptr, &buffer[readPtr], toReadFromBuffer);
readPtr = (readPtr + toReadFromBuffer) % buffSize;
len -= toReadFromBuffer;
length -= toReadFromBuffer;
ptr += toReadFromBuffer;
bytes += toReadFromBuffer;
toReadFromBuffer -= toReadFromBuffer;
}
if (len) {
// Still need more, try direct read from src
bytes += src->read(ptr, len);
// We're out of buffered data, need to force a complete refill. Thanks, @armSeb
readPtr = 0;
writePtr = 0;
length = 0;
filled = false;
cb.st(STATUS_UNDERFLOW, PSTR("Buffer underflow"));
}
fill();
return bytes;
}
void AudioFileSourceBuffer::fill()
{
if (!buffer) return;
if (length < buffSize) {
// Now try and opportunistically fill the buffer
if (readPtr > writePtr) {
if (readPtr == writePtr+1) return;
uint32_t bytesAvailMid = readPtr - writePtr - 1;
int cnt = src->readNonBlock(&buffer[writePtr], bytesAvailMid);
length += cnt;
writePtr = (writePtr + cnt) % buffSize;
return;
}
if (buffSize > writePtr) {
uint32_t bytesAvailEnd = buffSize - writePtr;
int cnt = src->readNonBlock(&buffer[writePtr], bytesAvailEnd);
length += cnt;
writePtr = (writePtr + cnt) % buffSize;
if (cnt != bytesAvailEnd) return;
}
if (readPtr > 1) {
uint32_t bytesAvailStart = readPtr - 1;
int cnt = src->readNonBlock(&buffer[writePtr], bytesAvailStart);
length += cnt;
writePtr = (writePtr + cnt) % buffSize;
}
}
}
bool AudioFileSourceBuffer::fill(uint32_t len, uint32_t max_delay)
{
if (!src->isOpen()) {
Serial.printf_P(PSTR("Source file not open\n"));
return false;
}
if (!src->loop()) return false;
fill();
uint32_t toFill = (len < buffSize) ? len : buffSize;
uint32_t startTimestamp = millis();
while (length < toFill && millis() - startTimestamp < max_delay) {
delay(100);
if (!src->loop()) return false;
fill();
}
filled = true;
if (length < len) return false;
return true;
}
bool AudioFileSourceBuffer::loop()
{
if (!src->loop()) return false;
fill();
return true;
}