Friday, April 19, 2013

The Math of Music (Part 1)

I've always been interested in the mathematical theory behind music.  When I started programming computers in 1977, I wrote programs to play music so I needed to calculate the note frequencies.  I was never able to understand, however, why the piano keys are the way they are.  Why is there no E# or Fb note? Why no B# or Cb?  Why is the same key sometimes called F# and sometimes called Gb?

I've since learned how this works and it's all simple math.  In this post, I'll try to explain it.

Let's start with Middle C on the piano keyboard.  I really don't care what frequency it is.  Musical notes are all about ratios, so let's just call the frequency c.  What notes sound good when you play them at the same time as the middle C note?  Well, a note that's twice the frequency sounds good.  For every cycle of the C note, the note at 2c makes two cycles. This blends nicely with the C and makes a nice sound.  This note is high C - the next C up on the keyboard.

Another note that sounds good along with middle C is at 1/2 c. This is the next C down on the keyboard. To go up and down by octaves, we double (to go up) or half (to go down) the frequency of a note.  From a scale perspective, I'm going to consider all C notes equivalent.

Ok, what note is halfway between c and 2c?  Obviously it's 1.5c. (Later we'll call this G but we'll get to that.) This note makes three cycles for every two cycles of the C note.  It sounds very nice played along with a C so we'll include it.

The next note that sounds good is halfway between 1c and 1.5c.  That's 1.25c. (Later we'll call this E.) This note makes 5 cycles for every 4 cycles of the C note.

If you play these three notes all at once you get what is called a major chord.

1c    1.25c   1.5c          2c     Major Chord
|------|-------|------------|

Major chords sound very nice.  Let's then build a major chord starting at the 1.5c note.  The frequencies would be (I'll drop the "c" notation for simplicity):

1.5 * 1 = 1.5
1.5 * 1.25 = 1.875
1.5 * 1.5 = 2.25

1     1.25    1.5           2     C Major Chord
|------|-------|------------|
1.5   1.875     2.25
|-------|-------|  G Major Chord

We'll later call these notes G, B and D but we'll get to that.

The note at 2.25 is musically equivalent to the same note an octave down so let's divide the frequency by 2 to put it into the range 1..2

1.5 * 1 = 1.5
1.5 * 1.25 = 1.875
1.5 * 1.5 / 2 = 1.125

1     1.25     1.5          2     C Major Chord
|------|-------|------------|
1.125        1.5   1.875
|-----------|-------|         G Major Chord

To make this major chord, we picked the middle note (1.5) and made a scale from it.  Let's do the opposite - let's make a chord where the C note is the middle note.  That means we get these frequencies:

1 / 1.5 = 0.6666...
1 / 1.25 = 0.8333...
1

1     1.25    1.5           2     C Major Chord
|------|-------|------------|
0.6666  0.8333  1        1.3333
|-----|-----|----------|

We'll later label these notes as F, A and C.

We've structured this so that the C note at 1 is halfway between 1.3333 and 0.6666.

Since 0.666 and 0.833 are less than 1, we can double them to make equivalent notes in the range 1..2:

1 / 1.5 * 2 = 1.3333...
1 / 1.25 * 2 = 1.6666...
1

1     1.25    1.5           2     C Major Chord
|------|-------|------------|
1       1.3333  1.6666
|---------|-------|

Ok, now let's take all the notes we have between 1 and 2 and arrange them by increasing frequency:

1
1.125
1.25
1.3333
1.5
1.6666
1.875
2

Now, we can finally give these all names based on sequential letters:

C 1
D 1.125
E 1.25
F 1.3333
G 1.5
A 1.6666
B 1.875
C 2

The notes C E G form one major chord (C), the notes G B D form another major chord (G) and finally the notes F A C form a third major chord (F).  All combined together, we'll call this the scale of C.

In the next article, I'll start talking about sharps and flats.

1. > I was never able to understand, however, why the
> piano keys are the way they are.
> Why is there no E# or Fb note? Why no B# or Cb?
> Why is the same key sometimes called F# and
> sometimes called Gb?

Because there is nothing special about the black keys on a piano. They are all half steps, same as the white keys. The keys were laid out this way for a couple of reasons, all related to stupid humans. One, if all the keys were white, it would be impossible to tell which key is which. If each pair of white keys had a black key between them, it would be equally impossible. So the twelve keys per octave are split up into seven white keys and five black keys, and the black keys are arranged in two groups, a group of two and a group of three. The identity of each white key can then be ascertained by its relationship to the black keys. The white key to the left of two black keys is a C. The white key to the left of three black keys is an F. The white key to the right of three black keys is a B. Etc through the entire keyboard. The other reason they are laid out this way is to enable humans to play octaves. If you want to play a C4 and a C5 with one hand, it would be impossible without black keys; with 12 white keys, the hand would not be large enough to reach.

2. The half steps you are used to are a result of the equal tempered scale. From a musical point of view, they produce music that's slightly out of tune. The equal tempered scale is a compromise that allows you to play songs in any key and they all sound relatively good.

The groups of two and three black keys aren't just a convenience. They derive from the math behind the equal tempered scale. When you crunch the numbers, it naturally breaks the keyboard into 12 semitones with groups of 2 and 3 sharp/flat notes. It's not just a coincidence or a convention. It's dictated by the math.