James Tauber

journeyman of some

blog > 2007 > 12 > 13 >

Numerical Representation of Pitch

A number of years ago, I started working on a Python library for music theory and composition called Sebastian and one of the first implementation decisions I needed to make was how to represent pitch. Assuming one allows for double-flats and double-sharps, there are 35 note names. What I was looking for was a way to represent these 35 note names in a way that was consistent with the relationships between them.

I ended up deciding to use the set of integers with D as 0 and each successor being a 5th higher. So A would be +1, E +2, G -1, Eb -5, F# +4 and so on.

This turns out to work quite nicely. Given one note, you can find the tone above/below by adding/subtracting 2 and the semitone (different letter) above/below by subtracting/adding 5. To augment/diminish (same letter) you can add/subtract 7.

You can determine if two notes are enharmonic by testing

(abs(val1 - val2) % 12) == 0

You generate the textual note name from this integer with:

modifiers = ((val + 3) - ((val + 3) % 7)) / 7
if modifiers == 0:
    m_name = ""
elif modifiers == 1:
    m_name = "#"
elif modifiers > 1:
    m_name = "x" * (modifiers - 1)
else: # m < 0
    m_name = "b" * -modifiers
return "DAEBFCG"[val % 7] + m_name

and the reverse with

letter = name[0]
base = "FCGDAEB".find(letter) - 3
if base == -4:
    raise ValueError
mod = name[1:]
if mod == "":
    m = 0
elif mod == "#":
    m = 1
elif mod == "x" * len(mod):
    m = len(mod) + 1
elif mod == "b" * len(mod):
    m = -len(mod)
else:
    raise ValueError
return base + m * 7

While it's true that moving the origin to F would eliminate some of those -3s and +3s, new ones would need to be introduced, so there's no really clear winner between an origin at D versus an origin at F.

Scales are easy to generate. The pattern for a major scale is [0, 2, 4, -1, 1, 3, 5] and so you can generate the scale for a particular tonic with something like

[tonic + i for i in [0, 2, 4, -1, 1, 3, 5]]

Another advantage of the system is it can be extended to 19-et and other tuning systems. I haven't given much thought to whether it's useful for tunings without a constant frequency ratio for the 5th, though.

I also haven't yet investigated how this system is inferior to Hewlett's base-40 system.

UPDATE: Okay, I've finally grokked the functional difference between Hewlett's system and mine. Mine can incorporate an infinite number of note names (triple flats, quadruple sharps, you name it) but it cannot express intervals of an octave or more. Hewlett's base-40 system can handle intervals of octaves or above but cannot, without change, handle more than the 35 note names of 12-tone chromatic music. My system (and others like it) require two numbers to handle more than an octave. Hewlett traded off infinite note-naming capability for the ability to include octave in a single number.

Categories:
prev « python » next
prev « music_theory
sebastian

Comments (6)

Tordek on Dec. 14, 2007:

Very little I know of music, so I'm bound to ask many questions:

1) Why is D 0, and not F, or A#, or... well, ANY other note?
2) Why jump from fifth to fifth?

I've played around with music to show programming to a friend who knows about music so I made a tiny program that makes scales, and I had come up with something like:

notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#","A","A#","B"]

def note(amount):
return(notes[amount % 12])

def major(scale):
return(map(note,[scale + i for i in [0,2,4,5,7,9,11]]))

I see mine doesn't manage flats, or the x in your code I don't understand.

Mind enlightening a not-too-musical mind?

Tordek on Dec. 14, 2007:

"The pattern for a major scale is [0, 2, 4, -1, 1, 3, 5]"

Hmm, okay, that makes sense once one realizes you're counting like a programmer... the first, third, and fifth... the -1 makes a bit less sense; why -1 and not 6 (or, er, whichever belongs)

(Also, if you use "DAEBFCG" in the second snippet (just as you did in the first), you skip the -3, the -1 is more "standard" (as oppossed to the -4 I had to see "oh, he did -3 from the find") and you don't add any +3 anywhere else, so I guess it's a winner :S)

James Tauber on Dec. 14, 2007:

Why is D 0 ? It doesn't really affect much else but it has the nice property that the note names are then symmetrical around 0. i.e. there are the same number of naturals either side, the first sharp/flat is at 4/-4 and so on.

Why jump from fifth to fifth? Because that enables you to handle all flat and sharps and double flats and double sharps without looping back on yourself (unless you want to for enharmonics -- in which case you can just use modulo arithmetic) It also has the property that the naturals are contiguous as are the sharps, flats, and so on.

You already mention your approach doesn't manage flats. Thinking about how it might would probably help you conceptualize why you need an approach more like mine. Also, there is no easy way in your approach to find the correct spelling of a note a particular interval from another (except in the simplest cases). Yours can't, for example, distinguish a major third from a diminished fourth.

My approach is in the class of interval-invariant numbering systems where, for example, the numerical difference between any two notes a diminished fourth apart is always the same.

Regarding your second comment:

On my pattern for the major scale: the jump back 5 instead of forward 2 is for exactly the same reason your sequence jumps 1 instead of 2 at one point -- the semitone step between 3rd and 4th degree.

You suggest using DAEBFCG in the second snippet, but if you try it, you'll find it gives the wrong result because of the non-fifth jump between B and F. I agree the -4 is a little obscure, perhaps I should just subtract the 3 latter.

Jacob on Dec. 14, 2007:

There is a lot of prior art here that would be worth considering. In 12-tone composition and the theory about it, the convention is to number pitches from zero chromatically (with fixed doh, C = 0) while you are numbering the chromatic scale by its M7 transformation (multiply by 7, modulo 12). Composers use the latter transform a lot. The numbering systems are isomorphic, but yours is much less transparent for perceiving simple chromatic distance. Personally I'd rather number by chromatic distance and calculate/consider circle-of-fifth distance as necessary, as chromatic distance, I'd argue, is perceptually more pertinent in most cases.

There is really not much point in using negative numbers in a module 12 system -- do you say it is -5 o'clock?

James Tauber on Dec. 14, 2007:

Jacob,

How does numbering chromatically distinguish between enharmonic notes? How are double flats and double sharps handled?

As to the negative numbers in modulo 12: I'm not really using modulo 12 except for determining enharmonics. I deliberately *don't* want to collapse notes into enharmonic equivalence classes. Given that flats and sharps extend infinitely in each direction, using negative numbers makes sense.

As to prior art, I do know of Hewlett's system (as mentioned in my original post). I have yet to read the work of Clements, Zimmerman or Agmon on this topic but I am aware of it.


Jacob on Dec. 14, 2007:

My bad; I misunderstand your intent initially. Indeed the numeric pitch-class notation normally used in music theory has a different purpose; it does not represent the scale-steps of tonal music, and hence does not represent enharmonic relationships, which only have meaning in that system; it represents pitch-class only (hence not register either). Of course you say in the first paragraph that you are looking to represent 35 names, but I didn't understand why you'd want to do that and hence I didn't take it in, since from my normal point of view those 35 names only point to 12 objects. You appear to be trying to model not pitch itself, but pitch as notated, which captures for each pitch, in addition to pitch class and register, information about the harmonic context. That's all that these enharmonic variants are -- they don't mean that the pitch objects being named are somehow different, just that they occur in different fields of harmonic influence. (You could argue that there are implications for pitch inflection/temperment that are inherent in pitch itself; I think that is a misinterpretation, as all musical events are intelligently modified according to their context in performance; that's contextual, too, and the various enharmonic notations for a pitch, while they may be suggestive of the functions of the note which might encourage such inflection, are not directly denotative of those inflections, in my opinion.) The connotative qualities of traditional notation are very valuable, because it influences how the performer reads in the musical text, and how close or distant a harmonic relationship appears, when there is a real ambiguity; but from a compositional, generative point of view, I question whether it is fruitful to deal with 35 nouns for 12 things. However, the proof of the pudding, etc.

Incidentally, to use the math question to ensure that I am human in this form you should require me to get it wrong occasionally. Or use your notation to implement questions like, what is a minor 9th above E double flat?

Add a Comment

Created: Dec. 13, 2007
Last Modified: Dec. 14, 2007
Author: jtauber