Compare commits

...

3 Commits

4 changed files with 141 additions and 91 deletions

View File

@ -6,40 +6,40 @@
//create the midi file //create the midi file
$filename = "a2600song_".date("Y-m-d_His").".mid"; //unique filename $filename = "a2600song_".date("Y-m-d_His").".mid"; //unique filename
$filehandle = fopen("songs/".$filename,"wb"); //create the file, and open for writing $filehandle = fopen("songs/".$filename,"wb"); //create the file, and open for writing
//----------------------------------------------// //----------------------------------------------//
$c1 = explode("\n",$_POST['c1_display']); $c1 = explode("\n",$_POST['c1_display']);
$c2 = explode("\n",$_POST['c2_display']); $c2 = explode("\n",$_POST['c2_display']);
$c1_data = array(); $c1_data = array();
$c2_data = array(); $c2_data = array();
for ($i = 0; $i < count($c1); $i++) //the -1 accounts for the last line, which just consists of a newline for ($i = 0; $i < count($c1); $i++) //the -1 accounts for the last line, which just consists of a newline
if (str_word_count($c1[$i],0,"#.0123456789") == 4) if (str_word_count($c1[$i],0,"#.0123456789") == 4)
array_push($c1_data,explode(" ",$c1[$i])); array_push($c1_data,explode(" ",$c1[$i]));
for ($i = 0; $i < count($c2); $i++) //the -1 accounts for the last line, which just consists of a newline for ($i = 0; $i < count($c2); $i++) //the -1 accounts for the last line, which just consists of a newline
if (str_word_count($c2[$i],0,"#.0123456789") == 4) if (str_word_count($c2[$i],0,"#.0123456789") == 4)
array_push($c2_data,explode(" ",$c2[$i])); array_push($c2_data,explode(" ",$c2[$i]));
$track_arr = array( $track_arr = array(
1 => array(0=>false,1=>false,2=>false,3=>false), 1 => array(0=>false,1=>false,2=>false,3=>false),
2 => array(0=>false,1=>false,2=>false,3=>false)); 2 => array(0=>false,1=>false,2=>false,3=>false));
foreach ($c1_data as $i => $arr) foreach ($c1_data as $i => $arr)
if ($arr[1] != "x") if ($arr[1] != "x")
$track_arr[1][$arr[1]] = true; $track_arr[1][$arr[1]] = true;
foreach ($c2_data as $i => $arr) foreach ($c2_data as $i => $arr)
if ($arr[1] != "x") if ($arr[1] != "x")
$track_arr[2][$arr[1]] = true; $track_arr[2][$arr[1]] = true;
$numtracks = 1; $numtracks = 1;
foreach ($track_arr as $var => $arr) foreach ($track_arr as $var => $arr)
foreach ($arr as $val) foreach ($arr as $val)
if ($val) if ($val)
$numtracks++; $numtracks++;
//----------------------------------------------// //----------------------------------------------//
//HEADER //HEADER
fwrite($filehandle,pack("I",77),1); fwrite($filehandle,pack("I",77),1);
fwrite($filehandle,pack("I",84),1); fwrite($filehandle,pack("I",84),1);
@ -47,52 +47,52 @@
fwrite($filehandle,pack("I",100),1); fwrite($filehandle,pack("I",100),1);
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
//Size of header //Size of header
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",6),1); fwrite($filehandle,pack("I",6),1);
//MIDI file format //MIDI file format
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",1),1); fwrite($filehandle,pack("I",1),1);
//# of tracks //# of tracks
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",$numtracks),1); //one for each voice for each channel, plus the default one to declare time signatures, etc. fwrite($filehandle,pack("I",$numtracks),1); //one for each voice for each channel, plus the default one to declare time signatures, etc.
//Time division //Time division
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",240),1); fwrite($filehandle,pack("I",240),1);
//END HEADER //END HEADER
//TRACK 1 DATA //TRACK 1 DATA
//Header //Header
fwrite($filehandle,pack("I",77),1); fwrite($filehandle,pack("I",77),1);
fwrite($filehandle,pack("I",84),1); fwrite($filehandle,pack("I",84),1);
fwrite($filehandle,pack("I",114),1); fwrite($filehandle,pack("I",114),1);
fwrite($filehandle,pack("I",107),1); fwrite($filehandle,pack("I",107),1);
//# of bytes in track data //# of bytes in track data
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",19),1); fwrite($filehandle,pack("I",19),1);
//Tempo //Tempo
$ms = round(60000000/$_POST['tempo']); $ms = round(60000000/$_POST['tempo']);
$ms = str_pad(dechex($ms),6,"0",STR_PAD_LEFT); $ms = str_pad(dechex($ms),6,"0",STR_PAD_LEFT);
$byte1 = hexdec(substr($ms,0,2)); $byte1 = hexdec(substr($ms,0,2));
$byte2 = hexdec(substr($ms,2,2)); $byte2 = hexdec(substr($ms,2,2));
$byte3 = hexdec(substr($ms,4,2)); $byte3 = hexdec(substr($ms,4,2));
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",255),1); fwrite($filehandle,pack("I",255),1);
fwrite($filehandle,pack("I",81),1); fwrite($filehandle,pack("I",81),1);
fwrite($filehandle,pack("I",3),1); fwrite($filehandle,pack("I",3),1);
fwrite($filehandle,pack("I",$byte1),1); //microseconds per beat (variable, depending on tempo) fwrite($filehandle,pack("I",$byte1),1); //microseconds per beat (variable, depending on tempo)
fwrite($filehandle,pack("I",$byte2),1); //microseconds per beat fwrite($filehandle,pack("I",$byte2),1); //microseconds per beat
fwrite($filehandle,pack("I",$byte3),1); //microseconds per beat fwrite($filehandle,pack("I",$byte3),1); //microseconds per beat
//Time signature //Time signature
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",255),1); fwrite($filehandle,pack("I",255),1);
@ -102,21 +102,21 @@
fwrite($filehandle,pack("I",log($_POST['timesig_de'],2)),1); //denominator^2 (variable) fwrite($filehandle,pack("I",log($_POST['timesig_de'],2)),1); //denominator^2 (variable)
fwrite($filehandle,pack("I",24),1); //Metronome fwrite($filehandle,pack("I",24),1); //Metronome
fwrite($filehandle,pack("I",8),1); //32nd notes per beat fwrite($filehandle,pack("I",8),1); //32nd notes per beat
//End track //End track
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",255),1); fwrite($filehandle,pack("I",255),1);
fwrite($filehandle,pack("I",47),1); fwrite($filehandle,pack("I",47),1);
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
//END TRACK 1 DATA //END TRACK 1 DATA
/* Array Format /* Array Format
* $c1_data[note][0] = note name * $c1_data[note][0] = note name
* $c1_data[note][1] = distortion # * $c1_data[note][1] = distortion #
* $c1_data[note][2] = atari note index * $c1_data[note][2] = atari note index
* $c1_data[note][3] = note value (quarter, eighth, etc.) * $c1_data[note][3] = note value (quarter, eighth, etc.)
*/ */
if ($track_arr[1][0]) require("square_1.php"); if ($track_arr[1][0]) require("square_1.php");
if ($track_arr[1][1]) require("lead_1.php"); if ($track_arr[1][1]) require("lead_1.php");
if ($track_arr[1][2]) require("saw_1.php"); if ($track_arr[1][2]) require("saw_1.php");
@ -125,7 +125,7 @@
if ($track_arr[2][1]) require("lead_2.php"); if ($track_arr[2][1]) require("lead_2.php");
if ($track_arr[2][2]) require("saw_2.php"); if ($track_arr[2][2]) require("saw_2.php");
if ($track_arr[2][3]) require("bass_2.php"); if ($track_arr[2][3]) require("bass_2.php");
fclose($filehandle); fclose($filehandle);
header("Content-type: audio/midi"); header("Content-type: audio/midi");
header("Content-Disposition: attachment; filename=$filename"); header("Content-Disposition: attachment; filename=$filename");
@ -135,5 +135,7 @@
{ {
echo "<script>alert('not enough data! argh!');</script>"; echo "<script>alert('not enough data! argh!');</script>";
} }
die();
} }
?> ?>

