Lab 09: Exploring Music
- Learn some fundamentals of sound and music
- Create musical compositions
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:
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!
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:
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.
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
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.
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:
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:
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.
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.