C# 中的实时麦克风幅度测量
我正在寻找简单的解决方案,它将返回 C# 中麦克风输入的整数值。我已经在网上检查了可用的示例,但它们都不能在 x64 环境中工作。 (VS2008 + W7 x64)。
是否有任何简单的解决方案可以返回 C# 中麦克风输入的幅度(或频率)值?
我尝试了 NAudio 但没有结果,如下: http://www. codeproject.com/KB/audio-video/cswavrec.aspx?msg=2155497 运气不好。
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).
Is there any simple solution that will return value of amplitude (or frequency) of microphone input in c#?
I tried NAudio without results and this: http://www.codeproject.com/KB/audio-video/cswavrec.aspx?msg=2155497 without luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最简单的方法是使用旧的 Windows 多媒体 API,因为它非常简单。
以下是 MSDN 的链接:http://msdn。 microsoft.com/en-us/library/dd743586(v=VS.85).aspx
您所做的就是使用
waveInOpen
函数获取输入设备。要确定要使用的设备,您无需枚举所有设备,但可以查询其中的每一个设备。已安装设备的数量通过调用waveInGetNumDevs
返回。然后,您可以为每个设备调用waveInGetDevCaps
并检查这些属性。当您拥有设备句柄后,您可以重复调用
waveInAddBuffer
来获取小块数据。根据您在waveInOpen
期间指定的格式,字节表示原始音频数据。以某个频率采样的 8 或 16 位有符号或无符号幅度。然后,您可以应用滚动平均值来平滑信号并打印出来。
据我所知,C# 没有完善的 API,因此您要做的就是使用 P/Invoke 来获取 Win32 API 函数。这相当简单,您只需移植 Win32 标头的小版本即可直接从 C# 调用它们。
如果您更顽固,您可以用 C++/CLI 编写包装器库。这并不是一个坏主意,因为它允许您使用现有的 Windows C/C++ 头文件并以有趣的方式混合 C++ 和托管代码。只要小心非托管资源,您很快就会拥有一个非常强大的 intropability 库。
但从 Windows Vista 开始,还有更高级的音频 API,Windows Core Audio 组件可能会更有趣。但对于基本的 I/O 操作,Windows 多媒体功能将使您更快地实现。
在构建简单的软件合成器时,我曾多次使用过这些函数。遗憾的是,该代码早已不复存在。
I reckon the easiest route to go is to use the old Windows multimedia API because it's really straight forward.
Here's the link to MSDN: http://msdn.microsoft.com/en-us/library/dd743586(v=VS.85).aspx
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 callingwaveInGetNumDevs
. You can then callwaveInGetDevCaps
for each device and inspect those properties.When you have your device handle you then repeatedly call
waveInAddBuffer
to get small chunks of data. Depending on the format you specified duringwaveInOpen
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# 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.
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.
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.
我推荐 SlimDX,因为它几乎可以在任何版本的 Windows(x86 或 x64)上运行,并提供最多的功能和灵活性。然而,由于没有好的完整代码示例,启动和运行起来很痛苦。我编写了一个包装类来简化其使用,因此可以像这样调用它(我在 Win7 x64 上测试了此代码):
这是 SlimDX 包装类的源代码:
它是完全多线程的,以最大限度地减少延迟。我最初编写它是为了实时信号处理分析工具,并使用浮点输出而不是短输出,但我修改了代码示例以匹配您请求的用法。如果您需要频率数据,我会使用 http://www.mathdotnet.com/Neodym.aspx或 http://www.exocortex.org/dsp/ 以获得良好的 C# FFT 库。
I recommend SlimDX since it should work on just about any version of windows (x86 or x64) and provides the most features and flexibility. However, it is a pain to get up and running since there are no good complete code samples. I wrote a wrapper class to simplify its usage though so it can be called like this (I tested this code on Win7 x64):
Here is the source for the SlimDX wrapper class:
It's fully multi-threaded to minimize latency. I originally wrote it for a real-time signal processing analysis tool and used float output instead of short but I modified the code sample to match your requested usage. If you need frequency data I would use http://www.mathdotnet.com/Neodym.aspx or http://www.exocortex.org/dsp/ for a good C# FFT library.