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

后端 未结 2 1006
青春惊慌失措
青春惊慌失措 2021-01-13 09:46

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

相关标签:
2条回答
  • 2021-01-13 10:41

    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.

    0 讨论(0)
  • 2021-01-13 10:43

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

    0 讨论(0)
提交回复
热议问题