librosa.key_to_notes
- librosa.key_to_notes(key, *, unicode=True, natural=False)[source]
List all 12 note names in the chromatic scale, as spelled according to a given key (major or minor) or mode (see below for details and accepted abbreviations).
This function exists to resolve enharmonic equivalences between different spellings for the same pitch (e.g. C♯ vs D♭), and is primarily useful when producing human-readable outputs (e.g. plotting) for pitch content.
Note names are decided by the following rules:
If the tonic of the key has an accidental (sharp or flat), that accidental will be used consistently for all notes.
If the tonic does not have an accidental, accidentals will be inferred to minimize the total number used for diatonic scale degrees.
If there is a tie (e.g., in the case of C:maj vs A:min), sharps will be preferred.
- Parameters:
- keystring
Must be in the form TONIC:key. Tonic must be upper case (
CDEFGAB
), key must be lower-case (major
,minor
,ionian
,dorian
,phrygian
,lydian
,mixolydian
,aeolian
,locrian
).The following abbreviations are supported for the modes: either the first three letters of the mode name (e.g. “mix”) or the mode name without “ian” (e.g. “mixolyd”).
Both
major
andmaj
are supported as mode abbreviations.Single and multiple accidentals (
b!♭
for flat,#♯
for sharp,𝄪𝄫
for double-accidentals, or any combination thereof) are supported.Examples:
C:maj, C:major, Dbb:min, A♭:min, D:aeo, E𝄪:phryg
.- unicodebool
If
True
(default), use Unicode symbols (♯𝄪♭𝄫)for accidentals.If
False
, Unicode symbols will be mapped to low-order ASCII representations:♯ -> #, 𝄪 -> ##, ♭ -> b, 𝄫 -> bb, ♮ -> n
- naturalbool
If ``True’’, mark natural accidentals with a natural symbol (♮).
If
False
(default), do not print natural symbols.For example, note_to_degrees(‘D:maj’)[0] is C if natural=False (default) and C♮ if natural=True.
- Returns:
- noteslist
notes[k]
is the name for semitonek
(starting from C) under the given key. All chromatic notes (0 through 11) are included.
See also
Examples
C:maj will use all sharps
>>> librosa.key_to_notes('C:maj') ['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']
A:min has the same notes
>>> librosa.key_to_notes('A:min') ['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']
A♯:min will use sharps, but spell note 0 (C) as B♯
>>> librosa.key_to_notes('A#:min') ['B♯', 'C♯', 'D', 'D♯', 'E', 'E♯', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']
G♯:maj will use a double-sharp to spell note 7 (G) as F𝄪:
>>> librosa.key_to_notes('G#:maj') ['B♯', 'C♯', 'D', 'D♯', 'E', 'E♯', 'F♯', 'F𝄪', 'G♯', 'A', 'A♯', 'B']
F♭:min will use double-flats
>>> librosa.key_to_notes('Fb:min') ['D𝄫', 'D♭', 'E𝄫', 'E♭', 'F♭', 'F', 'G♭', 'A𝄫', 'A♭', 'B𝄫', 'B♭', 'C♭']
G:loc uses flats
>>> librosa.key_to_notes('G:loc') ['C', 'D♭', 'D', 'E♭', 'E', 'F', 'G♭', 'G', 'A♭', 'A', 'B♭', 'B']
If natural=True, print natural accidentals.
>>> librosa.key_to_notes('G:loc', natural=True) ['C', 'D♭', 'D♮', 'E♭', 'E♮', 'F', 'G♭', 'G', 'A♭', 'A♮', 'B♭', 'B♮']
>>> librosa.key_to_notes('D:maj', natural=True) ['C♮', 'C♯', 'D', 'D♯', 'E', 'F♮', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']
>>> librosa.key_to_notes('G#:maj', unicode = False, natural = True) ['B#', 'C#', 'Dn', 'D#', 'En', 'E#', 'F#', 'F##', 'G#', 'An', 'A#', 'B']