home/ atoms/ sc-mididef-polyphony-array

Polyphonic MIDI in SuperCollider uses an array of 128 Synths indexed by note number

A standard pattern for polyphonic SuperCollider MIDI synthesis: allocate an empty Array of size 128 (Array.newClear(128)). On note-on, create a Synth and store it at index nn (the incoming MIDI note number). On note-off, set gate=0 on the Synth at index nn then set that slot to nil. This exploits the fact that nil.set(\gate, 0) silently does nothing, so iterating the full array on pitch-bend without checking for nil is safe. A naive alternative (storing a single Synth in a global variable) overwrites on new notes, making old notes impossible to release.

Examples

~notes = Array.newClear(128); MIDIdef.noteOn(\on, { arg vel, nn; ~notes[nn] = Synth(\tone, [\freq, nn.midicps, \gate, 1]) }); MIDIdef.noteOff(\off, { arg vel, nn; ~notes[nn].set(\gate, 0); ~notes[nn] = nil });

Assessment

Why is it safe to call .set(\bend, val) on every element of ~notes even though most slots are nil? What happens if you store each new Synth in a single global variable instead of an indexed array?

“When I play a note, I create a Synth, and store that Synth in the array, at an index equal to the incoming note number.”
corpus · supercollider-tutorials-full-transcripts-and-code-eli-fields · chunk 24