Combine/merge/mix audio files using PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



While already have the code/script needed to just combine .wav files into a single output file/track, it concatenates them, end-to-end:

//function to handle joining/merging
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;
}//end of joinWavs function

//and here's code to pass an array of file names to the above function, and spit out resulting file
$outgoing = joinWavs($arFiles);
header("Content-Type: audio/x-wav");
header("Content-Disposition: attachment; filename=dynamic.wav");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . strlen( $outgoing));
echo $outgoing;

Do also have some other code to handle something similar with .MP3 files, but, it's a bit longer/too big to post in a message here, but, either way, issue is would want to specifically mix/combine multiple files into 1 output stream/file, but would need/want to set possibly overlapping start times for the input chunks/tracks in the output track, if that makes sense?

Now, aside from trying to do this with something like a call to a command line command on different operating systems - that idea seems to get mentioned quite a bit on the 'net, am just thinking that something like this should be relatively doable/possible using binary streams/data from the source files, etc. - or am I wrong here?

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'

[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux