Csndsugui: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
m (categorize)
 
(3 intermediate revisions by one other user not shown)
Line 286: Line 286:
== Further help and where to get it ==
== Further help and where to get it ==


On-line help can be obtained by importing the
On-line help can be obtained by using PyDoc. See also examples in the
csndsugui module and involking help(csndsugui). See also examples in the
csndsugui repository [http://dev.laptop.org/git/activities/csndsugui] where you
can also get the latest code and a PyDoc-generated HTML reference file.
csndsugui repository http://dev.laptop.org/git/activities/csndsugui, where you

can also get the latest code.
[[Category:Audio]]
[[Category:Developers]]

Latest revision as of 00:38, 15 August 2008

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

Here is a complete Activity script of a simple csndsugui example. It uses banks of buttons and sliders:


 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()


The Csound code that goes with it is shown below. It is based on two instruments: instr 1 triggers instances (up to 8) of instr 2, which produces the sound. Instances are trigger on/off depending on button values (0 =not clicked, 1 = clicked):


 File: waves.csd
<CsoundSynthesizer>
<CsOptions> 
-odac -B2048 -b1024 -d
</CsOptions>
<CsInstruments>

/* button channels */
gkb1  chnexport "B1", 1
gkb2  chnexport "B2", 1
gkb3  chnexport "B3", 1
gkb4  chnexport "B4", 1
gkb5  chnexport "B5", 1
gkb6  chnexport "B6", 1
gkb7  chnexport "B7", 1
gkb8  chnexport "B8", 1

/* this instrument just triggers
   instr 2 depending on the status
   of buttons B1-B8 */
instr 1

k1 init 1
k2 init 1
k3 init 1
k4 init 1
k5 init 1
k6 init 1
k7 init 1
k8 init 1


if gkb1 == 1 then
if k1 == 1 then
event "i", 11.1, 0, -1, 1 
k1=0
endif
else 
if k1 == 0 then
event "i", -11.1, 0, -1,1
k1=1
endif
endif

if gkb2 == 1 then
if k2 == 1 then
event "i", 11.2, 0, -1, 2
k2=0
endif
else 
if k2 == 0 then
event "i", -11.2, 0, -1, 2
k2=1
endif
endif

if gkb3 == 1 then
if k3 == 1 then
event "i", 11.3, 0, -1, 3
k3=0
endif
else 
if k3 == 0 then
event "i", -11.3, 0, -1, 3
k3=1
endif
endif

if gkb4 == 1 then
if k4 == 1 then
event "i", 11.4, 0, -1, 4
k4=0
endif
else 
if k4 == 0 then
event "i", -11.4, 0, -1, 4
k4=1
endif
endif

if gkb5 == 1 then
if k5 == 1 then
event "i", 11.5, 0, -1, 5
k5=0
endif
else 
if k5 == 0 then
event "i", -11.5, 0, -1, 5
k5=1
endif
endif

if gkb6 == 1 then
if k6 == 1 then
event "i", 11.6, 0, -1, 6
k6=0
endif
else 
if k6 == 0 then
event "i", -11.6, 0, -1, 6
k6=1
endif
endif

if gkb7 == 1 then
if k7 == 1 then
event "i", 11.7, 0, -1, 7
k7=0
endif
else 
if k7 == 0 then
event "i", -11.7, 0, -1, 7
k7=1
endif
endif

if gkb8 == 1 then
if k8 == 1 then
event "i", 11.8, 0, -1, 8
k8=0
endif
else 
if k8 == 0 then
event "i", -11.8, 0, -1, 8
k8=1
endif
endif

endin

/* This is the synthesis instrument */
instr 11

/* This concatenates the instance number (p4) 
   and the slider number */
S1  sprintf "S%d", p4
/* This takes in control from sliders S1-S8 */   
k1  chnget S1
/* This takes in the main volume control */
k2  chnget "main_volume"
/* smoothing the volume control */
kv  tonek  k2/10, 10  
/* amplitude envelope */
ka  linenr  2000*kv,0.1,0.1,0.05
/* oscillator */
a1      oscili  ka, k1, 1
        out     a1
        endin

</CsInstruments>
<CsScore>
f1 0 1024 10 1 0.5 0.3 0.25 0.2 0.18 0.15 0.13 0.11 0.1 0.09
i1 0 3600

</CsScore>
</CsoundSynthesizer>

Further help and where to get it

On-line help can be obtained by using PyDoc. See also examples in the csndsugui repository [1] where you can also get the latest code and a PyDoc-generated HTML reference file.