Making music in a Jupyter notebook

Intro to music theory…in python!

“Music is the universal language of mankind” — Henry Wadsworth Longfellow

“Mathematics is the language in which God has written the universe” — Galileo Galilei

The connection between music and math is a perennially popular topic in textbooks and articles. However, I always find being hands-on really helps me understand ideas at a deeper level. So, I decided to make a Jupyter notebook to play some music. You can read the rest of this post without the notebook, but it’s more fun if you download it and follow along.

So, what exactly IS music?

We can’t all make the next hit single, but we can all tell apart heavy metal, rock, and country. And, of course, we can all distinguish between thunder, a car horn, a symphony, and random noise.

Why is that? What makes music, music? What makes music different from noise?

Music is made of sounds; sound is created by vibrations

It’s easy to see this in slow motion. In the youtube clip below, you can see what happens when you strum a guitar string:

What do these vibrating strings look like? A wave! More specifically, a sine wave.

Music and Trigonometry

Mathematically, a sine wave is defined as the following:

y(t) = A ∗ sin(2πft)
  1. f is the frequency (From Mariah Carey to Barry White?)
  2. A is the amplitude (From exploding windows to is this thing on?)

Frequency is measured in Hertz (Hz), or complete cycles per second. We can easily visualize this using numpy and matplotlib :

1 hertz means 1 complete cycle per second

From here if you want to hear the sounds, you can run your own Jupyter lab and open the notebook to follow along. Pretty cool that Jupyter can play Youtube videos and play audio signals right?

In case you don’t have Jupyter set up right now, here’s the code to play a sine wave:

Fundamentally this is how instruments work. For example, string instruments (like the guitar we saw above) have different strings that vibrate at different frequencies. When we hold down our left hand on one of the strings, it’s changing the frequency of the note by changing the length of the string.

This is also what distinguishes music from noise. Instead of generating a regular sine wave with a single frequency, what if we just generated a signal with random numbers?

data = np.random.randn(44100)
Audio(data, rate=44100)

Turns out nature also prefers patterns to randomness.

Music and Multiplication

Previous we saw a sine wave visualized in regular cycles. When we think in terms of cycles per second, it means that for a given root frequency f, any power of 2 times f will generate sine waves that start and end together.

Powers of 2 start and end together

Visually we can clearly see the common pattern that all of these powers of 2 have. It turns out that there is a common pattern in the sound too:

freq = 110
play(freq) # any base frequency
play(freq * 2)
play(freq * 4)

Try running the corresponding cells. Did you hear that? Sounds like they have the same “quality”, just at a higher or lower level right? In music, we call the distance between these powers of two an “octave”. Now, if you still remember your Greek/Latin word roots, you should immediately be saying, “aha, octave means 8!” Indeed if you played a piano keyboard, you’ll see that there are 8 white keys between two keys of the same letter, representing that they’re of the same base frequency but differ by a power of 2.

Notes with the same letter are a power of 2 apart in frequency

So where do the other notes come from? We can multiple numbers other than 2! If you’re following along in the Jupyter notebook, you can play multiples of 3 or 5 to see how the sound changes.

freq = 110
play(freq * 3)
play(freq * 5)

Music and Fractions

If we mapped out frequency multiples from 1–5, we’ll see that they all represent different intervals between successive multiples. For example, going from 2x to 3x is called a “perfect fifth”. 3x to 4x is a “perfect fourth”. And 4x to 5x is a “major third”. This captured in the picture below.

Different frequency multiples map to different sound intervals

We can now use these relationships to fill in the octave. For example, a perfect fifth (so) is 3/2 times the base frequency and a perfect fourth (fa) is 4/3 times the base frequency. You can play the whole scale in the notebook.

The hills are alive with the sound of music

This is just one particular type of tuning system called “5-limit tuning” (because we use multiples up to 5). There are other systems like 3-limit Pythagorean tuning, just intonation, and equal temperament, each with their own advantages and drawbacks with small differences in the actual sounds.

Have Fun!

It turns out music and math are pretty closely related. We’ve only explored the relationship between musical pitch and mathematical waves. Many other aspects of music like rhythm, meter, harmony, etc all have their parallels in mathematical concepts. So perhaps, math is the law of the universe, but it is through music that humans build an emotional connection to such abstract concepts.

Now, can you play a melody using the notebook? Post your melody in the form of python code in the comments!

Twitter: @changhiskhan

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store