title: PCM Byte Array Addition
body: <p>I have a few raw PCM audio files. I can successfully read a stream of bytes from these files and play them through an audio playing mechanism which accepts PCM data as input.</p>
  
  <p>When i read data from these files, i store it in byte[]. These tracks have the same size and are complementary in terms of sound (they sound good together). Therefore, I want to add several byte[] containing PCM data into a single byte[] of the same size, representing the final music.</p>
  
  <p>I tried it in an easy thoughtless manner by simply doing it like this:</p>
  
  <pre><code>for(int i=0; i&lt;finalbytes.length; i++)
  {
     finalbytes[i] = (byte) (music1bytes[i] + music2bytes[i]);
  }
  </code></pre>
  
  <p>It actually wasn't that bad. The final sound is indeed an addition of both tracks. The problem is, when a few tracks are added, sometimes in specific parts of the song, peaks of static noise can be heard. It is probably due to the addition resulting in non-clamped values or something, which I don't know how to solve.</p>
  
  <p>So, how to add two or more byte arrays of PCM data?</p>