given that I have a .wav file, what would be best way to read its Bit rate property in C#. I have tried Shell, and asked a question Is "Bit rate" property fixed i
With NAudio, the bitrate is calculated via the AverageBytesPerSecond
member of the WaveFormat
structure:
using (var reader = new WaveFileReader(filename))
{
bitrate = reader.WaveFormat.AverageBytesPerSecond * 8;
}
This will work for all uncompressed audio formats and most compressed. The WAV file format is a container format and can actually contain audio in any codec, even though it's most common use is PCM. But not all codecs are CBR (constant bit rate). Some have a variable bit rate, so the AverageBytesPerSecond
property may be an estimate.
You can easily read the value at offset 28 in the file.
int bitrate;
using (var f = File.OpenRead(filename))
{
f.Seek(28, SeekOrigin.Begin);
byte[] val = new byte[4];
f.Read(val, 0, 4);
bitrate = BitConverter.ToInt32(val, 0);
}
That should work for uncompressed WAV files (the most common type). See https://ccrma.stanford.edu/courses/422/projects/WaveFormat/.