Subscribe to RSS feed - electronic brain surgery since 2001

Joining .WAVs with PHP

I'm currently workin' on a CAPTCHA plugin fer DokuWiki and thought about providin' audio output fer users not able t' see th' image. Oho! This is pretty simple fer CAPTCHAs – there is no need fer complicated speech synthesis because ye only need recordin's o' th' 26 possible letters. But ye need a way o' joinin' those recordin's on th' fly…

I looked aroun' th' web fer a way t' 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 were bein' overly complicated fer me simple task. I then stumbled about a very short forum thread which together with a simple format specification led t' th' followin' 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 o' .wav files and it will return a new joined file. Send it with a audio/x-wav header and ye're done.

What I need now are th' audio files. I found some on th' web but I can't figure out who owns them and what th' license is… If anyone can help me out, mail me or leave a comment.

php, wav, join, merge
Similar posts:
Posted on Wednesday, November the 15th 2006 (8 years ago).


blog comments powered by Disqus