Key¶
key ¶
Musical keys with diatonic chords and harmonic analysis.
The Key namespace generates everything you need for harmonic analysis in
a major or minor key: the scale, the seven diatonic chords (triads and
seventh chords), their harmonic functions (T/SD/D), the chord-scale
mappings for each degree, and the secondary dominants + tritone-substitute
dominants used in jazz harmony.
Minor keys expose three variants — natural, harmonic, melodic — each with its own diatonic chord set.
Example
from tonal_py import Key ck = Key.major_key("C") ck.chords ('Cmaj7', 'Dm7', 'Em7', 'Fmaj7', 'G7', 'Am7', 'Bm7b5') ck.minor_relative 'A' ck.alteration 0
Source parity: @tonaljs/key
major_key ¶
Build a MajorKey for the given tonic.
The returned object has everything for harmonic analysis: the scale notes, the 7 diatonic 7th chords, their harmonic functions, secondary dominants, substitute dominants, and chord-scale mappings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tonic
|
str
|
The tonic note (e.g. |
required |
Returns:
| Type | Description |
|---|---|
MajorKey
|
A |
MajorKey
|
invalid. |
Example
from tonal_py import Key ck = Key.major_key("C") ck.chords ('Cmaj7', 'Dm7', 'Em7', 'Fmaj7', 'G7', 'Am7', 'Bm7b5') ck.chords_harmonic_function ('T', 'SD', 'T', 'SD', 'D', 'T', 'D') ck.minor_relative 'A' ck.alteration 0 Key.major_key("D").key_signature '##'
Source code in src/tonal_py/key.py
minor_key ¶
Build a MinorKey with natural, harmonic, and melodic sub-scales.
Each sub-scale (.natural, .harmonic, .melodic) is a KeyScale
with its own diatonic chords and harmonic functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tonic
|
str
|
The tonic note. |
required |
Returns:
| Type | Description |
|---|---|
MinorKey
|
A |
Example
from tonal_py import Key ak = Key.minor_key("A") ak.relative_major 'C' ak.alteration 0 ak.natural.chords ('Am7', 'Bm7b5', 'Cmaj7', 'Dm7', 'Em7', 'Fmaj7', 'G7')
Source code in src/tonal_py/key.py
major_key_chords ¶
Get all chords in a major key with their harmonic role tags.
Each chord may have multiple roles (e.g. a chord that's both a diatonic V and a secondary dominant for another key center).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tonic
|
str
|
The tonic note. |
required |
Returns:
| Type | Description |
|---|---|
list[KeyChord]
|
List of |
Example
from tonal_py import Key chords = Key.major_key_chords("C")
First chord is the tonic¶
chords[0].name 'Cmaj7' 'T' in chords[0].roles # tonic function True
Source code in src/tonal_py/key.py
minor_key_chords ¶
Get all chords in a minor key (combining natural/harmonic/melodic).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tonic
|
str
|
The tonic note. |
required |
Returns:
| Type | Description |
|---|---|
list[KeyChord]
|
List of |
list[KeyChord]
|
than the major-key list because each minor sub-scale contributes |
list[KeyChord]
|
chords. |
Example
from tonal_py import Key chords = Key.minor_key_chords("A")
Has at least the 7 natural diatonic chords¶
len(chords) >= 7 True
Source code in src/tonal_py/key.py
major_tonic_from_key_signature ¶
Convert a key signature to its major-key tonic.
The signature may be either a string ("###" for 3 sharps, "bb" for
2 flats) or an integer (number of fifths from C; positive = sharps,
negative = flats).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sig
|
Any
|
Either a sharps/flats string or an integer fifth count. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The tonic note name, or |
Example
from tonal_py import Key Key.major_tonic_from_key_signature("###") 'A' Key.major_tonic_from_key_signature("bb") 'Bb' Key.major_tonic_from_key_signature(2) # 2 fifths up from C 'D' Key.major_tonic_from_key_signature("garbage") is None True