<?php
// saw channel 1
$bin_data = array();	//an array of integers to write to the midi file
$ticksSinceLastNote = 0;

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],"2") < 0 || strcmp($c1_data[$i-1][1],"2") > 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],"2") == 0) //play a note (saw)
	{
		//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,146);	//note on, channel 2
		array_push($bin_data,$note_array['saw'][$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,226);	//pitch bend, channel 2
		array_push($bin_data,$note_array['saw'][$arr[2]][6]);	//left bits
		array_push($bin_data,$note_array['saw'][$arr[2]][7]);	//right bits
		
		//tick delay for note off
		$b_ticks = strrev(decbin((1/$arr[3])*960));

		$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]);
		}
		
		//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,130);	//note off, channel 2
		array_push($bin_data,$note_array['saw'][$arr[2]][5]);
		array_push($bin_data,100);	//velocity of release
		
		$ticksSinceLastNote = 0;
	}
}

//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
$numbytes = count($bin_data)+11; //the seven lines of changing voice and volume, plus the track end bytes
$h = str_pad(dechex($numbytes),8,"0",STR_PAD_LEFT);

$byte4 = hexdec(substr($h,6,2));
$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); 			

//change to lead voice
fwrite($filehandle,pack("I",0),1);		//delta time
fwrite($filehandle,pack("I",194),1);	//change voice, channel 2
fwrite($filehandle,pack("I",84),1);		//saw voice

//change volume
fwrite($filehandle,pack("I",0),1);		//delta time
fwrite($filehandle,pack("I",178),1);	//controller event, channel 2
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);	

//End track
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 3 DATA
?>