C# How to get Audio Decibel values with time span

一曲冷凌霜 提交于 2019-11-30 16:05:01

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!