我遇到一些问题,需要将存储在字节数组中的音频数据转换为大端短整型数组、编码,然后再将它转换回字节数组。这是我的代码。原始音频数据存储在 audioBytes2 中。我使用相同的解码格式来进行解码,只是 cos 函数上带有一个负号。不幸的是,改变字节和短整型数据类型是不可避免的。
short[] audioData = null;
int nlengthInSamples = audioBytes2.length / 2;
audioData = new short[nlengthInSamples];
for (int i = 0; i < nlengthInSamples; i++) {
short MSB = (short) audioBytes2[2*i+1];
short LSB = (short) audioBytes2[2*i];
audioData[i] = (short) (MSB << 8 | (255 & LSB));
}
int i = 0;
while (i < audioData.length) {
audioData[i] = (short)(audioData[i] + (short)5*Math.cos(2*Math.PI*i/(((Number)EncodeBox.getValue()).intValue())));
i++;
}
short x = 0;
i = 0;
while (i < audioData.length) {
x = audioData[i];
audioBytes2[2*i+1] = (byte)(x >>> 0);
audioBytes2[2*i] = (byte)(x >>> 8);
i++;
}
我已经尽我所能来使它工作,但最接近的情况是在每隔一个编码/解码时让它工作,我不知道为什么。谢谢任何帮助。
java.nio.ByteBuffer
来处理这种转换。 - LiuYan 刘研