Difference between revisions of "CS110:Lab10"

From IPRE Wiki
Jump to: navigation, search
(Useful Gamepad Details)
('''Creating an Orchestra''')
 
(15 intermediate revisions by 3 users not shown)
Line 1: Line 1:
='''Lab 10: Music Demo & The Game Pad''' =
+
='''Lab 09: Exploring Music''' =
  
 
=='''Objective'''==
 
=='''Objective'''==
* Give me a demo of your music program (Assignment 9)
+
* Learn some fundamentals of sound and music
* Explore the different functions of the game pad
+
* Create musical compositions
  
=='''Using Your Game Pad to Drive Your Robot Around'''==
+
=='''Useful Resources'''==
  
You should ensure that your game pad is plugged into a USB port before starting IDLE. The annotated image below shows a game pad similar to the one you will be using.
+
[[ChucK]]
  
[[Image:Gamepad1.JPG|center]]
+
=='''Sound'''==
 +
Having explored and used many of the robot commands by now, you have seen that your robot make beeps when you call the '''beep()''' function. You can also have Myro make a beep directly out of your computer, rather than the robot. For instance, if you execute the following command:
  
To use the game pad, type the following at the prompt:
+
<pre>
 +
computer.beep(3, 880)
 +
</pre>
 +
 
 +
This command tells your computer to play a tone at 880 Hertz for 3 seconds. Hertz is
 +
a unit that measures frequency. <br>
 +
 
 +
<b>1Hertz = 1cycle / second </b>
 +
 
 +
Therefore, a beep at 880 Hz represents 880 complete cycles per second.
 +
Humans can hear frequencies in the 20 Hz to 20000 Hz (or 20 Kilo Hertz) range and
 +
are able to distinguish sounds that differ only by a few
 +
Hertz (as little as 1 Hz). This ability varies from person to person.
 +
 
 +
Try the following commands and see if you can distinguish between the two tones:
  
 
<pre>
 
<pre>
gamepad()
+
computer.beep(1, 440)
 +
computer.beep(1, 450)
 
</pre>
 
</pre>
  
You should see the following options when you do this:
+
To make the tones more distinctive, place the commands above in a loop so
 +
that you can repeatedly hear the alternating tones. <br>
 +
 
 +
 
 +
'''Do This:''' Program your computer to create a siren by repeating two
 +
different tones. You will have to experiment
 +
with different pairs of frequencies (they may be close together or far apart) to
 +
produce a realistic sounding siren. Write your program to play the siren for 15
 +
seconds. The louder the better!
 +
 
 +
=='''Musical Scales'''==
 +
 
 +
In western music, a ''scale'' is divided into 12 notes (from 7 major notes:
 +
ABCDEFG). An ''octave'' in C comprises of the 12 notes shown below: <br>
 +
 
 +
<b> C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B </b> <br><br>
 +
 
 +
C# (pronounced "C sharp") is the same tone as Db (pronounced "D flat").
 +
Frequencies corresponding to a specific note, for example C, are multiplied (or
 +
divided) by 2 to generate the same note in a higher (or lower) octave. For instance
 +
in the two tones shown below, the second tone is one octave higher than the first: <br><br>
 +
computer.beep(1, 440)<br>
 +
computer.beep(1, 880) <br> <br>
 +
 
 +
Therefore in order to raise a tone by 1 octave, you multiply the frequency by 2.
 +
Likewise, to make a tone 1 octave lower, you divide by 2.
 +
Notes indicating an octave can be denoted as follows: <br>
 +
<b>C0 C1 C2 C3 C4 C5 C6 C7 C8 </b><br><br>
 +
That is, C0 is the note for C in the lowest (or 0) octave. The fifth octave
 +
(numbered 4) is commonly referred to as a middle octave. Thus C4 is the C
 +
note in the middle octave. The frequency corresponding to C4 is 261.63 Hz. <br> <br>
 +
 
 +
'''Do This:'''
 +
Try playing '''C4''' on the computer. Also try C5 (523.25) which is twice the
 +
frequency of C4 and C3 (130.815). <br>
 +
 
 +
=='''Computing the Computer's Range of Tones'''==
 +
 
 +
In common tuning, the 12 notes are equidistant. Therfore, if the frequency doubles every octave, each successive note is 21 / 12 apart. That is, if C4 is 261.63 Hz, C# (or Db) will be:
 +
 
 +
C#4/Db4 = 261.63 *2 ^(1/12) = 277.18
 +
 
 +
We can then compute all successive note frequencies:
 +
 
 +
* D4 = 277.18 * 2 ^ (1/12) = 293.66
 +
* D#4/Eb = 293.66 * 2 ^ (1/12) = 311.13 
 +
* etc. 
 +
 
 +
'''Note: ''' In python, the characters that denote the exponent are '''**'''. Therefore to raise 2 by the exponent 3, you would type:
  
 
<pre>
 
<pre>
        Pad  Action
+
2 ** 3
    ------  -------
+
</pre>
Left/Right  turnLeft() and turnRight()
+
    Up/Down  forward() and backward()
+
  
    Button  Action
+
The lowest tone that the Computer can play is A0 and the highest tone is C8. A0 has a frequency of 27.5 Hz, and C8 has a frequency of 4186 Hz. That's quite a range! See if you can you hear the entire range. Try this:
    ------  -------
+
          1  stop()
+
          2  takePicture()
+
          3  beep(.25, 523)
+
          4  beep(.25, 587)
+
          5   beep(.25, 659)
+
          6  speak('Hello. My name is Dribbler.')
+
          7  speak('Ouch! I'm a sensitive robot.')
+
          8  speak('I'm hungry. Do you have any batteries?')
+
  
Gamepad is now running... Press button 1 to stop.
+
<pre>
 +
computer.beep(1, 27.5)
 +
computer.beep(1, 4186)
 
</pre>
 
</pre>
  
You can then use the buttons on your game pad to move your robot around as well as to explore additional features of your robot (e.g. taking a picture, beeping or speaking).
+
'''Do This:''' Write a program to play all the 12 notes in an octave
 +
using the above computation. You may assume in your program that C0 is
 +
16.35 and then use that to compute all frequencies in a given octave (C4 is
 +
16.35 * 24). Your program should input an octave (a number from 0 through
 +
8), produce all the notes in that octave and also printout a frequency chart for
 +
each note in that octave.
  
== Useful Gamepad Details ==
+
== '''Making Music''' ==
  
In lab 02, you saw how you could use your game pad to control your robot's movements.
+
Now we turn from beeps to music. First, let's make an easy method of playing notes by name, rather than by frequencies:
In addition to the '''gamepad()''' command you learned in lab 2(you should revisit this lab for reference), there is the '''getGamepad()''' function which allows you do a wider variety of tasks with our game pad.
+
The parameters this function takes are listed below: <br><br>
+
  
# getGamepad("count") - returns (immediately) the number of gamepads connected to your computer.
+
<pre>
# getGamepad(ID, ITEM, ...) - return the ITEMs for gamepad ID. ID can be left out and will default to 0, the first one. If you request more than one ITEM, then they come back in a dictionary. Just request one ITEM and you'll get the value (as a list, string, or number).
+
from myro import *
# getGamepad([ID1, ID2...], ITEM, ...) - return the ITEMs for gamepad IDs as a list of lists of ID, RESULTS. For example:
+
#: >>> getGamepad([0, 1], "button", "axis")
+
#: [[0, {'button': [0, 0, 0, 0, 0, 0, 0, 0], 'axis': [0.0, 1.0]}], [1, {'button': [1, 1, 0, 0, 0, 0, 0, 0], 'axis': [-1.0, -1.0]}]]
+
#: >>> getGamepad([0, 1], "axis")
+
#: [[0, [0.0, 1.0]], [1, [-1.0, -1.0]]]
+
# getGamepad() has one keyword argument, "wait" for setting a sleep value between gamepad polls. Default is 0.05 seconds. Setting to zero will have small latency, but may eat up your CPU.
+
  
ITEM can be:
+
def getFrequency(noteName):
 +
    return media._frequency[noteName.lower()]
 +
</pre>
  
#"count" - returns (immediately) number of gamepads plugged in
+
To use this, you can give it the name of a note as a string, and get back the frequency. For example:
#"robot" - returns axis values (floats) ready to be used by move() Myro command as [translate, rotate]
+
#empty  - if nothing is passed in, it returns all of the following as a dictionary
+
#"init" - has this gamepad been initialized? as boolean
+
#"name" - name of gamepad, as string
+
#"axis" - returns values of axis controller in a list (as floats)
+
#"ball" - returns values of ball controller in a list
+
#"button" - returns values of buttons in a list (as integers)
+
#"hat" - returns values of hat in a list
+
  
== Gamepad Examples ==
 
 
<pre>
 
<pre>
>>> getGamepad("count")
+
>>> getFrequency("C4")
2
+
261.60000000000002
>>> getGamepad("button") # waits till you press at least one button
+
 
[0, 0, 1, 0, 1, 0, 0, 0]
+
>>> computer.beep(1, getFrequency("C4"))
>>> getGamepad(1, "button") # waits till ID 1 presses at least one button
+
[plays a lovely note at that frequency for 1 second]
[1, 0, 0, 0, 0, 0, 0, 0]
+
>>> getGamepad(range(getGamepad("count")), "button")
+
# waits till someone presses a button
+
[[0, [1, 0, 0, 0, 0, 0, 0, 0]],
+
[1, [0, 0, 0, 0, 0, 0, 0, 0]]]
+
>>> getGamepad("button", "axis", wait=.01)
+
{"button": [1, 0, 0, 0, 0, 0, 0, 0],
+
"axis": [-0.999969482421875, 0.0]}
+
(sometimes axis doesn't return exactly 1 or -1).
+
 
</pre>
 
</pre>
  
 +
You can also transcribe an entire song as a string using the makeSong() function in Myro. For example, to get the frequencies and timings for "A4 1/4; C5 1/8; C5 1/8; A4 1/4" you could:
  
You should explore the getGamepad("button") and write down the buttons that correspond to a '1' in the game pad list.
+
>>> makeSong("A4 1/4; C5 1/8; C5 1/8; A4 1/4")
For example, if you hit the button marked '3' in the game pad image above, after calling the getGamepad("button")
+
[(440.0, 0.25), (523.29999999999995, 0.125), (523.29999999999995, 0.125), (440.0, 0.25)]
command and this list is returned: [0, 0, 0, 0, 1, 0, 0, 0], then you will know that the button '3' corresponds to
+
 
the 5th element in the game pad list. Using this knowledge, you can create an array of using functions.
+
To use this, you could:
Here is an example:
+
  
 
<pre>
 
<pre>
 +
for freq, note in makeSong("A4 1/4; C5 1/8; C5 1/8; A4 1/4"):
 +
    # convert notes (eq, 1/4) into seconds:
 +
    beat = note * 4 * 0.4              # 1/4 = one beat, which lasts for 0.4 seconds
 +
    computer.beep(beat, freq)
 +
</pre>
  
def playMandolin():
+
You can also put the song in a separate file and use '''readSong(filename)'''. For more details on this function, see [[Song File Format]].
  m = Mandolin()
+
  m.connect()
+
  m.setGain(0.3)
+
  beat = 0.4
+
  wait(beat * 12) # wait for 4 measures / loop iterations
+
  for i in range(3):
+
    playOnce(m, beat, 1)
+
    playOnce(m, beat, 1)
+
    playOnce(m, beat/2, .8)
+
    playOnce(m, beat/2, .8)
+
  m.noteOn(1)
+
  
buttonsHit = getGamepad("button")
+
Now, explore the functions of [[ChucK]] i.e. '''Run the example commands you see!!''' ChucK is already installed on the machines in the lab so you can skip the section that deals with installation if you are doing these exercises in the lab. You will need to understand the basic operations to continue. <br>
if buttonsHit[4] == 1:
+
  playMandolin()
+
</pre>
+
  
Here is another example of a program to control your robot:
+
After you have used the functions in ChucK, you will see that you can change the
 +
frequencies of different instruments pretty easily by using the '''setFrequency''' command.
 +
So instead of changing the frequency of the computer and making it beep, you could change the
 +
frequency of a mandolin and pluck its strings using commands similar to the ones
 +
you just executed:  
  
 
<pre>
 
<pre>
done = False
+
man = Mandolin() #create a mandolin instrument
while not done:
+
man.connect()
    results = getGamepad("button", "robot")
+
for freq, note in makeSong("A4 1/4; C5 1/8; C5 1/8; A4 1/4"):
    move(*results["robot"])
+
     man.setFrequency(freq)
     if results["button"][1]: beep(.5, 440)
+
     wait(0.3)
     if results["button"][2]: beep(.5, 880)
+
     m.pluck(note)
     done = (results["button"][0] == 1)
+
 
</pre>
 
</pre>
  
= Assignment =
+
== Creating Instruments and Parts ==
 +
 
 +
For this example, we will create a 7 measure song, each measure having three beats. Let's define a beat to be 0.4 seconds long.
 +
 
 +
First, let's start with a percussion background. If we make a quarter note be one beat, then there are 3 quarter notes in a measure. This is known as 3 4 time.
 +
 
 +
Let's have our percussion play a quarter note, eighth note, eighth note, quarter.
 +
 
 +
To make this a little easier, let's create a generic function that will tell an instrument to play a note at a given strength, and wait a certain amount of time. This is very similar to how we move a robot:
 +
 
 +
# helper function to play one sound / note
 +
def playOnce(instrument, time, strength):
 +
    instrument.noteOn(strength)
 +
    wait(time)
 +
 
 +
Next, we define the percussion instrument (a Shaker) and its 7-measure part:
 +
 
 +
def playShakers():
 +
    shakers = Shakers()
 +
    shakers.connect()
 +
    beat = 0.4
 +
    for i in range(7):
 +
        playOnce(shakers, beat, 1)
 +
        playOnce(shakers, beat/2, .8)
 +
        playOnce(shakers, beat/2, .8)
 +
        playOnce(shakers, beat, 1)       
 +
    shakers.noteOn(1)
 +
 
 +
Notice that this repeats 7 measure of the 1/4 1/8 1/8 1/4 pattern. To leave the shakers in a normal state, we issue the command '''shakers.noteOn(1)'''. Also, we define the instrument as a function of no parameters. This is important for later.
 +
 
 +
You can test it with:
 +
 
 +
>>> playShakers()
 +
 
 +
And let's define another instrument, and its part:
 +
 
 +
def playBar():
 +
    bar = StruckBar()
 +
    bar.connect()
 +
    beat = 0.4
 +
    wait(beat * 6) # wait for 2 measures / loop iterations
 +
    for i in range(5):
 +
        playOnce(bar, beat/2, .8)       
 +
        playOnce(bar, beat/2, .8)
 +
        playOnce(bar, beat, 1)
 +
        playOnce(bar, beat, 1)
 +
    bar.noteOn(1)
 +
 
 +
and test it:
 +
 
 +
>>> playBar()
 +
 
 +
Notice how it waits for two measures (6 beats) and then plays 5 measures.
 +
 
 +
And finally, a Mandolin part:
 +
 
 +
def playMandolin():
 +
    m = Mandolin()
 +
    m.connect()
 +
    m.setGain(0.3)
 +
    beat = 0.4
 +
    wait(beat * 12) # wait for 4 measures / loop iterations
 +
    for i in range(3):
 +
        playOnce(m, beat, 1)
 +
        playOnce(m, beat, 1)
 +
        playOnce(m, beat/2, .8)
 +
        playOnce(m, beat/2, .8)
 +
    m.noteOn(1)
 +
 
 +
Notice that it waits 4 measures (12 beats) and then plays 3 measures.
 +
 
 +
This Mandolin part plays all the same note. You can change the frequency of such instruments using the '''setFrequency()''' method. See [[ChucK]] for more details. Combined with the '''makeSong()''' function (above) you can create beautiful duets between say, a Violin and a Piano.
 +
 
 +
== '''Creating an Orchestra'''==
 +
 
 +
To play multiple parts together, we'll use the '''doTogether''' function. It takes a series of function names, and plays them together.
 +
 
 +
>>> doTogether(playShakers, playBar, playMandolin)
 +
 
 +
DoTogether takes a series of functions, where each function takes zero arguments.
 +
 
 +
NOTE: don't put this line in your file. Instead, type it in the Python Shell.
 +
 
 +
= Assignment 09 =
 +
 
 +
'''Write a piece of music and perform it:''' The composition should be at least 1 minute 30 seconds in length. You should use at least three instruments, and one of those should use different frequencies. Your assignment will be graded on style of code, and style of music. Demos will be done the following week. HINT: you might want to find some musical compositions on the web to play some nice music. For example, a search for "notes to ode to joy" found this: [http://wiki.answers.com/Q/What_are_the_notes_ode_to_joy ode_to_joy].
  
'''1. ''' Create a musical instrument. You can use the axes of the game pad to change the octave of the instruments.
+
'''Bonus points''' will be given for extra components: more than three parts, use of frequencies, harmony, variety of instruments, fast parts/slow parts, etc.
Buttons can work together to play two-tone frequencies. Or play sound files. Be Creative.
+
<br>
+
'''2.''' Create a graphical scene (you can use your collage or the seasonal scenes you created in labs 7 and 8 or add to them).
+
Use the buttons to do tasks such as moving an object, flipping an image, coloring an object etc.
+
  
 
= Links to Course-Related Pages =
 
= Links to Course-Related Pages =
 
* Back to [http://wiki.roboteducation.org/CS110_Lab Lab Home Page]  
 
* Back to [http://wiki.roboteducation.org/CS110_Lab Lab Home Page]  
* Back to [http://cs.brynmawr.edu/Courses/cs110/fall2009/ Course Home Page]
+
* Back to [http://cs.brynmawr.edu/cs110 Course Home Page]

Latest revision as of 14:54, 29 March 2010

Lab 09: Exploring Music

Objective

  • Learn some fundamentals of sound and music
  • Create musical compositions

Useful Resources

ChucK

Sound

Having explored and used many of the robot commands by now, you have seen that your robot make beeps when you call the beep() function. You can also have Myro make a beep directly out of your computer, rather than the robot. For instance, if you execute the following command:

computer.beep(3, 880)

This command tells your computer to play a tone at 880 Hertz for 3 seconds. Hertz is a unit that measures frequency.

1Hertz = 1cycle / second

Therefore, a beep at 880 Hz represents 880 complete cycles per second. Humans can hear frequencies in the 20 Hz to 20000 Hz (or 20 Kilo Hertz) range and are able to distinguish sounds that differ only by a few Hertz (as little as 1 Hz). This ability varies from person to person.

Try the following commands and see if you can distinguish between the two tones:

computer.beep(1, 440)
computer.beep(1, 450)

To make the tones more distinctive, place the commands above in a loop so that you can repeatedly hear the alternating tones.


Do This: Program your computer to create a siren by repeating two different tones. You will have to experiment with different pairs of frequencies (they may be close together or far apart) to produce a realistic sounding siren. Write your program to play the siren for 15 seconds. The louder the better!

Musical Scales

In western music, a scale is divided into 12 notes (from 7 major notes: ABCDEFG). An octave in C comprises of the 12 notes shown below:

C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B

C# (pronounced "C sharp") is the same tone as Db (pronounced "D flat"). Frequencies corresponding to a specific note, for example C, are multiplied (or divided) by 2 to generate the same note in a higher (or lower) octave. For instance in the two tones shown below, the second tone is one octave higher than the first:

computer.beep(1, 440)
computer.beep(1, 880)

Therefore in order to raise a tone by 1 octave, you multiply the frequency by 2. Likewise, to make a tone 1 octave lower, you divide by 2. Notes indicating an octave can be denoted as follows:
C0 C1 C2 C3 C4 C5 C6 C7 C8

That is, C0 is the note for C in the lowest (or 0) octave. The fifth octave (numbered 4) is commonly referred to as a middle octave. Thus C4 is the C note in the middle octave. The frequency corresponding to C4 is 261.63 Hz.

Do This: Try playing C4 on the computer. Also try C5 (523.25) which is twice the frequency of C4 and C3 (130.815).

Computing the Computer's Range of Tones

In common tuning, the 12 notes are equidistant. Therfore, if the frequency doubles every octave, each successive note is 21 / 12 apart. That is, if C4 is 261.63 Hz, C# (or Db) will be:

C#4/Db4 = 261.63 *2 ^(1/12) = 277.18

We can then compute all successive note frequencies:

  • D4 = 277.18 * 2 ^ (1/12) = 293.66
  • D#4/Eb = 293.66 * 2 ^ (1/12) = 311.13
  • etc.

Note: In python, the characters that denote the exponent are **. Therefore to raise 2 by the exponent 3, you would type:

2 ** 3

The lowest tone that the Computer can play is A0 and the highest tone is C8. A0 has a frequency of 27.5 Hz, and C8 has a frequency of 4186 Hz. That's quite a range! See if you can you hear the entire range. Try this:

computer.beep(1, 27.5)
computer.beep(1, 4186)

Do This: Write a program to play all the 12 notes in an octave using the above computation. You may assume in your program that C0 is 16.35 and then use that to compute all frequencies in a given octave (C4 is 16.35 * 24). Your program should input an octave (a number from 0 through 8), produce all the notes in that octave and also printout a frequency chart for each note in that octave.

Making Music

Now we turn from beeps to music. First, let's make an easy method of playing notes by name, rather than by frequencies:

from myro import *

def getFrequency(noteName):
    return media._frequency[noteName.lower()]

To use this, you can give it the name of a note as a string, and get back the frequency. For example:

>>> getFrequency("C4")
261.60000000000002

>>> computer.beep(1, getFrequency("C4"))
[plays a lovely note at that frequency for 1 second]

You can also transcribe an entire song as a string using the makeSong() function in Myro. For example, to get the frequencies and timings for "A4 1/4; C5 1/8; C5 1/8; A4 1/4" you could:

>>> makeSong("A4 1/4; C5 1/8; C5 1/8; A4 1/4")
[(440.0, 0.25), (523.29999999999995, 0.125), (523.29999999999995, 0.125), (440.0, 0.25)]

To use this, you could:

for freq, note in makeSong("A4 1/4; C5 1/8; C5 1/8; A4 1/4"):
    # convert notes (eq, 1/4) into seconds:
    beat = note * 4 * 0.4              # 1/4 = one beat, which lasts for 0.4 seconds
    computer.beep(beat, freq)

You can also put the song in a separate file and use readSong(filename). For more details on this function, see Song File Format.

Now, explore the functions of ChucK i.e. Run the example commands you see!! ChucK is already installed on the machines in the lab so you can skip the section that deals with installation if you are doing these exercises in the lab. You will need to understand the basic operations to continue.

After you have used the functions in ChucK, you will see that you can change the frequencies of different instruments pretty easily by using the setFrequency command. So instead of changing the frequency of the computer and making it beep, you could change the frequency of a mandolin and pluck its strings using commands similar to the ones you just executed:

man = Mandolin() #create a mandolin instrument
man.connect()
for freq, note in makeSong("A4 1/4; C5 1/8; C5 1/8; A4 1/4"):
    man.setFrequency(freq)
    wait(0.3)
    m.pluck(note)

Creating Instruments and Parts

For this example, we will create a 7 measure song, each measure having three beats. Let's define a beat to be 0.4 seconds long.

First, let's start with a percussion background. If we make a quarter note be one beat, then there are 3 quarter notes in a measure. This is known as 3 4 time.

Let's have our percussion play a quarter note, eighth note, eighth note, quarter.

To make this a little easier, let's create a generic function that will tell an instrument to play a note at a given strength, and wait a certain amount of time. This is very similar to how we move a robot:

# helper function to play one sound / note
def playOnce(instrument, time, strength):
   instrument.noteOn(strength)
   wait(time)

Next, we define the percussion instrument (a Shaker) and its 7-measure part:

def playShakers():
   shakers = Shakers()
   shakers.connect()
   beat = 0.4
   for i in range(7):
       playOnce(shakers, beat, 1)
       playOnce(shakers, beat/2, .8)
       playOnce(shakers, beat/2, .8)
       playOnce(shakers, beat, 1)        
   shakers.noteOn(1)

Notice that this repeats 7 measure of the 1/4 1/8 1/8 1/4 pattern. To leave the shakers in a normal state, we issue the command shakers.noteOn(1). Also, we define the instrument as a function of no parameters. This is important for later.

You can test it with:

>>> playShakers()

And let's define another instrument, and its part:

def playBar():
   bar = StruckBar()
   bar.connect()
   beat = 0.4
   wait(beat * 6) # wait for 2 measures / loop iterations
   for i in range(5):
       playOnce(bar, beat/2, .8)        
       playOnce(bar, beat/2, .8)
       playOnce(bar, beat, 1)
       playOnce(bar, beat, 1)
   bar.noteOn(1)

and test it:

>>> playBar()

Notice how it waits for two measures (6 beats) and then plays 5 measures.

And finally, a Mandolin part:

def playMandolin():
   m = Mandolin()
   m.connect()
   m.setGain(0.3)
   beat = 0.4
   wait(beat * 12) # wait for 4 measures / loop iterations
   for i in range(3):
       playOnce(m, beat, 1)
       playOnce(m, beat, 1)
       playOnce(m, beat/2, .8)
       playOnce(m, beat/2, .8)
   m.noteOn(1)

Notice that it waits 4 measures (12 beats) and then plays 3 measures.

This Mandolin part plays all the same note. You can change the frequency of such instruments using the setFrequency() method. See ChucK for more details. Combined with the makeSong() function (above) you can create beautiful duets between say, a Violin and a Piano.

Creating an Orchestra

To play multiple parts together, we'll use the doTogether function. It takes a series of function names, and plays them together.

>>> doTogether(playShakers, playBar, playMandolin)

DoTogether takes a series of functions, where each function takes zero arguments.

NOTE: don't put this line in your file. Instead, type it in the Python Shell.

Assignment 09

Write a piece of music and perform it: The composition should be at least 1 minute 30 seconds in length. You should use at least three instruments, and one of those should use different frequencies. Your assignment will be graded on style of code, and style of music. Demos will be done the following week. HINT: you might want to find some musical compositions on the web to play some nice music. For example, a search for "notes to ode to joy" found this: ode_to_joy.

Bonus points will be given for extra components: more than three parts, use of frequencies, harmony, variety of instruments, fast parts/slow parts, etc.

Links to Course-Related Pages