使用C#进行实时麦克风幅度测量 [英] Live Microphone amplitude measurement in c#

查看:428
本文介绍了使用C#进行实时麦克风幅度测量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的解决方案,该解决方案将返回c#中麦克风输入的整数值.我已经在网上检查可用的样本,但是它们都不在x64环境中工作. (VS2008 + W7 x64).

I am looking for simple solution which will return integer value of microphone input in c#. I was already checking available samples on net, but none of them worked in a x64 environment. (VS2008 + W7 x64).

有没有简单的解决方案可以返回c#中麦克风输入的幅度(或频率)值?

Is there any simple solution that will return value of amplitude (or frequency) of microphone input in c#?

我尝试了NAudio,但没有结果,这是这样的: http://www.codeproject.com/KB/audio-video/cswavrec.aspx?msg=2155497 没有运气.

I tried NAudio without results and this: http://www.codeproject.com/KB/audio-video/cswavrec.aspx?msg=2155497 without luck.

推荐答案

我认为最简单的方法是使用旧的Windows多媒体API,因为它确实很简单.

I reckon the easiest route to go is to use the old Windows multimedia API because it's really straight forward.

这是MSDN的链接: http://msdn.microsoft.com/zh-CN/library/dd743586(v=VS.85).aspx

Here's the link to MSDN: http://msdn.microsoft.com/en-us/library/dd743586(v=VS.85).aspx

您要做的是使用waveInOpen功能来获取输入设备.要弄清楚要使用的设备,您无需列举所有设备,但可以查询其中的每一个.通过调用waveInGetNumDevs返回已安装设备的数量.然后,您可以为每个设备调用waveInGetDevCaps并检查这些属性.

What you do is that you use the waveInOpen function to get an input device. To figure out what device to use you don't enumerate all the devices but you can query each and one of them. The number of installed devices is returned by calling waveInGetNumDevs. You can then call waveInGetDevCaps for each device and inspect those properties.

当您处理好设备后,便会反复调用waveInAddBuffer以获取小块数据.根据您在waveInOpen中指定的格式,字节表示原始音频数据.以某个频率采样的8位或16位带符号或未带符号的幅度.

When you have your device handle you then repeatedly call waveInAddBuffer to get small chunks of data. Depending on the format you specified during waveInOpen the bytes represent the raw audio data. Amplitude in 8 or 16-bit signed or unisnged sampled at some frequency.

然后您可以应用滚动平均值来平滑信号并仅打印该信号.

You could then apply a rolling average to smooth the signal and just print that.

C#没有我所知道的声音API,因此您要做的是使用P/Invoke获取Win32 API函数.这很简单,您只需移植Win32标头的小版本即可直接从C#调用它们.

C# doesn't have a sound API that I know of, so what you do is that you use P/Invoke to get at the Win32 API functions. This is fairly straight forward, you only need to port small versions of the Win32 headers to be able to call them from C# directly.

如果您更加顽固,则可以使用C ++/CLI编写包装器库.这并不是一个坏主意,因为它使您可以使用现有的Windows C/C ++头文件,并以有趣的方式混合使用C ++和托管代码.只需小心非托管资源,您将立即拥有一个非常强大的可逆性库.

If you're more hardcore you could write a wrapper library in C++/CLI. That's not that bad of an idea because it let's you use existing Windows C/C++ header files and mix C++ and managed code in intresting ways. Just be careful with the unmanaged resources and you'll have a very powerful intropability library in no time.

但是从Windows Vista开始,还有更高级的音频API,这是Windows Core Audio组件,可能会进一步引起人们的兴趣.但是,对于基本的I/O操作,Windows多媒体功能将使您更快地到达那里.

But there's also more advanced audio APIs starting with Windows Vista, the Windows Core Audio components which could be more interesting further along the line. But for basic I/O operation the Windows multimedia functions will get you there faster.

在构建简单的软件合成器时,我曾多次使用这些功能.可悲的是,该代码早已不复存在.

I've used these functions on a number of occasions when building simple software synthesizers. Sadly that code is long gone.

这篇关于使用C#进行实时麦克风幅度测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