Automatically Assigning Pitches To Notes

This section describes an algorithm that takes a file of ordinary sheet music - expressed in the MusicXML code standard - and determines for each note the exact pitch required to make the piece sound in just intonation. The result is input for creating a sound file or to produce sheet music with pitch annotations added.

Preparing the input for the optimization algorithm

After reading the MusicXML score from a file, the software converts the score to a long list of subsequent chords, whereby octaves and note repetitions within one harmonic chord are ignored. For instance, a wide chord C-c-g-c'-e' becomes just C-E-G. Then each such chord is given a number of candidate pitch assignments, e.g. the two grey background groups below:

C♯ G♯ D♯ A♯ E♯ B♯ F♯♯ C♯♯ G♯♯
A E B F♯ C♯ G♯ D♯ A♯ E♯
F C G D A E B F♯ C♯
D♭ A♭ E♭ B♭ F C G D A
B♭♭ F♭ C♭ G♭ D♭ A♭ E♭ B♭ F

The actual matrix used in the pitch assignment algorithm is larger so that more than two candidate pitch assignments for each chord can be considered.

For the chord G-B-D-F there are four candidate pitch assignments in the limited matrix:

C♯ G♯ D♯ A♯ E♯ B♯ F♯♯ C♯♯ G♯♯
A E B F♯ C♯ G♯ D♯ A♯ E♯
F C G D A E B F♯ C♯
D♭ A♭ E♭ B♭ F C G D A
B♭♭ F♭ C♭ G♭ D♭ A♭ E♭ B♭ F
(1) (2) (3) (4) (5) (6) (7) (8) (9)

The four candidate pitch assignments of the notes G-B-D-F are those in columns:

From now on, the shorthand for these pitch assignments will be GBDF 1-4, GBDF 3-5 etc.

The pitch assignment GBDF 4-7 will not be allowed, for a combination of two reasons: (1) it has a perfect fifth GD that has a pitch ratio 40/27 instead of the ideal 3/2 as you can check and (2) there are alternative pitch assignments without the ratio 40/27, namely the four listed above.

Optimizer hints

One may add signs to notes in the score (not to be printed) which guide the optimizer.

One such sign indicates that it is highly desirable that the note's pitch be equal to the pitch of the previous note of the same name. That is: a syntonic comma between those notes is to be avoided if possible.

Another optimizer hint indicates that, if the note has a harmonic minor third on it, the interval must have a pitch ratio 6/5, not 32/27. See BWV 701 for a demonstration of its use. If the chord G-B-D-F above would have such an optimizer hint on the note D, that would rule out candidate pitch assignments GBDF 1-4 and GBDF 5-8, because those have a minor third D-F with a pitch ratio of 32/27 instead of 6/5.

In December 2019 the meaning of this latter optimizer hint was extended: a note with this optimizer hint now also requires a harmonic perfect fifth on that note (if the score has one) to have a pitch ratio 3/2. The optimizer must under no circumstances revert to a pitch ratio of 40/27. This feature was used to improve the pitch assignment of Sleep.

Finding the best combined pitch assignment

So now imagine we have a piece of sheet music with three successive chords: F-A-C, G-B-D-F and C-E-G. Then we have (in the very limited matrix above) successive pitch assignments:

  1. FAC 1-2 or FAC 5-6
  2. GBDF 1-3 or GBDF 3-5 or GBDF 5-8 or GBDF 7-9
  3. CEG 2-3 or CEG 6-7

The goal is now to find one combined pitch assignment that is the 'best' according to some relevant measurement. Each combined pitch assignment is a path, from top to bottom, through this list of three chords. There are 2 x 4 x 2 = 16 such paths in this tiny example. In a real piece of music, successive chords run in the hundreds, and when we use a larger matrix, as is done for the music on this website, the number of different paths is astronomical.

For Fugue in C minor BWV 574 paths number 5.24479 ⨯ 101851. If you wonder whether that counts as astronomical: the estimated number of atoms in the observable universe is 1080, according to https://en.wikipedia.org/wiki/Large_numbers.

You could picture an algorithm that takes the following steps:

  1. First it finds the path with the fewest syntonic commas marked in the score as very unwanted. Often that is zero, else it is just a few of such syntonic commas. But there are a zillion different paths with that same low number of marked syntonic commas.
  2. Then, within those zillion paths, the algorithm finds the path with the fewest mid-air syntonic commas. Those syntonic commas number 0 to a dozen, rarely more. But there are a yillion different paths with that same low number of mid-air syntonic commas.
  3. Then, within those yillion paths, the algorithm finds the path with the fewest direct syntonic commas. Those syntonic commas number 0 to a dozen, rarely more. But there are a xillion different paths with that same low number of direct syntonic commas.
  4. etc.

At the end, when there is an unknown number of paths left which obey the same set of - currently - 13 minimization criteria, the algorithm would just pick the first one. But don't try it at home this way. If you let a computer perform the steps this way, it will not produce a result in your lifetime.

Dynamic Programming

Fortunately there is an algorithm that finds the 'best' path in the wink of an eye: Dynamic Programming [Be 1957]. It performs the steps in a different order and makes a clever use of particular properties of the paths so it can skip a lot of work. Although it is a very fast and efficient algorithm, it could not be used to find the best possible pitch assignment while the organist plays (as in the approach reported here) because the algorithm needs to see the whole score before it can determine the best pitch assignment for even the first few chords of the piece. Therefore the solution here is to deliver (apart from a soundfile) a score with annotations representing the pitch assignments. See Automatically Annotating Sheet Music.

Now to get the Dynamic Programming algorithm running on any problem, we need to have a running total and a way to compare two running totals to decide which is the smallest. The simplest example is where a running total is a number. Say the running total is a number consisting of three digits. We usually decide which is the smallest by inspecting the digits from left to right:

running total A:digit A1,digit A2,digit A3.
running total B:digit B1,digit B2,digit B3.

If digit A1 < digit B1, we know right away that running total A < running total B.
If digit A1 > digit B1, we know right away that running total A > running total B.
If digit A1 = digit B1, we shift our attention to digits A2 and B2, etc.

In our pitch assignment problem the running total does not consist of simple digits, but of various counts, for instance in a running total R:

count R1= the number of syntonic commas marked in the score as very unwanted.
count R2= the number of syntonic commas occurring mid air in a tone;
count R3= the number of syntonic commas occurring at tone repetitions in the score;
(etc.)

And we compare two running totals R and S as follows:
If count R1 < count S1, we know right away that running total R < running total S.
If count R1 > count S1, we know right away that running total R > running total S.
If count R1 = count S1, we shift our attention to counts R2 and S2, etc.

If you are interested in more details of Automatically Assigning Pitches To Notes, please send an e-mail.

No definitive solution

The Dynamic Programming algorithm is guaranteed to find the optimum pitch assignment. But what that optimum is, depends heavily on the formula I have given it to assess two consecutive chords. That formula (partly described above) has been developed over the years: trying, listening, changing. Again and again. There is no reason to believe the formula could not be improved further.

But believe it or not: the best automatic pitch assignment is not the goal of my work. The goal is to demonstrate that just intonation for organ music is feasible. The keyboard is indispensible. Providing the organist with properly annotated sheet music is necessary to give her a chance to get going on such a keyboard. But nobody is going to stop her from improving on those sheet music annotations and hit different buttons on the keyboard. The sound files are there so you may decide for yourself whether you find this a worthwhile effort. Or if you don't care, I hope you enjoy them.

Next: Generating sound files

Previous: Just Intonation

Home