5

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,

Desolator
  • 22,411
  • 20
  • 73
  • 96
  • surely it depends on how loud your speaker volume is? ;-) – Adam Ralph Jun 01 '11 at 06:10
  • No i don't want to play the file.. I just want to load it somehow and get the decibel values for every second in the duration time.. – Desolator Jun 01 '11 at 06:15
  • I understand, but does MP3 have some kind of standard decibel measure internally? E.g. max wave height = 100? I don't know much about the internals of the MP3 format so I can't answer this. What I meant in my previous comment is that I can play an MP3 on my laptop's internal speakers and use some kind of sound meter to measure the volume at a certain decibel level, I can then play the same MP3 through my home cinema system and measure it at 10 times the decibel level. I've use the same file in both cases, so what is the files 'decibel' level? – Adam Ralph Jun 01 '11 at 06:24
  • 1
    it doesn't have.. its just a wav file.. you didn't get what I meant.. I need to get the decibels in order to generate a graph on my application.. sorry for my bad english.. I really don't know how to explain it properly.. – Desolator Jun 01 '11 at 07:33
  • 1
    @Adam: [decibel is a relative scale essentially (see dBFS)](http://en.wikipedia.org/wiki/DBFS). [Replay Gain is a proposed standard published by David Robinson in 2001 to measure the perceived loudness of audio in computer audio formats such as MP3 and Ogg Vorbis](http://en.wikipedia.org/wiki/Replay_Gain) – sehe Jun 01 '11 at 08:22

2 Answers2

20

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)

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • can you show me a sample for any wav file? I'm really lost.. :( – Desolator Jun 01 '11 at 17:42
  • lets say I have this file and I want to get the average dB when he says "three" http://www.mediafire.com/download.php?ygcs1mq9c28l1q3 – Desolator Jun 01 '11 at 20:52
  • @MarkHeath I can get the volume from `device.AudioMeterInformation.MasterPeakValue`. So I can just get the decibel value from that? Why would I need samples? – lbrahim Nov 15 '15 at 11:28
1

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

Desolator
  • 22,411
  • 20
  • 73
  • 96