3
votes

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.

The wav is a-law stereo 16-bit but i use decoding algoritm to get pcm value so 16 bits go to 8 bit.

8000Hz, 8 bits per sample, 64 Kbps, stereo, A-law

I need to generate 1500 pixels wide image.

It would be helpfull also some alghoritm or web page how to improve performance of the script.

Audacity generates image of the same sound in about 2-3 seconds :)

thanks. I will post some code if needed.

Here is the for cycle for reading the blocks of sound data

for ($i = 0; $i < $this->blocktotal / 4; $i+=4) 
    {
        // unpack 32 bit numbers from the sound data in groups of 4
        $blocks[] = @unpack('L', substr($this->data, $i * 4, 4));
        $blocks[] = @unpack('L', substr($this->data, ($i + 1) * 4, 4));
        $blocks[] = @unpack('L', substr($this->data, ($i + 2) * 4, 4));
        $blocks[] = @unpack('L', substr($this->data, ($i + 3) * 4, 4));
    }

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.

1
solved the problem. When the file is big like 20 MB i dont read some bytes at all i.e i jump them because anyway i need aproximation. I take every 10-th long number from the sound data. For smaller file I take every secodnd long number. So this way I solved this. - Vlad
@Vlad, that makes a lot of sense - why not put that in as answer and accept your answer - that way it won't show up as unanswered. - Peter Bagnall
i'm exploring this too.. any idea where i can get the full code to read the wav file and draw the graph - supersan

1 Answers

0
votes

solved the problem. When the file is big like 20 MB i dont read some bytes at all i.e i jump them because anyway i need aproximation. I take every 10-th long number from the sound data. For smaller file I take every secodnd long number. So this way I solved this.