如何在 Silverlight 4 中可视化麦克风声音/压力级别?

发布于 11-28 04:06 字数 1163 浏览 2 评论 0原文

我正在按照本教程了解如何制作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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好2024-12-05 04:06:24

试试这个...这是 (8000,8,1) 如果您使用 2 个通道,请将“index+=1”替换为“index+=2”

            for (int index = 0; index < sampleData.Length; index += 1)
            {
                short sample = (short)((sampleData[index + 1] << 8) | sampleData[index + 0]);

                //short sample = (short)(sampleData[index + 0]);
                float sample32 = sample / 32768f;

                float maxValue = 0;
                float minValue = 0;

                maxValue = Math.Max(maxValue, sample32);
                minValue = Math.Min(minValue, sample32);

                float lastPeak = Math.Max(maxValue, Math.Abs(minValue));

                this.MicLevel = (100 - (lastPeak * 100)) * 10;
                //System.Diagnostics.Debug.WriteLine("Mic Level: " + this.MicLevel.ToString());
            }

try this...this is for (8000,8,1) if you are using 2 channels replace "index+=1" with "index+=2"

            for (int index = 0; index < sampleData.Length; index += 1)
            {
                short sample = (short)((sampleData[index + 1] << 8) | sampleData[index + 0]);

                //short sample = (short)(sampleData[index + 0]);
                float sample32 = sample / 32768f;

                float maxValue = 0;
                float minValue = 0;

                maxValue = Math.Max(maxValue, sample32);
                minValue = Math.Min(minValue, sample32);

                float lastPeak = Math.Max(maxValue, Math.Abs(minValue));

                this.MicLevel = (100 - (lastPeak * 100)) * 10;
                //System.Diagnostics.Debug.WriteLine("Mic Level: " + this.MicLevel.ToString());
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文