我正在尝试使用NAudio编写峰值音量表。我的代码与http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder非常相似,但是我的代码和链接的录音机项目都遇到了问题。NAudio峰值音量表
当播放恒定频率和音量的声音时,音量表开始时会处于合理的水平,但会衰减到非常小的值。我不确定为什么会出现这种情况,因为NAudioDemo中的峰值音量计不会这样做。我试图在我的程序中复制NAudioDemo中的代码,但是我无法找到包含峰值音量表代码的代码文件。
有人可以指导我创建一个峰值音量表的替代解决方案,或帮助我确定为什么我的解决方案(和链接上提供的)都无法工作?
public MainWindow()
{
int waveInDevices = WaveIn.DeviceCount;
for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
{
WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
Console.WriteLine("Device {0}: {1}, {2} channels",
waveInDevice, deviceInfo.ProductName, deviceInfo.Channels);
WaveIn waveIn = new WaveIn();
waveIn.DeviceNumber = 0; //TODO: Let the user choose which device, this comes from the device numbers above
waveIn.DataAvailable += waveIn_DataAvailable;
int sampleRate = SAMPLE_RATE; // 8 kHz
int channels = 1; // mono
waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
waveIn.StartRecording();
}
}
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((e.Buffer[index + 1] << 8) |
e.Buffer[index + 0]);
float sample32 = sample/32768f;
ProcessSample(sample32);
}
}
void ProcessSample(float sample1)
{
samplenumber += 1;
if (sample1 > maxval)
{
maxval = sample1;
}
if (sample1 < minval)
{
minval = sample1;
}
//Run updateView every few loops
if (samplenumber > (double)SAMPLE_RATE/DISPLAY_UPDATE_RATE)
{
samplenumber = 0;
updateView(); //needs to be fast!
}
}
void updateView()
{
Console.WriteLine(maxval);
Console.WriteLine(minval);
progressBar1.Value = (maxval - minval)*50;
maxval = 0;
minval = 0;
}
我还没有检查过该项目,但它可能不是实际的峰值计。有几种类似“VU”的算法。 – Brad
什么是“VU”类算法? –
VU仪表(http://www.transitchicago.com/farechanges/)用于测量信号电平。针跟踪水平的方式不是瞬时的......它稍微滞后一点,平均了一些峰和谷。有些算法试图模拟这种行为。还有其他用于计量信号的算法。峰值计总是显示出您的样品组的最高水平。您链接到的项目可能会使用某种Delta算法,这就是为什么当没有变化时它会下降。这只是一个猜测,但我没有看过代码。 – Brad