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?

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

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

How do sine waves sound?

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

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)
play(freq * 3)
play(freq * 5)

Music and Fractions

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!

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