function encode(song) {
		var file = [];
		var i, j;
		try {
			var type = (song.tracks.length == 1)?0:1;
			file = chunkify(MThd, [0, type].concat(stov(song.tracks.length)).concat(stov(song.divisions)) );
			
			for (i = 0; i < song.tracks.length; i++) {
				var track = song.tracks[i].sort(function (a, b) {
					return b.time < a.time;
				});

				track[0].delta = track[0].time;

				for (j = 1; j < track.length; j++) {
					track[j].delta = track[j].time - track[j - 1].time;
				}

				var chunk = chunkify(MTrk, [].concat.apply([], track.map(function (cmd) {
					return ltobb(cmd.delta).concat(Array.prototype.slice.call(cmd.command));
				})));

				file = file.concat(chunk);
			}

		} catch (e) {
			console.warn(e);
			return;
		}

		return new Uint8Array(file);
	}
/*
	given a song object, serialize it
	*/
given a song object, serialize it
given a song object serialize it
function encode(song) {
		var file = [];
		var i, j;
		try {
			var type = (song.tracks.length == 1)?0:1;
			file = chunkify(MThd, [0, type].concat(stov(song.tracks.length)).concat(stov(song.divisions)) );
			for (i = 0; i < song.tracks.length; i++) {
				var track = song.tracks[i].sort(function (a, b) {
					return b.time < a.time;
				});
				track[0].delta = track[0].time;
				for (j = 1; j < track.length; j++) {
					track[j].delta = track[j].time - track[j - 1].time;
				}
				var chunk = chunkify(MTrk, [].concat.apply([], track.map(function (cmd) {
					return ltobb(cmd.delta).concat(Array.prototype.slice.call(cmd.command));
				})));
				file = file.concat(chunk);
			}
		} catch (e) {
			console.warn(e);
			return;
		}
		return new Uint8Array(file);
	}
function encode ( song ) { var file = [ ] ; var i , j ; try { var type = ( song . tracks . length == 1 ) ? 0 : 1 ; file = chunkify ( MThd , [ 0 , type ] . concat ( stov ( song . tracks . length ) ) . concat ( stov ( song . divisions ) ) ) ; for ( i = 0 ; i < song . tracks . length ; i ++ ) { var track = song . tracks [ i ] . sort ( function ( a , b ) { return b . time < a . time ; } ) ; track [ 0 ] . delta = track [ 0 ] . time ; for ( j = 1 ; j < track . length ; j ++ ) { track [ j ] . delta = track [ j ] . time - track [ j - 1 ] . time ; } var chunk = chunkify ( MTrk , [ ] . concat . apply ( [ ] , track . map ( function ( cmd ) { return ltobb ( cmd . delta ) . concat ( Array . prototype . slice . call ( cmd . command ) ) ; } ) ) ) ; file = file . concat ( chunk ) ; } } catch ( e ) { console . warn ( e ) ; return ; } return new Uint8Array ( file ) ; }
given a song object, serialize it
given a song object serialize it