如何在 Silverlight 4 中可视化麦克风声音/压力级别?
我正在按照本教程了解如何制作Silverlight录音机。我认为添加一个音量条指示器来为用户提供有关正在发生的事情的反馈会很棒。但是,我似乎无法让它正常工作。
OnSamples 方法提供原始 PCM 数据作为参数之一。另外,我设置了 AudioFrameSize 属性href="http://msdn.microsoft.com/en-us/library/system.windows.media.audiocapturedevice%28VS.95%29.aspx" rel="nofollow">AudioCaptureDevice 到 40 (1000 /40 == 25fps),因此 OnSamples 每 40ms 触发一次。
我的问题是如何从 PCM 数据中提取音量信息并将其显示为进度条中的百分比 [0-100]?
这就是我到目前为止所拥有的:
double average = 0;
for (int a = 0; a < sampleData.Length; ++a)
{
average += Math.Abs(sampleData[a]);
}
average /= sampleData.Length;
double volume = 20 * Math.Log10(average);
进度条的值然后设置为音量:
progressBar.Value = volume;
显然,我的代码不起作用,因为音量值几乎总是处于同一水平。
任何帮助表示赞赏!
I was following this tutorial on how to make a Silverlight audio recorder. I thought it would be great to add a volume bar indicator to provide user with a feedback on what's happening. However, I can't seem to get this to work properly.
OnSamples method of AudioSink class provides raw PCM data as one of the arguments. Also, I set the AudioFrameSize property of AudioCaptureDevice to 40 (1000/40 == 25fps), so OnSamples is triggered every 40ms.
My question is how to extract the sound volume information from PCM data and display it as percentage in a progress bar [0-100]?
This is what I have so far:
double average = 0;
for (int a = 0; a < sampleData.Length; ++a)
{
average += Math.Abs(sampleData[a]);
}
average /= sampleData.Length;
double volume = 20 * Math.Log10(average);
Value of the progress bar is then set to volume:
progressBar.Value = volume;
My code doesn't work, apparently, since the volume value is almost always at the same level.
Any help is appreciated!
试试这个...这是 (8000,8,1) 如果您使用 2 个通道,请将“index+=1”替换为“index+=2”
try this...this is for (8000,8,1) if you are using 2 channels replace "index+=1" with "index+=2"