/* * MIDI Dump - Parse and print MIDI file contents * * Usage: cell run examples/dump.ce */ var io = use('cellfs') var midi = use('midi') if (length(args) < 1) { log.console("Usage: cell run examples/dump.ce ") log.console("Example: cell run examples/dump.ce examples/invent8.mid") $stop() } var midi_file = args[0] log.console("Loading MIDI: " + midi_file) var midi_blob = io.slurp(midi_file) if (!midi_blob) { log.console("Error: Could not load MIDI file: " + midi_file) $stop() } var song = midi.parse(midi_blob) log.console("Duration: " + text((song.duration_ms / 1000)) + "s") log.console("Notes: " + text(song.note_count)) log.console("Events: " + text(length(song.events))) log.console("") var NOTE_NAMES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] function note_name(key) { var octave = floor(key / 12) - 1 var name = NOTE_NAMES[key % 12] return name + text(octave) } function pad(s, n) { var _s = text(s) while (length(_s) < n) { _s = _s + ' ' } return _s } var events = song.events var i = 0 var evt = null var line = '' for (i = 0; i < length(events); i++) { evt = events[i] line = pad(text(evt.time / 1000) + 's', 10) line = line + 'ch=' + pad(evt.channel, 4) if (evt.type == 'note_on') { line = line + pad('note_on', 12) + note_name(evt.key) + ' vel=' + text(evt.velocity) } else if (evt.type == 'note_off') { line = line + pad('note_off', 12) + note_name(evt.key) } else if (evt.type == 'control') { line = line + pad('control', 12) + 'cc=' + text(evt.control) + ' val=' + text(evt.value) } else if (evt.type == 'program') { line = line + pad('program', 12) + 'preset=' + text(evt.program) } else if (evt.type == 'pitch_bend') { line = line + pad('pitch_bend', 12) + 'val=' + text(evt.pitch_bend) } else if (evt.type == 'tempo') { line = line + pad('tempo', 12) + 'bpm=' + text(evt.tempo) } log.console(line) }