Bartosz Witkowski - Blog.

Recently I watched In Our Prime a fun 2022 Korean movie which might be summarized as Finding Forrester but With Mathematics.

The protagonist Ji-woo (played by Kim Dong-Hwi) is mentored by Lee Hak-Sung (played by Choi Min-Sik) in mathematics. The way he teaches him doesn’t fit the standard educational cirriculum but is both rigorous and playful in a way that Paul Lockhart would approve1

In one particular scene (referenced in the trailer) after Lee Hak-Sung does’t manage to awe Ji-woo with Euler’s Identity he tries tying maths to music: by playing the digits of pi:

And that’s neat but do the digits in pi really form a pleasing melody? And if yes how does it work?

I’ll try to find out using my limited understanding of music theory and some understanding of both scala and music theory is needed to follow along.

# Did the film makers lie?

If we look at the spectrogram of the song2:

Spectogram of the song. The high (white) areas are fundamental frequencies while the lower ones (red) are harmonics.

We can see that the notes might plausible follow the orders of the digits in pi as they cluster around something that could be plausible 1-5 (from 3.1415) and than a jump to 9. But this becomes especially visible if we look at note frequencies and annotate the spectogram with the pitches.

Note frequencies annotated on the spectogram

If we note down all the notes from the annotated spectogram we can see they match perfectly to the digits of pi:

 E₅ C₅ F₅ C₅ G₅ D₆ A₅ G₅ E₅ G₅ C₆ D₆ B₅ 3 1 4 1 5 9 2 6 5 3 5 8 9

# Why does it work?

So if it works what’s going on? Is π special? Not really!

If we used any random sequence of digits with the same encoding (C=1, D=2 etc) we would get a similarly pleasing result.

The trick here is that we limit ourselves to the major scale3. Confining ourselves to notes in the major scale will at the very least suggest a tonality. A particular melody (by which I mean the arrangement of the highest voice) along with an underlying harmony (by which I mean the arrangement of lower voices) might also imply a mode or a major/minor key.

Let’s give it a try. If we use java music as a dependency4 we can easily generate the notes in scala (3.0)

We’ll also need a way to write notes as a midi file. We can do this using this function:

Now calling the code:

We can listen to the examples:

The example melodies sound pleasing enough but because they end in a completely random place (I picked 20 “just because”) the effect sounds jarring. If we extend the notes to end on a C the effect would be much better.

Here the examples again with more digits (until we find a `1`):

We can make the melody be more interesting by holding more consonant notes longer and speeding past the dissonant notes. In a major scale nothing will be really dissonant except the major seven but the only true constant notes will be C, F, G - C and its perfect fourth/fifth.

The result:

To ground the melody further we can add a simple accompaniment. We will just play the Cmaj7 chord but to make it a little bit more interesting every other bar will start with a quarter rest.

The result:

The other differences can be explained by better use of note durations and rests, better accompaniment, and rich timbre of a real instrument.

# Making it Sound More Human

After consulting with a Friendly Neighbourhood Music Man who noted that currently the biggest problem with the generated melodies is the lack of any form of strong or weak beats I’ve decided to try implementing proper accenting notes.

Since the oroginal piece was performed in the classical style I’ve decided to accent the 1 and 3 beats (by setting the dynamic to FF and F respectively) and holding the notes longer on those beats. The code for generating the notes is a little longer than before:

But the result (especially of e) is really fun to hear:

# Footnotes

1. https://www.maa.org/external_archive/devlin/LockhartsLament.pdf

2. a.k.a poor man’s transcribing skills

3. I found another possible interpretation using the harmonic minor scale on https://www.youtube.com/watch?v=OMq9he-5HUU

4. simply by adding `"com.explodingart" % "jmusic" % "1.6.4"` to `libraryDependencies`