将 16 位 pcm 转换为 8 位 [英] Convert 16 bit pcm to 8 bit

查看:50
本文介绍了将 16 位 pcm 转换为 8 位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