Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WaveFile.cs #67

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions NWaves/Audio/WaveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,25 @@ 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);
// Additional info for the following: https://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
Notheisz57 marked this conversation as resolved.
Show resolved Hide resolved
// Here is a good link from that page for the Extension chunk: https://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
// 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".
// Above link says our AudioFormat dictates whether an extension chunk should actually exist, but we've been lenient to standards up to this point anyways.
if (waveFmt.AudioFormat == 0xFFFE)
Notheisz57 marked this conversation as resolved.
Show resolved Hide resolved
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