Capturing Sound From The Microphone Using SlimDX

sdx_icon_black_bigger-5B5-5D

SlimDX SlimDX is an open source library which allows .NET 2.0+ applications (including C#, VB.NET, IronPython, and F#) to use Microsoft’s DirectX APIs, as well as several related Microsoft multimedia APIs.

SlimDX is a direct replacement (albeit not exactly API compatible) for the now-defunct Microsoft’s Managed DirectX (MDX) wrappers.

 

However, not everything is perfect. The main issue with SlimDX is the current lack of documentation. Sure they do have a lot of examples, but unfortunately, only a few of them deal with DirectSound. And most of them assumes you already have experience with standard DirectSound (and DirectX itself) to understand them.

So to help completely newcomers to the DirectX world, here is a extremely simple example on how to read data from the microphone using SlimDX just to get you started.

Code

Tipically you would run this code in a separate thread so it doesn’t block the main application. I hope someone finds it useful! If you have questions please leave a comment.

11 Comments

  1. Hello,

    Great example. However, I am trying to display the output of the buffer in a progress bar. So when someone is talking in the mic the output will be displayed visually. However, I can’t seem to get this to work. My code I have used is the following.

    while (captureAudio)
    {
    captureBuff.Read(samples, 0, true);
    audioValue = Math.Abs(samples[i++]);
    Console.WriteLine(“i: [ ” + i + ” ]”);
    Console.WriteLine(“AudioValue: [ ” + audioValue + ” ]”);

    this.pbrAudioCaptured.Value = audioValue;
    }

  2. Hi Anonymous,

    Can you please send me an email? I have other examples on how to work with audio, including the visual display. They are a bit rough and unfinished, but I think they may send you in the right direction.

    By the way, what’s the output of your program? I haven’t got the chance to run it yet.

    Best regards,
    Cesar

  3. Hello!

    I need some help to write the code in C# to detect sound in the microphone.And when the sound is detected I have to play a message.I have already done the function of playing the message ,but now i have to detect the time when I have to play it.If you can help me , it would be great.
    Thanks

  4. Hello, great example, Can you give a tip how to seperate left & right channel from captured stream ?
    Thx
    Schift

  5. Hey,
    Thx for this very useful article!

    I would like to record a sound into a .wav with slimDX, and i want to ask you if you know how to do it?

    Thank you very much!

  6. Hi,
    Looks like they have changed the namespaces on you. Line 11 does not compile. It should be:

    waveFormat.FormatTag=SlimDX.Multimedia.WaveFormatTag.IeeeFloat;

Leave a Reply

Your email address will not be published. Required fields are marked *