Chichen Fu
Chichen Fu

Reputation: 13

Audio data byte array volume adjustment java

I am doing a volume control for my wifi speaker. I need to process the raw PCM data byte array to adjust volume. But my code give me lots of noise. follow is my code:

for (int i = 0; i < split.length; i+=2) {
short audioSample = (short) (((split[i+1] & 0xff) << 8) | (split[i] & 0xff));
audioSample = (short) (audioSample * 1 * equal.vol);
split[i] = (byte) audioSample;
split[i+1] = (byte) (audioSample >> 8);
}

split is raw data byte array

My audio profile: 22.05K sample rate, 16 bits per sample

Upvotes: 1

Views: 753

Answers (1)

Chuck Batson
Chuck Batson

Reputation: 2764

  1. Make sure that equal.vol is declared as a floating-point type such as float or double.
  2. Make sure your sample data is, in fact, little endian.
  3. Make sure that 0 <= equal.vol <= 1; that is, make sure you're only attenuating and not amplifying. If you want to amplify, you'll need to clamp the result of the multiplication to fit within the sample range, i.e., -32768 to +32767. But, your sound may experience clipping when amplified in such a manner.

Upvotes: 2

Related Questions