# Lab 09: Exploring Music

## Objective

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

## 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. For instance, if you execute the following command:

```beep(3, 880)
```

This command tells your robot 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:

```beep(1, 440)
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 Scribbler 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! You can also have Myro make a beep directly out of your computer, rather than the robot, with the command:

```computer.beep(1, 440)

```

## 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:
beep(1, 440) 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 it on the Scribbler. Also try C5 (523.25) which is twice the frequency of C4 and C3 (130.815).

## Computer the Scribbler'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.

The lowest tone that the Scribbler 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:

```beep(1, 27.5)
beep(1, 4186)
```

Do This: Write a Scribbler 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.