I'm trying to generate an .mp3 that can be used as a metronome (with accent/off-accent notes). I'm new to PHP streams as well as LAME. Using this documentation, I've taken a stab at it - but I don't think I'm using the streams correctly:
<?php
// mp3=/Bottle_120_4_4_16.mp3 (Bottle 4/4 time, 120bpm - 4 measures (16 beats))
$mp3 = strtolower(str_replace('/', '', $_GET['mp3']));
$m = explode('_', $mp3);
$sound = $m[0];//bottle
$BPM = $m[1];//120
$time_a = $m[2];//4
$time_b = $m[3];//4
$nBeats = $m[4];//16
header('Content-Type: audio/mpeg');
header('Content-Disposition:attachment;filename='.$mp3);
header('Pragma: no-cache');
$stream = fopen('php://output', 'a');
// GENERATE PCM
$sampleRate = 48000;
$channels = 1;//Mono
$bytePerSample = 2;
$bytesInSecond = ($sampleRate * $bytePerSample * $channels);
$beatLenInSec = (60 / $BPM);
if ($time_b == 8){
$beatLenInSec = ($beatLenInSec / 2);
}
$bytesInBeat = intval($beatLenInSec * $bytesInSecond);
$bytesInBeat = (intval($bytesInBeat / $bytePerSample) * $bytePerSample);
$accentFileName = 'sound/'.$sound.'/wav/'.$sound.'-accent_ws.wav';
$noteFileName = 'sound/'.$sound.'/wav/'.$sound.'-note_ws.wav';
$PCMDataPosition = 58;
$fileA = fopen($accentFileName, 'r');
$fileB = fopen($noteFileName, 'r');
$data = '';
for ($i = 0; $i < $nBeats; $i++) {
if (($i % $time_a) == 1){
fseek($fileA, $PCMDataPosition);
$data = fread($fileA, $bytesInBeat);
} else {
fseek($fileB, $PCMDataPosition);
$data = fread($fileB, $bytesInBeat);
}
fwrite($stream, $data);
}
$lame = fopen('php://output', 'a');
// LAME ENCODE
$path = '/home/html/';
system($path.'bin/lame -r -s 48.00 -m m -b 16 - - '.$stream.' '.$lame);
fclose($stream);
fclose($lame);
?>
Are two streams necessary for what I'm doing?