improve sharps/flats

• Jul 17, 2019 - 17:59

I'm writing something that's basically in C but mixes in Cm quite a bit. I'm entering notes via midi. Two examples of where I'm running into bad accidentals and I think the machine should know better:

1) A melodic line that goes C–Bb–Ab–(Bb) is spelled C–Bb–G#.
2) An Ab major chord's default spelling is C, Eb, G#.

I'm not sure if accidental spellings are chosen based on key or based on context.
In the former case, I think in major, lowered spellings should be more common except in the case of #4 vs b5, and in minor, raised spellings more common, except in the case of #1 vs b2.
In the latter case, I think spellings that avoid diminished/augmented intervals should be preferred.

Megan Lavengood
Asst Prof of Music Theory


If you enter an A or a B (with the keyboard or mouse), you can flatten it with the down-arrow key (or by applying a flat from the toolbar). With the keyboard or the mouse, it is impossible to get incorrect spellings. I assume you are trying to enter notes via a MIDI keyboard? G# is as common as Ab in C major. It does not analyze chords to determine how to spell notes, thank goodness. You can type "J" one or more times at any note and it will rotate through its enharmonic spellings.

As mentioned, when entering notes using mouse or computer keyboard, you are in complete control of the spelling, which is one reason I find computer keyboard the most efficient method of note input. If you're using MIDI (which I'm assuming you are, or maybe the built-in Piano Keyboard window?), there will indeed be guesswork involved. Spellings are chosen based on key, but MuseScore has no concept of major versus minor - it only knows the numbers of flats or sharps in the key signature. So the algorithm is based just on that - specifically, on how close a note is to the rest of the scale on the circle of fifths.

Here's an actual comment from thje source code illustration the algorithm:

// Examples for Prefer::NEAREST (n indicates explicit natural):
// C major will use Eb Bb [F C G D A E B] F# C# G#.
// E major will use Gn Dn [A E B F# C# G# D#] A# E# B#.
// F# major will use An En [B F# C# G# D# A# E#] B# Fx Cx.
// Eb major will use Gb Db [Ab Eb Bb F C G D] An En Bn.
// Gb major will use Bbb Fb [Cb Gb Db Ab Eb Bb F] Cn Gn Dn.

That is, for the key of C, Eb is only two fifths removed from the key (whereas D# would be four, which is why Eb is chosen. Similarly, C# is only two fifths removed where Db would be four. That much seems a reasonably logical heuristic.

Ab/G# are both three steps removed, but we prefer the flat in this case (same for b6/#5 in all other keys). Maybe we should adjust this one case to favor the #5. It would help with minor keys obviously, but also, probably V/vi is a more common chord than iv (although no doubt both are encountered a lot, and iv along with its partner-in-crime bVII7 may have the edge in jazz).

In reply to by mlavengood

Also true on both counts. That's a much bigger change to the code than what I proposed though, and it's still likely to guess wrong a lot. In the melodic case, C-Bb-G#-A is not an uncommon melodic motif - again, perhaps more so in jazz. In the harmonic case, augmented sixth chords will get misspelled every time. And often enough, neither check will provide useful information (it also could be conflicting). Worth thinking about as a research project - to what extent can this be automated, and what are all the inputs that would be required to the algorithm? But from a practical perspective, probably just shifting that diagram one notch to the left (which I think would entail changing the definition of Prefer::NEAREST from 11 to either 10 or 12, the algorithm itself is a nonsense-looking mathematical equation) would have the biggest bang for the buck. Until someone else complains anyhow :-)

In reply to by Marc Sabatella

You're naming edge cases that would be predicted wrong by adding this step, but it would predict more things correctly (like Ab major triads instead of Cm#5 triads). It's impossible for it to be perfect, but this would be an improvement. "+6 chords would be wrong every time"—well, right now, it's not getting major triads right.

Yes, I realize the code would need to be changed/rewritten, but this is the feature request forum, so I thought I'd request that feature.

Do you still have an unanswered question? Please log in first to post your question.