Music and mathematics are closely related. This was discovered by Pythagoras so it’s nothing new, and yet a majority of people don’t know something as simple as why the keys in the piano are what they are. I promise to tell you in a minimum of words things that you’d need many pages, from many sources, to get otherwise.

Okay, first off the bare essentials. Musical notes are vibrations in the air, which are picked up by our ears and transmitted to the brain. A musical sound, as opposed to a non-musical noise, is characterized by its regularity. A musical sound is periodic, that is, it repeats over and over in cycles.

A noise never repeats. The time elapsed between repeats in a musical sound is called the period. This period is typically

very short, of the order of thousands of a second, so often it is better to refer to it by its inverse, or how many of these cycles fit in one second. The higher a note, the higher the frequency. For instance, the note commonly known as “middle A” has a frequency that has been standardized to 440 cycles per second, or 440 Hertz. The B note that follows A (next white key on the right, in the piano) has a higher frequency, and so does again the C that follows the B.

The golden question that we’re trying to answer with this article is, what should be the frequency of B, C, and so forth be what they are and not something else? After all, we can tell if the piano starts going out of tune so the notes change even slightly because it no longer sounds good. So what is special about those notes that makes them sound well?

Here is where a traditional course in music theory would start talking about intervals, but I’m not going to do that. Instead, we’re going to stick with frequencies and frequency ratios. Observe what happens if I add a simple tone with a certain frequency and any other tone having a different frequency.

You can see that the result is less periodic, more noise-like. Any two tones don’t necessarily add well together. They do, however, if their frequencies are in a 1:2 ratio, like this.

In the 1:2 ratio, two cycles of the higher-pitched sound fit exactly within one cycle of the lower-pitched sound. The result ends up having the same frequency as the lower-pitched sound, and is quite hard to tell it apart from it without looking at a graph like this one. This 1:2 ratio is called an “octave”, and is the reason why musical notes repeat like this: A-B-C-D-E-F-G-A-B-C-, etc. The second A is twice the frequency of the first A, the second B is twice the frequency of the first B, and so forth. So if the first A is 440 hertz, the second is 880 Hertz, and the next will be 1760 Hertz. The octave, so named because the next A is the eighth note after the first A (if you start counting on that first A), is the simplest interval: 1:2.

Are there other frequency ratios that sound good together? Sure, take the 2:3 ratio like this.

Because the ratio of frequencies is a ratio of small integers, it doesn’t take very long before the combined sound starts repeating again, which the ear interprets as smoothness. This is the next simple interval, called the “fifth” because it matches (or rather, it used to match, originally, as we’ll see below) the distance between a white note in the piano and the fifth to its right, stating from the original. So if the first is middle A (440 Hertz), its fifth is E, at 660 Hertz.

Now, A is itself the fifth of another note in the previous period. These two notes should sound well together, too. A little math tells us that its frequency should be 2/3 of 440 hertz. If we look at its octave instead (which should still sound good when mixed with A), that means 2 x 2/3 = 4/3 of 440 Hertz. This 4/3 interval is called the “fourth” because it corresponds to the fourth note from A, counting from that first A.

I hope that by now you are seeing that any two sounds whose frequencies make a ratio that is equal to the ratio of two small integers are going to sound decent together. They also sound well when played one after the other, for the ear (actually, it’s the brain, but let’s give the poor ear some credit) “remembers” the first sound even as the second sound is played, and tends to blend them in the mind. Thus, if 1 is the original sound, the sounds that sound well with it have frequencies that are 2, 3/2, 4/3, 5/4, 6/5, etc. of the first sound.

For historical reasons, the root of the Western musical scale is not A, but C, which has a frequency of 261.626 Hertz. Here is a chart with the notes above that C but before the next C that sound well with that C:

note | C | D | E | F | G | A | B | C |

ratio | 1 | 9/8 | 5/4 | 4/3 | 3/2 | 5/3 | 15/8 | 2 |

decimal | 1 | 1.125 | 1.25 | 1.333 | 1.5 | 1.667 | 1.875 | 2 |

