Csound

From OLPC
Revision as of 17:41, 4 April 2008 by Victor (talk | contribs)
Jump to navigation Jump to search
  english | españolfrançais한국어 HowTo [ID# 123155]  +/-  


Csound is the music and audio signal processing language originally developed by MIT's Barry Vercoe and now expanded and maintained by a world-wide community, as Free Software [1]. Csound will provide audio services for the XO computer. Csound is both a programming language and a sound synthesis engine. Csound, as included in the OLPC project, can be used by Activities or directly by children and teachers. It can be accessed in a variety of ways. In the XO platform, two basic ways are provided:

  • Through the Python programming environment: eg. programmed in Activities.
  • Through its 'classic' command-line frontend, directly invoking it from the Terminal activity.

The version currently being shipped in all XO builds is 5.07, but with 5.08 having been recently released (see [2]), we expect to upgrade to this version in future releases. It is possible to install the latest version from an Rpm package (see below). Further information about Csound can be found on its official website: http://csounds.com/. The canonical Csound sources and multi-platform binaries are hosted by Sourceforge[3].

Activities

  • Csound Editor - view, edit and perform Csound files
  • Audio Loop Remixer - perform audio loops and apply a variety of effects
  • MIDI File Player - performs MIDI files using the donated General MIDI soundfont
  • Instrument Player - a keyboard interface to play a variety of instruments
  • TamTam - Tam Tam uses Csound, but you would never know it as its interface is designed to wrap the Csound engine with a child-friendly look and feel. This excellent group of Activities allows kids to make sounds, make music, jam, record and transform their voices in an intuitive way. TamTam Edit allows students to patch together Csound's opcodes (modules) and teaches them all about signals, synthesis, and synthesizers. TamTam Activities demonstrate well how the power of Csound can be harnessed in the XO platform.
  • GregCsoundActivities.zip - A number of Csound Server-based activities developed by Greg for Build 542 including a pretty cool Pitch-Tracker Bouncing Ball Activity and a Pitch Reverse Game – both lot's of fun for kids, but not currently supported by the latest builds and security models.
  • Pippy - Pippy uses Csound to help teach children the Python programming language and to build XO Activities.
  • Step - A simple 8-note step sequencer that children will use to play music and record their own loops for use in other sample-based activities. Step uses csndsugui.
  • Funny Talk - An activity that children can use to record their voices with the built-in microphone, and process them with effects such as reverb, echo, chorus, etc. Funny Talk allows a child to save their manipulated voices as soundfiles so that they can be used in other musical activities. Funny Talk uses csndsugui.

Csound XO Manual

View the online Csound XO manual here:

 http://www.csounds.com/manualXO/

Download the Csound XO manual here:

 http://www.csounds.com/manualXO/csound_manual_xo.tar.gz

There's a content bundle for the XO which is installed the same as an activity and appears automatically in the Web Activity Library (the Web Activity default page). You can find it here:

 http://www.csounds.com/manualXO/csound5.08_manual_xo.xol

Csound code and CSD files

Csound code is most commonly written in the Unified Csound File format (CSD). This uses XML-like tags to contain the different code elements in sections. Two of these are required: orchestra (<CsInstruments>) and score (<CsScore>) contained within the <CsoundSynthesizer> tags. Here is a trivial instrument and score:

<CsoundSynthesizer>
<CsInstruments>
instr 1
a1 oscili  p4, p5, 1 
   out a1
endin
</CsInstruments>
<CsScore>
f1 0 16384 10 1
i1 0 1 10000 440
e
</CsScore>
</CsoundSynthesizer> 
   

This will give you a nice 1-second sinewave beep.

Orchestra and Score

Csound is loosely based on the concept of an Orchestra, made up of Instruments, and a Score, made up of events, function table definitions etc.. Instruments contain the synthesis/processing code and the score the parameters to run these. In practice, you might not need a score, because realtime events (from MIDI, from Python or from other Csound instruments), so in that case an empty or minimal one will do. Instruments are written using a variety of opcodes (there are over 1000 of these), which are interconnected using audio signal (a-), control signal (k-), spectral signal(f-), initialisation (i-) and string (S-) variables. A slightly more interesting example shows some of these in operation:

<CsoundSynthesizer>
<CsInstruments>
instr 1
idur = p3
iamp = p4
ifrq = cpspch(p5)
kenv expon 1, idur, 0.01
asig oscili  kenv*iamp, ifrq, 1 
   out asig
endin
</CsInstruments>
<CsScore>
f1 0 16384 10 1
i1 0 1  8000 8.00
i1 + 1 12000 8.04
i1 + 1 16000 8.07
i1 + 2 24000 9.00
e
</CsScore>
</CsoundSynthesizer>

What you should hear is an arpeggio of 'ping' sounds, a bit like a chime or tuning fork, which is much more interesting than our previous attempt. You will note that the envelope signal generated by the opcode 'expon' is a control (k-) signal and the sound produced by the 'oscili' opcode (an oscillator) is an audio signal. The parameters for the instrument are held in initialisation (i-) variables. The instrument is run 4 times (there are 4 i-statements in the score) in a sequential manner (the '+' makes the start times of events aligned with the end times of previous events). The frequency is notated in octave.pitch-class format in the score (last parameter field, 5, in each event) and translated to Hz in the instrument code.

MIDI playback

Csound can play back MIDI files, as well as realtime MIDI events (say from a keyboard or a sequencer). All you need is Csound code that understands MIDI. For MIDI file playback, the compilation option -F <your-midi-file> is used.

Here is a version of the ping instrument used above, adapted for simple MIDI input (listening on MIDI channel 1).

<CsoundSynthesizer>
<CsInstruments>
instr 1
idec = 1
iamp ampmidi 32767
kfrq cpsmidib 2
kenv expsegr 1, idec, 0.1, 0.1, 0.01
asig oscili  kenv*iamp, kfrq, 1 
   out asig
endin
</CsInstruments>
<CsScore>
f0 36000
f1 0 16384 10 1
</CsScore>
</CsoundSynthesizer>

In this case, we just need to do the following simple alterations: add opcodes for using MIDI NOTE data (frequency, amplitude) and a release-sensing envelope generator; remove the not required score events; and include a 'f0 36000' line to keep Csound listening for realtime events for 36000 seconds.

A more generic (and complex) example is shown below: a General MIDI (GM) soundfont synthesizer for playing GM files. This is give a good example of how flexible Csound can be, doing almost anything you would like in terms of sound synthesis. The code is based on two Csound instruments: one that parses raw MIDI data and another one that actually plays it.

 File: GM_example.csd
<CsoundSynthesizer>
<CsInstruments>
ichn = 1
lp1: massign   ichn, 0
loop_le   ichn, 1, 16, lp1
pgmassign 0, 0
gisf   sfload    "gmgsBank1.sf2"
sfpassign  0, gisf

/* this instrument parses MIDI input
   to trigger the GM soundfont synthesis
   instrument (instr 10
*/
instr 1
idkit = 317 /* drum-kit preset */
tableiw idkit, 9, 1
irel = 0.5 /* release envelope */

ipg = 1
ivol = 2
ipan = 3

nxt:
  kst, kch, kd1, kd2 midiin

  if (kst != 0) then
    kch = kch - 1
    if (kst == 144 && kd2 != 0) then ; note on
        kpg table kch, ipg 
        /* instrument identifier is 10.[chn][note] */
        kinst = 10 + kd1/100000 + kch/100  
        if kch == 9 then
         /* exclusive identifiers */
         if kpg == idkit+7 then
           krel = 2    /* add extra release time for orch perc*/
         else
           krel = 0.5
         endif
         if (kd1 == 29 || kd1 == 30) then ; EXC7
          kinst = 10.97
         elseif (kd1 == 42 || kd1 == 44 || kd1 == 46 || kd1 == 49) then ; EXC1
           kinst = 10.91
         elseif (kd1 == 71 || kd1 == 72) then ; EXC2         
           kinst = 10.92
         elseif (kd1 == 73 || kd1 == 74) then ; EXC3         
           kinst = 10.93
         elseif (kd1 == 78 || kd1 == 79) then ; EXC4         
           kinst = 10.94
         elseif (kd1 == 80 || kd1 == 81) then ; EXC5         
           kinst = 10.95
         elseif (kd1 == 86 || kd1 == 87) then ; EXC6         
           kinst = 10.96
         endif
        else
         krel = 0.5
        endif
        event "i", kinst, 0, -1, kd1, kd2, kpg, kch,krel  
     
    elseif (kst == 128 || (kst == 144 && kd2 == 0)) then ; note off
        kpg table kch, ipg
        kinst = 10 +  kd1/100000 + kch/100
        if kch == 9 then
         if (kd1 == 29 || kd1 == 30) then ; EXC7
          kinst = 10.97
         elseif (kd1 == 42 || kd1 == 44 || kd1 == 46 || kd1 == 49) then ; EXC1
           kinst = 10.91
         elseif (kd1 == 71 || kd1 == 72) then ; EXC2         
           kinst = 10.92
         elseif (kd1 == 73 || kd1 == 74) then ; EXC3         
           kinst = 10.93
         elseif (kd1 == 78 || kd1 == 79) then ; EXC4         
           kinst = 10.94
         elseif (kd1 == 80 || kd1 == 81) then ; EXC5         
           kinst = 10.95
         elseif (kd1 == 86 || kd1 == 87) then ; EXC6         
           kinst = 10.96
         endif
        else
         kpg = 0
        endif
        event "i", -kinst, 0, 1 
     
    elseif (kst == 192) then /* program change msgs */
       if kch == 9 then
         kpg = idkit
         if kd1 == 8 then
         kpg = idkit+1
         elseif kd1 == 16 then
         kpg = idkit+2
         elseif kd1 == 24 then
         kpg = idkit+3
         elseif kd1 == 25 then
         kpg = idkit+4
         elseif kd1 == 32 then
         kpg = idkit+5
         elseif kd1 == 40 then
         kpg = idkit+6
         elseif kd1 == 48 then
         kpg = idkit+7
         endif
       else
       kpg = kd1 
       endif
       tablew  kpg, kch, ipg
    elseif (kst == 176 && kd1 == 11) then /* volume msgs */
       tablew kd2, kch, ivol
    elseif (kst == 176 && kd1 == 7) then /* pan msgs    */
       tablew kd2, kch, ipan
    endif
     kgoto nxt
  endif

endin

/* this is the GM soundfont synthesizer instrument */
instr 10
kenv linenr 1,0.001,p8,0.001
iamp table p5, 5
a1, a2 sfplay p5, p4, iamp,1, p6
kv table p7, 2
kvol tablei kv, 5 
kpan  table p7, 3
kpan = (kpan - 64)/128
       outs a1*kvol*(0.5-kpan/2)*kenv, a2*kvol*(0.5+kpan/2)*kenv 
endin

</CsInstruments>
<CsScore>
/* program preset (memory) table */
f1 0 16 -2 0 0 0 0 0 0 0 0 226 0 0 0 0 0 0 0
/* velocity (memory) table */ 
f2 0 16 -2 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 
/* pan (memory) table */
f3 0 16 -2 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 
f5 0 128 5 0.1 128 1   /* velocity mapping: less nuanced */
f6 0 128 5 0.01  128 1 /* velocity mapping: more nuanced */
i 1 0 360000
e
</CsScore>
</CsoundSynthesizer> 

The above example is not very typical of MIDI usage in Csound, as it handles MIDI data at its lowest level. Much simpler examples of how MIDI can be used by Csound exist. However the example is a good demonstration of how a universally-existing standard such as GM can be handled by Csound.

csndsugui

As you can see from some of the examples above, it is possible to use csndsugui for fast Activity development. It is basically Python toolkit for the development of Csound-based audio and music applications under sugar and GTK. The code, plus examples and documentation, can be found in:

 http://dev.laptop.org/git/activities/csndsugui

Tutorials and... TOOTS

See Csound tutorials for a wiki introduction to using Csound for music development. A Csound TOOTS page in this Wiki is also available.

Sample Code

  • A simple Python project using Csound to create an audible system load monitor using the standard Python Csound binding
  • TamTam uses a custom C++ wrapper around Csound and has a very large sound library available
  • Activity example shows a complete Python and Csound code for a simple Activity.
  • 4csEditor.zip is a collection of approximately 500 Csound csd files, covering everything from compositions to DSP processor instruments.

Updating Csound with RPM

The current Csound Rpm is csound-5.08.91-0.i386.rpm and can be downloaded here:

 http://csounds.com/csound-5.08.91-0.i386.rpm

To install, start by launching the Terminal_Activity. Use the command su to log in as super user. Type rpm -q csound to see which Csound package is currently installed. Type rpm -e csound-5.xx.x replacing "csound-5.xx.x" with the results of the previous command. To install Csound, type rpm -ivh csound-5.yy.rpm, replacing "csound-5.yy.rpm" with the correct name of the package. Type exit to log off as super user. If you have Csound5.07, you may also need to erase (-e) the csound-python RPM package, otherwise you might get a conflict on installation. The olpc csound-python RPM package is deprecated and no longer required from Csound5.08.

 wget http://csounds.com/csound-5.08.91-0.i386.rpm
 su
 rpm -q csound
 rpm -q csound-python
 rpm -e csound-python-5.xx.x
 rpm -e csound-5.xx.x
 rpm -ivh csound-5.yy.rpm
 exit