Csound Tutorial

Csound format is a mixture of case sensitve html and assembly language with its own special opcodes. Semicolons are used for comments

First you need to start with a <CsoundSynthesizer> block (Case matters). This will contain all the csound instructions.

Options

Then you need a <CsOptions> block. It should contain the default command line options for the file. -odac (output digital/analog converter) outputs to sound card. --ogg outputs to an ogg vorbis file, -o filename specifies filename. --wave outputs to .wav format. Example:

<CsOptions>
--ogg -o kitty.ogg
</CsOptions>

or, if you just want to hear the output immediately from your sound card:

<CsOptions>
-odac
</CsOptions>

Instruments

Another block you need is called <CsInstruments> It contains the definitions for instruments (basicly functions which produce sound) and some parameters for sound output. sr is the sample rate. ksmps number of samples in a control period--I think this has to do with buffering and how fine grained you want to vary instruments.

nchnls number of channels. 1 for mono, and 2 for stereo. 0dbfs indicates the value which will be the maximum amplitude (means @ 0 decibles and I don't know what fs is short for). Those are the basic parameters to get started.

Now on to the actual instruments...

You start an instrument with a instr command. The parameter is the number of the instrument. It is finished with a endin command. There are many functions for producing sound such as fractalnoise and oscils. They usually go in the format: ares function parameters, where ares is the output value variable. After your functions, you will want to use the outs command to write your samples to the output, one for each channel.

<CsInstruments>
sr=44100
ksmps=100
nchnls=2
0dbfs=1

instr 1
seed 0
aoutl fractalnoise 0.1,2
aoutr fractalnoise 0.2,0
outs aoutl,aoutr
endin

instr 2
aout oscils 0.2,1000,0
outs aout,aout
endin 

instr 3 ; you can also use math to combine two functions
aout1 oscils 1.0,100,0
aout2 fractalnoise 0.2,1
aout3=aout1*aout2
outs aout3,aout3
endin

</CsInstruments>

Score

The next block will be <CsScore> It tells csound which notes are to be played when. You indicate each note by the letter i and a number: i1 is the first instrument, i2 is the second, etc. Each note takes several parameters. If you didn't define any params yourself, a note takes two: the starting point of the note and its duration. For example, a line i1 0 2 would indicate the first instrument, starting at 0 time offset (the begining of the file), and last for two seconds. For the last bit, put a line which says just the letter e I think "e" is for execute?

<CsScore>
i1 0 2
i2 2 2
i3 4 2
e
</CsScore>

Now finish with a </CsoundSynthesizer> and you are done!

Example csd file: tutorial.csd, ogg vorbis output: tutorial.ogg.

line segment

The linseg function allows you to essentially graph a series of imaginary line segments for use as a variable. It allows you to vary a parameter of another function. It is in the form:
variable linseg value1, duration1, value2, duration2, value3 ...etc
Take for example this code, which varies the amplitude (volume) of a sine wave:

<CsInstruments>
sr=44100
ksmps=100
nchnls=1
0dbfs=1

instr 1
kitty linseg 0.0,p3/2,1.0,p3/2,0.0
aout poscil kitty, 1000
outs aout
endin


</CsInstruments>

<CsScore>
i1 0 6
e
</CsScore>

The poscil function had to be used because apparently the more simple oscils function doesn't work well with variables. Notice the p3/2 entries. p3 indicates the total duration parameter of the note, and it is divided by 2 because there are 2 segments and they should last the exact length of the note.