Skip to content

Instantly share code, notes, and snippets.

@niko-la-petrovic
Created March 23, 2022 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niko-la-petrovic/fbb7cd95d3cdd135d536edcd0c7eb5c1 to your computer and use it in GitHub Desktop.
Save niko-la-petrovic/fbb7cd95d3cdd135d536edcd0c7eb5c1 to your computer and use it in GitHub Desktop.
Wav Header Parsing
// Spec
// https://docs.fileformat.com/audio/wav/
public static WavHeader ParseWavHeader(this Stream fs)
{
int bitDepth;
int channelCount;
int samplingRate;
int dataSectionByteCount;
var headerBuffer = new byte[44];
var headerSpan = headerBuffer.AsSpan();
fs.Read(headerBuffer);
samplingRate = BitConverter.ToInt32(headerSpan.Slice(24, 4));
bitDepth = BitConverter.ToInt16(headerSpan.Slice(34, 2));
dataSectionByteCount = BitConverter.ToInt32(headerSpan.Slice(40, 4));
channelCount = BitConverter.ToInt16(headerSpan.Slice(22, 2));
var wavHeader = new WavHeader
{
BitDepth = bitDepth,
ChannelCount = channelCount,
DataSectionByteCount = dataSectionByteCount,
SamplingRate = samplingRate
};
return wavHeader;
}
public class WavHeader
{
public int BitDepth { get; set; }
public int ChannelCount { get; set; }
public int SamplingRate { get; set; }
public int DataSectionByteCount { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment