Algorithm to mix sound

后端 未结 20 2062
囚心锁ツ
囚心锁ツ 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:06

    I cannot believe that nobody knows the correct answer. Everyone is close enough but still, a pure philosophy. The nearest, i.e. the best was: (s1 + s2) -(s1 * s2). It's excelent approach, especially for MCUs.

    So, the algorithm goes:

    1. Find out the volume in which you want the output sound to be. It can be the average or maxima of one of the signals.
      factor = average(s1) You assume that both signals are already OK, not overflowing the 32767.0
    2. Normalize both signals with this factor:
      s1 = (s1/max(s1))*factor
      s2 = (s2/max(s2))*factor
    3. Add them together and normalize the result with the same factor
      output = ((s1+s2)/max(s1+s2))*factor

    Note that after the step 1. you don't really need to turn back to integers, you may work with floats in -1.0 to 1.0 interval and apply the return to integers at the end with the previously chosen power factor. I hope I didn't mistake now, cause I'm in a hurry.

提交回复
热议问题