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
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)
I found a solution from examples given in NAudio Library. since the solution I found is so big. I'm not gonna post it here. so I'm just gonna give hints in case if anybody wanted to do the same thing.. NAudioDemo application -> AudioPlayBackDemo Folder -> AudioPlayBackPanel.cs File...