我正在使用 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 分贝
正如我们所见,当没有输入声音,只有静音时,db 级别徘徊在 -55 左右。如果我以正常的语气用麦克风录制说“你好”,分贝值将变为-20 左右。我在某处读到,人类谈话的平均音量约为 20dB,而 -3dB 至 -6dB 是麦克风的零值范围。
问题:我是否正确计算了 dB 值? (我使用了其他人在这里提出的公式)...为什么 dB 总是负数?我是否遗漏了一个关键概念或机制?
我在 codeplex 上搜索了 nAudio 文档,但没有找到答案。根据我的观察,那里的文档需要更多的解释,然后只是一堆问答 [没有冒犯 nAudio :)]
最佳答案
如果我对公式的理解正确,那么你计算的实际值是 dBm,这绝对没问题,因为 dB 只是测量放大的单位,不能用于测量信号强度/幅度(即你可以说我把信号放大了 3 分贝,但不能说我的信号强度是 6 分贝)。
出现负值只是因为公式的对数转换部分(将瓦特/毫瓦转换为分贝),而且您处理的信号相对较弱。
因此,总而言之,看起来您做对了所有事情。 希望对您有所帮助。
编辑:顺便说一句,如您所见,静音和人类语音之间确实存在 ~23-25dbm 的差异
关于C# - 麦克风噪音检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330511/