0

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!

SteveP
  • 18,840
  • 9
  • 47
  • 60
  • I agree with CodesInChaos that Speex preprocessor does a pretty good job for Agc. You can find a ready to use .Net wrapper for it with a demo source code here: https://github.com/gribunin/SpeexPreprocessor You can also search SpeexPreprocessor package in Nuget if you dont want to compile it yourself. – Kirill Gribunin Dec 24 '21 at 18:28

4 Answers4

0

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Hi Paul, thank you for reply. Yes, AGC. I'm asking here exactly 'cause I don't think that it can be implemented in 2 lines of code. It will be perfect to find a .Net solution or even detailed description of such algorithm. – Dmitry Kadantsev Feb 05 '10 at 16:02
  • AGC works on the signal *after* you have recorded it, which is fine, but it is also very important first to set the appropriate mic gain before recording. Otherwise you will either have a poor signal to noise ratio, or distortion and clipping, both issues that cannot be fixed by AGC. – Mark Heath Nov 24 '10 at 09:29
  • @Mark Heath: AGC can be applied in hardware too - most audio interfaces have a programmable analogue gain stage prior to the A-D converter input - this enables AGC to be controlled by software but applied in hardware in a way that avoids the problems of clipping and SNR that you mention – Paul R Nov 24 '10 at 10:06
  • sounds like it would be useful, but I have not come across any Windows APIs that allow access to this feature. Do you know of any? – Mark Heath Nov 24 '10 at 10:15
  • @Mark Heath: sorry, no, I'm not a Windows person. – Paul R Nov 24 '10 at 13:09
0

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

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

jinsungy
  • 10,717
  • 24
  • 71
  • 79
  • Hi, thanks for reply. I have no problem with a capturing of current signal value at any moment. The problem is - how to tune mic's gain/volume depending on existing information about current and past mic's levels to hold a sound's volume in desired range? This is a point. – Dmitry Kadantsev Feb 05 '10 at 18:35
0

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.

Community
  • 1
  • 1
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
0

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.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262