title: Mixing 16 bit linear PCM streams and avoiding clipping/overflow
body: <p>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.</p>
  
  <p>I have following function ...</p>
  
  <pre><code>short int mix_sample(short int sample1, short int sample2)
  {
      return #mixing_algorithm#;
  }
  </code></pre>
  
  <p>... and here's what I have tried as #mixing_algorithm#</p>
  
  <pre><code>sample1/2 + sample2/2
  2*(sample1 + sample2) - 2*(sample1*sample2) - 65535
  (sample1 + sample2) - sample1*sample2
  (sample1 + sample2) - sample1*sample2 - 65535
  (sample1 + sample2) - ((sample1*sample2) &gt;&gt; 0x10) // same as divide by 65535
  </code></pre>
  
  <p>Some of them have produced better results than others but even the best result contained quite a lot of noise.</p>
  
  <p>Any ideas how to solve it?</p>