Microphone boost and volume with NAudio

Jan 23, 2013 at 6:05 AM

Hello,

how can I increase Microphone boost and volume using NAudio library?

Is it possible?

Coordinator
Jan 23, 2013 at 10:20 AM

NAudio does contain wrappers for the Windows mixer... APIs, which in theory can be used for this. In practice, it turns out to be incredibly hard to work out exactly which control you need to adjust for your microphone. What I found was that code which worked on XP wouldn't work on Vista and above. More concerningly, the newer audio APIs (e.g. WASAPI) seem to offer less access to these type of parameters, with Microsoft preferring you to pop open the Windows sound control panel and let the user adjust the settings themselves.

Have a look at my voice recorder app which tries to set the microphone level.

Jan 23, 2013 at 12:15 PM

Thank you!

Jan 31, 2013 at 1:55 AM
Edited Jan 31, 2013 at 1:55 AM
Interesting. Your voice recorder app only sets the windows volume control to it's max. How would you boost the audio signal, ie multiply it's amplitude with a given value and output it in realtime, so you can create virtually any volume you desire up to audio distortion?
Jan 31, 2013 at 3:10 AM
Don't bother I found it myself.

As http://stackoverflow.com/questions/5694326/record-input-from-naudio-wavein-and-output-to-naudio-waveout helped me well for the recording part I decided to share the amplification part for those who need it.
        int Verstärkung = 30;
        void wi_DataAvailable(object sender, WaveInEventArgs e)
        {
            //bwp2.AddSamples(e.Buffer, 0, e.BytesRecorded);
            var erg = new byte[e.BytesRecorded];

            for (int i = 0; i < e.BytesRecorded; i += 2)
            {
                var sample = (short)(e.Buffer[i] | (e.Buffer[i + 1] << 8));
                erg[i] = (byte)((sample * Verstärkung) & 0xff);
                erg[i + 1] = (byte)(((sample * Verstärkung) >> 8) & 0xff);
            }
            bwp2.AddSamples(erg, 0, e.BytesRecorded);
        }
Enter your desired amplification value for Verstärkung(80 seems to distort everything, use a slider to get to the limit)

One last question though: how do I improve latency?
Coordinator
Jan 31, 2013 at 5:32 AM
You can set the buffer size on WaveIn before you start recording. WaveIn doesn't support really low latency though, but you might find you can get down to around 50ms.
Your method of amplifying the sound will only work up to a point. It risks overflow as you are staying in 16 bit mode. Normally I would convert the output of the buffered wave provider to float. Then you can freely amplify as much as you like. Then, before converting back down to 16 bit to play or write to a WAV file, you clip any samples that go outside the +/- 1.0 range.

Mark
Jan 31, 2013 at 5:35 AM
Edited Jan 31, 2013 at 6:00 AM
What does that look like in code for my example?

Starting with wi.BufferMilliseconds = 50; I guess
Coordinator
Jan 31, 2013 at 3:37 PM
yes, that's right. You could use a Pcm16BitToSampleProvider to get to IEEE float, then use a VolumeSampleProvider to modify the volume, before finally using a SampleToWaveprovider16 to clip and go back to 16 bit

Mark