1

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

2
  • What is the type of equal.vol? Are you sure the data is in little endian format? Are you sure 0 <= equal.vol <= 1? Commented May 2, 2014 at 1:35
  • equal.vol is integer. its value from 0 to 9 Commented May 2, 2014 at 1:42

1 Answer 1

2
  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.
Sign up to request clarification or add additional context in comments.

1 Comment

Still has noise. really weird

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.