Music: The Original Programming Language

Michael Mucciarone
Capital One Tech
Published in
10 min readApr 29, 2020

Or, “How to Program Humans”

Sheet music overlaid with a waveform and programming code text

“Art is simply an infinitely more entertaining form of mathematics.” — Me

Ever wish you could program humans? Well, it turns out that you can. And no, I’m not talking about some kind of mind control, telepathy, or hypnosis. I’m talking about music — specifically written music or sheet music. In fact, to a skilled Music Interpreter (i.e. musician), a page of music is code that can be executed to create or reproduce a series of sequential and parallel audible frequencies in a manner intended by the original creator (i.e. music).

Sheet music for “12 Variations on a French Nursery Theme” by Mozart, the basis for Twinkle Twinkle Little Star et al.
Mystery sheet music by Mozart

Try this little experiment with your musically inclined friends. (If you don’t have musically inclined friends, please find some, we don’t usually bite). Take this piece by Mozart and put it in front of them without telling them what it is. See if they don’t start humming the tune. They may even be able to recognize it from the notes alone! For the record, when I started reading it I could hear the music in my head just looking at the first two lines. For a musician, just looking at sheet music is enough to kick off our musical interpreter.

What I want to show now is that there are several more parallels between music and coding, and that perhaps music is itself a programming language for humans.

A quick note…

My intention is for this article to be accessible for as many people as possible. While I will try to keep the programming and music theory at a high level, some of it will touch on some hardcore music theory and programming patterns. Generally I will use Python 3 as my go-to language because of its utility, ubiquity, readability, and because I know it well. I will generally refer to music from the perspective of a keyboardist (my undergraduate degree is in Pipe Organ Performance).

Programming Languages

Let’s start by talking about programming languages. As you know, there are lots of languages to program in — some compiled, some interpreted — but the end result is that code gets run on the computer. Some current popular languages include Javascript, Python, Go, C++, etc. While each language has its own strengths and weaknesses, idioms and syntax, they all share certain commonalities:

  • Each language has certain tasks that it excels at and some that it does not.
  • Each language is executed a little differently.
  • Each language has skilled practitioners that may not necessarily know the other languages.
  • For a person with a strong foundation in programming, learning a new language is easier than somebody who has never coded before.

Now let me propose something — musical Instruments are the programming languages of the music world.

“But music is music” you say! How do different musical instruments constitute a parallel to programming languages? Let’s look at how two different musical instruments — the piano and the trumpet — are played and notated.

The Piano

A piano is played with the hands and feet. The fingers press different combinations of keys, and the feet press the various pedals (primarily the damper pedal, though there are three pedals in total) on the piano. Learning how to play the keys correctly, efficiently, and without looking at them are some of the skills a pianist develops when learning to play the piano.

Chalk drawing of a Grand Piano with Bench
A Grand Piano

Piano music is notated in a specific way as well. Music for a piano is usually notated on a grand staff — that is a paired set of musical staves that typically has both a treble clef and a bass clef. Generally notes on the treble clef are played by the right hand and notes on the bass clef are played by the left hand. The piano is capable of sounding multiple notes at a time and the pianist is capable of engaging multiple keys at a time — this means that the piano is polyphonic. Polyphony is notated with clusters of notes vertically on the grand staff — an astute reader might see a parallel here between musical polyphony and concurrency in programming. There are also special markings for when to press or release the various pedals. Also of note is that the piano is pitched in C, meaning that the middle C note on a piano equates to a Concert C, or ~261.6 Hz.

A musical grand staff depicting two whole notes on middle C
A grand staff depicting two whole notes on middle C

The Trumpet

Picture of a Trumpet
A Trumpet

In contrast, a trumpet is played by the user compressing their lips into a cup in the mouthpiece and then buzzing the lips by blowing air through them — this is known as the embouchure. Notes are played by the performer engaging various combinations of valves on the trumpet in addition to varying the amount of air moving through the lips and the strength of the lip muscles pressing their lips together. Trumpet music is notated on a staff with only a treble clef. The trumpet is monophonic, that is it can only play one note at a time, and so is notated with a single line of notes across the staff for solo play. To make things even more confusing, the standard trumpet is pitched in Bb, that is to say that playing a C on the trumpet equates to playing a Bb key on the piano — it is a transposing instrument.

So you can see that each instrument is played differently, written for differently, and even has its own unique musical and spoken language. That said, they all use similar musical notation (Clefs, lines, spaces, etc) and similar constructs, meaning that it is easier for a pianist to begin playing the trumpet than it is for a non-musician to begin playing either instrument.

The Interpreter

Let’s try a variation of the experiment above — find some simple source code and put it in front of a programmer friend. Odds are that they can tell you what that code does, at least at a high level, even if they don’t know that particular language. In the same way that the programmer can read the source code, a musician can read music — but nothing happens until the code is executed by the proper interpreter.

Humans are the interpreters for music. Now, not all people who play an instrument can read sheet music — I know this from personal experience — and I purposely specified skilled music interpreters in my previous statement. Reading music is a skill, just like reading code.

When we want to execute code written in Python, we execute it using the Python interpreter. For example, if I have a script called hello.py, I would execute it by typing python hello.py at a command prompt. When you do this, the Python interpreter reads the script, translates (compiles) the script into bytecode that can be run on the local compute architecture, executes that bytecode in the context of the operating system and hardware architecture, and returns the result.

