Wow! By using “append” mode of the class File, we can record (save as a CSV file) the MIDI events in real time in SuperCollider:
(
s.waitForBoot{
var checkTime, started, notes, on, off, csv, baseFolder, path, file, write;
MIDIClient.init;
MIDIIn.connectAll;
notes = Array.newClear(128);
csv = "time, pitch, note-on velocity, note-off velocity, duration, channel\n";
baseFolder = thisProcess.nowExecutingPath.dirname;
path = baseFolder +/+ "test.csv" ;
write = {
file = File(path, "a");
file.write(csv);
file.close };
~open = { path.openOS };
~quit = {
MIDIdef(\on).free;
MIDIdef(\off).free;
MIDIIn.disconnectAll;
~open.() };
checkTime = { Main.elapsedTime };
MIDIdef.noteOn(\on, { |vel, num, chan, src|
var synth, pressed;
synth = Synth(\default, [
\freq, num.midicps,
\amp, vel.linlin(0, 127, -60, -6).dbamp
]);
pressed = checkTime.() - started;
notes[num] = [synth, vel, pressed];
(num.cs + vel).postln});
MIDIdef.noteOff(\off, { |vel, num, chan, src|
var noteOn, noteOnVel, pressed, released, dur;
noteOn = notes[num];
noteOnVel = noteOn[1];
pressed = noteOn[2];
notes[num][0].release;
notes[num][0] = nil;
released = checkTime.() - started;
dur = (released - pressed).cs.padRight(16, "0");
(num.cs + vel).postln;
csv = (
pressed.cs.padRight(15, "0") ++ "," +
num ++ "," +
noteOnVel ++ "," +
vel ++ "," +
dur ++ "," +
chan.cs ++ "\n");
write.() });
started = checkTime.();
write.()
}
)
~open.()
~quit.()