Friday, September 21, 2012

Tutorial 1: The CSD structure, and beating frequencies

Today, we will make some beats with Csound.

Text editors and/or frontends for Csound will not be discussed much, but they are required to run Csound. I personally write up everything in VIM, and then use Csound from the command line. Much information is available on the various tools you can use. Go explore. Don't worry, my examples should work.

Csound has two main sections. The orchestra is the section that makes the sound, and the score is the section that tells the orchestra what to play. These two sections used to be two separate files (eg: song.orc and song.sco) back in the day, but now they consolidated into one file with an extension of *.csd.

A CSD is an XML file. Open up a new file and type this:

<CsoundSynthesizer>

<CsOptions>

</CsOptions>

<CsInstruments>

</CsInstruments>

<CsScore>

</CsScore>

</CsoundSynthesizer>

To break it down:

1. everything is in encapsulated between <CsoundSynthesizer> and </CsoundSynthesizer>

2. everything between <CsOptions> and </CsOptions> will set command line flags.

3. everything between <CsInstruments> and </CsInstruments> will hold the orchestra

4. everything between <CsScore> and </CsScore> will hold the score.

The first thing we will do is make a header inside the <CsInstruments> tags:

<CsInstruments>

sr = 44100

ksmps = 1

nchnls = 2

0dbfs = 1

</CsInstruments>

Basically, we are telling Csound that we want a project with:

a sample rate 44.1khz (sr),

a control rate which matches the sample rate (ksmps),

stereo audio (nchnls),

and range of loudness where 0 is no sound, and 1 is full-scale digital audio (maximum loudness).

There's no real need to know exactly what these things do right now. Just know that we will be using this almost all the time in every example we have.

We will now make an instrument which produces a simple sine wave, right below the header we made. Do not copy and paste. Type it:

<CsInstruments>

sr = 44100

ksmps = 1

nchnls = 2

0dbfs = 1

gisine ftgen 0, 0, 4096, 10, 1

instr 1

a1 oscil .4, 100, gisine

outs a1, a1

endin

</CsInstruments>

Here is (basically) what is happening, line by line:

1. a variable called "gisine" which makes a sine wave. this is outside of the instrument.

2. the instrument starts. it has an instrument ID of 1.

3. oscillator module using the oscil opcode. It has an amplitude of .4, and a frequency of 100. it is reading the sine wave from the variable "gisine." the audio information is being sent to a variable called "a1."

4. We send the audio from the oscillator (a1) to the stereo speakers.

Now that we made the instrument, we need to instruct Csound to play it. In the <CsScore> section of our CSD file, add this:

<CsScore>

i1 0 4

</CsScore>

This is our score. As I said in the last post, it is only a text table. Each value is called a parameter value, or p-value:

p1 ("i1") tells us that we want instrument 1 to play a note.

p2 ("0") tells us that we want to play a note right when we compile (0 seconds after performance starts).

p3 ("4") tells us that we want to hold the note for 4 seconds.

If you are going to run Csound from the commandline, you need to tell Csound to play the file in realtime and not render it to an audio file. Add this to the <CsOptions> section:

<CsOptions>

-odac

</CsOptions>

Now run it!

If you got a sinewave, good. If not, check the terminal output and see if there are any error messages. Typos and syntax errors are very common.

Supposing that we want to control pitch from the score. We can use this by adding additional parameters and p-values. We can modify our instrument like so:

gisine ftgen 0, 0, 4096, 10, 1

instr 1

a1 oscil .2, p4, gisine

outs a1, a1

endin

We can now declare the frequency of our oscillator in the p4 slot in our score:

i1 0 4 100

Does it have to be 100hz? nope. It can be 101, 200, 1000, 20000, 1.41760hz, or any numerical value. Lets have a little bit of fun by making some "beats" by using this as your score:

<CsScore>

i1 0 4 300

i1 0 4 301

</CsScore>

Two or more simultaneous tones with similar frequencies will clash with one another. Here we have a 300hz sine and a 301hz sine. The farther apart they are, the faster they get. Try changing 301hz to 305hz.

Harmonic beats, though simple, can be kind of fun. Try running this as your score:

<CsScore>

i1 0 16 300

i1 0 4 305

i1 4 4 308

i1 8 2 310

i1 10 2 308

i1 12 4 305

</CsScore>

Figure out what each line is doing. What is their frequency? When do they start? how long do they stay on? Try making your own scores from sine waves. Remember that things get louder when you start adding sine waves together. Turn the volume down and be careful with your ears!