Created
March 23, 2022 15:48
-
-
Save niko-la-petrovic/fbb7cd95d3cdd135d536edcd0c7eb5c1 to your computer and use it in GitHub Desktop.
Wav Header Parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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