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 ?