View File

@ -104,42 +104,38 @@ function validateForm()
function toggleColor() function toggleColor()
{ {
showColor = !showColor; showColor = !showColor;
var x = document.getElementsByName('key'); var keys = document.getElementsByName('key');
const distMap = {
square: 0,
lead: 1,
saw: 2,
bass: 3,
};
if (showColor) if (showColor)
{ {
for (i = 0; i < x.length; i++) for (let i = 0; i < keys.length; i++)
{ {
var dist = x[i].id.substring(0,x[i].id.indexOf("_")); const [ dist, atariIndex ] = keys[i].id.split('_');
var atariIndex = x[i].id.substring(x[i].id.indexOf("_")+1); const distValue = distMap[dist];
if (atariIndex != "nil") const noteData = typeof distValue === 'number' && noteArray[distValue] && noteArray[distValue][atariIndex];
{ if (noteData) {
if (dist == "square") dist = 0; console.log(noteData[8]);
else if (dist == "lead") dist = 1; keys[i].style.backgroundColor = noteData[8];
else if (dist == "saw") dist = 2;
else if (dist == "bass") dist = 3;
x[i].style.backgroundColor = noteArray[dist][atariIndex][8];
} }
} }
} }
else else
{ {
for (i = 0; i < x.length; i++) for (let i = 0; i < keys.length; i++)
{ {
var dist = x[i].id.substring(0,x[i].id.indexOf("_")); const [dist, atariIndex] = keys[i].id.split('_');
var atariIndex = x[i].id.substring(x[i].id.indexOf("_")+1); const distValue = distMap[dist];
if (atariIndex != "nil") if (typeof distValue === 'number' && atariIndex !== 'nil') {
{ keys[i].style.backgroundColor = keys[i].classList.contains('wkey') ? '#FFFFFF' : '#000000';
if (dist == "square") dist = 0;
else if (dist == "lead") dist = 1;
else if (dist == "saw") dist = 2;
else if (dist == "bass") dist = 3;
if (x[i].className.match("wkey")) x[i].style.backgroundColor = "#FFFFFF";
else x[i].style.backgroundColor = "#000000";
} }
} }
} }
@ -185,22 +181,24 @@ function showFloatingDiv(keyID,e)
function exchange() function exchange()
{ {
var dist = activeKeyID.substring(0,activeKeyID.indexOf("_")); var distName = activeKeyID.substring(0,activeKeyID.indexOf("_"));
var atariIndex = activeKeyID.substring(activeKeyID.indexOf("_")+1); var atariIndex = activeKeyID.substring(activeKeyID.indexOf("_")+1);
let dist = null;
if (dist == "square") dist = 0; if (distName == "square") dist = 0;
else if (dist == "lead") dist = 1; else if (distName == "lead") dist = 1;
else if (dist == "saw") dist = 2; else if (distName == "saw") dist = 2;
else if (dist == "bass") dist = 3; else if (distName == "bass") dist = 3;
else return;
for (j = 0; j < 32; j++) for (j = 0; j < 32; j++) {
if (j != atariIndex && noteArray[dist][atariIndex][1] == noteArray[dist][j][1]) if (j != atariIndex && noteArray[dist][atariIndex][1] == noteArray[dist][j][1]) {
{ document.getElementById(activeKeyID).id = distName + "_" + j;
document.getElementById(activeKeyID).id = dist+"_"+j; activeKeyID = distName + "_" + j;
activeKeyID = dist+"_"+j;
atariIndex = j; atariIndex = j;
break; break;
} }
}
//update floating div //update floating div
var x = document.getElementById('floatDiv'); var x = document.getElementById('floatDiv');

View File

@ -70,26 +70,65 @@
while (($file = readdir($dir)) != null) while (($file = readdir($dir)) != null)
{ {
//format: distortion_atari note index_note name_octave_freq_error_MIDI note index_pitch bend left bits_pitch bend right bits // $note_array[dist][x]:
// [0] audio file name
// [1] note name w/ octave
// [2] octave
// [3] frequency
// [4] error (cents)
// [5] MIDI note (0-255)
// [6] pitch bend left bits
// [7] pitch bend right bits
// [8] color
if (strpos($file,".ogg")) if (strpos($file,".ogg"))
{ {
$info = explode("_",$file); $info = explode("_",$file);
$info[2] = str_replace("+","#",$info[2]); $info[2] = str_replace("+","#",$info[2]);
$info[count($info)-1] = substr($info[count($info)-1],0,strpos($info[count($info)-1],".ogg")); $info[count($info)-1] = substr($info[count($info)-1],0,strpos($info[count($info)-1],".ogg"));
// print_r($info);
if (!isset($note_array[$info[0]][$info[1]])) $dist = $info[0];
$note_array[$info[0]][$info[1]] = array(); $noteIndex = $info[1];
array_push($note_array[$info[0]][$info[1]],$file); // [0] distortion name
for ($i = 2; $i < count($info); $i++) // [1] note index
array_push($note_array[$info[0]][$info[1]],$info[$i]); // [2] note name w/ octave
// [3] octave
// [4] frequency
// [5] error (cents)
// [6] MIDI note (0-255)
// [7] pitch bend left bits
// [8] pitch bend right bits
if (!isset($note_array[$dist][$noteIndex]))
$note_array[$dist][$noteIndex] = array();
array_push($note_array[$dist][$noteIndex],$file);
for ($i = 2; $i < count($info); $i++) {
array_push($note_array[$dist][$noteIndex], $info[$i]);
}
$hue = 135;
$sat = 50;
$lum = 65;
// info = [ distortion, note index, note name w/ octave, octave, freq error
$hueCorrection = 270 - $hue;
$errorCents = $info[5];
$errorHue = $hueCorrection * ($errorCents / 50);
$hue = $hue + $errorHue;
$red = str_pad(dechex(100+2*$note_array[$info[0]][$info[1]][4]),2,"0",STR_PAD_LEFT); // $red = str_pad(dechex(100+2*$note_array[$info[0]][$info[1]][4]),2,"0",STR_PAD_LEFT);
$green = str_pad(dechex(150),2,"0",STR_PAD_LEFT); // $green = str_pad(dechex(150),2,"0",STR_PAD_LEFT);
$blue = str_pad(dechex(100-2*$note_array[$info[0]][$info[1]][4]),2,"0",STR_PAD_LEFT); // $blue = str_pad(dechex(100-2*$note_array[$info[0]][$info[1]][4]),2,"0",STR_PAD_LEFT);
$color = $red.$green.$blue; // $color = $red.$green.$blue;
$note_array[$info[0]][$info[1]][8] = '#' . $color; $note_array[$dist][$noteIndex][8] = "hsl({$hue} {$sat} {$lum})"; //'#' . $color;
} }
} }
@ -163,6 +202,7 @@
</head> </head>
<body> <body>
<?php <?php
$xstart = 10; $xstart = 10;

View File

@ -7,43 +7,51 @@ foreach ($c1_data as $i => $arr)
{ {
//tick delay---------------------------------// //tick delay---------------------------------//
$b_arr = array(0); $b_arr = array(0);
//only if previous note was a rest or another voice //only if previous note was a rest or another voice
if ($i > 0 && strcmp($c1_data[$i-1][1],"0") > 0) if ($i > 0 && strcmp($c1_data[$i-1][1],"0") > 0)
{ {
$b_ticks = strrev(decbin($ticksSinceLastNote)); $b_ticks = strrev(decbin($ticksSinceLastNote));
$b_arr = str_split($b_ticks,7); $b_arr = str_split($b_ticks,7);
for ($j = 0; $j < count($b_arr); $j++) for ($j = 0; $j < count($b_arr); $j++)
{ {
if (strlen($b_arr[$j]) < 7) if (strlen($b_arr[$j]) < 7)
$b_arr[$j] = str_pad($b_arr[$j],7,"0",STR_PAD_RIGHT); $b_arr[$j] = str_pad($b_arr[$j],7,"0",STR_PAD_RIGHT);
if ($j > 0) if ($j > 0)
$b_arr[$j][7] = "1"; $b_arr[$j][7] = "1";
$b_arr[$j] = strrev($b_arr[$j]); $b_arr[$j] = strrev($b_arr[$j]);
} }
} }
//--------------------------------------------// //--------------------------------------------//
$ticksSinceLastNote += (1/$arr[3])*960; $ticksSinceLastNote += (1/$arr[3])*960;
if (strcmp($arr[1],"0") == 0) //play a note (square) if (strcmp($arr[1],"0") == 0) //play a note (square)
{ {
// change volume every other note to half volume
$volume = 80;
$bin_data[] = 0; // delta
$bin_data[] = 176; // controller event, channel 0
$bin_data[] = 7; // change volume
$bin_data[] = $i % 2 === 0 ? $volume : ($volume / 2); // volume level
//note on event //note on event
for ($j = count($b_arr)-1; $j >= 0; $j--) //tick delay for ($j = count($b_arr)-1; $j >= 0; $j--) //tick delay
array_push($bin_data,bindec($b_arr[$j])); array_push($bin_data,bindec($b_arr[$j]));
array_push($bin_data,144); //note on, channel 0 array_push($bin_data,144); //note on, channel 0
array_push($bin_data,$note_array['square'][$arr[2]][5]); //MIDI note index array_push($bin_data,$note_array['square'][$arr[2]][5]); //MIDI note index
array_push($bin_data,100); //velocity array_push($bin_data,100); //velocity
//pitch bend //pitch bend
array_push($bin_data,0); //delta time (immediate bend) array_push($bin_data,0); //delta time (immediate bend)
array_push($bin_data,224); //pitch bend, channel 0 array_push($bin_data,224); //pitch bend, channel 0
array_push($bin_data,$note_array['square'][$arr[2]][6]); //left bits array_push($bin_data,$note_array['square'][$arr[2]][6]); //left bits
array_push($bin_data,$note_array['square'][$arr[2]][7]); //right bits array_push($bin_data,$note_array['square'][$arr[2]][7]); //right bits
//tick delay for note off //tick delay for note off
$b_ticks = strrev(decbin((1/$arr[3])*960)); $b_ticks = strrev(decbin((1/$arr[3])*960));
@ -52,20 +60,21 @@ foreach ($c1_data as $i => $arr)
{ {
if (strlen($b_arr[$j]) < 7) if (strlen($b_arr[$j]) < 7)
$b_arr[$j] = str_pad($b_arr[$j],7,"0",STR_PAD_RIGHT); $b_arr[$j] = str_pad($b_arr[$j],7,"0",STR_PAD_RIGHT);
if ($j > 0) if ($j > 0)
$b_arr[$j][7] = "1"; $b_arr[$j][7] = "1";
$b_arr[$j] = strrev($b_arr[$j]); $b_arr[$j] = strrev($b_arr[$j]);
} }
//note off //note off
for ($j = count($b_arr)-1; $j >= 0; $j--) //tick delay for ($j = count($b_arr)-1; $j >= 0; $j--) //tick delay
array_push($bin_data,bindec($b_arr[$j])); array_push($bin_data,bindec($b_arr[$j]));
array_push($bin_data,128); //note off, channel 0 array_push($bin_data,128); //note off, channel 0
array_push($bin_data,$note_array['square'][$arr[2]][5]); array_push($bin_data,$note_array['square'][$arr[2]][5]);
array_push($bin_data,100); //velocity of release array_push($bin_data,100); //velocity of release
$ticksSinceLastNote = 0; $ticksSinceLastNote = 0;
} }
} }
@ -85,10 +94,10 @@ $byte3 = hexdec(substr($h,4,2));
$byte2 = hexdec(substr($h,2,2)); $byte2 = hexdec(substr($h,2,2));
$byte1 = hexdec(substr($h,0,2)); $byte1 = hexdec(substr($h,0,2));
fwrite($filehandle,pack("I",$byte1),1); fwrite($filehandle,pack("I",$byte1),1);
fwrite($filehandle,pack("I",$byte2),1); fwrite($filehandle,pack("I",$byte2),1);
fwrite($filehandle,pack("I",$byte3),1); fwrite($filehandle,pack("I",$byte3),1);
fwrite($filehandle,pack("I",$byte4),1); fwrite($filehandle,pack("I",$byte4),1);
//change to square voice //change to square voice
fwrite($filehandle,pack("I",0),1); //delta time fwrite($filehandle,pack("I",0),1); //delta time
@ -101,13 +110,14 @@ fwrite($filehandle,pack("I",176),1); //controller event, channel 0
fwrite($filehandle,pack("I",7),1); //change volume fwrite($filehandle,pack("I",7),1); //change volume
fwrite($filehandle,pack("I",80),1); //volume level fwrite($filehandle,pack("I",80),1); //volume level
foreach ($bin_data as $val) foreach ($bin_data as $val) {
fwrite($filehandle,pack("I",$val),1); fwrite($filehandle, pack("I", $val), 1);
}
//End track //End track
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",255),1); fwrite($filehandle,pack("I",255),1);
fwrite($filehandle,pack("I",47),1); fwrite($filehandle,pack("I",47),1);
fwrite($filehandle,pack("I",0),1); fwrite($filehandle,pack("I",0),1);
//END TRACK 2 DATA //END TRACK 2 DATA
?> ?>