Subsection 6.5.1 Adding sound
This
sheet contains our Alma Mater music. Your goal will be to get our speaker to produce at least the basic melody. We’ll explain some of the basics below.
First we need to code the frequencies for various notes. As you may know, each note has a corresponding frequency. But it also happens that the same note can be played at a higher "octave", and for each octave increase we have to double the frequency. For example what is often referred to as the "middle C", which we will denote by
C4, will be at the frequency of 261.63Hz. The next higher version of C, called
C5, would be at
\(2\times 261.63 = 523.26\)Hz, and the next one,
C6, would be 1046.52Hz.
This range from one C to the next is divided into 12 parts called
semitones. To compute each semitone in order we have to multiply the previous one with "two to the power of
\(\frac{1}{12}\)"", which is equal to 1.059463. After twelve of those multiplications happen, it would be as if we multiplied by 2 (note that
\(1.059463^{12} = 2\)).
We now mark those 12 semitones with symbols. In a piano keyboard these correspond exactly to the 12 keys (white and black). We already used C for the first one, the next one is a black key that we can call "C-sharp" or "D-flat". In code we will denote those as
C4s and
D4b respectively. Then we continue in this manner. Here is a
notes.py file that does all this math, include it into your project, and add a
from notes import * line somewhere near the top. You should also take a moment to open this file in your favorite text/code editor and view its contents.
We need to set up two useful constants for our work. One is the tempo we want measured in
BPM (beats per minute). Set this to equal 100 for now, but know that you can adjust it to speed-up or slow-down the rhythm of the piece.
Then we need to compute the
BEATMS, how many milliseconds a "beat" should last. This is computed directly from the BPM: It should equal 60 times 1000 divided by BPM. As we play the piece, we’ll need to use this variable to compute the duration for each note.
Now to look at the sheet music and match it to "note and frequency". First off you want to only focus on the lines above the words. Each circle denotes a note, and you’ll see they come in pairs, we want to focus on the top note from each pair. So the very first note is basically just below the bottom of the 5 lines. This would correspond to D4, and we go up from there: The bottom line exactly is E4, the space between the bottom two lines is F4, the second line is G4 and so on. Note that there is a specific key signature here that tells us that we should use "E-flat" and "B-flat", so whenever you would run into those you want to use the -b variants, so it would actually be
E4b. Note also that after G4 we have A4, not A5. Then after A4 we have B4, then we switch to C5. so this should help you match a note frequency to each of the notes in the sheet.
The next step is to figure out durations. A black circle with a line next to it is "1 beat", which would play for duration
BEATMS for us. If it has a little tail at the top it should be half that, and if it has a little dot next to it (like the first note does) it is one and a half times that. Every segment (marked by the vertical lines) should add up to 4. A hollowed out circle as you see at the end of the first line counts for 4 by itself, while a hollow circle with a line up from it (like you see in "live our" in the third block) is 2 beats.
Practice 6.5.1.
Set up your speaker to play the Alma Mater, the first two lines in the sheet at least. But do so as follows: You should have an array where each entry is a pair of a note and a number of beats, for example the very first one would be
(D4, 1.5). Then in a for loop you extract these pairs and use the
tone function to play the corresponding note for the corresponding duration (remember to multiple the number like 1.5 with the
BEATMS factor).
This playing is the first objective of this lab.