|cSounds.com - tutorials - An Instrument Design TOOTorial
Csound instruments are created in an orchestra file, and the list of notes to play is written in a separate score file. Both are created using a standard word processor. When you run Csound on a specific orchestra and score, the score is sorted and ordered in time, the orchestra is translated and loaded, the wavetables are computed and filled, and then the score is performed. The score drives the orchestra by telling the specific instruments when and for how long to play, and what parameters to use during the course of each note event.
Unlike today's commercial hardware synthesizers, which have a limited set of oscillators, envelope generators, filters, and a fixed number of ways in which these can be interconnected, Csound's power is not limited. If you want an instrument with hundreds of oscillators, envelope generators, and filters you just type them in. More important is the freedom to interconnect the modules, and to interrelate the parameters which control them. Like acoustic instruments, Csound instruments can exhibit a sensitivity to the musical context, and display a level of "musical intelligence" to which hardware synthesizers can only aspire.
Because the intent of this tutorial is to familiarize the novice with the syntax of the language, we will design several simple instruments. You will find many instruments of the sophistication described above in The Csound Book or by following the links on the MIT Csound Front Page. A study of these will reveal Csound's real power. Still, hopefully you will find in these simple examples the building block you require and I encourage you to modify and extend each of them and recommend that you try to make each score more involved and more musical. Well, let's get started!
In Csound, the orchestra file has two main parts:
1. the header section - defining the sample rate, control rate, and number of output channels.
A Csound orchestra generates signals at two rates - an audio sample rate and a control sample rate. Each can represent signals with frequencies no higher than half that rate, but the distinction between audio signals and sub-audio control signals is useful since it allows slower moving signals to require less compute time. In the header below, we have specified a sample rate of 44.1 kHz, a control rate of 4410 Hz, and then calculated the number of samples in each control period using the formula: ksmps = sr / kr
sr = 44100 kr = 4410 ksmps = 10 nchnls = 1
In Csound orchestras and scores, spacing is arbitrary. It is important to be consistent in laying out your files, and you can use spaces to help this. In the Tutorial Instruments shown below you will see we have adopted one convention. The reader can choose his or her own.
All instruments are numbered and are referenced thus in the score. Csound instruments are similar to patches on a hardware synthesizer. Each instrument consists of a set of "unit generators," or software "modules," which are "patched" together with "i/o" blocks i-, k-, or a-rate variables. Unlike a hardware module, a software module has a number of variable "arguments" which the user sets to determine its behavior. The four types of variables are:
Each statement occupies a single line and has the same basic format:
result action arguments
To include an oscillator in our orchestra, you might specify it as follows:
a1 oscil 10000, 440, 1
The three "arguments" for this oscillator set its amplitude (10000), its frequency (440Hz), and its wave shape (1). The output is put in i/o block a1. This output symbol is significant in prescribing the rate at which the oscillator should generate output here the audio rate. We could have named the result anything (e.g. asig) as long as it began with the letter "a".
To include text in the orchestra or score which will not be interpreted by the program, precede it with a semicolon. This allows you to fully comment your code. On each line, any text which follows a semicolon will be ignored by the orchestra and score translators.