我实现了以下算法将 PCM 16 位音频数据转换为 8 位:
if(encoding == AudioFormat.ENCODING_PCM_8BIT){
int len = data.length;
data1 = new byte[len/2];
int tempint;
for (int k = 0, i=1; i < len; i+=2, k++) {
tempint = ((int)data[i]) ^ 0x00000080;
data1[k] = (byte)tempint;
}
data=null;
}
哪里data is byte[]
。运行此代码后,输出contains a lot of noise
并建议我在这里做错了。What should I do besides dropping the lower byte?
[编辑]:修改代码:
if(encoding == AudioFormat.ENCODING_PCM_8BIT){
int len = data.length;
data1 = new byte[len/2];
for (int i = 0; i < len/2; i++) {
data1[i] = data[i*2+1];
}
}
the input/output looks like:
Original data(counter:0) = 4
Original data(counter:1) = -1
Original data(counter:2) = 75
Original data(counter:3) = -1
Original data(counter:4) = 16
Original data(counter:5) = -1
Original data(counter:6) = 44
Original data(counter:7) = -1
Original data(counter:8) = 7
Original data(counter:9) = -1
Original data(counter:10) = 22
Original data(counter:11) = -1
Original data(counter:12) = 22
Original data(counter:13) = -1
Original data(counter:14) = 12
Original data(counter:15) = -1
Output data:(counter:0) = -1
Output data:(counter:1) = -1
Output data:(counter:2) = -1
Output data:(counter:3) = -1
Output data:(counter:4) = -1
Output data:(counter:5) = -1
Output data:(counter:6) = -1
Output data:(counter:7) = -1
Output data:(counter:8) = -1
Output data:(counter:9) = -1
Output data:(counter:10) = -1
Output data:(counter:11) = -1
Output data:(counter:12) = -1
Output data:(counter:13) = -1
Output data:(counter:14) = -1
Output data:(counter:15) = -1
不管我丢掉第一个字节还是第二个字节,噪音仍然存在。在这里我删除了第一个字节(而不是第二个)