The pitch class to spell.
The spelled tonic (its letter anchors the spelling), as a note
name, a MIDI number, note data, or a Note.
The key/scale, as a key name, a key/scale, or a Key.
Optionalcontext: SpellingContext
Optional surrounding evidence; an absent or empty context spells exactly as the key alone does.
The spelled note (without octave).
import { spellPitchClass, formatNote, parseNote, majorKey } from '@libraz/libcantus';
const c = parseNote('C');
formatNote(spellPitchClass(6, c, majorKey(0))); // 'F#'
// The third of D7, the secondary dominant of G.
formatNote(spellPitchClass(6, 'C', 'C major', { chordRoot: 2 })); // 'F#'
// A chromatic passing tone falling from G to F.
formatNote(spellPitchClass(6, c, majorKey(0), { previous: 7, next: 5 })); // 'Gb'
Spell a single pitch class relative to a spelled tonic and key, optionally refined by the music around it.
Without a context the key decides alone: the tonic spells as itself, each degree of a heptatonic key takes the next letter above the tonic, a gapped scale gives each of its own tones a letter wherever its pitch set allows one, and every other pitch class takes the conventional interval spelling above the tonic's letter, so a flat-side key keeps flat-side names.
A context only ever refines a pitch class the key leaves ambiguous — one that is not a tone of
key. Scale tones, and the tonic, keep their key spelling whatever the context says.The melodic rules read the two neighbours differently, because a chromatic note is an inflection of the note it came from but a leading tone into the note it goes to:
nextmoves the letter,previouskeeps it. For a chromatic pitch class the evidence is weighed in this order, and the first spelling within a double accidental wins:chordRootis given and the pitch is an unambiguous chord tone above it (root, third, fifth or seventh) — the third of a secondary dominant D7 in C major spells F#, not Gb.nexta semitone above makes the pitch a leading tone into it, one letter below — the C# rising to D in D minor.previousa semitone below inflects that note upward, keeping its letter, so a chromatic ascent spells with sharps — F F# G in C major.nexta semitone below puts the pitch one letter above it, so a falling chromatic line spells with flats — A Ab G in C major. Failing that,previousa semitone above inflects that note downward on its own letter, which spells the same line from the note before instead.If no rule applies, or every candidate would need more than a double accidental, the key spelling stands.