Skip to content

Instantly share code, notes, and snippets.

@jkbd
Last active October 3, 2022 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkbd/78bdc851bc035346bc9c4fd6f8f82a74 to your computer and use it in GitHub Desktop.
Save jkbd/78bdc851bc035346bc9c4fd6f8f82a74 to your computer and use it in GitHub Desktop.
Polyphonic instrument in SuperCollider
// ISC License
//
// Copyright (c) 2022 Jakob Dübel
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// Initialize MIDI
(
MIDIClient.init;
MIDIIn.connectAll(verbose: true);
MIDIFunc.trace(bool: true);
)
// Global variables for housekeeping
(
~master_group = Group.new(Server.default);
~mep = MIDIIn.findPort(deviceName: "UA-25", portName: "UA-25 MIDI 1");
~srcID = ~mep.asMIDIInPortUID;
// The state of the keybed. Each key can be pressed or not.
~keys = Array.fill(128, { False });
// The state of the sustain pedal. Released is 0, pressed is 1. Some hardware allows for values in between.
~sustain = 0.0;
// One group for each key to store Synths.
~groups = Array.fill(128, { Group.new(~master_group, \addToTail) });
)
// Functions to handle MIDI NoteOn, NoteOff and the sustain pedal, which is Control Change 64
(
~handleNoteOn = MIDIFunc.noteOn({
arg ...args;
var velocity, amp, note, freq;
velocity = args[0];
note = args[1];
~keys[note] = True; // key pressed
// Start a synth voice and store its handle in the appropriate group.
Synth(defName: \default,
args: [
\freq: note.midicps,
\amp: velocity.linlin(0, 127, 0.0, 0.2)
],
target: ~groups.at(note)
);
});
~handleNoteOff = MIDIFunc.noteOff({
arg ...args;
var note, freq;
note = args[1];
~keys[note] = False; // Key released
// If the sustain pedal is up, end the Synth voice
if (~sustain < 0.5, {
~groups[note].set(\gate, 0);
}, {
// else nothing
});
});
~handleSustainPedal = MIDIFunc.cc({
arg ...args;
var value;
value = args[0].linlin(0, 127, 0.0, 1.0);
~sustain = value; // Update the stored state used in above functions
if (value < 0.5, {
// If the pedal is released, end all synth voices, whose key is not pressed
~keys.do({
arg value, note;
if (~keys[note] == False, {
~groups[note].set(\gate, 0); // Release the ringing notes
}, {
// else nothing
})
});
}, {
// If the pedal is pressed: do nothing.
});
}, ccNum: 64, chan: 0, srcID: ~srcID);
)
@jkbd
Copy link
Author

jkbd commented Oct 3, 2022

This code allows to play the \default Synth in SuperCollider polyphonically with a MIDI keyboard. The number of running synths is not limited, so XRUNs are possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment