Wednesday, November 29, 2006

Sound Detection using C#

I few weeks ago I needed to detect sound through a microphone. What I wanted to do was turn on a computer screen when a sound was heard (talking, clapping etc). After a long google session, all I turned up were several expensive sound detection systems and a piece of VB6 code which sets up the soundcard for sampling using the Win32 API.

Instead of porting all the API code to C#, and having an 'interesting' history with VB6 (who doesn't), I hacked the VB6 code into a dll. This library now provides a list of available recording devices, listens to the microphone and raises an event when the volume is greater than a given threshold. And best of all its easily accessible from .NET code.

Internally, it
  • Grabs a buffer of 8-bit samples from the soundcard. 8-bit means each sample value is 0-256, with 127 being silence.
  • Loops through the buffer, and if there is a value greater than the threshold, raise an event

Get the Detector library (+ source + sample C# application)
Feel free to use it in your own projects. Enjoy!

6 comments:

Unknown said...

Hi, I'm interested in capturing audio myself and downloaded your solution, but the Reference to Detector is broken. How do I fix that? Referencing Interop.Detector.dll or Interop.SoundDetector.dll made the program compile with error 80040154:

Retrieving the COM class factory for component with CLSID {C153004D-7066-465E-8C04-B94124A88595} failed due to the following error: 80040154.

Any clues would be nice :)

Regards,
Jonas

KLG said...

Hi I have the same problem as Jonas. I get "Retrieving the COM class factory for component with CLSID {C153004D-7066-465E-8C04-B94124A88595} failed due to the following error: 80040154"

Ca you help?

Kleanthis

Unknown said...

If you're getting the COM error, it's because you only have the Interop.Detector.DLL in the bin\Debug directory, and not the actual Detector.DLL file.

I noticed the Detector.DLL did not appear to be in the package. You'll want to unzip the DetectorDLL_source.zip, open the project in Visual Basic 6 and do a File-> Make Detector.dll, then add a reference to the .NET application to this DLL and everything should work fine.

Janzour said...

Hi Jeff,

In your Sound detector library, when I load your project, there was a reference to "Detector" that is not found. Do you know where it is?

Thanks,
N

KaLLy said...

Hi!

I have tried your application (Sound detection in c#) and I've got the same error : Retrieving the COM class factory for component with CLSID {C153004D-7066-465E-8C04-B94124A88595}.I saw that the Detector reference is missing.Can you tell how can I solve this problem?I don't know VB6. Thanks

Vania Imran said...

hi i am unable to dl your files
Imran