简体   繁体   English

音频数据字节数组音量调整Java

[英]Audio data byte array volume adjustment java

I am doing a volume control for my wifi speaker. 我正在为我的wifi扬声器进行音量控制。 I need to process the raw PCM data byte array to adjust volume. 我需要处理原始PCM数据字节数组以调整音量。 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 split是原始数据字节数组

My audio profile: 22.05K sample rate, 16 bits per sample 我的音频配置文件:22.05K采样率,每个采样16位

  1. Make sure that equal.vol is declared as a floating-point type such as float or double . 确保将equal.vol声明为浮点类型,例如floatdouble
  2. Make sure your sample data is, in fact, little endian. 确保您的样本数据实际上是小端的。
  3. Make sure that 0 <= equal.vol <= 1; 确保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. 如果要放大,则需要钳制乘法结果以适合样本范围,即-32768至+32767。 But, your sound may experience clipping when amplified in such a manner. 但是,以这种方式放大时,声音可能会出现削波。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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