Let's get started making music with Csound!
Start by making a file, with any name you wish, but with .csd as the extension. This file is where we will make your instrument. It can be used directly in a terminal by typing for example, if your file is called "foo," the following command without the quotes: "csound foo.csd" Another way to manage your .csd file is through an application that allows you to manipulate and test your file, such as QTcsound. There are also other frontend applications for managing Csound files such as Cabbage and Blue. For our initial tutorials, we will stay at the terminal (command line) level.
Now that you have your first Csound file, open it using a text editor of your choosing.
Csound basically requires 3 main sections. The first section is the options section, where you will need to specify some important things, such as your computer sample rate, your audio driver and your midi port, if needed.
The second section is called the Instrument (Orchestra) Section and is where we will make our instrument(s), whether these are made using oscillators, audio samples, etc.
The third section is called the Score Section. This section can be used as a sequencer or even a full-scale instrument score, where notes and their expression can be indicated. Otherwise, this section can be used simply to denote the length you want the instrument to run for, such as 1 hour, 1 day or 1 year. This is used, for example, when you want to use an external MIDI controller to play your instrument live. As with all Csound files, to stop the program just press "ctrl" and "c" and it should stop. Using Csound from the terminal will allow you to see if it has indeed closed correctly, as well as if there were errors during performance.
So let's start trying some examples. Copy the following code into your new .csd file:
-d -odac
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
asound rand 1
out asound
endin
i1 0 10
After saving this code, try running it from your terminal. What you should hear are 10 seconds of white noise. So, let's evaluate what we've done:
Any Csound file must be contained within the and tags. Within the and tags we put the options which tell Csound to use the default audio system within your computer, and to output to the Digital-Audio-Converter (DAC). A nice feature that can be put here is to tell Csound to create a .wav file of whatever audio is produced when the program is run. If we want the .wav file to be called "Tom," this is what we would need to add in the code:
-d -odac -o Tom.wav
NOTE: This addition will not output any actual audio to your speakers. Also, remember that the created audio file will be overwritten if the first file is not moved or renamed!
Okay, so what else is going on in this file? Moving to the area within the and brackets, we see that the sample rate (sr) is set to 44,100 Hz. If your audio system is set to another frequency, such as 48,000 Hz, you should change your Csound file to match this! The argument "ksmps" is the number of samples in a control period. It is recommended to use a number that is a power of 2. Increasing the ksmps, and/or reducing the sr, should lighten the load on CPU performance.
The number of audio channels is set to 2 (stereo), although monophonic audio output, as well as more complex audio output set-ups, are also possible. The argument 0dbfs = 1 is used as a standard for setting the maximum audio level, so in most cases this is the setting to use.
Moving on, we see the beginning of our instrument, denoted "instr 1." The "asound" argument was of our naming choice.. we could call it "atree" or "acar" or just about anything else, provided that it starts with "a," which tells Csound that we want to create an audio signal. What kind of audio signal? Here, we have chosen to use a random signal called "rand." And the number 1 denotes the amplitude of this random audio signal. The "out" argument tells Csound to send the signal out of both speakers. The "endin" argument indicates the end of this instrument.
The last section is the Score section. Here we indicated instrument number 1 (i1) starting at 0 seconds and lasting for 10 seconds.