For my new website I have been working on for a while I want to generate an image of the waveform of all audio files uploaded on the server. A trip down google avenue quickly led me to this post on andrewfreiday.com. He describes his search for the very same and comes up with a solution using lame to convert the mp3 file to a wave file and a php script to generate the image. I’m going to use his approach and couldn’t resist rewriting the script.
You can download my script for generating a waveform. It is licensed under the Apache License v2.0 because the original script was (I usually don’t bother with a license).
Too fully get what I’m talking about you should really first read the post on andrewfreiday.com and look at the source code of both his and my script.
The original script gets the input via a POST request and uses eval() to call lame for the conversion. Since I do not want to use eval() my script should be used a a command-line tool and I’ll probably watch a directory or use the cron daemon to call a shell script which will call lame and my PHP script. It takes two parameters: the wave and png file (eg. php waveform.php file.wav file.phg). Same difference, and if you want the original approach it shouldn’t be hard to change it back. The actual waveform generating part consists of two parts: reading the wave file and drawing the image.
Reading the wave file is bases a post by “zvoneM” on DevShed. I’ve looked closely at the wave file specifications and the code that reads the header of the file but changed little. There are some checks on the format of the file, that’s all. I did completely rewrite how the actual is read.
The original code reads one value and then skips a bunch. This approach is not flexible enough and takes more time on large files. Since I am going to post two-hour DJ sets this needed to change. How much values need to be read depends on the width of the image generated. The image is also drawn within the same loop as opposed to looping over an array after reading the file.
The drawing part is mainly the original code, but to improve the quality of the image it is generated and then resized resulting in a more accurate and nicely antialiased image. Multiple values are read for one line to reduce the chance that the one value read is not the most representable for the block of audio it represents. This results in a more full image because less (or no) peaks are missing or to short. The image at the top of this post is generated with a zoom level of 10 and 100 values per line.

Hi,
I used your code to generate wave forms. I tried using 667 MB wav audio to convert into wave image. I am getting memory limit error. I set memory_limit as 512 mb in ini file.
Any help will be appreciated..
Thanks,
Palanisamy
The size of the wave file doesn’t matter much for the memory usage, it is mostly the image that is being created that takes up memory. My local memory limit is 128MB, and I was perfectly able to run it (even on large audio files).
I must admit I am no expert in how PHP manages its memory. You could try printing
memory_get_usage()to see where it goes wrong, and adjust the parameters (constants defined after the license) to get it working and see what does fit in the memory.Thanks for ur reply..let me check..
Hello, thanks for the script. I have question. How i can make transparent background.
Default is white. i want no background.L-)
Regards.
I’m just having a quick look at the code and hope this works. If not I need to actually dive into it (haven’t used this script for a while). Look for the line
define('BACKGROUND', '#FFFFFF'); // blank for transparent, have you tried defining the background as an empty string? That should work (unless there is some bug there).Bloody handy. Thanks!!