Musical Notes and Scales
Sorry for the delay. Everyone in my household came down with egregious sicknesses. And I had a bunch of unsatisfying things in my music theory that needed to get worked out. But we’re mostly better and the theoretical issues are resolved, so we’re BACK baby. Over the last two posts we discussed abstract musical pitches and (some) efficient voice leadings which help move us smoothly between different configurations in musical space. It’s been all very interesting musically , but you must admit there hasn’t been much in the way of functional programming going on. Today we’re going to get back to brass tacks and see about making all of these notions precise, compositional, and fun to work with. Where we left things is with a type: which has exactly distinct values. We then made a type, whose semantics are “exactly one of distinct values, with an octave component.” We represent this implicitly by way of an : and give its semantics by way of a function: where the output is which octave the degree ended up in. We have then been discussing “scales” as mappings of the form . These have always been in scare quotes because this is not really how musicians traditionally think about scales! Whatever this mapping concept is, it doesn’t really have a standard name in musical vocabulary. But scales are the closest thing semantically, so we’re going to just shadow that name and go about our business. It’s a bit of a tricky line to balance. So whenever we’re talking about the loose musical idea, we’ll use the word “scale.” And whenever we mean this mapping, we’ll go with . However, it’s been a bit of a simplification to say a is a mapping . We’d like our s to be monotone mappings, which is to say that if , then . Enforcing monotonicity on -to- mappings is possible, but it requires us to preserve the ordering of pitch classes within a single octave. That is to say, it means we can’t do any sort of permutation. Thus, perhaps we should think about s as mappings . This is much closer to what we want (although it has some shortcomings that we will see shortly.) But let’s move forward with this attempt in order to motivate some of the later decisions: We can introduce a new scale by way of : which enumerates all values in , sorts them (in order to ensure monotonicity), and then builds a that uses the to select from the sorted values. We can apply a scale to a by way of : which ensures we preserve the necessary semantics of given earlier. All is so far well and good. But not for long. In some cases we might want to reverse a ’s mapping, eg going from to . This makes a lot of sense musically. For example, we might want to include a neighbor note , or a passing tone when writing out otherwise-triadic music. More directly, projecting down to a smaller abstract pitch class is the exact mechanism of tintinnabuli . Unfortunately however, reversing a mapping is obvious hogwash from the perspective of our functional programming ivory tower. In the usual case that , this is a very non-injective function, so we have no chance of turning it around. And yet, it’s musically meaningful. So, regardless of whether we can do it in Haskell, we somehow must. Otherwise whatever it is we’ve built is modeling the wrong thing. Our mental pattern matchers that say “you can’t reverse a non-bijective function” are absolutely correct. But it’s worth taking a moment to think about why exactly we can’t in this case. Consider the musically-common case of working in a . Here, some of the s are targeted by the . In particular, get targeted by the scale introduced in a previous post. So reversing is easy enough — we just send them to . The hard part is deciding where ought we send . Well, why don’t we just send them to themselves ? Sounds a little crazy, I know, but check this out. If we could attach other scalar degree offsets to each note, we could invert a scale by mapping the image directly to the domain whenever possible. And when it’s not possible, we could represent it as an offset from the nearest thing in the domain. A little example might help. Say we have and we’d like to reverse them across a . If we represent exact points in as white note heads, and “offsets” as black note heads, one possible reverse mapping looks like this: If we assume we have some operation , this diagram corresponds to: Of course, such a mapping is not unique. For example, we can also do a “round up, offset down” approach: which is similarly: All of this is to say that s aren’t quite what we’re looking for! As we’ve seen, s aren’t quite powerful enough to represent these offsets. So we’ll first need to make a type to hold the offsets. The closest standard-musical terminology here is “ornamentation,” so that’s what we’ll go with. An is a mapping from scalar sizes to offsets. Rather than keeping the scalar sizes around in the type system (as we have done for and and ), here we’re going to simplify things down to an : Two are equal when they have the same non-zero elements: We can also introduce : and eliminate them: Here returns the ornament, and returns a new with that scale degree removed. If you don’t care about the removal, you’re welcome to call directly. Armed with , we can now make , which wraps a degree with ornaments: s admit a instance, which combines ornaments pointwise: and we can add an ornamental offset to a by way of : An invariant enforced by is that you never have an ornament of size attached to a note also of size . If you attempt to, we just change the instead. Now that we have s, we can define s in terms of them, and we are rewarded with a convenient place to stick offsets when we invert scales. and change only in that we reference instead of : However, needs to do slightly more work: it must respect incoming and outgoing ornamentation. The might have a -sized ornament, in which case we must add it immediately. And then we must combine the remaining ornaments with any which might arise from the ’s mapping: With all of this work, we’re now ready to tackle inverting scales. The thrust of the idea here is a relatively simple algorithm: Of course, the devil is in the details. Getting the “best candidate” might require looking in a different octave. We wrap the candidate logic into a chooser: which receives the desired pitch class, the image, and must return one element of the image along with an octave displacement. Since the entire thing is stuck in a , the chooser is allowed only to look at the s to determine what is the best candidate. As a little illustration here, we might want to round up to the nearest pitch class in the image. So we’d want to filter the image by anything . But if is already the largest thing in the image, we’d instead want to return the smallest thing in the image with a +1 octave offset. If you’re curious, the logic in its full glory is thus: which we specialize with a few different choices of chooser: As a little aside, I haven’t found a great way of naming the first two functions. Here “above” means “round down and offset up”, and below is the opposite. Any suggestions for more obvious names here would be greatly appreciated. That’s all for today. In the next post we’ll investigate the types behind voice leadings, with a similar attention to precision. Compute the image of the scale. Take the resulting pitch class, and associate it with the corresponding input pitch class. Negate the octave and ornaments when doing this. Check if the inverse pitch class exist directly in your image map. If so, return it directly. If not, select the best candidate of the image with respect to the inverse pitch class. Compute how far the best candidate is away from the input, and add that distance as an extra ornament.