Csndsugui

From OLPC
Jump to navigation Jump to search

csndsugui is a toolkit for Csound-sugar activity development

Writing activities using csndsugui

In general, Activities are built according to the basic procedures outlined in Activity tutorial.

Basics

  • Import the relevant modules
import csndsugui
from sugar.activity import activity
  • Create an empty activity class such as
class MyCsoundActivity(activity.Activity):
  def __init__(self, handle):
    activity.Activity.__init__(self, handle) 
  • Instantiate a CsoundGUI object, passing the

activity instance as an argument:

  win = csndsugui.CsoundGUI(self)
  • Set and compile Csound code:
  win.CSD("mycode.csd")

It is CRUCIAL that Csound compiles successfully before the widgets below are created. Otherwise they will not be assigned channels in the software bus and thus will not communicate with Csound. This method returns 0 if successful.

Adding Widgets

Use boxes to format and contain widgets, child boxes can be contained within parent boxes. The top-level box does not have any parents.

   box = win.box()

See the documentation on PyGTK on how boxes are used to set the formatting. CsoundGUI will take care of all the packing for you. You can then add widgets to it, ie:

   win.button(box,"oscil")
   win.slider(1.0,0.25,4.0,90,250,box,"pitch", linear=False) 

Making the connections

In your Csound code, you can retrieve the value of each of the controls in channels of the software bus:

   kosc  chnget "oscil"   
   kpit  chnget "pitch"

Channel names will be linked to widget labels ("oscil" and "pitch" in the example above) Most widgets will work within the principles outlined above. Important exceptions are:

  • Message button:
 mbutton(self,box,mess,title="")

where mess is a RT score event or message to be sent to Csound (in most cases an i-statement, but f-statements are also possibilities).

  • Callback button:
 cbbutton(self,box,callback,title="")

where callback is a Python function that will be invoked when the button is clicked.

Special button names: "play", "pause" and "reset". These are assigned special messages that are not captured by the software bus. Instead, they can control Csound performance, starting, pausing and resetting Csound performance (they are linked to CsoundGUI.play(), CsoundGUI.pause() and CsoundGUI.reset() methods.)

A complete example

 File: WavesActivity.py
 import csndsugui
 from sugar.activity import activity
 class Waves(activity.Activity):

  def __init__(self, handle):
  
   activity.Activity.__init__(self, handle)
 
   # colours
   red = (0xFFFF,0,0)
   bg  = (0xF000, 0xFF00, 0xFFFF)
   win = csndsugui.CsoundGUI(self,bg)
 
   win.csd("waves.csd")
   txt = win.text("Making Waves")
   sfbox = win.box(False)
   bfbox = win.box(False)
   sbox = win.framebox("frequencies", False, sfbox, red, 40) 
   bbox = win.framebox("oscillators", False, bfbox, red, 40)

   win.buttonbank(8,bbox)
   win.spin(0,0,10,0.5,1,bbox,0, "main_volume")
   win.vsliderbank(8,400.0,400.0,500.0,80,200,sbox)

   win.play()

Further help

On-line help can be obtained by importing the csndsugui module and involking help(csndsugui). See also examples in the csndsugui repository http://dev.laptop.org/git/activities/csndsugui