java pcm16位,将16位PCM到8位

博客讨论了如何将16位的脉冲编码调制(PCM)音频数据转换为8位。作者提出简单的方法是丢弃每个样本的低8位,但建议如果原始样本非线性,可能需要在丢弃位之前进行线性化处理。还提到了一种优化方案,即在丢弃高位之前根据低位是否大于一半最大值来决定是否对高位进行四舍五入。
摘要由CSDN通过智能技术生成

I have pcm audio stored in a byte array. It is 16 bits per sample. I want to make it 8 bit per sample audio.

Can anyone suggest a good algorithm to do that?

I haven't mentioned the bitrate because I think it isn't important for the algorithm - right?

解决方案

I can't see right now why it's not enough to just take the upper byte, i.e. discard the lower 8 bits of each sample.

That of course assumes that the samples are linear; if they're not then maybe you need to do something to linearize them before dropping bits.

short sixteenBit = 0xfeed;

byte eightBit = sixteenBit >> 8;

// eightBit is now 0xfe.

As suggested by AShelly in a comment, it might be a good idea to round, i.e. add 1 if the byte we're discarding is higher than half its maximum:

eightBit += eightBit < 0xff && ((sixteenBit & 0xff) > 0x80);

The test against 0xff implements clamping, so we don't risk adding 1 to 0xff and wrapping that to 0x00 which would be bad.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值