# SuperCollider: MIDI Keyboard Control ## 1. Boot the Server ```supercollider s.boot; ``` ## 2. Create the Synth The `gate` controls the envelope. A note sustains while the key is held and releases when the key is released. ```supercollider ( SynthDef(\basic, { |freq = 440, amp = 0.2, gate = 1| var env = EnvGen.kr( Env.asr(0.01, 1, 0.2), gate, doneAction: 2 ); Out.ar(0, (SinOsc.ar(freq) * env * amp)!2); }).add; ) ``` ## 3. Connect MIDI ```supercollider MIDIClient.init; MIDIIn.connectAll; ``` (Optional) List available MIDI devices: ```supercollider MIDIClient.sources; MIDIFunc.trace(true); ``` ## 4. Store Active Notes ```supercollider ~notes = IdentityDictionary.new; ``` ## 5. Play Notes ```supercollider ( MIDIdef.noteOn(\noteOn, { |vel, note| ~notes[note] = Synth(\basic, [ \freq, note.midicps, \amp, vel.linlin(1, 127, 0.02, 0.3) ]); }); ) ``` ## 6. Release Notes ```supercollider ( MIDIdef.noteOff(\noteOff, { |vel, note| ~notes[note].set(\gate, 0); ~notes.removeAt(note); }); ) ```