Skip to content

Instantly share code, notes, and snippets.

@makotom
Created June 23, 2012 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makotom/2977824 to your computer and use it in GitHub Desktop.
Save makotom/2977824 to your computer and use it in GitHub Desktop.
Converts CD-DA clone image by readom to CUE sheet music
<?php
if(empty($argv[1]) || empty($argv[2])){
exit();
}
function put_binary($data){
global $fh_b;
fwrite($fh_b, $data);
return;
}
function put_cue($line){
global $fh_c;
fwrite($fh_c, "{$line}\n");
return;
}
function get_timestr_from_numframe($frame){
$time = array(0, 0, 0);
$time[2] = $frame % 75;
$frame = ($frame - $time[2]) / 75;
$time[1] = $frame % 60;
$time[0] = ($frame - $time[1]) / 60;
return sprintf("%02d:%02d:%02d", $time[0], $time[1], $time[2]);
}
if(!($fh_s = fopen($argv[1], "r"))){
exit("Could not open the source file. Exit.\n");
}
$cue_name = preg_replace("/\.wav$/i", "", $argv[2]) . ".cue";
if(file_exists($argv[2]) || file_exists($cue_name)){
exit("Output file exists. Exit.\n");
}
if(!($fh_b = fopen($argv[2], "w")) || !($fh_c = fopen($cue_name, "w"))){
exit("Could not open some of the output files. Exit.\n");
}
$datasize = filesize($argv[1]) * 0x930 / (0x930 + 0x60);
$chunksize = 4 + 8 + 16 + 8 + $datasize;
put_binary("RIFF" . pack("V", $chunksize) . "WAVE");
put_binary("fmt " . pack("VvvVVvv", 16, 1, 2, 44100, 16 * 44100 * 2 / 8, 16 * 2 / 8, 16));
put_binary("data" . pack("V", $datasize));
put_cue("FILE \"" . basename($argv[2]) . "\" WAVE");
put_cue(" TRACK 01 AUDIO");
put_cue(" INDEX 01 00:00:00");
$f = 0;
$t = 2;
$cue_tmp = array();
$flag = FALSE;
while($body = fread($fh_s, 0x930)){
$head = bin2hex(substr(fread($fh_s, 0x60), 0, 6));
put_binary($body);
if($f === 0){
$f += 1;
continue;
}
if($flag === FALSE && $head === "808080808080"){
$flag = TRUE;
$cue_tmp[] = sprintf(" TRACK %02d AUDIO", $t);
$cue_tmp[] = " INDEX 00 " . get_timestr_from_numframe($f);
}else if($flag === TRUE && $head !== "808080808080"){
$flag = FALSE;
$cue_tmp[] = " INDEX 01 " . get_timestr_from_numframe($f);
put_cue(implode("\n", $cue_tmp));
$cue_tmp = array();
$t += 1;
}
$f += 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment