How to read Bit rate of .wav file in C#
Asked Answered
G

2

5

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 in index 28? with no asnwers so for. Also I now believe Shell is not the best way to read audio file properties. I have researched on different open source media libraries, cant find much.

TagLib#: This one works alright but have two issues. it does not reflect actual bit rate in some cases where bit rate is very low (like less than 30), it just returns 0. Secondly, I am not sure if I can use it for commercial use. The License says http://opensource.org/licenses/LGPL-2.1

NAudio: Simply does not expose any property to read the bitrate, so I have to calculate it. after research I got this formula to calculate the bit rate. bitrate = Samplerate * Channels * BitsPerSample. This works fine in most cases, however fails if you got BitsPerSample = 0 for some .wav files. Yes the file is absolutely fine, I can play it, but still BitsPerSample is 0.

MediaInfo: again no straight forward property, have to loop through all the properties of audio file and then search for "overall bit rate". moreover have to add two dlls in project, one is .net wraper and other is actual dll.

Apologies for such a long summary, but I dont wanted to ask "hey how can I get the bit rate of audio file" without showing what I already have done. So, if you have a .wav audio file what library/method would you use to get the bitrate?

Guenevere answered 5/2, 2014 at 3:12 Comment(0)
I
5

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.

Illusionary answered 5/2, 2014 at 7:31 Comment(1)
Thanks Mark and @Jim, all of sudden I got two solutions, both working. Just to add one last bit for someone who comes to this thread looking for bitrates: we need to divide that bitrate by 1000 to get the kbps.Guenevere
D
5

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/.

Dihybrid answered 5/2, 2014 at 5:25 Comment(2)
thanks Jim, the idea seems right but its not working. it is returning me '8000' for a valid .wav file which has bitrate 64kbps. Tried with different .wav files, no luck.Guenevere
the value needs to be multiplied by 8, because this is the average bytes per second, not the bitrate.Illusionary
I
5

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.

Illusionary answered 5/2, 2014 at 7:31 Comment(1)
Thanks Mark and @Jim, all of sudden I got two solutions, both working. Just to add one last bit for someone who comes to this thread looking for bitrates: we need to divide that bitrate by 1000 to get the kbps.Guenevere

© 2022 - 2024 — McMap. All rights reserved.