Supercollider simple piano sampler23 Feb 2017
In this post I will run through the process of creating a simple sampler using the SuperCollider platform. If you don’t know what SuperCollider is, I highly recommend you to visit the official SuperCollider website.
I’ve also found this really well-made series of video tutorial by Eli Fieldsteel a great way to start learning it.
Some time ago, I found this beautiful collection of piano samples from freesound.org (by the way, freesound is one of my favourite websites but this is actually another topic). These are very good samples recorded from a Steinway & Sons piano.
I wanted to create a virtual instrument in SuperCollider with these requisites:
- it should have used the samples from the collection;
- it should have been playable via MIDI, using a MIDI keyboard connected to the computer.
If I download the sample collection from freesound.org and take a look at the samples file names, I will see that they force the entire collection to appear completely unorganized as shown in the image below.
If I play the audio files in order, from the first one onward, I will hear sounds from different octaves unevenly mixed up.
The problem remains even after I import them inside buffer objects in SuperCollider.
The solution stands in renaming all the audio files in a convenient way in order to load them in ascending order, from the lower tone to the higher one.
Even if the easiest solution remains that of renaming the files manually, I wanted the job done in a quicker and automated way, so I started thinking about a Python script that could do the job. At first I started looking the file name structure, then tryed to figure out what operations could accomplish my goal.
Let’s take a look at the first audio file from the collection:
As you see the file name starts with something that means nothing to us. I would like only to preserve informations about the note name (
db), the octave (
4) and the stroke pressure (
At the same time I wanted to order the files starting from lower the sound to the higher one.
Eventually I came out with this python script (maybe it is not the best declared one but is seemed to work pretty well):
I decided to use the standard MIDI note numbering for indexing the files so that a e7, for example, would have corresponded to the index 100, or a c4 to the 60:
148577__neatonk__piano-med-e7.wavwould be renamed this way
148432__neatonk__piano-loud-c4.wavwould be renamed this way
0I’ve used to be consistent with a 3 digit enumeration);
Here’s an image from the folder containing all the
med renamed files:
Time to move to SuperCollider. First I loaded the sound files inside buffers using the following code:
Then I created a SynthDef. I declared some argument in order to be able to modify them from the outside:
bufwill be the number of the buffer to play the sound from. I’ll be able to identify it thanks to the MIDI note number;
gatewill be useful to trigger attack and release portions of the amplitude envelope;
velthis is the velocity value I will use to calculate an overall volume multiplication.
Next I made an array to store all of the synth instances I’d create every time a new key is pressed:
Now I needed MIDI methods and definitions to manage the incoming MIDI messages from the external MIDI keyboard. In particular I needed a
noteOn method to create and store the synth inside the
~keys array and to send it the
Then I needed a method to deal with the noteOff MIDI messages. This method, called
noteOff, simply looks inside the
~keys array for the correct synth to refer to.
Then it sets its
gate value to zero in order to start the releasing phase and eventually free the synth from the server (see the doneAction argument for the EnvGen method inside the SynthDef).