title: Drawing sound wave in php
body: <p>I developed a script in php to draw the sound wave of a wav file. Althoug it works well sounds long about 20 minutes take 2-3 minutes to draw the image. I am using the peak value of group of samples to represent the wave in certain pixel. Also I get long values i.e 4 bytes at same time to get left, right, both negative and positive values. Also one improvement is that i take 4 long values at same iteration. </p>
  
  <p>The wav is a-law stereo 16-bit but i use decoding algoritm to get pcm value so 16 bits go to 8 bit. </p>
  
  <p>8000Hz, 8 bits per sample, 64 Kbps,
  stereo, A-law</p>
  
  <p>I need to generate 1500 pixels wide image.</p>
  
  <p>It would be helpfull also some alghoritm or web page how to improve performance of the script. </p>
  
  <p>Audacity generates image of the same sound in about 2-3 seconds :)</p>
  
  <p>thanks. I will post some code if needed.</p>
  
  <p>Here is the for cycle for reading the blocks of sound data</p>
  
  <pre><code>for ($i = 0; $i &lt; $this-&gt;blocktotal / 4; $i+=4) 
      {
          // unpack 32 bit numbers from the sound data in groups of 4
          $blocks[] = @unpack('L', substr($this-&gt;data, $i * 4, 4));
          $blocks[] = @unpack('L', substr($this-&gt;data, ($i + 1) * 4, 4));
          $blocks[] = @unpack('L', substr($this-&gt;data, ($i + 2) * 4, 4));
          $blocks[] = @unpack('L', substr($this-&gt;data, ($i + 3) * 4, 4));
      }
  </code></pre>
  
  <p>32 bit = 4 * 8 bit, each bit is left right left right channel. So I take 2 values for left and two for right channel for one line of the above cycle. But i get that $this->blocktotal= 20147456 for 20 MB file so the above cycle will repeat about 5 milion times. Any idea for improvement?  I tried to read 8 Long values at same time, but that gives other image than expected, I dont know why.</p>