Created
October 26, 2023 04:59
-
-
Save rktalusani/aee2b673c89d26a0535eb18bd1bb2aa2 to your computer and use it in GitHub Desktop.
Increase the volume of an audio buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public byte[] increaseVolume(byte[] audioBuffer) { | |
for (int i = 0; i < audioBuffer.length; i += 2) { | |
// Convert 16-bit little-endian PCM samples to a short | |
short sample = (short)((audioBuffer[i] & 0xFF) | (audioBuffer[i + 1] << 8)); | |
// Increase the volume by 10% | |
sample = (short)(sample * 1.1); | |
// Clip the value to stay within the 16-bit range | |
if (sample > Short.MAX_VALUE) { | |
sample = Short.MAX_VALUE; | |
} else if (sample < Short.MIN_VALUE) { | |
sample = Short.MIN_VALUE; | |
} | |
// Convert the short back to little-endian bytes | |
audioBuffer[i] = (byte)(sample & 0xFF); | |
audioBuffer[i + 1] = (byte)((sample >> 8) & 0xFF); | |
} | |
return audioBuffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment