Skip to content

Instantly share code, notes, and snippets.

@zzox
Last active December 10, 2023 01:25
Show Gist options
  • Save zzox/bf5205a60cdafdb95642f64111fa2454 to your computer and use it in GitHub Desktop.
Save zzox/bf5205a60cdafdb95642f64111fa2454 to your computer and use it in GitHub Desktop.
Convert midi files outputted by ableton into a json file with time of every note
const { Midi } = require('@tonejs/midi')
const { readFileSync, writeFileSync } = require('fs')
const FILENAME = process.argv[2]
const BPM = process.argv[3]
const DIR_SPECIFIC = process.argv[4] === "true"
const MS_PER_MINUTE = 60000
const ABLETON_PPQ = 96
try {
const msPerTick = (MS_PER_MINUTE / (BPM * ABLETON_PPQ))
const data = {}
for (let i = 5; i < process.argv.length; i++) {
let midiUrl = process.argv[i]
let trackName
if (DIR_SPECIFIC) {
// if we are using specific LRUD dirs, we have tracks tied to a specific dir
trackName = midiUrl.split('=')[0]
midiUrl = midiUrl.split('=')[1]
} else {
trackName = midiUrl.slice(0, midiUrl.indexOf('.mid'))
}
const midi = new Midi(readFileSync(midiUrl))
const notes = midi.tracks[0].notes.map(({ ticks }) => ticks * msPerTick)
data[trackName] = notes
}
let name = FILENAME + '.json'
writeFileSync(name, JSON.stringify(data, null, 2), 'utf8')
console.info(`SUCCESS! \n"${name}" created.`)
} catch (e) {
console.error(
'FAILED!\n' +
'Usage: node midiToJson.js [Exported Track Name] [BPM] [=true] [dir=track-1] [dir=track-2]...[track-4]\n' +
'OR: node midiToJson.js [Exported Track Name] [BPM] [=false] [track-1] [track-2]...[track-n]\n'
)
}
// sample query:
// node midiToJSON.js think 132 true left=midi/think-left.mid right=midi/think-right.mid up=midi/think-up.mid down=midi/think-down.mid
// or for non-dir spcific track names:
// node midiToJSON.js think 132 false think-left.mid think-right.mid think-up.mid think-down.mid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment