imr*_*155 5 c# audio microphone recording naudio
我正在使用nAudio Library来捕获麦克风输入.但我遇到了一个问题.我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)
下面是一个2秒WAV文件的结果,没有声音,只有麦克风噪音.
-54.089102453893 in dB
-51.9171950072361 in dB
-53.3478098666891 in dB
-53.1845794096928 in dB
-53.8851764055102 in dB
-57.5541358628342 in dB
-54.0121140454216 in dB
-55.5204248291508 in dB
-54.9012326746571 in dB
-53.683126746571 in dB
-53.6831017096011 in dB
-52.8728852678309 in dB -55.7028800863786 in dB
我们可以看到,当没有输入声音时,db级别会在-55左右徘徊,只有静音.如果我在麦克风中以正常音调录制"Hello",则db值将转到-20左右.我在某处读到平均人声约为20dB,-3dB到-6dB是麦克风的ZERO值范围.
问题:我是否正确计算了dB值?(我使用了其他人提出的公式)...为什么dB总是出现负值?我错过了一个关键概念或机制吗?
我在codeplex搜索了nAudio文档但没有找到答案.在我的观察中,那里的文档需要更多的解释,然后只是一堆问答[没有冒犯nAudio :)]
如果我正确理解了公式,那么您计算的实际值是 dBm,这绝对没问题,因为 dB 只是测量放大的单位,不能用于测量信号强度/幅度(即您可以说我放大了信号强度为 3 分贝,但不能说我的信号强度为 6 分贝)。
负值的存在只是因为公式的对数转换部分(将瓦/毫瓦转换为分贝)并且因为您正在处理的信号相对较弱。
总而言之,看起来您所做的一切都是正确的。希望能帮助到你。
编辑:顺便说一句,正如您所看到的,沉默和人类语音之间确实存在约 23-25dbm 的差异