Algorithm to mix sound

后端 未结 20 2050
囚心锁ツ
囚心锁ツ 2020-11-29 16:55

I have two raw sound streams that I need to add together. For the purposes of this question, we can assume they are the same bitrate and bit depth (say 16 bit sample, 44.1k

20条回答
  •  醉梦人生
    2020-11-29 17:32

    I'd prefer to comment on one of the two highly ranked replies but owing to my meager reputation (I assume) I cannot.

    The "ticked" answer: add together and clip is correct, but not if you want to avoid clipping.

    The answer with the link starts with a workable voodoo algorithm for two positive signals in [0,1] but then applies some very faulty algebra to derive a completely incorrect algorithm for signed values and 8-bit values. The algorithm also does not scale to three or more inputs (the product of the signals will go down while the sum increases).

    So - convert input signals to float, scale them to [0,1] (e.g. A signed 16-bit value would become
    float v = ( s + 32767.0 ) / 65536.0 (close enough...))
    and then sum them.

    To scale the input signals you should probably do some actual work rather than multiply by or subtract a voodoo value. I'd suggest keeping a running average volume and then if it starts to drift high (above 0.25 say) or low (below 0.01 say) start applying a scaling value based on the volume. This essentially becomes an automatic level implementation, and it scales with any number of inputs. Best of all, in most cases it won't mess with your signal at all.

提交回复
热议问题