Simple instrument - playing notes


Martin McBride, 2017-09-24

We have seen how to control the frequency of our simple instrument. But what if we want to play a particular musical note?

Using notes in CSound

You could create tunes in CSound by specifying the exact frequencies for each note. But that would be tedious and error prone.

Instead we can use

So far, our simple instrument isn't much fun, because it can only play one note! Let's change it so that we can play different notes. While we are here, we will also give it the ability to play louder and softer notes, so we can make it a bit more expressive.

Note parameters

If you recall, our notes list looks like this:

i 1 0 0.5
i 1 1 0.5
i 1 2 0.5

e

Each line contains 3 parameters, which represent the [instrument], the [start time] and the [duration]. These parameters are named p1, p2 and p3.

We can extend the notes list to include extra parameters:

i 1 0 0.5 400 0.2
i 1 1 0.5 500 0.4
i 1 2 0.5 600 0.6

e

Here we have added two extra parameters. These parameters are named p4 and p5.

What do these parameters do? Well they can do anything you want them to. They are passed into our instrument definition, and we can use them to change the sound however we wish.

You are not limited to just two extra parameters, you can add as many as you need. But for now, we will use p4 to control the frequency, or pitch, of the note, and p5 to control the amplitude.

Updating the instrument

Here is our original instrument:

instr 1
  asig oscil 0.5, 440, 1
  out asig
endin

In this case, the oscil statement uses 0.5 for the amplitude, and 440 for the frequency. We can easily change this to use p5 for the amplitude, and p4 for the frequency:

instr 1
  asig oscil p5, p4, 1
  out asig
endin

That is all you need to do. The frequency and amplitude can now be controlled by the note list.

The full script

Here is the full .csd file to try out in CSound:

<CsoundSynthesizer>

<CsOptions>
</CsOptions>

<CsInstruments>
ar = 44100
ksmps = 10
nchnls = 1
0dbfs  = 1

instr 1
  asig oscil p5, p4, 1
  out asig
endin
</CsInstruments>

<CsScore>
f 1 0 16384 10 1

i 1 0 0.5 400 0.2
i 1 1 0.5 500 0.4
i 1 2 0.5 600 0.6

e
</CsScore>

</CsoundSynthesizer>

And here is what it should sound like:

Copyright (c) Axlesoft Ltd 2020