How to adjust microphone sensitivity while recording audio in android

user3472378 picture user3472378 · Aug 22, 2014 · Viewed 22.5k times · Source

I'm working on a voice recording app. In it, I have a Seekbar to change the input voice gain. I couldn't find any way to adjust the input voice gain.

I am using the AudioRecord class to record voice.

 recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING, bufferSize);

    recorder.startRecording();

I've seen an app in the Google Play Store using this functionality.

Answer

Anton Savin picture Anton Savin · Aug 22, 2014

As I understand you don't want any automatic adjustments, only manual from the UI. There is no built-in functionality for this in Android, instead you have to modify your data manually.

Suppose you use read (short[] audioData, int offsetInShorts, int sizeInShorts) for reading the stream. So you should just do something like this:

float gain = getGain(); // taken from the UI control, perhaps in range from 0.0 to 2.0
int numRead = read(audioData, 0, SIZE);
if (numRead > 0) {
    for (int i = 0; i < numRead; ++i) {
        audioData[i] = (short)Math.min((int)(audioData[i] * gain), (int)Short.MAX_VALUE);
    }
}

Math.min is used to prevent overflow if gain is greater than 1.