-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
AudioGeneratorWAVStatic.cpp
297 lines (256 loc) · 8.99 KB
/
AudioGeneratorWAVStatic.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
AudioGeneratorWAVStatic
Audio output generator that reads 8 and 16-bit WAV files
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 "AudioGeneratorWAVStatic.h"
AudioGeneratorWAVStatic::~AudioGeneratorWAVStatic(){}
bool AudioGeneratorWAVStatic::stop()
{
if (!running) return true;
running = false;
output->stop();
return file->close();
}
bool AudioGeneratorWAVStatic::isRunning()
{
return running;
}
// Handle buffered reading, reload each time we run out of data
bool AudioGeneratorWAVStatic::GetBufferedData(int bytes, void *dest)
{
if (!running) return false; // Nothing to do here!
uint8_t *p = reinterpret_cast<uint8_t*>(dest);
while (bytes--) {
// Potentially load next batch of data...
if (buffPtr >= buffLen) {
buffPtr = 0;
uint32_t toRead = availBytes > BUFFER_SIZE ? BUFFER_SIZE : availBytes;
buffLen = file->read( buff, toRead );
availBytes -= buffLen;
}
if (buffPtr >= buffLen)
return false; // No data left!
*(p++) = buff[buffPtr++];
}
return true;
}
bool AudioGeneratorWAVStatic::loop()
{
if (!running) goto done; // Nothing to do here!
// First, try and push in the stored sample. If we can't, then punt and try later
if (!output->ConsumeSample(lastSample)) goto done; // Can't send, but no error detected
// Try and stuff the buffer one sample at a time
do
{
if (bitsPerSample == 8) {
uint8_t l, r;
if (!GetBufferedData(1, &l)) stop();
if (channels == 2) {
if (!GetBufferedData(1, &r)) stop();
} else {
r = 0;
}
lastSample[AudioOutput::LEFTCHANNEL] = l;
lastSample[AudioOutput::RIGHTCHANNEL] = r;
} else if (bitsPerSample == 16) {
if (!GetBufferedData(2, &lastSample[AudioOutput::LEFTCHANNEL])) stop();
if (channels == 2) {
if (!GetBufferedData(2, &lastSample[AudioOutput::RIGHTCHANNEL])) stop();
} else {
lastSample[AudioOutput::RIGHTCHANNEL] = 0;
}
}
} while (running && output->ConsumeSample(lastSample));
done:
file->loop();
output->loop();
return running;
}
bool AudioGeneratorWAVStatic::ReadWAVInfo()
{
uint32_t u32;
uint16_t u16;
int toSkip;
// WAV specification document:
// https://www.aelius.com/njh/wavemetatools/doc/riffmci.pdf
// Header == "RIFF"
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if (u32 != 0x46464952) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: cannot read WAV, invalid RIFF header, got: %08X \n"), (uint32_t) u32);
return false;
}
// Skip ChunkSize
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
// Format == "WAVE"
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if (u32 != 0x45564157) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: cannot read WAV, invalid WAVE header, got: %08X \n"), (uint32_t) u32);
return false;
}
// there might be JUNK or PAD - ignore it by continuing reading until we get to "fmt "
while (1) {
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if (u32 == 0x20746d66) break; // 'fmt '
};
// subchunk size
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if (u32 == 16) { toSkip = 0; }
else if (u32 == 18) { toSkip = 18 - 16; }
else if (u32 == 40) { toSkip = 40 - 16; }
else {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: cannot read WAV, appears not to be standard PCM \n"));
return false;
} // we only do standard PCM
// AudioFormat
if (!ReadU16(&u16)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if (u16 != 1) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: cannot read WAV, AudioFormat appears not to be standard PCM \n"));
return false;
} // we only do standard PCM
// NumChannels
if (!ReadU16(&channels)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if ((channels<1) || (channels>2)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: cannot read WAV, only mono and stereo are supported \n"));
return false;
} // Mono or stereo support only
// SampleRate
if (!ReadU32(&sampleRate)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if (sampleRate < 1) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: cannot read WAV, unknown sample rate \n"));
return false;
} // Weird rate, punt. Will need to check w/DAC to see if supported
// Ignore byterate and blockalign
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if (!ReadU16(&u16)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
// Bits per sample
if (!ReadU16(&bitsPerSample)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if ((bitsPerSample!=8) && (bitsPerSample != 16)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: cannot read WAV, only 8 or 16 bits is supported \n"));
return false;
} // Only 8 or 16 bits
// Skip any extra header
while (toSkip) {
uint8_t ign;
if (!ReadU8(&ign)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
toSkip--;
}
// look for data subchunk
do {
// id == "data"
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if (u32 == 0x61746164) break; // "data"
// Skip size, read until end of chunk
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
if(!file->seek(u32, SEEK_CUR)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data, seek failed\n"));
return false;
}
} while (1);
if (!file->isOpen()) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: cannot read WAV, file is not open\n"));
return false;
};
// Skip size, read until end of file...
if (!ReadU32(&u32)) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::ReadWAVInfo: failed to read WAV data\n"));
return false;
};
availBytes = u32;
buffPtr = 0;
buffLen = 0;
return true;
}
bool AudioGeneratorWAVStatic::begin(AudioFileSource *source, AudioOutput *output) {
running = false;
buffPtr = 0;
buffLen = 0;
if (!source) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::begin: failed: invalid source\n"));
return false;
}
file = source;
if (!output) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::begin: invalid output\n"));
return false;
}
this->output = output;
if (!file->isOpen()) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::begin: file not open\n"));
return false;
} // Error
if (!ReadWAVInfo()) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::begin: failed during ReadWAVInfo\n"));
return false;
}
if (!output->SetRate( sampleRate )) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::begin: failed to SetRate in output\n"));
return false;
}
if (!output->SetBitsPerSample( bitsPerSample )) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::begin: failed to SetBitsPerSample in output\n"));
return false;
}
if (!output->SetChannels( channels )) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::begin: failed to SetChannels in output\n"));
return false;
}
if (!output->begin()) {
audioLogger->printf_P(PSTR("AudioGeneratorWAVStatic::begin: output's begin did not return true\n"));
return false;
}
running = true;
return true;
}