【发布时间】:2015-11-26 15:00:14
【问题描述】:
我正在使用 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 以 dB 为单位
-51.9171950072361 分贝
-53.3478098666891 分贝
-53.1845794096928 分贝
-53.8851764055102 分贝
-57.5541358628342 分贝
-54.0121140454216 分贝
-55.5204248291508 分贝
-54.9012326746571 分贝
-53.6831017096011 以 dB 为单位
-52.8728852678309 分贝
-55.7021600863786 dB
正如我们所见,当没有输入声音,只有静音时,db 级别徘徊在 -55 左右。如果我以正常的音调在麦克风中录制“你好”,则分贝值将变为 -20 左右。我在某处读到,人类的平均说话量约为 20dB,而 -3dB 到 -6dB 是麦克风的零值范围。
问题:我是否正确计算了 dB 值? (我使用了其他人在这里提出的公式)...为什么 dB 总是出现负数?我是否遗漏了一个关键概念或机制?
我在 codeplex 搜索了 nAudio 文档,但没有找到答案。在我的观察中,那里的文档需要更多的解释性,而不是一堆问答[没有冒犯 nAudio :)]
【问题讨论】:
标签: c# audio naudio microphone recording