Skip to content

Commit c7ce79d

Browse files
committed
Add support for fmt's extrabyte
Added, in readheader internal API, a logic to ignore the extrabyte in fmt header's chunk in WAV file.
1 parent 80f7202 commit c7ce79d

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/SDWaveFile.cpp

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct WaveFileHeader {
3939
} subChunk2;
4040
} __attribute__((packed));
4141

42+
struct WaveFileHeader header;
43+
4244
SDWaveFile::SDWaveFile() :
4345
SDWaveFile(NULL)
4446
{
@@ -173,6 +175,55 @@ int SDWaveFile::begin()
173175
return 1;
174176
}
175177

178+
int SDWaveFile::findData() {
179+
uint8_t byteread;
180+
int count = 0, fileSize;
181+
uint32_t size = 0, data;
182+
uint8_t buffer[] = {0, 0, 0, 0};
183+
184+
fileSize = _file.size() - 36;
185+
186+
//Read the first WAV header's chunk
187+
if (_file.read(&header, 36) != 36) {
188+
_file.close();
189+
return -1;
190+
}
191+
192+
//check if "data" field is present
193+
while (count < fileSize) {
194+
if (_file.read((void *)&byteread, 1) != 1) {
195+
_file.close();
196+
return -1;
197+
}
198+
for (int i = 0; i <= 2; i++) {
199+
buffer[i] = buffer[i + 1];
200+
}
201+
buffer[3] = byteread;
202+
count++;
203+
204+
if (count >= 4) {
205+
data = 0;
206+
data += buffer[0] << 24;
207+
data += buffer[1] << 16;
208+
data += buffer[2] << 8;
209+
data += buffer[3];
210+
//if find data field read the last header part
211+
if (data == 0x64617461) {
212+
if (_file.read((void *)&size, 4) != 4) {
213+
_file.close();
214+
return -1;
215+
}
216+
217+
//set second WAV header's chunk and return
218+
header.subChunk2.id = data;
219+
header.subChunk2.size = size;
220+
return 1;
221+
}
222+
}
223+
}
224+
return -1;
225+
}
226+
176227
int SDWaveFile::read(void* buffer, size_t size)
177228
{
178229
uint32_t position = _file.position();
@@ -225,9 +276,7 @@ void SDWaveFile::readHeader()
225276
return;
226277
}
227278

228-
struct WaveFileHeader header;
229-
230-
if (_file.read(&header, sizeof(header)) != sizeof(header)) {
279+
if (findData() == -1) {
231280
_file.close();
232281
return;
233282
}
@@ -238,7 +287,6 @@ void SDWaveFile::readHeader()
238287
header.chunkId = __REV(header.chunkId);
239288
header.format = __REV(header.format);
240289
header.subChunk1.id = __REV(header.subChunk1.id);
241-
header.subChunk2.id = __REV(header.subChunk2.id);
242290

243291
if (header.chunkId != 0x52494646) { // "RIFF"
244292
return;
@@ -256,11 +304,6 @@ void SDWaveFile::readHeader()
256304
return;
257305
}
258306

259-
if (header.subChunk1.size != 16 || header.subChunk1.audioFormat != 1) {
260-
// not PCM
261-
return;
262-
}
263-
264307
if (header.subChunk2.id != 0x64617461) { // "data"
265308
return;
266309
}

src/SDWaveFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class SDWaveFile : public SoundFile
5555

5656
private:
5757
void readHeader();
58+
int findData();
5859

5960
private:
6061
bool _headerRead;

0 commit comments

Comments
 (0)