Main Index
Tutorials Index

PART 2
Simple Guide to Csound:

STRUCTURE
  Orchestra
  Score
  Example

OSCIL Examples
  Melody
  Synthesis
  Decrescendo
  Synchronisation
  Beats
  Overlapping Beats
  Stagger

Appendix: Running Csound

PART 1
Origins of Csound

PART 3
Signal Processing


CDP Logo

~ GETTING STARTED WITH CSOUND ~
by Andy Hunt

PART 2 – Simple Guide to Csound Operation

  • STRUCTURE
    • The 'Orchestra'
    • The 'Score'
    • Example Orc & Score

  • OSCIL Examples
    • Ex. 2: A 3-note melody
    • Ex. 3: Additive synthesis
    • Ex. 4: Amplitudes diminish
    • Ex. 5: Simultaneous starts
    • Ex. 6: Creating beat frequencies
    • Ex. 7: Overlapping beat frequencies
    • Ex. 8: Staggered entries / altered frequencies

  • Appendix: Running Csound

The aims of this Part are to:

  • introduce the concepts of 'score' and 'orchestra'
  • show how to proceed logically through the tutorials, and
  • how to access Csound on the computer.


Csound, the structure of the language

The key concepts are those of the 'orchestra' and the 'score'. They are separate entities but are used in a way which provides great power and flexibility.

Orchestra

The orchestra has two sections: headers and instrument definitions.

The header deals with global issues: the sample rate, the number of channels etc. Here's an example. By the way, anything after a semicolon ';' is called a 'comment' and is disregarded when the program runs. For other Falcon sample rates, see p.14 of the Csound manual.


sr   = 22050	;the sample rate is set at 22050 samples per second  – Falcon:  24585
kr     = 1470	;there will be 1470 control points per second  – Falcon:  1639
ksamps = 15	;i.e., there will be a control point every 15 samples: sr\kr
nchnls   =  1	;there will be one channel, ie mono

The second part of the orchestra defines an instrument. The following shows how a simple oscillator is defined.


instr 1			   ;we are now going to define instrument 1
a1 oscil  10000, 440, 1	   ;make an audio rate oscillator with an amplitude of 10000, at a
			   ;frequency of 440 Hz, using wavetable number 1  (defined later)
	  out a1	   ;send audio output to the real world
endin			   ;end instrument definition

Seen as a flow chart, this instrument shows amplitude, frequency and wavetable inputs being joined together and producing sound samples as a1.

[Flow diagram of the OSCIL function]

Score

In the score part of the language, we say what this instrument is going to do. This comprises:

  1. defining the wavetable used for the sound
  2. timed instrument triggers, ie sound / note events

The wavetable definition is called a function table (hence the use of an 'f'). Here's how we write the definition of 'wavetable no. 1, to exist from time 0, with 4096 elements, using GENerator function no. 10, with a particular setting for the relative strength of the harmonics' (in this case, just a single fundamental):


;name start size  GEN  rel. strength
f1    0     4096  10   1

Now in the second part of the score we'll define a note event for instrument 1, starting at time 0, and lasting for 4 seconds:


;instr start duration
i1     0     4	;NB - For this example the amplitude and pitch are stated in the .orc file above;
		;We shall see that this is perhaps not the best way of doing things.
e		;end of score

Listen to this after compiling it. Pretty simple isn't it? Yes it is, you exclaim, and isn't it a rather fiddly way to achieve very little? Well, hang on in there, please. Much, much more effective results are around the corner as we learn to speak the Csound language.

A first glimpse of the power of this 'synthesis engine' can be seen in the following orchestra & score.

Example Orchestra & Score

