Search code examples
c#windowsmcisendstring

Detect Silence from Microphone


Using mcisendstring, I want to stop recording if a silence period (no input from microphone) is reached. I understand a timer would be involved, so after x seconds a check needs to be made, but what to check exactly?

Currently,I record using:

i = mciSendString("record capture", Nothing, 0, 0)

Thanks.


Solution

  • You will need an audio library that allows you to access the record buffer while recording is in progress. I don't think MCI will let you do that. So it sounds like you are in the market for a more nuanced audio library.

    Here is a link to a StackOverflow question that lists several digital audio libraries.

    You might also consider using an open source .NET framework like NAudio, where they have done a lot of the work for you already.

    To make a recording, you will most likely need to allocate a primary buffer, plus a callback that will allocate secondary/permanent buffers as needed (e.g. as the recording gets longer and longer). To detect silence, you will need to perform some sort of signal processing on the primary buffer as bytes are added to it.

    The "signal processing" in this case could be very simple, e.g. you could take a moving average (essentially a low pass filter) and determine if the average amplitude is below a certain threshold level, which could be calibrated during the silence at the beginning of the recording (which presumably will give you a decent baseline).

    A more advanced process would attempt to filter out ambient noise or line noise (e.g. a 60 hz filter to remove the hum caused by household AC). You could go pretty deep with this if you wanted to.