Jason (jcreed) wrote,

Oh man I got realtime midi working in Linux with Supercollider! I was quite impressed at the fact that the usb midi device itself just worked as soon as I plugged it in. I installed kmidimon with apt-get, chose the "connect to midi devices" menu thingy, and played some notes on the keyboard, and they magically showed up.

Convincing supercollider was a little harder. The thing that worked for me was hard-coding the device number (this particular concept of device number being specific to either supercollider or maybe to ALSA) that the keyboard showed up as. Dunno why, but devices 0, 1, and 2 were mapped to system timers and MIDI thru and stuff, and 3 was MIDI in.

Anyway, it sounds like this. This is me trying to play the beginning of Bach's "Prelude in C major" from the Well-Tempered Clavier. The latency was just barely noticeable while playing, but not terrible.

MIDIIn.connect(device: 3); // Perhaps you might want different values of 3

// MIDI Callbacks
var notes, on, off;
notes = Array.newClear(128);  // array has one slot per possible MIDI note
on = NoteOnResponder({ |src, chan, num, veloc|
notes[num] = Synth(\sine_osc, [\freq, num.midicps,
\amp, veloc * 0.00315]);
off = NoteOffResponder({ |src, chan, num, veloc|
q = { on.remove; off.remove; };

// To turn off midi callbacks:

// Definition of sinewave patch
        SynthDef(\sine_osc, {
                arg amp = 0.1, gate = 1, freq = 440;
                // I tried to figure out how to use .do for this but it eluded me this time!
                var freq_array = [2 * freq, freq];
                var ampmod_array = [MouseX.kr(0.0, 1.0), MouseY.kr(1.0, 0.0)];

                // Five SinOscs with randomly selected frequency are created and spread across the stereo image
                // The amp mod applied to left and right output channels is controlled by mouse position
                Out.ar(0, Splay.ar(SinOsc.ar(freq_array, 0, amp)));
                FreeSelf.kr(1 - gate);

// Run these to start recording
b= Buffer.alloc(s, 65536, 2);
b.write("/tmp/diskouttest.aiff", "aiff", "int16", 0, 0, true);
d = Synth.tail(nil, "help-Diskout", ["bufnum", b.bufnum]);

// Run these to stop recording

Tags: music, supercollider

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded