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

    Function detectKeyFromNotes

    • Rank keys by how well they contain a set of note events, weighting each note by how much of the music it actually occupies.

      detectKey counts every pitch once, which lets a run of fast ornamental notes outvote the sustained harmony that establishes the key. This weighs each note by the same measure chord inference weighs its own histogram by, so the two agree on what the music emphasises: duration times velocity, with a note that carries no velocity counted at full weight rather than at an assumed one. Notes that never sound (zero or negative duration) are ignored. The weighted distribution is then ranked exactly as detectKey ranks it, by correlation against a key profile, so score is a finite correlation in [-1, 1].

      The one term of that measure this cannot share is the metric accent, which chord inference adds to an onset by the beat it falls on: the notes arrive here without the meter they are counted in, and reading them against an assumed one would rank the same notes differently under a signature the caller never named. Ranking a whole piece is the coarser question, and the accents are what separate one window from the next rather than one key from another.

      Parameters

      • notes: readonly NoteEvent[]

        The note events to weigh.

      • opts: Omit<DetectKeyOptions, "weights"> = {}

        Which profile to rank with, whether the modes take part, and the budget for processing imported note events; the weights are derived from the notes themselves.

      Returns KeyMatch[]

      Ranked key interpretations (empty when nothing sounds).

      When profile is not a known name or a valid pair of 12-entry vectors, or modes names something that is not a church mode.

      When a note event is malformed or the input exceeds the budget.

      import { detectKeyFromNotes } from '@libraz/libcantus';
      detectKeyFromNotes([{ pitch: 60, startBeat: 0, durationBeat: 4 }])[0]?.key.rootPc; // 0