CSound Tutorial: Instruments and Score Code

by Jake Hills

Csound is one of the most powerful computer programming languages for generating and processing sound. There is almost nothing in the realm of digital audio that Csound can not do. It was developed by Barry Vercoe at MIT in the mid 1980s and was based on previous work by Max Mathews called Music-N.  Csound is free and open source, and is tended to by an experienced core of programmers and musicians and is supported by a large online community. Because of that community, help is readily available for anyone who wishes to get started with audio programming. This tutorial will teach how to make sound in Csound.

Csound does not have it’s own GUI (graphical user interface).  One can not simply double click and open Csound.  The easiest way to use Csound is through what is called a front end, or an integrated development environment or IDE for short. Most installations of Csound come with an IDE called CSoundqt.  Although there other IDEs available, this program is a fine tool for working with Csound.  Upon opening Csoundqt, the user is greeted with this:

<CsoundSynthesizer>
<CsOptions>
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 128
nchnls = 2
0dbfs = 1.0

</CsInstruments>
<CsScore>

</CsScore>
</CsoundSynthesizer>

Like in other programming languages, we see a bunch of opening tags, and closing tags.  At first, it can be confusing to look at something like this. Below is the same code, except with added comments which make the layout easier to understand.

<CsoundSynthesizer> ; everything under this tag, is part of the 
Csound program

<CsOptions> ; Under the CsOptions tag, we put specific options 
that we want.  We don’t need any to make sound for now.

</CsOptions> ; this is the close of the options tag

<CsInstruments> ; under this tag, we put the definitions for the 
instruments that we will make.

sr = 44100 ;Here, we set the sample rate to 44.1khz
ksmps = 128 ;Think of this as a buffer.
nchnls = 2 ;This is how many channels we produce. Leave this set 
at 2 for now.
0dbfs = 1.0 ; This is 0dB Full Scale.  When set to one, that meansmaximum amplitude for your program is 1.

; Here is where create our instrument.

</CsInstruments> ; Here is the end of the instrument tag.

<CsScore>; Here is where we tell Csound what to do with the 
instruments.

; This section is broken up into columns.  The first is tells 
csound which of the instruments to play, the second is when to 
begin, and the third is how long it lasts.  Any subsequent columnsare assigned by the programmer.

</CsScore> ; This is the end of the score section
</CsoundSynthesizer> ; This tag closes the Csound file.  Any 
information after this tag will be ignored by the compiler.

Now that we have a basic understanding of the layout of a Csound file, it is time to make a little noise.  Inside the <CsInstruments> tag, below 0dbfs = 1.0 is where we make our first instrument.  It may look something like this:

instr 1 ; This is the name of the instrument.  We just called it 
1.

aSig poscil 0.5, 440 ; Here, we use the opcode ‘poscil’ which can take two arguments, in this case amplitude (0.5) and frequency 
(440) and generates a sine wave and puts it in something called 
aSig.

outs aSig, aSig ; Here, whatever was in aSig is sent to the 
speakers using ‘outs’.

endin ; This is the end of the instrument we have created.

With our first instrument created, we now need to tell Csound what to do with it. This is done in the <CsScore> section. The first three columns here will tell Csound what is playing, when it plays, and how long it lasts.  For example:

;what when length
 i1   0    2

This code says to play an instrument called 1, which we made in the (CsInstruments> tag, beginning at time 0 for 2 beats.  By default, Csound is set at 60 beats per minute, so 2 beats equals 2 seconds for now.  Here is the completed code without comments:

<CsoundSynthesizer>
<CsOptions>
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 128
nchnls = 2
0dbfs = 1.0

instr 1
aSig posil 0.5, 440
outs aSig, aSig
endin

</CsInstruments>
<CsScore>

i1 0 2

</CsScore>
</CsoundSynthesizer>

We can now run the code by pressing command (or control) + enter.  We should then hear a sine wave with a frequency of 440 hz for 2 seconds.

Csound is extremely powerful, and once mastered, it can be used to do just about anything with audio imaginable.  This tutorial covered one of the most basic skills in Csound.  One of the best resources about learning how to use csound is found on the internet at http://en.flossmanuals.net/csound/ .  A complete list of opcodes with examples can be found here: http://www.csounds.com/manual/html/ .  With these resources, a little time and effort, Csound can be a powerful tool in audio production and processing.

Advertisements

Author: dndrew

Orchestral, chamber and interactive music composer Digital musical instrumentalist Real-time software systems designer Computer music educator

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s