MUSIC TECHNOLOGY AND COMPUTERS
Computer Project No. 2

Csound


Below you will find some of the Csound orchestra and score files presented in class, plus a few others. The actual content of each file is the text inside the areas bordered by lines of asterisks. (You can copy each one out of your browser and save them as individual text files.)

Try them out yourself, then begin making some slight modifications to make your own sounds, as suggested in class (and as suggested by the comments below). To help your understanding of Csound, read chapters 0 and 1 of the Csound manual, and the Tutorial by Richard Boulanger in the Appendix of the manual, which can be found online in the Csound folder on computers 1-3 of the Music Media Center, and a printed version of which is also on reserve in the MMC.

The "official" assignment, due November 25, is to design an FM instrument (naturally it can be based on the design presented in class) that sounds like a real instrument, and play a brief melodic excerpt on it. Work first on understanding the instruments provided, then try changing one or two things at a time to hear the effect. Use the information in the Chowning article on simulating real instruments to help you in designing the amplitude envelope, choosing the harmonicity ratio, etc. I suggest trumpet as a relatively easy one to start on; bell and drum sounds work well, also. Experiment with finding just the right amplitude envelope, try different modulation indexes for different timbres, etc.

You may very well want to team up with one or more classmates (try to find someone who seems to understand this stuff better than you do) and work together at first. As you work, keep track of questions and problems you encounter. We'll discuss your questions in class on November 20.


The first orchestra file is an instrument that plays sine tones. Note that the "oscil" generator (an oscillator) requires three bits of information: amplitude, frequency, and function (wave shape). The amplitude and frequency of the sine wave is determined by p-fields 4 and 5 in the score file; the function is also defined in the score file.

********************ex1.orc************************
sr = 44100 ; set the audio sampling rate
kr = 2205 ; set the control rate, for non-audio controlling functions
ksmps = 20 ; must equal sr/kr

        instr   1 ; begin instrument #1
awaveform       oscil   p4*32767,p5,1 ; oscil sends its output to "awaveform"
        out     awaveform ; output jack, gets its input from "awaveform"
        endin ; end instrument #1
**************************************************

Here is a score file that fills function table 1 with a single sine wave, then plays three notes on instrument 1. (Note that this score file will work for all of the first three orchestra files.)

********************ex1.sco************************
f1      0       1024    10      1
; fills a 1024-number table with a single cycle of a sine wave

; p1=ins        p2=start        p3=dur  p4=amp  p5=freq
i1      0       1       1       440 ; 1-second A-440 at full amplitude
i1      1       .5      .5      660 ; 1/2-second E-660 at 1/2 amplitude
i1      2       2       1       880 ; 2-second A-880 at full amplitude
e ; end of note list
**************************************************

In example 2 we modulate (change) the frequency of the main oscillator (called the carrier oscillator) with another oscillator (called the modulating oscillator). We do this by adding the output of the modulator to the frequency of the carrier. The frequency of the modulator determines the RATE of modulation (vibrator rate) and the amplitude of the modulator determines the DEPTH of the frequency deviation it will cause. By adding in a low-amplitude, low-frequency sine wave as the modulator, we cause a slight vibrato in the carrier oscillator's frequency. (The vibrato has a deviation depth of 1% of the carrier's frequency--about 1/5 of a semitone, in musical terms--and a rate of 6Hz.)

********************ex2.orc************************
sr = 44100
kr = 2205
ksmps = 20

        instr   1
avibwave        oscil   p5*.01,6,1 ; sine modulator, amp=1%, freq=6Hz
awaveform       oscil   p4*32767,p5+avibwave,1 ; add modulator's output to freq
        out     awaveform
        endin
**************************************************

In example 3 we control the over-all amplitude of the carrier oscillator with a downward ramp. The ramp is just a line going from 1 to 0, over the duration of each note. The amplitude of the carrier sine wave at each instant is multiplied by the value of the ramp at that instant, so the over-all amplitude of the sine wave decreases down to 0 over the course of each note. This ramp is not an audio wave that we will actually listen to; it's just a value that controls the amplitude of an audio wave, so we use the control rate for it (as shown by the fact that its output name begins with a "k").

