gpt4 book ai didi

java - 从音频 PCM 16 位到 8 位 - 有噪音

转载 作者:太空狗 更新时间:2023-10-29 16:20:08 24 4
gpt4 key购买 nike

我实现了以下算法将 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 是 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];
}

}

输入/输出看起来像:

 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

丢弃第一个或第二个字节并不重要,噪声仍然存在。在这里我丢弃了第一个字节(而不是第二个字节)

最佳答案

以下算法大大减少了噪声量,但不能完全消除:

if(encoding == AudioFormat.ENCODING_PCM_8BIT){  
ShortBuffer intBuf = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
short[] samples16Bit = new short[intBuf.remaining()];
intBuf.get(samples16Bit);
data1 = new byte[samples16Bit.length];
for (int i = 0; i < samples16Bit.length; i++) {
data1[i] = (byte)((samples16Bit[i] / 256)+128);
}
}

关于java - 从音频 PCM 16 位到 8 位 - 有噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com