Convert 16 bit pcm to 8 bit

后端 未结 4 1901
青春惊慌失措
青春惊慌失措 2020-12-10 19:33

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?

<
4条回答
  •  盖世英雄少女心
    2020-12-10 19:52

    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.

提交回复
热议问题