Learn creative coding writing simple programs

137. Synths and SynthDefs

In previous episodes we used something like { SinOsc.ar }.play to play sounds. This is a trick to get sounds while typing less, but it's not very efficient. When we do that we are creating a Synth, sending it to the sound server, and playing it immediately. It's a bit like building a new piano each time we press a key. It's better to build the piano first, and then play the keys many times. This is what we can achieve by using SynthDef and Synth.
SynthDef().add creates an "instrument" and sends it to the server, and Synth plays instruments. Watch the episode to see how it works.

Three little tricks:
1) if you press CTRL+Shift+Space when typing the arguments of a function, if SuperCollider recognizes that function, it will show you the argument names and default values for that function. This window is automatically shown when opening parenthesis, but you can get it back any time by pressing that key combination.
2) Arguments in functions often have default values. For example the first argument in SinOsc.ar() is the frequency, and it has a default value of 440. If you are fine with the default frequency but want to enter "mul" (to set the volume), which is the third argument, you can do this: SinOsc.ar(mul:0.2). In Processing it's not possible to send arguments by name, but in SuperCollider you can type argument_name:value. This makes the code longer, but easier to understand.
3) You can give a default value to your own arguments using this format: arg amountOfMoons=1; What this does is to give the argument amountOfMoons the value 1 when not specified. So if you specify that amountOfMoons is equal to 3, then it will be 3. If you don't say how many moons you want, it will be 1.

Tags: supercollider, synth, synthdef, sound

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```