Controlling Synths with MIDI in SuperCollider

Notes on acoustics

I previously showed how to set up SuperCollider to communicate with other programs and external hardware using MIDI. Today I am going to use these connections to manipulate instruments.

Controlling the tone with MIDI

In my notes on setting up SuperCollider, I created a function that generated a simple tone.

g = {, 0, 0.1) + }.play;;

To give more control over the tone, we need to define the generator using SynthDef. This class can be thought of as the instructions or recipe which can be used to create Synth instances.\sinpk, 
    {,, 0, 0.1) + }

Let us deconstruct this SynthDef. \sinpk is the name of the SynthDef. It can be used when creating instances, for example by calling\sinpk). The definition itself contains the same tone generator function used previously, but the output is being explicitly sent to the first bus in ensures that the sound is in stereo.

Of course, we are going to want to add some parameters so that we can modify the tone over time.\sinpk, { arg freq = 440;,, 0, 0.1) +;

freq is an argument representing the frequency of the sine wave. Arguments are parameters which can be sent when creating a new Synth and which can be modified later on. Instances of a Synth can be created by calling Synth.

h = Synth(\sinpk, [\freq, 440]);

This call creates a new Synth node and assigns it to the variable h. The frequency is being set to 440 hertz. MIDI uses incriminating integers instead to represent notes, so we will need to convert these numbers to frequencies using midicps.

h.set("freq", (69).midicps);

We can now use MIDI to control the note being generated by the node stored in h.

MIDIdef.noteOn(\changefreq, {arg val, num, chan, src;
	h.set("freq", (num).midicps);

This attaches a new functions that responds to MIDI note presses called \changefreq. The function is passed arguments representing the velocity, note, channel, and source. Each time a note is pressed, the frequency will be changed to match the note.

To unattach the function and any other function that is triggered by MIDI, run MIDIdef.freeAll.

Creating an instrument

The note generator is monophonic and the note continues to play perpetually. To make it polyphonic, we are going to do things slightly differently. First we need a sound for SuperCollider to generate whenever a note is pressed. We also need to make sure that the sound stops being made when the note is released. In SuperCollider, this is typically done by setting gate variable when the note ends.

SynthDef(\sinpk, { arg freq = 440, gate = 1;
    var x;
    x =, 0, 0.1) +;
    x =, gate, doneAction: 2) * x;,;

We need a way to keep track of which notes are currently pressed. To do this, create an array which can store the notes. Each time a note is pressed, create a new Synth and add it to the position in the array corresponding to the note. Every time a key is pressed, release the note.

var keys;
keys = Array.newClear(128);

~noteOnFunc = {arg val, num, chan, src;
	var node;
	node =;
	if (node.notNil, {
		keys.put(num, nil);
	node = Synth(\sinpk, [\freq, num.midicps]);
	keys.put(num, node);

MIDIdef.noteOn(\on, ~noteOnFunc);

~noteOffFunc = {arg val, num, chan, src;
	var node;
	node =;
	if (node.notNil, {
		keys.put(num, nil);

MIDIdef.noteOff(\off, ~noteOffFunc);

Evaluating this block allows notes to be pressed and released by pressing and releasing the keys.

The instrument now can be controlled over MIDI. In the next post, I will be setting up multiple instruments which can be selected using one of the sixteen MIDI channels.