Skip to content

Instantly share code, notes, and snippets.

@funmaker
Created August 11, 2018 09:15
Show Gist options
  • Save funmaker/fb97397eab56e466fcf5455bc83c2900 to your computer and use it in GitHub Desktop.
Save funmaker/fb97397eab56e466fcf5455bc83c2900 to your computer and use it in GitHub Desktop.
const midi = [
// copy your notes here, like this:
// {
// "name": "E3",
// "midi": 52,
// "time": 52.81666666666657,
// "velocity": 0.7086614173228346,
// "duration": 0.13333333333333286
// }
// I've generated them from midi file using https://tonejs.github.io/MidiConvert/
];
const step = 1 / 33;
let curtime = 0;
let notes = [];
let tracks = [[], []];
let ends = [0, 0];
for(let note of midi) {
note.duration = Math.round((note.time + note.duration) / step) - Math.round(note.time / step);
note.time = Math.round(note.time / step);
if(ends[0] <= note.time) {
tracks[0].push(note);
ends[0] = note.time + note.duration;
} else if(ends[1] <= note.time) {
tracks[1].push(note);
ends[1] = note.time + note.duration;
} else {
console.log("Dropping ", note);
}
}
const toData = track => {
let curtime = 0;
let data = [];
for(let note of track) {
if(note.time > curtime){
data.push(0, note.time - curtime);
curtime = note.time;
}
data.push(note.midi, note.duration);
curtime += note.duration;
}
data.push(0, 0);
let str = "DATA";
for(let d of data) {
if((str + " " + d).length > 24) {
console.log(str);
str = "DATA";
}
str += " " + d;
}
console.log(str);
}
console.log("# TRACK 1");
toData(tracks[0]);
console.log("# TRACK 2");
toData(tracks[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment