Spell a key's tonic the conventional way: the letter and accidental the key
is written with, paired with the key exactly as given.
The candidates are the tonics keys are actually written on — everything from
Cb to A#, which names each pitch class once or twice (C, but Db and C#) — and
which of them names the key depends on whether the key has a signature of its
own. A major or minor key, a church mode, and the harmonic and melodic minor
are spelled on the tonic their signature is written on: pitch class 1 in
major is Db, five flats, rather than C#, seven sharps; pitch class 8 in minor
is G#, five sharps, rather than Ab, seven flats; and G# harmonic minor writes
its raised seventh F## as an accidental over that signature rather than
respelling the key. Where two signatures are equally long — pitch class 6 in
major is F# at +6 and Gb at -6 — the flat side is taken.
Every other scale only borrows the signature of its parallel major or minor,
so nothing follows from it and the scale is spelled on the tonic it reads
best from: the fewest double accidentals first, then the fewest accidentals
altogether, and only then the shorter signature. That is why pitch class 1
altered is C# — C# D E F G A B — rather than Db, which would have to write
four double flats to say the same thing, and why pitch class 8 minor
pentatonic is G# B C# D# F# rather than Ab B Db Eb Gb, which reads its third
as an augmented second.
The mask's third picks the mode the signature is measured in — a minor third
and no major third reads as minor, anything else as major — and the key is
returned unchanged, so a scale keeps its own mask and only borrows a tonic
spelling. A caller that has a tonic spelling of its own always keeps it: this
only answers for a key that arrives as a bare pitch class.
Every 12-bit mask and every integer root is answered — a root outside [0, 11]
is reduced first, and a mask with no third at all counts as major.
The key is paired with the answer exactly as it was handed in, root and mask
untouched, so this takes a plain key/scale rather than the wider forms the
relations below accept: a coerced key could not be given back verbatim.
Spell a key's tonic the conventional way: the letter and accidental the key is written with, paired with the key exactly as given.
The candidates are the tonics keys are actually written on — everything from Cb to A#, which names each pitch class once or twice (C, but Db and C#) — and which of them names the key depends on whether the key has a signature of its own. A major or minor key, a church mode, and the harmonic and melodic minor are spelled on the tonic their signature is written on: pitch class 1 in major is Db, five flats, rather than C#, seven sharps; pitch class 8 in minor is G#, five sharps, rather than Ab, seven flats; and G# harmonic minor writes its raised seventh F## as an accidental over that signature rather than respelling the key. Where two signatures are equally long — pitch class 6 in major is F# at +6 and Gb at -6 — the flat side is taken.
Every other scale only borrows the signature of its parallel major or minor, so nothing follows from it and the scale is spelled on the tonic it reads best from: the fewest double accidentals first, then the fewest accidentals altogether, and only then the shorter signature. That is why pitch class 1 altered is C# — C# D E F G A B — rather than Db, which would have to write four double flats to say the same thing, and why pitch class 8 minor pentatonic is G# B C# D# F# rather than Ab B Db Eb Gb, which reads its third as an augmented second.
The mask's third picks the mode the signature is measured in — a minor third and no major third reads as minor, anything else as major — and the key is returned unchanged, so a scale keeps its own mask and only borrows a tonic spelling. A caller that has a tonic spelling of its own always keeps it: this only answers for a key that arrives as a bare pitch class.
Every 12-bit mask and every integer root is answered — a root outside [0, 11] is reduced first, and a mask with no third at all counts as major.
The key is paired with the answer exactly as it was handed in, root and mask untouched, so this takes a plain key/scale rather than the wider forms the relations below accept: a coerced key could not be given back verbatim.