teaching machines

Intervals in Deltaphone

For a few years now, schools and industry have been telling kids that they can code. They say the jobs are plentiful, and the salary is enviable—the workforce is waiting. The nobler agents of educational reform will also tell our kids that programming is a creative exercise that will make them better thinkers in other disciplines. However, programming is often taught as a context in and of itself. It’s hard to learn two things at a time, and it’s even harder to teach them.

The past year or so I’ve been taking piano lessons from a guy I met on the internet. The lessons expose me to ideas about music theory that I want to understand, but I’ve gotten to the point in life where I don’t feel like I understand anything unless I program it. So, I started working on Deltaphone not as a tool to teach computer science, but as a tool to teach myself music. I will be slowly writing up the things I learn about music by coding it.

Today our subject is intervals. Pick any two notes. The gap between their frequencies is their interval. Play the notes at the same time or in succession. If they sound pleasing together, we say that the interval is consonant, which derives from the Latin word for “to sound together.” If they don’t sound pleasing, they are dissonant.

On a rainy day it might be interesting to play all possible intervals and decide which ones we think are consonant. But there are so many possible intervals. We’d probably need a rainy week! However, we may not need a week for three reasons:

  • The musicians of past centuries have already affixed the labels consonant and dissonant to all the intervals that we could dream up.
  • We don’t typically play just any two notes. We tend to play notes that are in the same scale.
  • The labeling exercise is pointless. Just because an interval is dissonant, that doesn’t mean musicians don’t play it.

The standard intervals that our Western musical ancestors defined for us are as follows:

  • Unison: a note paired with itself.
  • Second: a note paired with the second note in the scale.
  • Third: a note paired with the third note in the scale.
  • Fourth: a note paired with the fourth note in the scale.
  • Fifth: a note paired with the fifth note in the scale.
  • Sixth: a note paired with the sixth note in the scale.
  • Seventh: a note paired with the seventh note in the scale.
  • Eighth: a note paired with itself in the next higher octave.

Let’s make this idea concrete by writing a Deltaphone program to play all of these intervals, and we can decide for ourselves about their consonance. We start by dragging out the play block that supports chords, and we combine C4 and the note 0 steps above it in the current key:

The other intervals can be formed in similar fashion. We need only change the delta:

While this works, it’s very repetitive. After the first intervals, the ideas stop being new. Let’s generalize this into a function. We’ll have the parameter be the interval’s number.

You may have noticed in the previous video that the second used a delta of +1. This is confusing. For our function, we’ll make it so that if the caller passes a 2, we’ll play the second. If the caller passes a 3, we’ll play the third. And so on. We’ll need to subtract 1 from the parameter to get the delta:

This feels pretty good, but let’s generalize interval so that it can play from any root note, and let’s also use a for loop to walk through our numbers:

That’s enough about intervals for now. In the next installment, we’ll see how these intervals change when the underlying scale changes.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *