【发布时间】:2013-01-16 03:10:50
【问题描述】:
我正在尝试使用 NAudio 编写峰值音量计。我的代码与http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder 非常相似,但我的代码和链接的 Voice Recorder 项目都存在问题。
当播放恒定频率和音量的声音时,音量表最初以合理的电平开始,但随后衰减到非常小的值。我不确定为什么会这样,因为 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”的算法。
-
什么是类似“VU”的算法?
-
一个 VU 表 (transitchicago.com/farechanges) 测量信号电平。指针跟踪水平的方式不是瞬时的......它有点落后,并且平均了一些高峰和低谷。有一些算法试图模仿这种行为。还有其他用于测量信号的算法。峰值计总是向您显示该组样本的最高级别。您链接到的项目可能使用某种 delta 算法,这就是为什么它在没有任何变化时下降的原因。这只是一个猜测,我没有看过代码。
-
@Brad 对不起,芝加哥交通票价变更页面的链接与 VU 计价器有何关联? ;)
-
@JPollack,哈,抱歉,我一定是粘贴了链接而不复制。我不记得我要链接到什么...也许这个? en.wikipedia.org/wiki/File:VU-meter-reponse-graph.svg