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!


Thursday, September 20, 2012

What is Csound, why it sucks and why it rocks

I'm going to try to make a few mini-lessons here on Csound. We'll see how this goes.

I'm assuming that you have never used Csound before, so I'll start from the beginning. 

Csound can be thought of as a text-based modular synthesizer. Synthesizers are "built" inside of a text editor using modules called opcodes. Modules include various kinds of oscillators, filters, LFOs, and envelopes, but there are more unique opcodes for things like physical modeling, granular synthesis, additive resynthesis, and phase vocoding. There are ways to implement programming idioms, but Csound is not a programming language. This is a tool for musicians and musically-minded individuals, not a way for programmers to incorporate musical elements into their work. 

I learned about Csound 4 years ago, and have been using it quite intensely the past year and a half. It is one of my most used programs for synthesis and sound design. 



To start things off, I'd like to give four reasons why Csound sucks and is a terrible program to use:

1. It's old. The source code is an accumulation of over 20 years of work. It can be very difficult to incorporate Csound seamlessly with modern tools in a DAW. It has quirky old-timey conventions.

2. The score. It's a terribly old-fashioned text table with timings and other information which Csound will use to play instruments. Writing out note-sequences can be very time-consuming, and can be done much quicker in a DAW. 

3. The orchestra. It's basically the section of a Csound synthesizer where you build the synthesizer. It's only text, so it can be very hard to read at times. It's very hard to come up with sounds quickly in Csound, since they are typed out and not "patched" or "twiddled."

4. Everyone in the Csound community makes strange music that's hard to listen to. It's rare that you find a (good) Csound composition with a beat, something in 4/4 time, or even something with western tonality! To me, the stereotypical Csound composition is droney, with shrill blips and scary clusters of notes. 


Conversely, I also would like to give four reasons why Csound rocks and is an incredible tool for electronic music:

1. It's old. Not only that, but the community is very fervent on backwards compatibility. Csound compositions written twenty years ago can still run flawlessly on the most recent version of Csound. In the electronic music world, it is very typical to have to learn new software every 5 years or so, and to essentially lose any projects made with the older software (backwards compatibility is not a guarantee.) You take the time to Learn Csound, and your projects will be timeless.

2. The score. it's only a text file. One can very easily generate scores for your instruments in any programming language with file I/O (which is basically every modern programming language.) Your scores are not bound by the constrictions found in a MIDI sequencer. I'll get more into this later, but things like audio-precise timing and floating point resolution parameters. 

3. The orchestra. It forces you to think at a very low level for sound design. Working with Csound, you will gain expert knowledge on how to make engaging and unique sounds with synthesizers. As an added bonus, this can be applied to any available commercial synthesizer. 

4. Everyone in the Csound community makes strange music that's hard to listen to. And that's a good thing. There is so much easy music out there. Hard music is food for your brain. As a musician, it's the hard music that makes you grow.