From db51895a289f067f0bf4f477c9768b9a3b9a5d2c Mon Sep 17 00:00:00 2001 From: Chris Angelico Date: Thu, 31 Oct 2024 08:27:10 +1100 Subject: [PATCH] Make a quick and dirty MIDI event dumper --- mididump.pike | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 mididump.pike diff --git a/mididump.pike b/mididump.pike new file mode 100644 index 0000000..41d4061 --- /dev/null +++ b/mididump.pike @@ -0,0 +1,45 @@ +int last_command = 0; +Stdio.Buffer data = Stdio.Buffer(); +void parsemessage() { + while (sizeof(data)) { + Stdio.Buffer.RewindKey rewind = data->rewind_on_error(); + int command = data->read_int8(); + if (command < 128) {data->unread(1); command = last_command;} //Running status + else last_command = command; + array ev = ({command}); + switch (command) + { + case 0x00..0x7F: error("Status byte expected. Running status with no previous status.\n"); + case 0x80..0x8F: //Note off + case 0x90..0x9F: //Note on + case 0xA0..0xAF: //Note aftertouch + case 0xB0..0xBF: //Controller + case 0xE0..0xEF: //Pitch bend + case 0xF2: //Song Position + //Two data bytes for these + ev += ({data->read_int8(), data->read_int8()}); + break; + case 0xC0..0xCF: //Program change + case 0xD0..0xDF: //Channel aftertouch + case 0xF3: + //One data byte. + ev+=({data->read_int8()}); + break; + case 0xF1: case 0xF4..0xF6: case 0xF8..0xFE: //System Common various + //No data bytes. + break; + case 0xF0: case 0xF7: error("SysEx not currently supported\n"); + case 0xFF: //Meta event + error("Meta event?? In a MIDI stream??\n"); + } + rewind->release(); + write("%{%02X %}\n", ev); + } +} + +int main(int argc, array(string) argv) { + Stdio.File dev = Stdio.File("/dev/midi7"); //TODO: Parameterize + dev->set_buffer_mode(data, 0); + dev->set_nonblocking(parsemessage); + return -1; +}