This week I was learning the basics of Csound and built a simple synth as an AU plugin to use it in a DAW.
What is Csound
Csound is a music computing system developed from 1985 and is still maintained today. It is part of the family of MUSIC-N Languages, which evolved from MUSIC-V which is one of the first programming languages for sound.
A key concept of those languages is the unit generator. It can be seen a a function that does one specific thing, like an oscillator, envelope or filter.
Another concept is that the program is split in two parts:
The orchestra contains code related to instruments and how they produce sound
The Score defines how the instruments are played in a sequence
Using Csound
You can use Csound directly in the browser (you need to create an account for this to work) or download it and use it with an IDE of your choice. In my case I was using Cabbage since it has the ability to export plugins from it.
Resources to learn Csound
While there are many different resources out there, that are all over the place. I was using those:
What Csound Code looks like
The code you write in Csound has an XML like structure. It is all wrapped in a <CsoundSythesizer> tag and in there it has three sections:
Options (settings like audio devices or midi)
Instruments (global variables and the code for your instruments)
Score (how the instruments will play)
<CsoundSynthesizer>
<CsOptions>
-o dac
</CsOptions>
<CsInstruments>
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
kEnv madsr .1, .2, .6, .4
aOsc vco2 0.2, 400
outs aOsc * kEnv, aOsc * kEnv
endin
</CsInstruments>
<CsScore>
i 1 0 2
</CsScore>
</CsoundSynthesizer>
Using unit generators
As explained before the key concept of those programming languages is the idea of unit generators. In Csound those are called Opcodes and the language has over 1500 of them which is quite a lot.
To not get lost, the Opcode guide in the FLOSS Manual is quite helpful.
But to get something going quickly you only need to understand that the are different types of variables you need to use and that Opcodes mostly have inputs and outputs.
<CsInstruments>
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
iFreq = p4
kEnv madsr .1, .2, .6, .4
aOsc vco2 0.2, iFreq
outs aOsc * kEnv, aOsc * kEnv
endin
</CsInstruments>
<CsScore>
i 1 0 2 440
i 1 2 4 880
</CsScore>
The Instrument Section
iRate Variables
In the code above we first create a variable called iFreq. The i specifies that this variables is set only once, when the instrument is instantiated. It is set to p4, which is a pointer to the fourth place in the score line (in this case it is 440 for the first and 880 for the second note).
kRate Variables
Then we declare a variable called kEnv. The k specifies that this variable is updated at k-Rate which is a fraction of the audio rate and is defined by the ksmps variable. This is useful for performance. It is used with the Opcode madsr which has for inputs on the right (attack, decay, sustain and release) and one output on the left, which is our kEnv variable.
aRate Variables
Then we declare a variable called aOsc. The a specifies that it is updated at audio rate, which is depending on your samplerate something around 44100 times a second. It is using the vco2 Opcode which creates a sawtooth wave. It has two inputs on the right. The amplitude and the frequency. Note that we are using the variable iFreq to set the frequency from the score.
Last we use the outs Opcode to send the oscillator multiplied by the envelope that gets triggered each time we play a note to the left and right channels of our speakers.
The Score Section
To hear something we need to use the score and tell it which instrument to play and how long each note should be. We can also use it to give information to the instrument. In our case this is the frequency we want to use.
The code “i 1 0 2 440” means:
Play instrument number 1 (p1) from time 0s (p2) for 2s (p3) and assign the next value (p4) to 440 in this instrument instance.
Then we play another note at a different frequency. You can play as many notes as you want at asking as many additional p values to an instrument as you need. This makes it very flexible to use.
Building a plugin
While it is interesting to use the score to produce a more or less static piece of music. You can use a tool such as Cabbage to make plugins that you can use wherever you want.
In my case I wanted to create vst3 plugin, but this did not work for some reason. But it was possible to create an AU plugin that could be used in a DAW very easily.
Conclusion
Csound seems like a nice programming language that can do a lot of things. The amount of Opcodes it includes is just awesome and you feel that it has a very long history. But since the possibilities of the language are so huge, it feels a bit hard as a beginner to do things in a simple way. Maybe this is just a matter of practice but let’s see.
What I find interesting is that there is an option to build plugins for unity, which is something that I definitely have to try and see what is possible with that.
Other things I did this week
Animations
Drawing
That’s it for this week. Hope you liked it.