A musician executes sheet music in a similar way. The musician sits down at the piano with the music in front of them and interprets it. That is, the musician’s eyes scan the page and their brain translates the various musical symbols they see into physical actions that are executed by their body. In the case of a keyboard, that translates to hitting the correct keys, or combination of keys, at the correct time. It may also translate to pressing one or more pedals with the feet, as well as leaning toward one end of the keyboard so as to reach notes on the high or low end of the keyboard.

Object Oriented

Many modern programming languages are generally considered to be Object Oriented, that is, everything is an object that has certain properties. Objects can be of varying types including string, integer, float, array, dictionary, etc. Each of those types have different methods available on them. For example, in Python, strings have various methods available to them for manipulating the string such as upper(), lower(), etc. Python also has operators for performing operations like addition, subtraction, concatenation etc.

Music also has similar structures. One could say that the musical equivalent of a programming object is the note. Notes in music have different meanings based on their environment. The base duration of a note is specified based on whether or not the note is filled, and the presence or absence of a stem and flags. A Whole Note is an unfilled oval without a stem and is so named because it takes up the duration of a whole measure. The Whole Note can be subdivided by adding a vertical stem to the unfilled whole note, creating a Half Note (i.e. half of a whole note). A Quarter Note is a filled in oval with a stem. The Quarter Note can be further subdivided by adding flags to them, creating an Eighth Note (one flag), a Sixteenth Note (two flags) and so on. You’ll notice another pattern here, that of durations based on powers of 2.

A diagram of notes, displaying their temporal names and durations
A diagram of notes, displaying their temporal names and durations

A note can also have other effects applied to them on an individual basis. For example, adding a dot to the right of the note adds half of the duration of the note it’s attached to to the duration of that note— e.g. a dotted half note, in 4/4 time, would receive 3 beats; 2 beats for the half note and 1 additional beat for the dot.

Another example is what is known as an accidental, i.e. an in-line sharp, flat, or natural sign. A sharp causes the note to be raised by a half-step, a flat causes the note to be lowered by a half-step, and a natural sign cancels out a sharp or a flat (either accidental or from the key signature).

Example of Accidentals showing a G Sharp, G Flat, and G Natural
Example of Accidentals showing a G Sharp, G Flat, and G Natural

There are more examples in music theory such as accent markings, staccato, legato, and phrasing. In programming there are several parallels here; some could be considered methods; others could be properties; and some of them, such as the turn or mordent, could be considered decorators (they’re even called ornaments in music theory).

Stop, Iterate, and Listen…

Another common feature of programming languages is the concept of iterators; that is, data structures that can be looped over for various purposes, and the loop functions such that are used to iterate over such data. The expression for item in list: in Python is a very common construct.

Yes, music has iterators too. There are several flavors, such as Da Capo, Dal Segno, and repeat signs. Here I’m going to talk about repeat signs, mainly because they’re the most straightforward to understand and they’re the most obvious from a symbolic and visual perspective. Repeat signs, in their most basic form, are placed around a number of measures to indicate to the musician that they should repeat those measures. The number of repetitions generally defaults to two, but can be defined by making a textual notation above the staff. This is what basic repeat signs look like:

An excerpt of music surrounded by repeat signs
An excerpt of music surrounded by repeat signs

Another variation on repeat signs allows for the same notes to be repeated, but allows the last few measures to be changed based on which iteration the performer is in.

Musical Notation of 1st, 2nd, and 3rd endings
Musical Notation of 1st, 2nd, and 3rd endings

In this example, the first measure is repeated three times. The first time this passage is played, the music under the big 1. bracket is played. The second time, the music under the 1. bracket is ignored and the music under the 2. bracket is played. The third and final time the music under both the 1. and 2. brackets is ignored and the music under the 3. bracket and beyond is played. If we wanted to represent this as pseudo-code, it would look something like this:

x = 0for x in range(4):
play measure 1
if x == 1:
play measure 2
elif x == 2:
play measure 3
elif x == 3:
play measure 4 to end
x += 1

Return True

Hopefully I’ve convinced you that music and programming are not disparate disciplines, but are instead very similar both in construct and in execution. What I’ve explored in this piece is really just scratching the surface of the similarities of music theory and programming. If you would like to explore some other musical topics that have obvious parallels in the programming world, here is a list of topics to explore and a programming concept to compare them to.

I would also add that while I have stayed relatively high level with the music theory, as you explore deeper into it the parallels to code become even more clear. I would encourage you to explore deeper into any of the topics I mention.

Appendix

Some of the basic music examples for this article were generated using lilypond, and if you are interested in generating sheet music of your own, it is quite easy to get started with and incredibly powerful. The remaining examples were pulled from public domain music from musopen.org.

DISCLOSURE STATEMENT: © 2020 Capital One. Opinions are those of the individual author. Unless noted otherwise in this post, Capital One is not affiliated with, nor endorsed by, any of the companies mentioned. All trademarks and other intellectual property used or displayed are property of their respective owners.

--

--

Michael Mucciarone
Capital One Tech

Devops Engineer; Artist at heart; Technologist by day. Container and DevOps Enthusiast with a side of UI/UX designer and a degree in Music.