Csound Basics: A Simple Oscillator and Envelope

In Csound, we typically visualize our patches using a "block diagram" to show the signal flow, parameter settings, and interconnections of the opcodes. Like Csound itself, these block diagrams are read from top to bottom. In figure 2 we see a block diagram and the Csound code for a simple instrument consisting of an oscillator with an envelope generator and the score file to play it.


[index] [prev] [next]


[+] Home
[+] Tutorials

Figure 2: Block Diagram of a
Simple Csound Instrument

The actual Csound orchestra file for this simple instrument is show in figure 3.

Figure 3: Csound .orc file of Simple Instrument shown in Figure 2

In general, the "syntax" of the Csound orchestra statement is as follows:

output opcode arguments

Each opcode has a unique set of parameters. For instance the linen opcode has the following syntax:

k/ar linen k/xamp, irise, idur, idec

This means that the linen opcode can render its output at either the control or audio rate (k/ar). It also indicates that the amplitude can be dynamically altered by another opcode at the control or audio rate, or be set to a constant value (k/xamp). Finally, it shows that the rise time, duration, and decay times must be set as constants when the instrument is "initialized" (when a note is played), and cannot be altered during the course of the note (irise, idur, idec).

The oscil opcode has the following arguments:

k/ar oscil k/xamp, k/xcps, ifn[, iphs]

As with linen, the output of the oscil opcode can be rendered at either the control or audio rate (k/ar). The amplitude can be either a constant or modified at the control or audio rate (k/xamp). The frequency can be either a constant or modified at the control or audio rate (k/xcps). And the wavetable that the oscillator will play is set at initialization time and cannot change during the course of a note (ifn). Also, we see that we can optionally set the oscillator's phase offset to begin reading the table at 45, 90, 180, etc., degrees (iphs).

As you can see, in Csound, opcodes set their input parameters on the right and output their result on the left. Also, outputs can be rendered at either the lower resolution control rate (indicated by a "k" output variable (kr), or the sampling or "audio-rate" (indicated by the "a" output variable (ar).

Furthermore, Csound allows you to set and reset the parameters of an instrument from p-fields in the .sco file. We have done this by assigning the frequency argument of the oscil opcode to p5 in the instrument above. Now, the fifth column of the note list can be used to reset the frequency of the instrument on a note-by-note basis. Similarly, because the ifn argument is assigned to p6, we can use the sixth column in the note list to change the oscillator's waveshape of every note too. Figure 4 shows the score for our simple oscillator and envelope instrument. Notice that p7 and p8 are being used to set the attack and release time of the linen opcode.

Figure 4: A Csound score file with user-defined "p-fields" for amplitude, frequency, waveshape, attack and release-time.

It is important to understand and appreciate the fact that of Csound's input arguments can be updated at the control rate. This means that the output of one signal generator can be used to dynamically modify a k-parameter of a signal generator or modifier during the course of a note. In the case of our first instrument, we could have chosen to modify the amplitude and the frequency of the oscillator at the k-rate, but we have only chosen to modify the amplitude in this way. We are applying an amplitude envelope to the oscillator by "patching" the k-rate output of the linen opcode into the k/xamp amplitude argument of the oscil.

  [next] Csound Basics: Simple Granular