Csound orchestra: (NB - we'll be using this same orchestra with all the scores in Part 2)


;osc1.orc - making our musical instrument
sr  = 22050  		;22050 samples per second  – Falcon:  24585
kr    = 1470		;1470 control points per second  – Falcon:  1639
ksmps = 15		;yielding  15 samples per control period
nchnls =  1		;mono

instr 1
a1 oscil  p4, p5, 1
     out    a1
endin

Csound score:


;osc1.sc - making our score or event list

f1  0    512  10   1		;this is the wavetable definition

;p1    p2    p3  p4    p5	;this comment shows which pfields are being used
;instr start dur ampl  freq	;this comment names what the pfields are
i1     0     3   3000  440	;this line is the actual note event definition in which i1 connects with instr 1 		;of the orchestra  – an orchestra can have more than one instrument!
e				;the e marks the end of the score

Note here that the line defining the note event has 5 columns, known as 'pfields' (p for 'parameter'):

  • instrument (p1)
  • start time  (p2)
  • duration    (p3)
  • amplitude  (p4)
  • frequency  (p5)

The definition of the oscillator in this orchestra does not give fixed numbers for the amplitude and frequency as before, but asks the score for this information, ie in pfields 4 and 5 (put in bold print here in order to emphasize this interaction of score and orchestra). In this way, the instrument definition can comprise not just one note always the same, but all the changing values listed in the score.

The way you will compile and run your orchestra and score will vary from system to system. A review of how to run Csound on the CDP System is given in Appendix I.


Scores using OSCIL

Now let's get to work. Our approach will be to explore a simple orchestra very thoroughly. We will hopefully learn to use it so well that, although in itself very simple, we can get beyond the mechanics to some reasonable musical results. Mastery of Csound's simplest commands in this way provides the best way to get really serious results in the future. This approach is a long way from handing you the Csound manual and saying, 'Good luck!', or even from looking at each available Csound command methodically, but without really exploring what it can do. We'll have a lot more confidence if we can really ring the changes with each command. Then we will really know that we're beginning to master its use.

Our starting point is the OSCIL we've defined above. We won't change the orchestra definition, but we'll ring the changes with a variety of scores.

Example 2


;osc2.sc – a three note melody

f1  0  512  10  1

;p1    p2    p3       p4   p5
;instr start duration ampl freq
i1     0     3        3000 440
i1     3     1        3000 493   ;why do you think 'start' is 3?
i1     4     4        3000 440
e

Now 'compile' with Csound and listen to the results. It is very important to do this with each example. Note that we're using the same osc1.orc with each new score. We compile with Csound with the following command line:

csound -oosc2 osc1.orc osc2.sc

Now we have succeeded in creating more than one note event. Next we try to add some timbral richness to the sound.

Example 3


;osc3.sc - additive synthesis

f1   0    512  10   1

;p1    p2    p3       p4   p5
;instr start duration ampl freq
i1     0     12       3000  220
i1     2     12       3000  440
i1     4     12       3000  660
i1     6     12       3000  880
i1     8     12       3000 1100
i1    10     12       3000 1320
e

To compile with Csound:  csound -oosc3 osc1.orc osc3.sc

You can hear how these note events overlap because the last one comes in while the first one is still sounding. All the harmonics (integer multiples of the fundamental frequency: 220 x 2, x 3, x 4, etc.) are, however, at the same amplitude, something which doesn't happen in acoustic instruments. The overlap and the amplitudes are illustrated in the diagrams below.

[Diagram for Score 3]

Example 4

Let's try to make the sound more natural in the next score by reducing the amplitude of the higher harmonics (but keeping everything else the same):


;osc4.sc - amplitudes diminish

f1  0  512  10  1

;p1    p2    p3       p4   p5
;instr start duration ampl freq
i1     0     12       3000 220
i1     2     12       1500 440
i1     4     12        700 660
i1     6     12        350 880
i1     8     12        175 1100
i1    10     12         85 1320
e

To compile with Csound:  csound -oosc4 osc1.orc osc4.sc

Now the effect is rather like that of an arpeggiated chord. Again, this is illustrated graphically in the diagrams below.

[Diagram for Score 4]

Example 5

Let's listen to what happens if we make all the harmonics come in at once. All we have to do is make all the start times the same. Then all these note events will sound simultaneously:


;osc5.sc - simultaneous starts

f1  0  512  101  1

;p1    p2    p3       p4    p5
;instr start duration ampl  freq
i1     0     12       3000  220
i1     0     12       1500  440
i1     0     12       700   660
i1     0     12       350   880
i1     0     12       175   1100
i1     0     12       85    1320
e

To compile with Csound:  csound -oosc5 osc1.orc osc5.sc

When you listen to this, you may observe that the partials now begin to fuse into one sound.

Still the same 2-line orchestra, but now the tone is richer. The diagrams show the simultaneous frequencies and the diminishing amplitudes.

[Diagram for Score 5]

Example 6

Our next OSCIL score explores the creation of beat frequencies. When two frequencies which differ by a single cycle per second sound together, you can hear this 1 cycle difference as an additional 'beat' pulsing through the sound. In the score which follows there are 9 sets of beating note pairs, each of which is 5 seconds long. Notice how, as the beats get faster, this pulsation begins to fuse with the sound, creating a new, timbral dimension, which we normally refer to as a 'chord'.


;osc6.sc - creating beat frequencies

f1  0  512 10  1

;p1    p2    p3   p4    p5
;instr start dur  ampl  freq
i1     0     5    3000  220
i1     0     5    3000  221

i1     6     5    3000  220
i1     6     5    3000  221

i1     12    5    3000  220
i1     12    5    2800  222

i1     18    5    3000  220
i1     18    5    1500  224

i1     24    5    3000  220
i1     24    5    2800  228

i1     30    5    3000  220
i1     30    5    3000  236

i1     36    5    3000  220
i1     36    5    2800  256

i1     42    5    3000  220
i1     42    5    3000  273

i1     48    5    3000  220
i1     48    5    3000  330
e

To compile:  csound -oosc6 osc1.orc osc6.sc

Example 7

Now let's try to develop this 'complex tone' feature. In the following score we're going to increase the beating factor by creating groups of three frequences, one less and one more than the original. They will all start together, but the higher ones will end sooner, just as often happens in real sounds. See if this doesn't begin to sound to you like a single complex sound; in each of the four harmonically related partials, we hear the sound move (change) over time due to the different beat frequencies:


;osc7.sc - complex, overlapping beat frequencies

f1  0  512  10  1

;p1    p2    p3   p4    p5
;instr start dur  ampl  freq
i1     0     11   3000  219
i1     0     11   3000  220
i1     0     11   3000  221
                                      
i1     0     9    3000  436
i1     0     9    3000  440
i1     0     9    3000  444
                                     
i1     0     7    3000  650
i1     0     7    3000  660
i1     0     7    3000  670

i1     0     5    3000  860
i1     0     5    3000  880
i1     0     5    3000  900
e

To compile:  csound -oosc7 osc1.orc osc7.sc

Example 8

In the next version, it becomes more complex yet by staggering the entries and adjusting the frequencies:


;osc8.sc - adding staggered entries and altered frequencies

f1  0  512  10  1

;p1    p2    p3   p4    p5
;instr start dur  ampl  freq
i1     0     11   3000  219
i1     0     11   3000  220
i1     0     11   3000  221

i1     0      9   3000  436
i1     0      9   3000  440
i1     0      9   3000  444

i1     3      7   3000  630
i1     3      7   3000  640
i1     3      7   3000  650

i1     6      5   3000  820
i1     6      5   3000  840
i1     6      5   3000  860
e

To compile:  csound -oosc8 osc1.orc osc8.sc

I now suggest that you try your own variations of all the examples we've looked at so far. Explore the ideas until you feel really ready to move on.

In the 3rd Part of this tutorial, we'll move on to some basic signal processing operations.


Appendix:  On Running Csound on the CDP System – a quick synopsis

If using Winsound on the PC, which has a minimal graphic interface, you enter the orchestra, score and output soundfile names in the boxes provided and tick 'play at end'. The orchestra and score should be in the same folder as 'winsound.exe', and this is also where the output soundfile will be placed. If you run again with an altered orchestra or score, it will overwrite the named soundfile unless you change the name. [The following procedures are probably redundant or optional with current versions of Csound. Check out the Csound website.]

Now we need to make this sound and listen to it. This means accessing our computer successfully, running Csound, and playing the result. A big step. Please revise Mastering the CDP System I if you have any doubts about how to proceed. Once the environment is set up, the basic steps for working with Csound are:

1. Use your text editor to edit your Csound orchestras and scores

2. Compile the orchestra and score: Type in the command line (or create a 'perf' batchfile to run Csound. The perf.bat file is fully explained in Mastering the CDP System I. The batchfile to play different scores with the same orchestra will be especially useful with this tutorial. To save you looking it up, it is:

Name: perf2.bat (or whatever), containing:

csound -o%1.out %2.orc %3.sco

and to run it you enter:

perf2 outname orcname scorename (without any extensions)

The normal perf.bat (when both orchestra and score have the same name) is:

csound -o%1.out %1.orc %1.sco

and to run it you enter:

perf generic_name (without any extensions)

(The '.out' in the outname is optional. It is sometimes used to identify soundfiles made with Csound.)

3. PLAY the sounds.

© 1993 Andy Hunt, York, N. Yorks., England
Appendix slightly edited, 2 November 2010

Last Updated Jan. 2016 -- HTML5 version
Revisions: Robert Fraser