********************ex3.orc************************
sr = 44100
kr = 2205
ksmps = 20

        instr   1
kampenv line    1,p3,0 ; duration of the ramp = duration of each note
avibwave        oscil   p5*.01,6,1
awaveform       oscil   p4*32767*kampenv,p5+avibwave,1
        ; multiply amplitude by ramp value
        out     awaveform
        endin
**************************************************

Examples 2 and 3 are examples of frequency modulation (a.k.a. FM), which means modulating the frequency of the carrier oscillator with the output of the modulating oscillator. In those cases we used a LOW FREQUENCY modulating oscillator (a.k.a. LFO) at low amplitude to create a subtle vibrato at the rate of 6Hz (6 sinusoidal up-down movements) per second.

However, a different phenomenon occurs when the modulating oscillator is itself at an audible frequency (and especially when it also has a large amplitude). This creates what are called "sidebands": the presence of additional frequencies around the carrier frequency. The frequency of those sidebands is directly dependent on the frequencies of the carrier and modulating oscillators. The sideband frequencies can be calculated as c+m, c-m, c+2m,c-2m, c+3m,c-3m,etc. These relationships can essentially be summarized with a single number: the ratio of carrier frequency to modulating frequency (Fcar/Fmod) is called the "harmonicity ratio", and will determine the relationship between the different partials. Another important factor, which determines the NUMBER of sidebands, is how strong the amplitude of the modulating oscillator is, relative to its frequency: the ratio Amod/Fmod is called the "modulation index". The harmonicity ratio and the modulation index are the main factors in determining the number, placement, and relative strength of the different partials of the resulting sound.

In example 4, we add in a (high frequency) modulating oscillator to create these sidebands (in addition to the LFO we already used to create subtle vibrato). We want the modulating index and harmonicity ratio to be specifiable for each note (to control the timbre of each note), so we specify them as p6 and p7. So, in the instrument design, the amplitude and frequency of this new modulating oscillator will do the necessary calculations (based on the values of p6 and p7) to obtain the ratios needed (as described in the preceding paragraph).

Note that there are two different definitions of the "harmonicity ratio". In John Chowning's landmark article "The Synthesis of Complex Audio Spectra by Means of Frequency Modulation", he presents the index as "Fcar/Fmod". In F.R. Moore's primary text Elements of Computer Music he presents the "harmonicity index" (same intention) as "Fmod/Fcar". The idea is the same in either case: to express the relationship between the carrier and modulating frequencies. I used Moore's definition in class. These examples use Chowning's definition, which is the inversion of Moore's.

Since the harmonicity ratio = Fcar/Fmod, then it follows that Fmod = Fcar/harmonicity ratio. We know that Fcar is p5 and the harmonicity ratio is p7, so in the instrument we will set the frequency of the modulating oscillator to p5/p7.

********************ex4.orc************************
sr = 44100
kr = 2205
ksmps = 20

        instr   1
kampenv linseg  0,0.05,1,0.1,0.5,p3-0.25,0.5,0.1,0
amodwave        oscil   p6*p5/p7,p5/p7,1 ; p6=mod. index, p7=harm. ratio
avibwave        oscil   p5*.01,6,1
awaveform       oscil   p4*32767*kampenv,p5+avibwave+amodwave,1
        out     awaveform
        endin
**************************************************

In example 4 we also used a different kind of amplitude envelope. The "linseg" generator makes a shape out of a succession of line segments--unlike "line" which is just for a single straight line. For linseg you specify points on the y axis and transition times between points, in the format y1, t1, y2, t2, ... yn, ending with the final y point. In this example then, we start at 0, take .05 seconds to go to 1, take .1 second to go to .5, take p3-.25 seconds to go to .5 (i.e., we stay at .5 for that whole time), and then take .1 second to go back down to 0. Look at the amplitude shape of the resulting notes in Sound Edit 16, and you will see the shape described by this set of line segments.

