Skip to content

Instantly share code, notes, and snippets.

@NoahRoseLedesma
Created March 3, 2016 19:57
Show Gist options
  • Save NoahRoseLedesma/d5028fad236fa874604d to your computer and use it in GitHub Desktop.
Save NoahRoseLedesma/d5028fad236fa874604d to your computer and use it in GitHub Desktop.
Program to convert Midi files into an array of frequencies.
var midiFileParser = require('midi-file-parser');
var MIDIUtils = require('midiutils');
var song = "hotline_bling"
var file = require('fs').readFileSync(song + '.mid', 'binary')
var midi = midiFileParser(file);
notes = [];
noteStatus = false;
var track = 0;
for(var i = 0; i < midi.tracks[track].length; i++)
{
currentObject = midi.tracks[track][i];
switch (currentObject.subtype) {
case 'noteOn':
if(!noteStatus)
{
// No note is being played, get the next note
notes[notes.length] = [MIDIUtils.noteNumberToFrequency(currentObject.noteNumber), 0]
noteStatus = true;
}
break;
case 'noteOff':
if(noteStatus && MIDIUtils.noteNumberToFrequency(currentObject.noteNumber) == notes[notes.length - 1][0])
{
notes[notes.length - 1][1] = currentObject.deltaTime;
noteStatus = false;
}
// if(noteStatus)
// {
// // A note is being played, get the off signal
// if(currentObject.noteNumber == notes[notes.length - 1][0])
// {
// notes[notes.length - 1][1] == currentObject.deltaTime;
// noteStatus = false;
// }
// }
break;
default:
break;
}
}
console.log(notes);
require('fs').writeFile(song + '.rsong', JSON.stringify(notes), (err) => {
if(err) throw err;
console.log("Saved song to file");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment