Python Functions to Find Music Chords Based on the Circle of Fifths

Pia Riachi
3 min readOct 13, 2024

--

Photo by Markus Gjengaar on Unsplash

If you can’t code it, you don’t understand it well enough.

Music theory, while beautiful and intricate, could be a complex subject to grasp. To better understand the underlying logic and patterns, I decided to code key concepts into Python functions.

This started as a fun weekend project while practicing piano, but it did indeed help me gain a deeper appreciation and understanding of the magic behind music theory.

I wrote a few Python functions that help return scales, triads, and chord families based on the circle of fifths.

Understanding Scales and Triads

The first step was to define the notes of the chromatic scale and the intervals used to construct major and minor scales. In Python, we can represent notes as strings and intervals as lists of integers. To understand triads (chords consisting of three notes), we can define the intervals for major and minor chords and use them to construct the chord notes from a given root:

Output:

C Major Scale: ['C', 'D', 'E', 'F', 'G', 'A', 'B']
A Minor Scale: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
G Major Scale: ['G', 'A', 'B', 'C', 'D', 'E', 'F#']
B Major Scale: ['B', 'C#', 'D#', 'E', 'F#', 'G#', 'A#']

Explanation: Major and minor scales on the piano follow specific patterns of whole (W) and half steps (H), creating different moods. Major scales (W-W-H-W-W-W-H) sound bright and cheerful, while natural minor scales (W-H-W-W-H-W-W) sound darker and sadder. On a piano, each note is separated from the next one (regardless of whether it’s a black or white note) by half a step. So to move a whole step, we need to jump 2 notes, hence, in the above code, the interval for a whole note would be ‘2’.

For the major scale, the intervals are [0, 2, 4, 5, 7, 9, 11] which is W-W-H-W-W-W-H.

Primary and Secondary Chords

Image by Author

The circle of fifths is a fundamental concept in music theory that relates different keys and chords. One important use of the circle of fifths is to derive chords that sound good together (primary chords), and also secondary or ‘cousin’ chords that could also be matched with the primary chords. To understand this concept, we can calculate the primary and secondary chords for a given key. This function returns the primary (I, IV, V) and secondary (ii, iii, vi) chords for a given key, along with their corresponding triads.

The above would give the following output for the ‘C’ key:

Primary Chords:
C
F
G

Secondary Chords:
D
E
A


Primary Triads:
['C', 'E', 'G']
['F', 'A', 'C']
['G', 'B', 'D']

Secondary Triads:
['D', 'F#', 'A']
['E', 'G#', 'B']
['A', 'C#', 'E']

Here’s how this is visualized on the circle of fifths:

This approach allowed me to experiment with different key signatures, chord progressions, and harmonic structures in a hands-on way. More code snippets to come!

--

--

Pia Riachi
Pia Riachi

Written by Pia Riachi

Engineer @Google | Advertising Solutions Engineering | Business Intelligence | Data Engineering | Artificial Intelligence (AI)

No responses yet