混合16位线性PCM流和避免削波/溢出 [英] Mixing 16 bit linear PCM streams and avoiding clipping/overflow
问题描述
我试图混合2 16bit的线性PCM音频流在一起,我似乎无法克服噪声问题。我想他们是从溢流样品混合在一起的时候来了。
I've trying to mix together 2 16bit linear PCM audio streams and I can't seem to overcome the noise issues. I think they are coming from overflow when mixing samples together.
我有以下功能...
short int mix_sample(short int sample1, short int sample2)
{
return #mixing_algorithm#;
}
...这里就是我试图为#mixing_algorithm#
... and here's what I have tried as #mixing_algorithm#
sample1/2 + sample2/2
2*(sample1 + sample2) - 2*(sample1*sample2) - 65535
(sample1 + sample2) - sample1*sample2
(sample1 + sample2) - sample1*sample2 - 65535
(sample1 + sample2) - ((sample1*sample2) >> 0x10) // same as divide by 65535
他们中的一些已经产生比别人更好的结果,但即使是最好的结果,包含了不少噪音。
Some of them have produced better results than others but even the best result contained quite a lot of noise.
任何想法如何解决呢?
推荐答案
下面是一个描述性的实现:
here's a descriptive implementation:
short int mix_sample(short int sample1, short int sample2) {
const int32_t result(static_cast<int32_t>(sample1) + static_cast<int32_t>(sample2));
typedef std::numeric_limits<short int> Range;
if (Range::max() < result)
return Range::max();
else if (Range::min() > result)
return Range::min();
else
return result;
}
混合,它只是添加和剪辑!
to mix, it's just add and clip!
要避免削波伪像,你将要使用的饱和或限制器。理想情况下,你将有一个小的 int32_t
与前瞻少量的缓冲。这将引入的延迟。
to avoid clipping artifacts, you will want to use saturation or a limiter. ideally, you will have a small int32_t
buffer with a small amount of lookahead. this will introduce latency.
不是限制到处多见,是在你的信号留下几个比特的价值的净空。
more common than limiting everywhere, is to leave a few bits' worth of 'headroom' in your signal.
这篇关于混合16位线性PCM流和避免削波/溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!