Chapter Four: Synthesis

14. A Digital Synthesis Language Sampler | Page 10



Csound was written by New Zealand-born composer/computer scientist Barry Vercoe in 1985.  It was based on his Music 11, which Vercoe wrote (with others) in 1973 for the smaller DEC PDP-11 mini-computer at MIT.  Music 11 had grown out of Vercoe’s Music 360, a port of MUSIC IV for the Princeton IBM 360 computer in 1965, and so Csound is considered to be part of the MUSIC-N lineage.  As the name implies, Csound was written in the c programming language, and could be compiled on just about any computer with a c language library.  It is therefore not machine- or processor-specific.  Csound has undergone several incarnations, with many contributors over the years including Richard Karpen, Matt Ingalls,  John ffitch, and Richard Boulanger (who wrote/edited the definitive The Csound Book in 2000).  The current application version as of this writing is Csound 6.


Csound features nearly 2000 generators called opcodes.  It is capable of standard synthesis and audio processing tasks, but can also perform many higher-level techniques as well.  These include phase vocoding, linear predictive coding (LPC), and FOF.  Originally Csound was exclusively a “non-real-time” environment.  Like MUSIC V, it used the orchestra/score separate file division.  The files were run via a command line, or GUI program interface. An audio output file was generated, which could then be played back following the render.  Csound could also generate non-audio sound analysis files for use with phase vocoding and LPC, and read existing audio files into its wavetables. CsoundQt, which includes graphic widgets as well, is the current GUI of choice and can run Csound in real time.

Csound 5 (2006) added the ability for real-time performance via MIDI, and Csound 6 (2013) added the ability to not only have the score modified during a performance, but also the orchestra, opening the door for live coding performances.  Additionally, Csound 6 added a unified file structure whereby the orchestra and score could be contained and run from a single file using markup language.

Sample Code with Explanation

Csound's code structure is very similar to MUSIC V and should be quickly recognizable if you have made your way through the previous section. Whether you are using an IDE GUI shell, or terminal command line, the orchestra and score syntax will be the same.

Csound’s extensive code descriptions can be found in the Canonical Csound Reference Manual. Here you will find the syntax and arguments for the orchestra and score, and in particular, manual pages for the vast numbers of opcodes and GEN functions, with code examples of their use.  For example, the oscil simple oscillator opcode used below is listed in the manual as:

    ares oscil xamp, xcps [, ifn, iphs]
    kres oscil kamp, kcps [, ifn, iphs]

meaning use an a-rate audio rate) or k-rate (control rate) variable assignment for the opcode's result, then the oscil opcode itself followed by its required and optional comma-separated arguments: raw amps (or a conversion equivalent), frequency in cps (or a conversion equivalent), and optionally the function table number ifn (if omitted, uses a sine), and the starting phase iphs (0-1).  The arguments that start with an x mean the value can be a control (k) or audio (a) rate variable, or a fixed value.  Those that start with an i are evaluated only once at the beginning of the note, meaning here that you can’t change function tables in the middle of a note sounding.

The orchestra begins with a header that specifies the project’s sampling rate, control rate, # of samples per control period and the number of output channels, then the orchestra itself where instruments are defined, and finally the score to create the function tables and play those instruments. Csound uses inline comments following a semicolon.

Orchestra Header
sr = 44100 ; sample rate
kr = 4410   ; control signal rate.
ksmps  = 10  ; samples per control signal
nchnls = 2  ; number of output channels. This project is stereo, so project will use outs opcode for instruments.  Use out opcode for mono.
instr 1 ; instrument 1 definition.  An instrument that will simply play a periodic signal from a wavetable.
asig oscil p4, p5, 1 ; place result in audio-rate variable asig, use score p-field p4 for raw amplitude, p5 for frequency in cps, and use waveform from wavetable 1.
outs asig, asig   ; outputs asig to file or live sound in stereo
endin ; end instrument 1 definition
f1 0 8192 10 1 ; create table 1 a time 0, table size 8192, using GEN 10, which creates the sum of sines (so here, just one fundamental sine wave at full strength)
i1 0 2 10000 440 ; play a note using instrument 1 (p1), starting at time 0 (p2), for 2 seconds (p3), at 10000 raw amps (p4), at 440 Hz (p5)*
i1 2 3 5000 880 ; play a note beginning at second 2, for 3 seconds at amplitude 5000 and 880 Hz
e ; end score

A slightly more complicated example uses three new concepts.  An envelope-producing opcode called linen which will be used at k-rate (control rate), a frequency-to-pitch-class converter called cpspch, and performing inline math to chorus a second oscillator (ifreq * .99) using a previously defined variable (ifreq). Linen automatically subtracts attack and release time from total note duration specified by p3 and rises to and decays from a peak amplitude (p4) from a zero amp baseline. 

instr 2 ; instrument 2 definition
ifreq = cpspch(p5) ; creates a variable called ifreq to use in p5 pitch field that converts pitch-classes to cps. p5 = 8.0 plays middle C, 8.1=C#, etc.
kenv linen p4, p6, p3, p7 ; p3=note duration, p4=peak amplitude, p6=attack time, p7=release
asig oscil kenv, ifreq, 2 ; use linen envelope from above to control amplitude, cpspch converter for pitch and wavetable 2 for waveform
asig2 oscil kenv, ifreq * .99, 2 ; slightly chorused version of oscil above, assigned to different variable (asig2), multiplies ifreq pitch by .99
outs asig, asig2 ; send asig left, asig2 right
endin ; end instrument 2 definition
f2 0 8192 10 1 .5 .2 ; GEN 10 sum of sines, first three partials in descending strength
i2 0 4 5000 8.0 .4 .2 ; at time 0, play a 4 second note, to a max amp of 5000, middle C pitch, with a .4 second attack and a .2 second decay
e ; end score

The examples above demonstrate some of the most rudimentary things Csound can do just to pique someone’s interest.  The resources below can lead you to more extensive tutorials and more complex orchestra and score examples.

*The discerning reader may have noticed that Csound combines the first two note-instrument# p-fields (p1 and p2) of MUSIC V (eg. NOT 1) into a single note-instrument # p-field p1 (eg. i1), so that the note start time and note duration are now p2 and p3 respectively, not p3 and p4 as in MUSIC V.

Resources (tutorials, Csound download and more)
Canonical Csound Reference Manual
Introduction to Sound Design in Csound Richard Boulanger Chapter One (excellent introductory explanation and tutorial examples from one of the csound masters)
Csound Online Interative FLOSS manual
CsoundQt (current best Csound multi-platform GUI programming environment as of this writing)