denote.keys¶
Key estimation from pitch-class profiles (the Krumhansl-Schmuckler method).
Correlate a 12-bin pitch-class vector (the mean chroma of a recording, or a note
histogram of a score) with a major and a minor key profile rotated to each of the
12 tonics. The best of the 24 Pearson correlations names the key. This module is the
audio-free core; denote.get_key computes the chroma and calls it.
>>> major = KEY_PROFILES["krumhansl"].major
>>> r = key_from_chroma(major) # the C major profile itself
>>> r.key, round(r.confidence, 3)
('C major', 1.0)
>>> r.relative
'A minor'
>>> key_from_chroma(np.roll(major, 3)).key # rotated up three semitones
'Eb major'
>>> parse_key("F# minor")
(6, 'minor')
Profiles (KEY_PROFILES), each a published table of how well each pitch class
fits a key, tonic first:
"krumhansl": Krumhansl and Kessler’s probe-tone ratings (Krumhansl & Kessler 1982; Krumhansl, Cognitive Foundations of Musical Pitch, 1990, Table 2.1)."temperley": Temperley’s profiles from the Kostka-Payne corpus (Temperley, Music and Probability, 2007), as listed by Humdrumkeycor.
Both sets of numbers are as given at https://extras.humdrum.org/man/keycor/ (and in
music21’s analysis.discrete). Any other (major, minor) pair of 12 numbers
can be passed as profile.
Key names follow the fewest-accidentals convention (Eb major, C# minor, Gb major,
Eb minor), the one chordsym uses for chord spelling.
Module Attributes
A vector whose spread is below this fraction of its largest value is flat. |
Functions
|
The best of the 24 keys for a chroma vector or chromagram. |
|
|
|
|
|
|
|
A |
|
The key sharing |
|
A |
Classes
|
A major and a minor key profile: 12 weights each, tonic first (C = index 0). |
- denote.keys.FLAT_RTOL = 1e-06¶
A vector whose spread is below this fraction of its largest value is flat.
- class denote.keys.KeyProfile(major, minor, source='')[source]¶
Bases:
NamedTupleA major and a minor key profile: 12 weights each, tonic first (C = index 0).
- denote.keys.key_from_chroma(chroma, *, profile='krumhansl', weights=None, backend='')[source]¶
The best of the 24 keys for a chroma vector or chromagram.
- Parameters:
chroma –
(12,)pitch-class weights (C first) or a(12, frames)chromagram, averaged over frames.profile (
Union[str,KeyProfile,Tuple[Sequence[float],Sequence[float]]]) – A name inKEY_PROFILES(default"krumhansl"), aKeyProfile, or a(major, minor)pair of 12 weights each.weights – Optional per-frame weights for the average (e.g. frame RMS).
backend (
str) – Recorded on the result.
- Return type:
- Returns:
KeyResult whose
confidenceis the winner’s correlation,marginthe gap to the runner-up,scoresevery key’s correlation (best first), andraw["chroma"]the 12-bin vector correlated.
- denote.keys.key_name(tonic_pc, mode)[source]¶
"Eb major": the key on pitch classtonic_pc(C = 0) inmode.>>> key_name(3, "major"), key_name(1, "minor") ('Eb major', 'C# minor')- Return type:
- denote.keys.key_scores(chroma, *, profile='krumhansl')[source]¶
{key name: Pearson correlation}for all 24 keys, best first.Raises
ValueErrorwhen the vector is flat to withinFLAT_RTOL(silence, or no pitched content), since a correlation with a constant is undefined. Level gating (very quiet or very short audio) is the audio adapter’s job: per-frame chroma normalization makes even faint noise look pitched here.
- denote.keys.parse_key(name)[source]¶
(tonic_pc, mode)from a key name.Accepted:
"Eb major","c# minor","A min","Am","Amin","Amaj"and a bare uppercase tonic ("Bb", major). A bare lowercase tonic ("a") is rejected, because some conventions read it as minor; say"a minor"or"am".>>> parse_key("Eb major"), parse_key("Am"), parse_key("Bbmaj") ((3, 'major'), (9, 'minor'), (10, 'major'))
- denote.keys.pitch_class_vector(chroma, *, weights=None)[source]¶
A
(12,)vector:chromaitself, or its mean over frames if(12, frames).weights(one non-negative number per frame) makes it a weighted mean, e.g. by frame energy so quiet frames count less.- Return type:
ndarray
- denote.keys.relative_key(name)[source]¶
The key sharing
name’s key signature: C major <-> A minor.>>> relative_key("C major"), relative_key("F# minor") ('A minor', 'A major')- Return type: