Lab 09: Exploring Music

Objective

Useful Resources

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:

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:

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:

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.

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.