简体   繁体   中英

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

  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, ie, -32768 to +32767. But, your sound may experience clipping when amplified in such a manner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM