Keith Hamel's Csound Course

Week 5

In this tutorial:


Index Next Week Previous Week


Program Control Statements

At times it is useful to include conditional statements in an instrument definition. Conditional statements allow different parts of code to be processed under different circumstances. Conditional statements take the form:

if (something is greater than, equal to or less than something else)
goto somewhere

the conditional part is an expression using the following operators:

>      greater than
<      less than
==     equal to
>=     greater than or equal to
<=     less than or equal to
!=     not equal to

This statement is placed in parentheses ( ) following the if. After the statement, there is a goto followed by a label. The goto can operate at a-time(goto), k-time (kgoto) or i-time (igoto). The label can be any word consisting of lower case letters. The label must appear somewhere else in the instrument definition followed by a colon -- this marks the place to which the program jumps whenever the condition is true. When the condition is true the code between the if--goto and the label are skipped.

if (p3 >= 1.0) goto longnote

; the code for a short note goes in here ------- 

longnote:

; the code for a long note goes in here ------ 

Conditional statements can be used to provide several different envelopes for an instrument. For example, if you wanted 3 different envelopes within a single instrument, you could set a parameter in the score to tell the instrument which envelope to use. Once you have jumped to a label and processed the appropriate lines of code, you must make sure your jump passed the other envelope definitions with another goto, kgoto, or igoto expression.

(a value of 1, 2 or 3 is passed from the score in p7) 

if    (p7 == 1)  kgoto   envelope1
if    (p7 == 2)  kgoto   envelope 2
if    (p7 == 3)  kgoto   envelope 3
envelope1:
k1    linseg     0, p3 *.5, 1, p3*.5, 0     ; up then down
kgoto mainpart
envelope2:
k1    linseg    1, p3 *.5, 0, p3*.5, 1     ; down then up
kgoto mainpart                             ; jump to audio part
envelope3:
k1    linseg    1, p3, 0                   ; down in p3
kgoto mainpart                             ; this is redundant
mainpart:
asig  oscil     p4 * k1, cpspch(p5), 1;    ; main oscillator
outs     asig, asig                        ; output to file

In the above example, all three envelopes use the same variable name (k1) since that variable is needed in the oscil. If you used different variable names, the oscillator would not always have a value for the variable.

goto top Index


Table

The table generator provides access to any function (f- statement) defined in the score. These functions may be generated by any GEN routine, and can be accessed at a variety of rates. (In fact, an oscil is really just a table-lookup generator which reads quickly through a sinewave table).

goto top Index


GEN 2

To the GEN-routines introduced thus far, we add GEN 2. This routine allows discrete values to be loaded into a table. The table size must still be a power of 2 or power or 2 + 1. Usually raw values (i.e. unscaled values) are specified. If 2 appears as the fourth parameter of the f-statement, the values are normalized (i.e. scaled to 1), if -2 appears as the fourth parameter, the actual values are loaded into the table. Normally, unscaled values are desired in Gen-2, so -2 is used.

f1   0   16   -2   9 1 3 5 2 8 7 10 2 0 4 3 5 7 11 8
f2   0    8   -2  .01 .04 .03 .05 .00 .06 .02 .07

goto top Index


Phasor

Although the description of phasor in the manual is somewhat confusing, it is best understood as a generator than provides an index into a table - you specify the speed at which the phasor moves through the table. If the table is a sine wave and the phasor moves quickly through it, you have an oscillator. If the table contains a small number of discrete values, and the phasor speed is slow, you can walk through table at a slow speed.

(uses f-2 containing .00 .03 .05 .02 .06 .07 .04 .01)

k1     phasor   2                         ; speed of phasor is 2 cps
kptch  table    k1* 8, 2                  ; read table f-2 with phasor
asig   oscil    p4, cpspch(p5+kptch), 1   ; oscillator


goto top Index


Oscil1

A similar effect can be created by oscil1 which reads through a table once in the duration specified. You can also indicate the delay on the first table values before the rest of the values are read.

kptc   oscil1   0, 1, 3, 2                 ; read function 2 in 3 seconds
asig   oscil    p4, cpspch(p5+kptch), 1    ; oscillator

goto top Index


Suggested Assignment

Create a new orchestra containing 3 instruments. At least one must contain conditional statements for different envelopes, and one should use a phasor or oscil1 to read values from a table. (N.B. The values need not be applied to pitch). The score should be designed to demonstrate the instruments. The total duration of the score should be about 1 minute.


goto top Index Next Week Previous Week