Skip to content

Instantly share code, notes, and snippets.

@rktalusani
Created October 26, 2023 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rktalusani/aee2b673c89d26a0535eb18bd1bb2aa2 to your computer and use it in GitHub Desktop.
Save rktalusani/aee2b673c89d26a0535eb18bd1bb2aa2 to your computer and use it in GitHub Desktop.
Increase the volume of an audio buffer
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