Joining .WAVs with PHP
I'm currently working on a CAPTCHA plugin for DokuWiki and thought about providing audio output for users not able to see the image. This is pretty simple for CAPTCHAs – there is no need for complicated speech synthesis because you only need recordings of the 26 possible letters. But you need a way of joining those recordings on the fly…
I looked around the web for a way to concatenate multiple wave files with PHP and found an article by Phillip Perkins called Create an audio stitching tool in PHP. But at a closer look his example code was overly complicated for my simple task. I then stumbled about a very short forum thread which together with a simple format specification led to the following function:
function joinwavs($wavs){ $fields = join('/',array( 'H8ChunkID', 'VChunkSize', 'H8Format', 'H8Subchunk1ID', 'VSubchunk1Size', 'vAudioFormat', 'vNumChannels', 'VSampleRate', 'VByteRate', 'vBlockAlign', 'vBitsPerSample' )); $data = ''; foreach($wavs as $wav){ $fp = fopen($wav,'rb'); $header = fread($fp,36); $info = unpack($fields,$header); // read optional extra stuff if($info['Subchunk1Size'] > 16){ $header .= fread($fp,($info['Subchunk1Size']-16)); } // read SubChunk2ID $header .= fread($fp,4); // read Subchunk2Size $size = unpack('vsize',fread($fp, 4)); $size = $size['size']; // read data $data .= fread($fp,$size); } return $header.pack('V',strlen($data)).$data; }
Just fill it with a bunch of .wav files and it will return a new joined file. Send it with a audio/x-wav
header and you're done.
What I need now are the audio files. I found some on the web but I can't figure out who owns them and what the license is… If anyone can help me out, mail me or leave a comment.