3

I have an audio streaming app, I'm trying to increase the input gain of the microphone.

I've already checked out these links:

how-to-adjust-microphone-sensitivity-while-recording-audio-in-android

how-to-adjust-microphone-volume-in-android-programmatically

here what I've tried to achieve:

float gain = 5f; // this could be from 1 to 10

int length = mAudioRecord.read(buffer, bufferSize);

byte[] bytesArray = new byte[buffer.capacity()];
buffer.get(bytesArray, 0, bufferSize); // get audio samples

buffer.clear(); // clear the buffer, to be filled later with the new bytes with gain

for (int i = 0; i < bytesArray.length; i+=2) {
            // convert byte pair to int
            short buf1 = bytesArray[i+1];
            short buf2 = bytesArray[i];

            buf1 = (short) ((buf1 & 0xff) << 8);
            buf2 = (short) (buf2 & 0xff);

            short res= (short) (buf1 | buf2);

            res = (short) (res * gain); // increase the volume

            if (res>32767) {res = 32767;}
            if (res<-32768) {res = -32768;}

            buffer.putShort(res);
}

if (length == AudioRecord.ERROR_INVALID_OPERATION || length == AudioRecord.ERROR_BAD_VALUE) {
 
 Log.e(TAG, "An error occurred with the AudioRecord");

} else {

 mMediaCodec.queueInputBuffer(bufferIndex, 0, length, System.nanoTime() / 1000, 0);

}

The above code is working fine, the volume is increased, but there is a lot of static noise while increasing the gain.

How can I keep the audio clear without having these static noises ?

What have I done wrong with the above code ?

Walid
  • 700
  • 2
  • 10
  • 29
  • 1
    What you are doing is multiplying the amplitude of the signal, so even noise gets amplified. In order to remove noise, I think you can find heuristic approaches to do it. For a rudimentary approach, you could add a check before amplifying to only amplify signals between certain dB based on your requirement. Or use FFT and clip certain frequencies as well. – AagitoEx Jul 17 '22 at 11:28

0 Answers0