-
Notifications
You must be signed in to change notification settings - Fork 8
/
WavFile.cpp
374 lines (325 loc) · 8.7 KB
/
WavFile.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* WavFile.cpp
*
* Class(es) for reading audio samples from WAV/RIFF files
*
* Copyright (C) 2018, Christopher Collins
*/
#include "WavFile.h"
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/*
* There is a HUGE assumption in here right now that everything is
* Little Endian - this is now true for X-Plane. It will need to be fixed
* if this ever changes.
*
* - CC.
*/
/** minimumBlockAlignment calculates the minimum permissible block size for the
* specified sampleSize & channel count.
*
* It does this obeying the rules established by Microsoft for multichannel
* audio data & WAVE file, which in short, it is required that individual
* samples are padded up to a full byte, with channels in sequential order.
*
* There's nothing to say that a larger block alignment can't be used, which
* results in with wasted space.
*
* If a file reports a block alignment smaller than this, it must be invalid.
*
* We will repack the audio down to this size on load as not to waste memory.
*
* @param sampleSize size of each sample in bits.
* @param numChannels total number of channels per sample
*/
inline int
minimumBlockAlignment(int sampleSize, int numChannels)
{
return ((sampleSize + 7) / 8) * numChannels;
}
AudioSampleData::AudioSampleData(int numChannels, int bitsPerSample, int sampleRate, bool isFloat) :
mNumChannels(numChannels),
mBitsPerSample(bitsPerSample),
mSampleRate(sampleRate),
mIsFloat(isFloat)
{
mSampleAlignment = minimumBlockAlignment(mBitsPerSample, mNumChannels);
mSampleCount = 0;
mSampleData = nullptr;
}
AudioSampleData::AudioSampleData(AudioSampleData &&move_src) :
mNumChannels(move_src.mNumChannels),
mBitsPerSample(move_src.mBitsPerSample),
mSampleAlignment(move_src.mSampleAlignment),
mSampleRate(move_src.mBitsPerSample)
{
mSampleData = move_src.mSampleData;
mSampleCount = move_src.mSampleCount;
move_src.mSampleData = nullptr;
move_src.mSampleCount = 0;
}
AudioSampleData::AudioSampleData(const AudioSampleData &cpy_src) :
mNumChannels(cpy_src.mNumChannels),
mBitsPerSample(cpy_src.mBitsPerSample),
mSampleAlignment(cpy_src.mSampleAlignment),
mSampleRate(cpy_src.mBitsPerSample)
{
mSampleCount = cpy_src.mSampleCount;
mSampleData = malloc(cpy_src.mSampleCount * cpy_src.mSampleAlignment);
if (mSampleData == nullptr) {
mSampleCount = 0;
} else {
memcpy(mSampleData, cpy_src.mSampleData, cpy_src.mSampleCount * cpy_src.mSampleAlignment);
}
}
AudioSampleData::~AudioSampleData()
{
if (mSampleData != nullptr) {
free(mSampleData);
mSampleData = nullptr;
mSampleCount = 0;
}
}
void
AudioSampleData::AppendSamples(uint8_t blockSize, unsigned count, void *data)
{
const unsigned newCount = mSampleCount + count;
mSampleData = realloc(mSampleData, newCount*mSampleAlignment);
if (mSampleData == nullptr) {
mSampleCount = 0;
return;
}
auto *dptr = reinterpret_cast<uint8_t *>(mSampleData);
dptr += mSampleCount * mSampleAlignment;
if (blockSize == mSampleAlignment) {
memcpy(dptr, data, count*blockSize);
} else {
auto *sptr = reinterpret_cast<uint8_t *>(data);
for (unsigned c = 0; c < count; c++) {
memcpy(dptr, sptr, mSampleAlignment);
sptr += blockSize;
dptr += mSampleAlignment;
}
}
mSampleCount = newCount;
}
/* ==== Getters ==== */
int8_t
AudioSampleData::getNumChannels() const
{
return mNumChannels;
}
int8_t
AudioSampleData::getBitsPerSample() const
{
return mBitsPerSample;
}
uint8_t
AudioSampleData::getSampleAlignment() const
{
return mSampleAlignment;
}
int
AudioSampleData::getSampleRate() const
{
return mSampleRate;
}
size_t
AudioSampleData::getSampleCount() const
{
return mSampleCount;
}
const void *
AudioSampleData::getSampleData() const
{
return mSampleData;
}
bool
AudioSampleData::isFloat() const
{
return mIsFloat;
}
/* ==== WAVE Loading Logic Below ==== */
static const char * WavTopChunkID = "RIFF";
static const char * WavFormatChunkID = "fmt ";
static const char * WavDataChunkID = "data";
#pragma pack(push,1)
struct ChunkHeader {
char chunkID[4];
uint32_t chunkSize;
};
/* names below from the Microsoft headers/documentation for consistency.
*/
struct WavFormatChunk {
uint16_t wFormatTag;
uint16_t nChannels;
uint32_t nSamplesPerSec;
uint32_t nAvgBytesPerSec;
uint16_t nBlockAlign;
uint16_t wBitsPerSample;
/* 18 byte body... */
uint16_t cbSize;
/* 40 byte body */
uint16_t wValidBitsPerSample;
uint32_t dwChannelMask;
char SubFormat[16];
};
#pragma pack(pop)
const uint16_t WAV_FORMAT_PCM = 1;
const uint16_t WAV_FORMAT_IEEE_FLOAT = 3;
typedef struct WavTOCEntry {
struct ChunkHeader ch;
uint32_t offset;
} WavTOCEntry;
static vector<WavTOCEntry>::const_iterator
FindTOCFor(const vector<WavTOCEntry> &toc, const char *chunk_id)
{
return std::find_if(toc.cbegin(), toc.cend(), [chunk_id](const WavTOCEntry &x) -> bool {return !memcmp(x.ch.chunkID, chunk_id, 4); });
}
static bool
readHeader(FILE *srcFile, const struct ChunkHeader &ch, vector<WavTOCEntry> &o_HeaderItems)
{
uint32_t expectedBytesLeft = ch.chunkSize - 4;
while (expectedBytesLeft > 0) {
/* if there's less than a chunkheader less, there's a problem. */
if (expectedBytesLeft < sizeof(struct ChunkHeader)) {
return false;
}
WavTOCEntry e;
if (1 != fread(&e.ch, sizeof(e.ch), 1, srcFile)) {
return false;
}
expectedBytesLeft -= sizeof(e.ch);
/* make sure the expected file size is large enough */
if (e.ch.chunkSize > expectedBytesLeft) {
return false;
}
e.offset = ftell(srcFile);
o_HeaderItems.emplace_back(e);
int pad = 0;
if ((e.ch.chunkSize % 2) != 0) {
pad = 1;
}
/* skip forward to the next chunk */
fseek(srcFile, e.ch.chunkSize + pad, SEEK_CUR);
expectedBytesLeft -= e.ch.chunkSize + pad;
}
return true;
}
static
AudioSampleData *extractData(FILE* fh, const vector<WavTOCEntry> &toc)
{
uint8_t *dbuf = nullptr;
AudioSampleData *asd = nullptr;
size_t samplesRead = 0;
/* if we get here without an error, it means the file was well formed and
* we now have a ToC for the file chunks
*
* Find the format header.
*/
auto ti = FindTOCFor(toc, WavFormatChunkID);
if (ti == toc.end()) {
// couldn't find the format chunk
return nullptr;
}
fseek(fh, ti->offset, SEEK_SET);
int chunkSize = ti->ch.chunkSize;
/* safety check - the chunk can't be smaller than 16 bytes */
if (chunkSize < 16) {
return nullptr;
}
/* and cap it at the size of the header struct so we don't buffer overflow */
if (chunkSize > sizeof(WavFormatChunk)) {
chunkSize = sizeof(WavFormatChunk);
}
struct WavFormatChunk fc;
if (1 != fread(&fc, chunkSize, 1, fh)) {
return nullptr;
}
/* now that we have the header, verify that we can support the format. */
if (fc.wFormatTag != WAV_FORMAT_PCM && fc.wFormatTag != WAV_FORMAT_IEEE_FLOAT) {
return nullptr;
}
/* sanity check the block stride */
if (fc.nBlockAlign < minimumBlockAlignment(fc.wBitsPerSample, fc.nChannels)) {
return nullptr;
}
asd = new AudioSampleData(fc.nChannels, fc.wBitsPerSample, fc.nSamplesPerSec, fc.wFormatTag == WAV_FORMAT_IEEE_FLOAT);
/* now to check the data block */
ti = FindTOCFor(toc, WavDataChunkID);
if (ti == toc.end()) {
goto fail2;
}
/* we can live with short reads here, so we use a slightly different strategy */
fseek(fh, ti->offset, SEEK_SET);
dbuf = reinterpret_cast<uint8_t *>(malloc(ti->ch.chunkSize));
if (nullptr == dbuf) {
goto fail2;
}
samplesRead = fread(dbuf, fc.nBlockAlign, ti->ch.chunkSize / fc.nBlockAlign, fh);
if (samplesRead <= 0) {
goto fail;
}
asd->AppendSamples(fc.nBlockAlign, static_cast<unsigned>(samplesRead), dbuf);
return asd;
fail:
free(dbuf);
fail2:
delete asd;
return nullptr;
}
AudioSampleData *
LoadWav(const char *fileName)
{
FILE *fh = nullptr;
vector<WavTOCEntry> toc;
AudioSampleData* asd = nullptr;
fh = fopen(fileName, "rb");
if (fh == nullptr) {
return nullptr;
}
/* first of all, verify that we have an actual wavfile */
struct ChunkHeader ch;
if (1 != fread(&ch, sizeof(ch), 1, fh)) {
/* short read or error */
goto fail;
}
/* check the top level header. */
if (memcmp(WavTopChunkID, ch.chunkID, 4)) {
goto fail;
}
/* minimum sensible size is the WAVE identifier, 2 chunk headers
* and the minimum legal WAVFormatChunk body
*/
if (ch.chunkSize < (4+16+8+8)) {
goto fail;
}
/* check the WAVE magic */
char waveMagic[4];
if (1 != fread(&waveMagic, 4, 1, fh)) {
goto fail;
}
if (memcmp("WAVE", waveMagic, 4)) {
goto fail;
}
/* if were're still here, the file is valid so far, now we need to read
* scan the wave file to find the blocks we need */
if (!readHeader(fh, ch, toc)) {
goto fail;
}
asd = extractData(fh, toc);
if (asd == nullptr) {
goto fail;
}
return asd;
fail:
if (fh != nullptr) {
fclose(fh);
fh = nullptr;
}
return nullptr;
}