r/programming 12d ago

Music theory for programmers

https://runjs.app/blog/music-theory-for-programmers
503 Upvotes

41 comments sorted by

119

u/reality_boy 12d ago

There is an excellent 2 book set called Musimathics that collects up all the math involved in music and audio. It would complement this nicely.

And the “science and engineers guide to DSP” is a free book online that is also wonderful. It shows programmers how low level DSP theory works, and can serve as a foundation for a lot more audio knowledge.

8

u/the_gnarts 12d ago

I just started reading Music: A Mathematical Offering by D. Benson and found this post a great visualization of some of the topics it discusses.

4

u/larsbrinkhoff 12d ago

The author of Musimathics is Gareth Loy, who was at Stanford CCRMA and used the AI lab PDP-6 and PDP-10 software to synthesize sound and music, and later the "Samson Box" hardware synthesizer. He was around when CCRMA developed FM sound synthesis and licensed it to Yamaha.

3

u/CrasseMaximum 12d ago

Thank you very much stranger for this book recommendation, did not know about Musimathics!

2

u/lukehaas 12d ago

Musimathics looks really interesting. Thanks for recommending!

44

u/connor-ts 12d ago

Very fun! Though to be a bit more accurate, this is more the math/physics behind music, as music theory is usually what we use to analyze (typically classical and jazz) music at a higher level.

40

u/loup-vaillant 12d ago

I can't play an instrument.

I can. Fell Into it when I was little. And I have some feedback: good work. Congratulations, really. It's the kind of thing I wish I knew sooner, but only learned about as an adult, when I became exposed to ancient music (before the romantic period, renaissance and baroque mostly).

There is a slow beat in the second pair if you listen for it, and that beat is present in every fifth played on every piano on Earth.

Ackchyually...

Western music didn't always use the equal temperament, with the octave divided in 12 equal intervals (frequency ratios for those who followed the article). I can cite two major reasons: first, our ancestors' ears were keenly accustomed to exact intervals (perfect fifth (3/2), perfect fourth (4/3)...), for the very acoustic reasons you mentioned.

Second, ancient instruments (lute, harpsichord...) just weren't loud enough. They had to be tuned to maximise resonance, which means as many exact intervals as they could muster. The octave was not equally divided. For instance, you almost certainly know of the Pythagorean temperament, where all fifths are perfect, except one: the wolf interval. That geometric abomination is way too small for a fifth, it sounds absolutely horrible, and was of course never played.

The Pythagorean temperament was perfect for some medieval music that only used fourths and fifths (French, hopefully the auto-generated subs are enough). We quickly got past Gregorian chants however, and played with thirds (4/5, 5/6) as well. So now we have to deal with powers of five in addition to the powers of two and three.

This is starting to become unworkable: those new thirds sound like crap on a Pythagorean temperament, and we sacrificed one fifth already, what can we possibly do? Well, sacrifice even more fifths of course. That's what meantone temperaments are for. But of fucking course, we didn't stop there. Soon we started to want to play weird shit in every tone, and have it sound good on that harpsichord, which still sounded like crap with an equal temperament!!

Until the piano came: finally an instrument loud enough, with a timbre rich enough (lots of overtones), to withstand that long awaited for, liberating equal temperament: sure there is that faint beat on those fifths (and thirds, and anything but the octave), but unlike the harpsichord we can barely hear it.

Ackchyually...

Ah, yes. Turns out many modern pianos do have perfect fifths. They sacrifice the octave of all things, stretch them a little bit. Not all of them do that, many sound better on a more equal temperament, but I believe most of the really good ones sound better when stretched.

Which suits me well: I play the cello, and much prefer to tune its four strings in perfect fifths.


A small note about sharps and flats: like many other kids doing modern classical music (romantic period mostly), I was taught to raise my sharp notes, and lower my flat notes. But this is mostly bullshit: what we really want is chords that sound exact, just like those medieval monks. This means constantly adjusting my intonation to match that of my play mates. Often, the same note will have a slightly different pitch depending on what the others are playing.

That's how we solve the impossible equation: we cheat.

And by the way, I wish electronic music made more use of temperaments and exact intervals. A robotically tuned equal temperament for everything can sound a little dull.


And by the way, good harmony doesn't always land on simple fractions. It depends on the main overtones, and they don't always fall squarely on the harmonic series (we call them inharmonic overtones). They generally do with strings, but bells are different.

I learned of that one super recently.

7

u/want_to_want 11d ago edited 10d ago

And by the way, I wish electronic music made more use of temperaments and exact intervals. A robotically tuned equal temperament for everything can sound a little dull.

I tried it in Logic for a while. What I found is that dance music doesn't necessarily need this. Music with just intonation chords sounds kinda static, makes you want to sit and admire the chord. While the "restlessness" and "blur" of impure chords helps push the music rhythmically forward.

The same thing happened when I retuned a guitar to make some just intonation chords work. Strumming became super weird, I'd strum a chord once and then not want to continue. So the "hum" of impure chords is an important part of rhythm guitar too.

11

u/C0rinthian 12d ago edited 12d ago

I highly recommend you watch minutephysics’s The Physics of Dissonance because a lot of this is rooted in western musical tradition and assumes it is universal. That is not to diminish what you have written, rather it is really important to realize this fundamental misunderstanding.

Even the bits you acknowledge you don’t know, like voice leading, is not some universal truth, but rather a convention largely established in baroque Western Europe. It serves a purpose, but should be properly contextualized.

3

u/lukehaas 11d ago

Looks interesting. I'll give it a watch. Thanks for sharing!

3

u/DTM0 11d ago

I'd also recommend David Bruce's videos on different tuning systems that are not 12 TET which I think will also give you a glimpse picture of systems not based on the western tradition. I think of note Arabic music with it's maqam system that has notes that don't fit neatly in the 12 tones we have and the Gamelan music is pretty good at least know about to avoid assumptions on universality.

2

u/Full-Spectral 7d ago

One interesting point that Howard Goodall brought up in one of his music origins videos is that the pentatonic scale is basically there in every culture's music and acts as somewhat of a universal component of human music systems.

13

u/countkillalot 12d ago

Het nice write up. The AI like writing style is a bit distracting, but otherwise we'll put together.

3

u/parkamobil 12d ago

I am a programmer who works on software synths, I must say this is a wonderful resource I wish I could have when starting. And still I learned couple of things, this is full of information

4

u/scrdest 12d ago

I enjoyed the writeup overall, I like the 'it's not just arbitrary nonsense' theme and some of the deeper dives were quite useful!

Some of the limitations deserve calling out though. 

The big one is tuplets: rhythms don't only work in powers of two or dotted durations, there's an escape hatch for that, too - i.e. squeezing an arbitrary number of even subdivisions into one space. Threes are common, and already baked into some common rhythms like 6/8 (effectively 2/4 triplets).

On the harmony side, chord extensions are notably not included, even though it's a pretty obvious progression - we can do 3 thirds, we can do 4, why not 5 or 7? That's not getting into sus chords or quartal harmony or whatevs. 

The dunk on regular triads seems a bit random considering they are like 80% of all music past and present, too.

I do think it's a good 101-level crash course, it could just use more signposting where the picture is a bit of Lies To Children.

7

u/GuidanceWaste2585 12d ago

Having cobbled this knowledge together myself over many years, fantastic write up. Wish I'd seen it sooner!

3

u/EC36339 10d ago

Just what I needed, just in time.

Is there something similar to this somewhere, but for color theory?

2

u/BigPeteB 5d ago

The one that helped most for me is this video from Acerola: https://youtu.be/fv-wlo8yVhk. He basically builds up color theory from first principles. He isn't working through the math, but his explanation was good enough that a lot of stuff finally clicked for the first time and I was able to go learn more on my own.

7

u/Orbital_12000 12d ago

Omg... I have a friend who needs this.

2

u/agnul 12d ago

hey friend ;-)

3

u/this_knee 12d ago

Hello to fellow /r/musicprogramming fam that’s here too.

2

u/MellowMarsach 11d ago

I learned so much from this, amazing, mind-blowing for me

1

u/lukehaas 11d ago

Thanks! Really good to hear this.

2

u/Full-Spectral 11d ago

As hard core developer and life long musician, this is something I've always found interesting. But, in the end, I've come to reject as much digital as possible in my music making, and so wouldn't want to create any such tools as a developer. Though the theory is still always interesting.

2

u/CherryLongjump1989 9d ago edited 8d ago

I learned a lot more than I expected from this post.

1

u/shysta 12d ago

Very cool, though the audio demos aren’t working for me on iPhone. They render as if they are playing but no noise.

3

u/NorthwindSamson 12d ago

For me I had to take my phone off silent

1

u/Giacomand 11d ago

yeah, sadly very common iphone issue as someone who had to use the Web Audio API for their job; especially since silent mode lets you play MediaElements just fine!

1

u/amestrianphilosopher 11d ago

If this is supposed to produce sound when you run the demos, it doesn’t work in any iOS browser I’ve tried

1

u/lukehaas 11d ago

You may need to unmute your phone.

1

u/amestrianphilosopher 10d ago

