Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to get Audio Decibel values with time span

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.213623
Time: 1, DB: 0.2692261
Time: 2, DB: 0.2355957
Time: 3, DB: 0.2363281
Time: 4, DB: 0.3799744
Time: 5, DB: 0.3580322
Time: 6, DB: 0.1331177
Time: 7, DB: 0.3091431
Time: 8, DB: 0.2984009

I'd really appreciate your help :)

regards,

like image 761
Desolator Avatar asked Jun 01 '11 06:06

Desolator


1 Answers

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)

like image 86
Mark Heath Avatar answered Sep 22 '22 20:09

Mark Heath