我正在使用nAudio库来捕获麦克风输入,但我遇到了一个问题。我使用的是从nAudio示例应用程序中略微修改的代码。该代码基于麦克风输入生成WAV文件并将其呈现为波形。以下是该代码:
private void RenderFile()
{
SampleAggregator.RaiseRestart();
using (WaveFileReader reader = new WaveFileReader(this.voiceRecorderState.ActiveFile))
{
this.samplesPerSecond = reader.WaveFormat.SampleRate;
SampleAggregator.NotificationCount = reader.WaveFormat.SampleRate/10;
//Sample rate is 44100
byte[] buffer = new byte[1024];
WaveBuffer waveBuffer = new WaveBuffer(buffer);
waveBuffer.ByteBufferCount = buffer.Length;
int bytesRead;
do
{
bytesRead = reader.Read(waveBuffer, 0, buffer.Length);
int samples = bytesRead / 2;
double sum = 0;
for (int sample = 0; sample < samples; sample++)
{
if (bytesRead > 0)
{
sampleAggregator.Add(waveBuffer.ShortBuffer[sample] / 32768f);
double sample1 = waveBuffer.ShortBuffer[sample] / 32768.0;
sum += (sample1 * sample1);
}
}
double rms = Math.Sqrt(sum / (SampleAggregator.NotificationCount));
var decibel = 20 * Math.Log10(rms);
System.Diagnostics.Debug.WriteLine(decibel.ToString() + " in dB");
} while (bytesRead > 0);
int totalSamples = (int)reader.Length / 2;
TotalWaveFormSamples = totalSamples / sampleAggregator.NotificationCount;
SelectAll();
}
audioPlayer.LoadFile(this.voiceRecorderState.ActiveFile);
}
以下是一个2秒无声,只有麦克风噪音的WAV文件结果中的一小段内容:
-54.089102453893 分贝 -51.9171950072361 分贝 -53.3478098666891 分贝 -53.1845794096928 分贝 -53.8851764055102 分贝 -57.5541358628342 分贝 -54.0121140454216 分贝 -55.5204248291508 分贝 -54.9012326746571 分贝 -53.6831017096011 分贝 -52.8728852678309 分贝 -55.7021600863786 分贝
我们可以看到,在没有输入声音,只有沉默的情况下,分贝级别围绕着-55左右。如果我以正常音调在麦克风中录制说“你好”,分贝值将达到约-20左右。我在某处读到,人类平均谈话为20dB,而-3dB至-6dB是麦克风的零值范围。
问题:我是否正确计算了分贝值?(我使用了此处某人提出的公式)...为什么分贝总是呈负数?我是否缺少重要的概念或机制?
我在codeplex上搜索了nAudio文档,但没有找到答案。据我观察,那里的文档需要更多解释性内容,而不仅仅是一堆问答。