C# How to get Audio Decibel values with time span

后端 未结 2 793
一整个雨季
一整个雨季 2021-01-03 11:21

how can I get Decibel values of a wav/mp3 file I have every 1 second? using any audio library that works with C#..

something like:

Time: 0, DB: 0.213         


        
2条回答
  •  清酒与你
    2021-01-03 12:07

    With NAudio you can use the WaveFileReader and Mp3FileReader classes to get access to the sample data within the file as a byte array. Then you would need to read through the file and get the sample values (e.g. for 16 bit audio, every two bytes represents a short). If the file is stereo, the it will alternate left sample, right sample.

    Then you need to come up with a strategy for measuring the decibels. Are you going to look for the loudest sample in each second, or the average sample volume in each second, or just select whatever sample is playing at that second? Having got that value, it needs to be normalised so that 1 is the loudest (so for 16 bit audio, divide your value by 32768). Also, use the absolute value of the sample. Now the value in decibels can be calculated:

    short sample16Bit = BitConverter.ToShort(buffer,index);
    double volume = Math.Abs(sample16Bit / 32768.0);
    double decibels = 20 * Math.Log10(volume);
    

    In the NAudio demo app, a "SampleAggregator" is used to collect the min and max sample values over a given period of time, which is then in turn used to draw the audio waveform, and to update a volume meter. You could make use of this same class to provide you with values to pass into your decibel conversion function.

    (see this page for a more detailed explanation of decibels)

提交回复
热议问题