Freq.(Hz) | 261.626 | 294.33 | 327.03 | 348.83 | 392.44 | 436.04 | 490.55 | 523.25 |

name | tonic | major 2nd | major 3rd | fourth | fifth | major 6th | major 7th | octave |

A few obvious ratios, like 6/5, 7/6, and 8/7 are not here because they are too close to other ratios already on the list. But 9/8 is on the list because it falls roughly halfway within the interval between the original note and 5/4. 9/8 is also the fifth of the fifth 9 (one octave down), which is a major interval. A similar situation happens with sounds close to the octave, where 15/8 is the simplest ratio that is not too close to another already on the list (it’s also 3/2 of another note already on the scale).

This is the major scale, which is used a lot in Western music. Starting in C, it is the scale formed by the white keys in a piano. If the cycle starts with A instead of C, as one would expect alphabetically, these are the ratios one encounters:

note | A | B | C | D | E | F | G | A |

ratio | 1 | 9/8 | 6/5 | 4/3 | 3/2 | 8/5 | 18/10 | 2 |

decimal | 1 | 1.125 | 1.2 | 1.333 | 1.5 | 1.6 | 1.8 | 2 |

name | tonic | major 2nd | minor 3rd | fourth | fifth | minor 6th | minor 7th | octave |

This is the minor scale, which is also used a lot in Western music. The major and minor scales have the unique property that of all the groups of three alternating notes one can make (as in first-third-fifth, or second-fourth-sixth and so on), those starting with the tonic, the fourth, and the fifth have the same interval structure, and therefore sound alike in character. This is very useful when several voices are used in order to create harmony as well as melody.

Observe that the frequency ratios are different from those in the major scale only at three points (3^{rd}, 6^{th} and 7^{th}), where the minor scale notes have less frequency (they are “flatter”, to use the musical term) than the corresponding ones in the major scale. These ratios happen to fall roughly halfway between two ratios in the major scale, so it is convenient to add keys for those whenever a ratio not already on the list is encountered. Those are the black keys in the piano. With this addition, the interval between any key and its octave ends up having twelve keys: seven white and five black. These would be the ratios starting and ending in C:

Key | C | C# | D | Eb | E | F | F# | G | Ab | A | Bb | B | C |

just ratio | 1 | 16/15 | 9/8 | 6/5 | 5/4 | 4/3 | 7/5 | 3/2 | 8/5 | 5/3 | 18/10 | 15/8 | 2 |

decimal | 1 | 1.067 | 1.125 | 1.2 | 1.25 | 1.333 | 1.4 | 1.5 | 1.6 | 1.667 | 1.8 | 1.875 | 2 |

equal temp. | 1 | 1.059 | 1.122 | 1.189 | 1.26 | 1.335 | 1.414 | 1.498 | 1.587 | 1.682 | 1.782 | 1.888 | 2 |

A keen reader might have noticed that middle A in the first table was at 436 hertz and not at 440 Hertz. This is because of something called “equal temperament.” It is actually quite simple mathematically. Since the ratios between any two keys (black and white, or white and white depending on location) are all close to the same value (roughly 1.06) but sometimes they are larger, sometimes they are smaller, a clever musician said, “Hey, why don’t we make all those ratios exactly the same, and then we can get the major and minor scales starting from any key.” Mathematically, that would be making all those ratios equal to the twelfth root of two, which is 1.059463… (infinite digits). Using that rule, the ratios end up being those in the last row of the last table, labeled, “equal temperament.” As you can see, they don’t wander too far off from the just ratios. With this system, the middle A frequency is 261.626 x 1.682 = 440 as we have come to get used to.

Notes in the equal tempered scale don’t sound as good together as the notes in the scales using whole number ratios, called “just tempered” scales, but we have grown so used to them that we no longer can tell the difference. This was not so in the 18^{th} century, when equal temperament was introduced. It took a while for Western ears to accept equal temperament, and it did so only because of the additional beauty that could be achieved if instruments could go from one key to another (a major or minor scale starting from any of the keys) still sounding decent. Pieces like Bach’s “The Well-Tempered Clavier” were all about experimenting with this possibility, with stunning results.