Skip to content

Commit 090e38d

Browse files
authored
Merge pull request #112 from uvcat7/sukibaby-patch-wavloader
Fix wav format compliance
2 parents 2f5574c + f2fafe7 commit 090e38d

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/Editor/LoadWav.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ namespace {
1414
#pragma pack(1)
1515
struct WaveHeader
1616
{
17-
uint8_t chunkId[4];
17+
uint8_t chunkId[4]; // "RIFF"
1818
uint32_t chunkSize;
19-
uint8_t format[4];
20-
uint8_t subChunkId[4];
21-
uint32_t subChunkSize;
19+
uint8_t format[4]; // "WAVE"
20+
uint8_t subchunk1Id[4]; // "fmt "
21+
uint32_t subchunk1Size;
2222
uint16_t audioFormat;
2323
uint16_t numChannels;
2424
uint32_t sampleRate;
2525
uint32_t byteRate;
2626
uint16_t blockAlign;
27-
uint16_t bps;
27+
uint16_t bitsPerSample;
2828
};
2929
struct WaveData
3030
{
@@ -72,32 +72,38 @@ SoundSource* LoadWav(FileReader* file, String& title, String& artist)
7272
if(file->read(&header, sizeof(WaveHeader), 1) == 0
7373
|| memcmp(header.chunkId, "RIFF", 4) != 0
7474
|| memcmp(header.format, "WAVE", 4) != 0
75-
|| memcmp(header.subChunkId, "fmt ", 4) != 0
75+
|| memcmp(header.subchunk1Id, "fmt ", 4) != 0
7676
|| header.audioFormat != 1
7777
|| header.sampleRate == 0
7878
|| header.numChannels == 0
79-
|| (header.bps != 8 && header.bps != 16 && header.bps != 24))
79+
|| (header.bitsPerSample != 8 && header.bitsPerSample != 16 && header.bitsPerSample != 24))
8080
{
8181
return nullptr;
8282
}
8383

8484
// Skip over additional parameters at the end of the format chunk.
85-
file->skip(header.subChunkSize - 16);
85+
if (header.subchunk1Size > 16)
86+
{
87+
size_t extraBytes = static_cast<size_t>(header.subchunk1Size) - 16;
88+
file->skip(extraBytes);
89+
}
8690

8791
// Read the start of the data chunk.
8892
WaveData data;
89-
if(file->read(&data, sizeof(WaveData), 1) == 0
90-
|| memcmp(data.chunkId, "data", 4) != 0)
91-
{
92-
return nullptr;
93+
while (true) {
94+
if (file->read(&data, sizeof(WaveData), 1) == 0)
95+
return nullptr;
96+
if (memcmp(data.chunkId, "data", 4) == 0)
97+
break;
98+
file->skip(data.chunkSize);
9399
}
94100

95101
// Create a wav loader that will read the contents of the data chunk.
96102
WavLoader* loader = new WavLoader;
97103

98104
loader->frequency = header.sampleRate;
99105
loader->numChannels = header.numChannels;
100-
loader->bytesPerSample = header.bps / 8;
106+
loader->bytesPerSample = header.bitsPerSample / 8;
101107
loader->numFrames = data.chunkSize / (loader->bytesPerSample * loader->numChannels);
102108
loader->numFramesLeft = loader->numFrames;
103109
loader->file = file;

0 commit comments

Comments
 (0)