We also need a new score file--one in which the note statements have p6 and p7 values--in order to make this new FM instrument work. Here's an example. Note that the first six notes have a harmonicity ratio of 1 (Fm will equal Fc) and increasing modulation index from 0 (no modulation) to 5. The last three notes have a harmonicity ratio of .707107 (1 over the square root of 2) to create an inharmonic spectrum. This score file also works for example 5.

********************ex4.sco************************
f1      0       1024    10      1

; p1=ins p2=start p3=dur p4=amp p5=freq p6=mod p7=harm
i1      0       1       1       440     0       1
i1      1       1       1       440     1       1
i1      2       1       1       440     2       1
i1      3       1       1       440     3       1
i1      4       1       1       440     4       1
i1      5       1       1       440     5       1
i1      6       1       1       440     1       .707107
i1      7       1       1       440     2       .707107
i1      8       1       1       440     3       .707107
e
**************************************************

In the fifth example, the shape of the amplitude envelope is changed slightly, and it's made directly dependent on the amplitude value given in the note statement (p4). Also (and more significantly), the same envelope is used to shape the amplitude of the modulating oscillator. This means that as the sound gets louder it also contains more partials (because its modulation deviation increases). This is true of most acoustic instruments, so it gives a more realistic timbre. In a new score file, try different p4 (amplitude) values for notes that are otherwise identical, to see how this affects the timbre of the note.

********************ex5.orc************************
sr = 44100
kr = 2205
ksmps = 20

        instr   1
kampenv linseg  0,0.01,p4,0.1,0.33*p4,p3-0.25,0.0625*p4,0.4,0
amodwave        oscil   kampenv*p6*p5/p7,p5/p7,1
avibwave        oscil   p5*.01,6,1
awaveform       oscil   32767*kampenv,p5+avibwave+amodwave,1
        out     awaveform
        endin
**************************************************

To play an actual melody, you will want to use pitch notation in the score file instead of frequencies. Csound provides a function called cpspch() to translate pitch to cycles per second. This function expects pitch to be notated as an octave (8 is middle C) plus some number of semitones (as two decimal places after the octave number). So, for example, the A above midle C is written "cpspch(8.09)"--middle C plus 9 semitones.

Below is a version of example 5 that will allow pitch to be notated as octave.semitones in the score file. To make this easy, we'll add a line in the instrument that defines a variable called ifreq, which will contain the carrier frequency, as calculated from the pitch information in the score. In that way, we can just put ifreq in place of p5 everywhere else in the instrument. An example of a brief melody written in this way is shown in the score file that follows.

********************ex6.orc************************
sr = 44100
kr = 2205
ksmps = 20

        instr   1
ifreq	=	cpspch(p5)
kampenv linseg  0,0.01,p4,0.1,0.33*p4,p3-0.25,0.0625*p4,0.4,0
amodwave        oscil   kampenv*p6*ifreq/p7,ifreq/p7,1
avibwave        oscil   ifreq*.01,6,1
awaveform       oscil   32767*kampenv,ifreq+avibwave+amodwave,1
        out     awaveform
        endin
**************************************************

********************ex6.sco************************
f1      0       1024    10      1

; p1=ins p2=start p3=dur p4=amp p5=pitch p6=mod p7=harm
i1      0       .5       0.25       9.02     3       1
i1      .5       .5       0.125       9.04     3       1
i1      1       .5       0.17       9.06     3       1
i1      1.5       .4       0.125       9.02     3       1
i1      2       .5       0.25       9.02     2       1
i1      2.5       .5       0.125       9.04     2       1
i1      3       .5       0.17       9.06     2       1
i1      3.5       .4       0.125       9.02     2       1
i1      4       .5       0.25       9.06     3       1
i1      4.5       .5       0.25       9.07     3       1
i1      5       0.9       0.4       9.09     3.25       1
i1      6       .5       0.3       9.06     3       1
i1      6.5       .5       0.3       9.07     3.5       1
i1      7       1.1       0.45       9.09     4       1
e

**************************************************