Representing Music in Code: Pitch
By: Sam Matucheski
If you are writing a program that involves music such as a music generator or a music theory analysis tool you’re going to run into the problem of how to represent music in whatever programming language you’re using. (For the purposes of this article, when I talk about music I am talking about music represented as notation, not recorded music as in sound files.) Basically I’m going to be talking about ways to represent this:
When you’re programming.
The first property of music I’m going to talk about is pitch. Pitch is probably the easiest property to represent. The way that I like to do it the most is to represent them numerically using an existing system, the MIDI standard. The MIDI specification represents all 12 notes in the chromatic scale as numbers between 0 -127 (the range of a signed byte) and sets note 60 as middle C. The value of all other notes are based off of this, so 61 is the C# above middle C, 59 is the B below middle C, 72 is the C an octave above middle C.
The music above in MIDI values would be 67, 71, 74, 69, 66, 67.
Here’s a chart with all of MIDI note values with their corresponding note name and octave.
Now there are different ways to notate note names with octaves. I personally like scientific pitch potation where middle C is C4. When combined with MIDI with note 60 as C4 the first 12 numbers (0 - 11) are octave -1 which looks a little weird when you write it out. The MIDI spec is nice because you have plenty of notes to work with (recall that a standard grand piano has 88 keys) and when you have access to all notes and octaves of the chromatic scale you can do some quick math to alter notes.
Want to increase the octave of a note? Simply add 12 times the amount of octaves you want to increase it by. Note += (12 x number of octaves) So to go from A3 to A5: 57 + (12 x 2) = 81
Want to build a major triad up from one note?
//We'll do this example in something C like with an array int note = 60; //C4 int triad; triad = note; triad = note + 4; //64 = E4 triad = note + 7; //67 = G4
Pretty neat, right? The only real problem I experience is when I'm doing something like making diatonic music. For example if I'm generating notes only found in C major with MIDI numbers it can be easiy to make something outside of C major and then you have to put some extra work into figuring out what notes belong in what key. One way is to translate the note into it's scientific pitch notation form but without the octave (so 48, 60, 72 ect. will be both C) and checking that against an array that holds just the notes for whatever key you're checking.
So that's some things you can do when you represent pitch as MIDI numbers. Hope ya liked it and maybe found it useful. If you have any suggestions for me to write about like this contact info is on the about page, drop me an email and let me know what you think.
"But what about rhythm?" you may be asking. Don't worry, I'll cover it next time. Spoiler warning: it's trickier than pitch!
Till next time folks.