Compare commits

..

No commits in common. "e9591a23885caa12354e291fd3c03ba6ad6c8fcd" and "b5b7a339c3e5e30ee4e71c3a99308167848f5ce0" have entirely different histories.

4 changed files with 91 additions and 141 deletions

View File

@ -6,40 +6,40 @@
//create the midi file
$filename = "a2600song_".date("Y-m-d_His").".mid"; //unique filename
$filehandle = fopen("songs/".$filename,"wb"); //create the file, and open for writing
//----------------------------------------------//
$c1 = explode("\n",$_POST['c1_display']);
$c2 = explode("\n",$_POST['c2_display']);
$c1_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
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
if (str_word_count($c2[$i],0,"#.0123456789") == 4)
array_push($c2_data,explode(" ",$c2[$i]));
$track_arr = array(
1 => 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)
if ($arr[1] != "x")
$track_arr[1][$arr[1]] = true;
foreach ($c2_data as $i => $arr)
if ($arr[1] != "x")
$track_arr[2][$arr[1]] = true;
$numtracks = 1;
foreach ($track_arr as $var => $arr)
foreach ($arr as $val)
if ($val)
$numtracks++;
//----------------------------------------------//
//HEADER
fwrite($filehandle,pack("I",77),1);
fwrite($filehandle,pack("I",84),1);
@ -47,52 +47,52 @@
fwrite($filehandle,pack("I",100),1);
fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",0),1);
//Size of header
fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",6),1);
//MIDI file format
fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",1),1);
//# of tracks
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.
//Time division
fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",240),1);
//END HEADER
//TRACK 1 DATA
//Header
fwrite($filehandle,pack("I",77),1);
fwrite($filehandle,pack("I",84),1);
fwrite($filehandle,pack("I",114),1);
fwrite($filehandle,pack("I",107),1);
//# 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",19),1);
//Tempo
$ms = round(60000000/$_POST['tempo']);
$ms = str_pad(dechex($ms),6,"0",STR_PAD_LEFT);
$byte1 = hexdec(substr($ms,0,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",255),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",$byte2),1); //microseconds per beat
fwrite($filehandle,pack("I",$byte3),1); //microseconds per beat
//Time signature
fwrite($filehandle,pack("I",0),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",24),1); //Metronome
fwrite($filehandle,pack("I",8),1); //32nd notes per beat
//End track
fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",0),1);
fwrite($filehandle,pack("I",255),1);
fwrite($filehandle,pack("I",47),1);
fwrite($filehandle,pack("I",0),1);
//END TRACK 1 DATA
/* Array Format
* $c1_data[note][0] = note name
* $c1_data[note][1] = distortion #
* $c1_data[note][2] = atari note index
* $c1_data[note][3] = note value (quarter, eighth, etc.)
*/
*/
if ($track_arr[1][0]) require("square_1.php");
if ($track_arr[1][1]) require("lead_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][2]) require("saw_2.php");
if ($track_arr[2][3]) require("bass_2.php");
fclose($filehandle);
header("Content-type: audio/midi");
header("Content-Disposition: attachment; filename=$filename");
@ -135,7 +135,5 @@
{
echo "<script>alert('not enough data! argh!');</script>";
}
die();
}
?>
?>

View File

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

View File

@ -70,65 +70,26 @@
while (($file = readdir($dir)) != null)
{
// $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
//format: distortion_atari note index_note name_octave_freq_error_MIDI note index_pitch bend left bits_pitch bend right bits
if (strpos($file,".ogg"))
{
$info = explode("_",$file);
$info[2] = str_replace("+","#",$info[2]);
$info[count($info)-1] = substr($info[count($info)-1],0,strpos($info[count($info)-1],".ogg"));
// print_r($info);
$dist = $info[0];
$noteIndex = $info[1];
if (!isset($note_array[$info[0]][$info[1]]))
$note_array[$info[0]][$info[1]] = array();
// [0] distortion name
// [1] note index
// [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;
array_push($note_array[$info[0]][$info[1]],$file);
for ($i = 2; $i < count($info); $i++)
array_push($note_array[$info[0]][$info[1]],$info[$i]);
// $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);
// $blue = str_pad(dechex(100-2*$note_array[$info[0]][$info[1]][4]),2,"0",STR_PAD_LEFT);
// $color = $red.$green.$blue;
$note_array[$dist][$noteIndex][8] = "hsl({$hue} {$sat} {$lum})"; //'#' . $color;
$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);
$blue = str_pad(dechex(100-2*$note_array[$info[0]][$info[1]][4]),2,"0",STR_PAD_LEFT);
$color = $red.$green.$blue;
$note_array[$info[0]][$info[1]][8] = '#' . $color;
}
}
@ -202,7 +163,6 @@
</head>
<body>
<?php
$xstart = 10;

View File

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