ChucK

From IPRE Wiki
Revision as of 19:09, 31 October 2008 by Ami (Talk | contribs) (draft / in progress)

Jump to: navigation, search

Setting up ChucK with Myro

ChucK:

  • Download ChucK.
    • On Windows:
      • Get the Windows executable (first link in windows section)
      • Put bin\chuck.exe in c:\windows\system32\ (despite the system's hesitation)
      • You should now be able to run chuck from the command line (e.g. enter "chuck --help")
    • On other platforms:
    • ChucK is a specialized audio programming language, to which the sound synthesis part is "outsourced".


Myro:

  • Upgrade Myro.


SimpleOSC: should come with the upgrade, so these steps are probably not required:

  • Download SimpleOSC library for Python (the very last link on the page)
    • On Windows:
      • Install by typing python setup.py install in the command line, or by copying the osc folder into c:\python2.x\lib\site-packages\
    • On other platforms:
      • Enter python setup.py install in the terminal
    • Open Sound Control (OSC) provides a way to communicate between Python and ChucK.


Getting started

First, import the Myro ChucK module:

>>> from myro.chuck import *

Next, set up the communication and synthesis framework with this initialization function:

>>> initChucK()

If it works, a command window is created (on windows). This is running a ChucK program that will synthesize sound on demand. The best way to test it is to go ahead and write some sound-generating code in the Python shell:

>>> s = SineWave()
>>> s.connect()

The first line above creates a SineWave generator and assigns it to the variable s.

The second line connects s to your computer's sound output device (speakers or headphones).

As a result, you hear a sine tone playing on and on. It sounds a little like the Myro beep(), except that it keeps going until you make it stop. How do you make it stop? By disconnecting it from your sound output device.

>>> s.disconnect()

Besides connecting and disconnecting the SineWave generator, you can also modify it in some ways. For example, try:

>>> s.connect() # connect it again first, to hear it
>>> s.setGain(1.0)
>>> wait(0.5)
>>> s.setGain(0.0)
>>> wait(0.5)
>>> s.setGain(0.5)

What would you do expect this to do? What does it do?

The setGain() function changes the sound's gain, which is closely related to its loudness. A higher gain results in a louder sound. A lower gain results in a softer sound. A 0.0 gain results in silence even though s has not been disconnected -- it has been muted.

Another way to modify the sound is to change its frequency... while the sound is playing!

>>> s.setFrequency(200)
>>> wait(1.0)
>>> s.setFrequency(400)
>>> wait(1.0)
>>> s.setFrequency(444.5)

The setFrequency() function takes a frequency in Hertz and modifies s to have that frequency. This leads to plenty of fun. For example, what does this code do?

>>> freq = 100.0
>>> while timeRemaining(30):
        s.setFrequency(freq)
        wait(0.2)
        freq = freq * 1.01

Listen to the result of this while loop and think of ways to modify it. How can you make it change the frequency more often? By greater amounts? Write some code that randomly changes the frequency of the SineWave 10 times per second (i.e. once every 0.1 seconds).


More instruments

Because the SineWave generates sound, we think of it as a type of instrument. But we have many other instruments too. Each of them lets you setFrequency() and setGain(), but many allow further changes more suitable to the specific instrument. For example, a mandolin can be plucked, or a saxophone can be blown into. Try this:

>>> m = Mandolin()
>>> m.connect()

This creates a virtual mandolin, assigns it to the variable m, and connects it to the sound output device. But unlike with the SineWave, at this point you still don't hear anything. This is because a mandolin won't make sound until you do something to it -- specifically, you need to pluck it:

>>> m.pluck(0.2)
>>> wait(1)
>>> m.pluck(1.0)

The pluck() function works for mandolins or other pluckable instruments. It takes a value between 0.0 and 1.0, which represents how hard you are plucking the string.

Because many instruments don't make sound until you do something to them, we have two more functions that all instruments allow:

  • noteOn(): start playing a note
  • noteOff(): stop playing a note

Each of these takes an argument between 0.0 and 1.0, representing the strength or velocity of the noteOn or noteOff operation. For example, try:

>>> x = Saxophone()
>>> x.connect()
>>> x.noteOn(0.8)
>>> wait(1.0)
>>> x.noteOff(0.2)

Try this also with other values in place of 0.8 and 0.2.