@libraz/libcantus - v1.1.0
    Preparing search index...

    Function spellPitchClass

    • 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: next moves the letter, previous keeps it. For a chromatic pitch class the evidence is weighed in this order, and the first spelling within a double accidental wins:

      1. The sounding chord, when chordRoot is 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.
      2. Resolution: next a semitone above makes the pitch a leading tone into it, one letter below — the C# rising to D in D minor.
      3. Approach: previous a semitone below inflects that note upward, keeping its letter, so a chromatic ascent spells with sharps — F F# G in C major.
      4. Descent: next a 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, previous a 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.

      Parameters

      • pc: number

        The pitch class to spell.

      • tonic: NoteLike

        The spelled tonic (its letter anchors the spelling), as a note name, a MIDI number, note data, or a Note.

      • key: KeyLike

        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.

      Returns NoteData

      The spelled note (without octave).

      If tonic does not sound the key's root pitch class.

      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'