Reputation: 13
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
Reputation: 2764
equal.vol
is declared as a floating-point type such as float
or double
.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