Wednesday 21 April 2010

OpenMusic and CSound - om2csound library

We've looked at algorithmic composition with OpenMusic in a few posts before. This post looks as using OpenMusic and CSound.

CSound is a very powerful synthesis program that is cross platform, free to download under the GNU Lesser General Public License and available here. It has hundreds of built in synthesis algorithms ranging from simple to advanced, cutting edge synthesis and sound processing methods.

CSound works with two files:
  • A .orc Orchestra file that specifies the instruments
  • A .sco Score file that specifies how the instruments will be played e.g. the notes
Although the synthesis algorithms are very powerful, writing scores by hand can be tedious, here's an example score:

f1  0      4096  10 1   
;p1 p2     p3    p4 p5   
 i1 0      .50   71 61    
 i1 .5000  .50   67 78    
 i1 1.0000 .50   63 70    
 i1 1.5000 .50   61 71    
 i1 2.0000 .50   76 78

This example has only 5 p-fields but an instrument can have many more p-fields than this. The standard 5 p-fields are:
  • p1 = instrument number
  • p2 = onset
  • p3 = duration
  • p4 = amplitude
  • p5 = frequency
The above CSound score specifies only 5 notes, an entire musical piece with lots of instruments could involve thousands of notes and would be time consuming to type in. Fortunately, we can use algorithmic composition techniques to generate our CSound scores for us.

1. OpenMusic has a CSound library, you need to enable this in preferences:


2. Create a new patch and add an instrument0 object:



3. This object takes inputs for the various p-fields and creates a list of all of the them to create our CSound score. As our instrument will have 5 p-fields we need to create some more inputs. Select the instrument0 object and press the > key twice, this will create two more inputs:


4. Now we create processes to fill these 5 p-fields, we can use any method we like but we'll keep things simple to start with. The first p-field in our example is easy, it's our instrument number and in this example is instrument 1.


5. The second p-field is onset time: the time the note starts. We'll keep our example simple and have 40 notes starting every half a second. For this we use an arithm-ser function (arithmetic series). For this we give a start value (0) an end value (19.5) and the step size:

This outputs a list of onsets that looks like this:
(0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5)

6. Our next p-field is duration. We'll keep this simple and use .5 as a duration for all our notes. We can use a repeat-n function to do this:


Our patch now looks like this:
7. Our next p-field (p4) is amplitude. Again we could use any process to generate these numbers, but for now we'll make a simple random choice. CSound can work with amplitude values in dB and raw amplitude values and can convert from one to the other. Here we'll work with decibels and get OpenMusic to choose 40 random values between 60 and 90 using om-random and repeat-n functions:

8. Our 5th and final p-field is frequency, CSound supports a number of different pitch representations and can convert between them including frequency (Hertz), MIDI pitches, octave pitchclasses etc. We'll work with MIDI pitches and use the same method to generate our pitches as we used for amplitude: the om-random and repeat-n function:


Our patch now looks like this:


9. When we evaluate our patch it outputs a list of lists with all our algorithmic musical data in it. We next need to add in an editsco function that converts this and saves it as a .sco file.


10. We also need to add a function table into this. Function tables in CSound are used to store data that we need to access, this might be:
  • The points of a sine wave
  • A sample
  • An envelope shape etc.,
Our final patch looks like this (click on the image to see a larger version):



When we evaluate the editsco function, this will bring up a dialog box and save the file to disk. The final score file produced looks like this:

f  1       0          4096       10      1   
;p1        p2         p3         p4      p5   
i  1      0          .5000      64      81   
i  1      .5000      .5000      65      64   
i  1      1.0000     .5000      67      64   
i  1      1.5000     .5000      74      66   
i  1      2.0000     .5000      80      72   
i  1      2.5000     .5000      89      66   
i  1      3.0000     .5000      69      82   
i  1      3.5000     .5000      86      61   
i  1      4.0000     .5000      79      69   
i  1      4.5000     .5000      81      61   
i  1      5.0000     .5000      61      66   
i  1      5.5000     .5000      81      62   
i  1      6.0000     .5000      61      78   
i  1      6.5000     .5000      85      79   
i  1      7.0000     .5000      67      76   
i  1      7.5000     .5000      72      81   
i  1      8.0000     .5000      67      77   
i  1      8.5000     .5000      78      81   
i  1      9.0000     .5000      79      80   
i  1      9.5000     .5000      64      69   
i  1      10.0000    .5000      81      74   
i  1      10.500     .5000      86      62   
i  1      11.0000    .5000      82      72   
i  1      11.5000    .5000      71      75   
i  1      12.0000    .5000      61      65   
i  1      12.5000    .5000      66      77   
i  1      13.0000    .5000      67      73   
i  1      13.500     .5000      70      82   
i  1      14.0000    .5000      65      67   
i  1      14.5000    .5000      90      67   
i  1      15.0000    .5000      88      60   
i  1      15.5000    .5000      69      62   
i  1      16.0000    .5000      75      60   
i  1      16.5000    .5000      89      68   
i  1      17.0000    .5000      83      75   
i  1      17.5000    .5000      76      71   
i  1      18.0000    .5000      61      78   
i  1      18.5000    .5000      85      70   
i  1      19.0000    .5000      61      81   
i  1      19.5000    .5000      68      63   
e

Not only would this have been very time consuming to type in by hand, we now have the big advantage of using algorithmic composition techniques with OpenMusic and CSound: if we want to change anything or how we generate any of the musical parameters we're using (instrument, onsets, duration, amplitude, pitch) we can do this very easily.

For example in a previous post we looked at using different probabilities to algorithmically compose musical data. Let's say we want to use a triangle distribution instead of our current om-random function, we can keep our patch and plug a trian-seq function into our pitch instead (we've also used an om-round function to ensure each note is an integer.


To do this by hand would involve typing out and replacing lots of our score elements by hand, using algorithmic composition we can evaluate the patch again to output a new score and keep revising it until we like it.

Playing Your Algorithmic Score In CSound
You now have a .sco file. Save this example orchestra as a .orc file, you can paste this into a standard text editor and save it as basic.orc for example.

instr     1
a1    oscil     ampdb(p4), cpsmidi (p5), 1
      out       a1
endin


Open up your .orc and .sco files in CSound to render them to an audio file and listen to them. Our instrument is a simple sine wave oscillator oscil with just 3 parameters: amplitude, pitch and the function table we're using. Instruments can get much more complicated (and interesting) than this, and often end up with lots more p-fields in the score. This makes the range of sounds much more interesting but also means the score is more complex to complete by hand. Fortunately we can use algorithmic composition techniques to generate the score for us.

In future posts we'll have another look at creating CSound scores in OpenMusic and look at more interesting and developed methods of algorithmic composition and more developed CSound instruments. If you're interested in CSound there are some very good tutorials available at www.csounds.com.


Try modifying your patch to use some of the other different OpenMusic algorithmic composition methods we've looked at so far to generate CSound scores and check back to http://www.algorithmiccomposer.com/ soon for more algorithmic composition techniques.

No comments:

Post a Comment