I want to continuously sample from my PC's audio line in using C# (then process that data). What is the best way to do the sampling?
views:
749answers:
4There are no built-in libraries in the .NET framework for dealing with sound, but if you're on Win32, you can use an unmanaged library like DirectSound to do it.
Ianier Munoz shows how to write a full-duplex audio player in C# using waveIn via P/Invoke on CodeProject. He mentions Managed DirectSound as a more general method.
There is the Alvas Audio library as well, not free, has a nagging screen if you don't pay, but works beautifully. And the documentation is nice and, if you find a bug or something, the support is fine too.
You can do some (basic) audio capture using the open source NAudio .NET Audio Library. Have a look at the NAudioDemo project to see a simple example of recording to a WAV file using the WaveIn functions. I am planning to upgrade it to support capture using WASAPI which will be Vista only but hopefully should be higher performance.
Managed DirectX supports direct capture of audio and is very easy to use, but is no longer supported and was removed from the DirectX SDK last year. It's still possible to get it by installing an SDK version from before August 2007.
While not strictly meeting your requirements, a more robust approach would be to create a C++/CLI wrapper assembly around the native C++ DirectSound API, again from the DirectX SDK. This could then be called directly from C# code. This is definitely a more powerful and maintainable approach, despite requiring some knowledge of C++ and COM.
I have used both of these techniques in the past and they both work well.