Skip to content

Commit

Permalink
Parse WAV files which use an Extension chunk to describe PCM audio fo…
Browse files Browse the repository at this point in the history
…rmats greater than 16-bit (#67)

Fix to allow parsing WAV files which use an Extension chunk to describe PCM audio formats greater than 16-bit.
Microsoft states that any WAV file with a Bit Depth of more than 16 should use the Extension chunk to describe it's Audio Format, but it still uses the same Audio Format constants that are used at the container-level Audio Format definition.
  • Loading branch information
Notheisz57 authored Sep 27, 2022
1 parent 0728ca6 commit c1a6a4a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions NWaves/Audio/WaveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,26 @@ protected void ReadWaveStream(Stream waveStream, bool normalized = true)
waveFmt.Align = reader.ReadInt16();
waveFmt.BitsPerSample = reader.ReadInt16();

WaveFmt = waveFmt;

if (fmtSize == 18)
// Header size might not include sizeof extension block.
if (fmtSize == 18 || fmtSize == 40)
{
var fmtExtraSize = reader.ReadInt16();
reader.ReadBytes(fmtExtraSize);
// Any non-16bit WAV file should include a format extension chunk describing how the data should be interpreted.
var fmtUsedBitsPerSample = reader.ReadInt16(); // Number of bits-per-sample actually used of container-specified bits-per-sample
var fmtChannelSpeakerMap = reader.ReadInt32(); // Bitmask/flags indicating which channels are included in the file.
var fmtSubFormatCode = reader.ReadInt16(); // Similar to container-level format code (1 = PCM, 3 = IEEE, etc.).
var fmtSubFormatRemainder = reader.ReadBytes(14); // Remainder of SubFormat GUID. Usually just "\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71".
if (waveFmt.AudioFormat == 0xFFFE)
{
waveFmt.AudioFormat = fmtSubFormatCode;
}
if (fmtExtraSize > 22) // Read any leftovers
{
reader.ReadBytes(fmtExtraSize - 22);
}
}

WaveFmt = waveFmt;

// there may be some wavefile meta info here,
// so try to find "data" header in the file:
Expand Down

0 comments on commit c1a6a4a

Please sign in to comment.