我正在尝试将我的音频输入的低音提升到 12 dB。这是代码
public class BassBoost {
double xn1, xn2, yn1, yn2;
double omega, sn, cs, a, shape, beta, b0, b1, b2, a0, a1, a2;
public BassBoost(int frequency, int dB_boost, int sampleRate) {
xn1 = 0;
xn2 = 0;
yn1 = 0;
yn2 = 0;
omega = 2 * Utils.pi * frequency / sampleRate;
sn = Math.sin(omega);
cs = Math.cos(omega);
a = Math.exp(Math.log(10.0) * dB_boost / 40);
shape = 1.0;
beta = Math.sqrt((a * a + 1) / shape - (Math.pow((a - 1), 2)));
/* Coefficients */
b0 = a * ((a + 1) - (a - 1) * cs + beta * sn);
b1 = 2 * a * ((a - 1) - (a + 1) * cs);
b2 = a * ((a + 1) - (a - 1) * cs - beta * sn);
a0 = ((a + 1) + (a - 1) * cs + beta * sn);
a1 = -2 * ((a - 1) + (a + 1) * cs);
a2 = (a + 1) + (a - 1) * cs - beta * sn;
}
public void process(double[] buffer, int length) {
double out, in = 0;
for (int i = 0; i < length; i++) {
in = buffer[i];
out = (b0 * in + b1 * xn1 + b2 * xn2 - a1 * yn1 - a2 * yn2) / a0;
xn2 = xn1;
xn1 = in;
yn2 = yn1;
yn1 = out;
if (out < -1.0)
out = -1.0;
else if (out > 1.0)
out = 1.0; // Prevents clipping
buffer[i] = out;
}
}
}
这是调用该过程的伪代码
音频被记录并存储在短缓冲区中。缓冲区分成小数组并复制到双数组进行过滤处理。如果我不划分,我会在 Android 手机上尝试此操作时获得 OutofMemory。应用过滤(LPF,HPF) 输出双缓冲区在写入字节数组之前使用以下代码进行放大。
void doOutput(int outlen, boolean maxGain) {
int qi;
int i, i2;
while (true) {
int max = 0;
i = outbp;
for (i2 = 0; i2 < outlen; i2 += 4) {
qi = (int) (leftBuffer[i] * outputGain);
if (qi > max)
max = qi;
if (qi < -max)
max = -qi;
ob[i2 + 1] = (byte) (qi >> 8);
ob[i2] = (byte) qi;
i = (i + 1) & fbufmask;
}
i = outbp;
for (i2 = 2; i2 < outlen; i2 += 4) {
qi = (int) (rightBuffer[i] * outputGain);
if (qi > max)
max = qi;
if (qi < -max)
max = -qi;
ob[i2 + 1] = (byte) (qi >> 8);
ob[i2] = (byte) qi;
i = (i + 1) & fbufmask;
}
// if we're getting overflow, adjust the gain
if (max > 32767) {
outputGain *= 30000. / max;
if (outputGain < 1e-8 || Double.isInfinite(outputGain)) {
unstable = true;
break;
}
continue;
} else if (maxGain && max < 24000) {
if (max == 0) {
if (outputGain == 1)
break;
outputGain = 1;
} else
outputGain *= 30000. / max;
continue;
}
break;
}
if (unstable)
return;
outbp = i;
out.write(ob, 0, outlen);
}
此代码适用于 android,因此它必须具有内存效率。我尝试使用 Arraylist ,但这也导致内存不足。
问题是要通过缓冲区应用一致的输出增益,我目前正在应用两次过滤并写入一次 byteOutputStream。在应用单个 outputGain 之前,我尝试使用 Arraylist 来存储输出缓冲区,但这会导致内存不足来存储整个 Double 缓冲区(1 分钟记录)。同样对于 BassBoost,我想将其应用于整个输出。是否有内存有效的方法来做到这一点。使用 ArrayList 是没有问题的。为了获得一致的输出增益,我必须遍历整个输出缓冲区并使用单个输出增益应用它。
想让上面的示例代码高效,这样我就不必通过缓冲区两次。如果我将低音增强应用到块上,也会让我在连续的声音中中断。
谢谢