Mixing 16 bit linear PCM streams and avoiding clipping/overflow

前端 未结 6 541
逝去的感伤
逝去的感伤 2021-01-31 12:45

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.

6条回答
  •  清酒与你
    2021-01-31 13:13

    here's a descriptive implementation:

    short int mix_sample(short int sample1, short int sample2) {
        const int32_t result(static_cast(sample1) + static_cast(sample2));
        typedef std::numeric_limits 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!

    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.

提交回复
热议问题