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.
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.
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.