Huh turning off silent mode did make it work. That’s odd though, I’m always on silent and never had this issue with any other website. Must be something about the default audio device?

1

u/YakumoFuji 8d ago

The full version is ADSR

AHDSR and its two brothers Multi-Stage (MSEG) and Functional Envelope Generators (FEG) say "Hold my beer!".

1

u/elihu 17h ago

I like that it introduces pitch intervals via just intonation.

You have to go all the way to 29 and 41 before the fifth gets better, and nobody is building a keyboard with 41 keys per octave. 

Not quite true. In fact, I've made one (it's a variant of the Desiderata Systems Mosaichord, but I haven't posted anything about it online yet) and a friend of mine makes something called the TS41 (a simple 41-EDO keyboard with tactile switches), another friend made something called the Xenachord, another friend is making a microtonal port of the Linnstrument firmware so he could use the same layout as a 41-tone guitar layout he invented, and another friend is making a grid controller that can be used in a 41-tone tuning.

Yes, it's kind of weird that this many people in one place are making microtonal keyboards, but I think it's great that we can compare notes.

1

u/lukehaas 16h ago

Very cool! I’d be really curious to see and hear these

1

u/elihu 9h ago

This is the 7-limit just intonation version of the Mosaichord: https://www.youtube.com/watch?v=rqbWnDhip0A (The 41 tone one is similar, but with more keys per octave and a more regular layout.) This is the TS-41: https://www.youtube.com/watch?v=iIrrkTCjKPc

Timmy's grid controller shows up at the beginning of this video, as does another Mosaichord: https://www.youtube.com/watch?v=4XPkCIiCz4Q

I don't know if there's a Xenachord video out there or one that shows the Linnstrument firmware fork.

1

u/loup-vaillant 11d ago

Forgot to talk about staves and clefs. I learned about them just like you did, like this. It's wrong. I mean, it works, but I recently learned a much simpler, much more principled, and perhaps even a little more historically accurate way.

The idea is to take a piano score, and pull its two staves together so they're separated by only one invisible line. Keep the G and F clefs were they are, fourth line from the top and fourth line from the bottom respectively. You will note that the invisible line in the middle represents the same note no matter which staff you start from: C4. Draw that line, and put the C clef there.

Here.

Now it all fits together! One Grand Staff to rule them all, 11 lines big, where you can write pretty much all the notes. Three clefs on it, F C and G, merging into one giant C clef at the middle of it all. (Now we programmers know the real reason why C is at the centre of everything.)

OK that's a little bulky. We rarely need that many lines, and even when we do it's fairly illegible. So instead we just pick 5 consecutive lines to draw, and omit the rest of the staff. And that's it. We're done. The impression we have that we move clefs around to determine which notes the staff represent is mostly incorrect. The clefs never move, they just tell us which 6 lines we erased from the one grand staff.

2

u/neutronbob 11d ago

The impression we have that we move clefs around to determine which notes the staff represent is mostly incorrect. The clefs never move, they just tell us which 6 lines we erased from the one grand staff.

This is not correct. Staffs/staves do move around. See here The appearance that staves are fixed is because music mostly uses one of three staves. But there are many as the diagram shows and they're used in specialized contexts. And many instruments uses multiple staves: the cello, the bass clarinet, etc.

1

u/loup-vaillant 11d ago

[...] The clefs never move [...]

This is not correct. Staffs/staves do move around.

You sure you did not misunderstand what you just quoted? I said the clefs never move. The curly sign at the left of the staff if I wasn't clear.

In a sense this is obvious: with few exceptions (transpositions, tenors who only read the treble clef...) the F clef always refers to F3, the C clef always refers to C4, and the G clef always refers to G4.

Take the tenor and alto clefs for instance. They appear to be placed differently on the staff (3rd and 4th lines respectively), but that's an illusion, a convenience so we can draw our 5 staff lines uninterrupted across a whole page. But both refer to C4, in the middle (6th) line of the grand staff.

And many instruments uses multiple staves: the cello, the bass clarinet, etc.

I play the cello, so I'm well aware. Let's take that as an example.

Most of the time I read the bass clef. My staff is thus made of the 5 bottom lines of the grand staff: G2, B2, D3, F3, and A3. The 6 top lines, from C4 to F5, are not drawn. When I switch to the tenor clef, we delete the bottom two lines, G2 and B2, and add two lines on top, C3 and E3.

Now editors would murder me if I asked them to actually engrave their scores like that, but you get the idea.

-11

u/Connect_Group6232 12d ago

Great! I'll have my AI summarize this into actionable items with a dashboard!

9

u/iris700 12d ago

Slop zombie

-2

u/Feeling-Position2772 12d ago

easily trolled