Question

I'm looking for solution to tune mic's level in my VoIP app. I can get a level of sound, but also want to tune the mic's "volume" when it too low or too high in real-time. Like in Skype...

It looks easy ex facte, but I think the algorithm should be a bit smarter then just turning the volume when level comes over high/low limits.

I'm using .Net 2.0 on Win platform. Are there any ready libraries for .Net or even on C (so I can port it) or even just description of such methods/algorithms? I've tried to google but with no luck.

Thank you!

Was it helpful?

Solution

There are two parts to this.

First, you need to start capturing audio from the microphone and measure its signal intensity. If a person is speaking into the microphone to calibrate it, you could simply look for the highest peak over the previous few seconds. If it is below -6dB (say), raise the gain a bit, if it is above -3dB, reduce it a bit. The point is that you want high signal to noise ratio, but at all costs you want to avoid clipping or distortion with a signal too loud.

The second part is to work out which of the multitude of mixer controls returned by the mixer APIs is the one controlling the microphone gain. This turns out to be much harder than it ought to be, particularly if you want it to work with all sound cards and on XP and Vista/Win 7. See this question for some suggested solutions.

You can use NAudio for both parts. It has wrappers for the waveIn and mixer APIs.

OTHER TIPS

The commonly used name for this is AGC (Automatic Gain Control) and it is not quite as easy as it sounds. In a basic AGC scheme you have a fast "attack", which means that you reduce the gain very quickly when the input level increases, and then use a larger time constant to increase the gain more slowly when the input level drops.

Try this for a start: Get and set the wave sound volume

It uses Windows API in C# through P/Invoke.

I recommend using the Speex-Pre-Processor. As a bonus you get noise suppression.

My Xiph project contains bindings to it:
https://github.com/CodesInChaos/Xiph

In particular you need these two files:
https://github.com/CodesInChaos/Xiph/blob/master/Easy/Speex/SpeexPreProcessor.cs
https://github.com/CodesInChaos/Xiph/blob/master/LowLevel/SpeexPreProcessor.cs

It's probably not production quality, but a starting point. If you improve them please sent me the changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top