When non-pianists approach a piano, they will strike random keys—but often just one key at a time. If they play more than one, the sound is likely to be unpleasant. But that’s only because they don’t know which keys to strike at the same time. Let’s work out in Deltaphone a system of notes that do sound good together: the seven chords of the major scale.
The major scale is defined to be a list of notes that starts at some root note, jumps up 2 halfsteps, jumps up 2 more, jumps up 1, jumps up 2, jumps up 2, jumps up 2, and jumps up 1. All told, we travel 12 halfsteps up from the root, landing one octave above.
In this program, we generate the C major scale using these offsets, starting at C in the fourth octave and landing on C in the fifth octave:
The notes we visit are C, D, E, F, G, A, B, and C.
Now, consider this simple rule for finding notes in this scale that sound pleasing together:
That set of three notes is called a triad. Let’s write a function that formalizes the algorithm:
That sounds pleasing, right? The best part is that the algorithm is versatile. It can be applied at any of the notes in the major scale. Let’s walk up the scale and apply it at each note:
All those triads should sound pleasing to your ears. But the code is awfully repetitive. Let’s walk up the whole scale with a loop instead:
Our algorithm for picking notes works across the whole scale!
There’s still one improvement we can make. Musicians give names to each of the triads using Roman numerals. When we pick the scale’s root as the triad’s root, we have a I chord. When we pick the second note of the scale as the triad’s root, we have a ii chord. When we pick the third note, a iii chord. The fourth note, a IV chord. The fifth note, a V chord. The sixth note, a vi chord. And the seventh note, a vii chord.
Let’s write some helper functions that abstract away the I, IV, V, and vi chords:
Now we have a language for compactly describing chords, and we can create chord progressions like I, V, vi, IV:
This progression and other orderings of the same four chords are quite common in popular music.
There’s one last trick we can apply to our triads, but let’s save inversions for next time.