将16位PCM到8位 [英] Convert 16 bit pcm to 8 bit
问题描述
我有存储在一个字节数组PCM音频。它是每个样本16位。我想让它每采样音频8位。
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?
推荐答案
我不明白现在为什么它是不够的,只是取上层字节,即舍弃低8位每个样品。
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.
作为评论建议的AShelly,它可能是圆一个不错的主意,即加1,如果我们丢弃的字节是其最大的一半高:
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);
针对0xFF的工具测试夹具,所以我们不要冒险加1至0xFF,并包裹了为0x00,这将是坏的。
The test against 0xff implements clamping, so we don't risk adding 1 to 0xff and wrapping that to 0x00 which would be bad.
这篇关于将16位PCM到8位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!