Skip to content

Chords

chord(tonic_note)

Generate the notes and degrees for a chord based on a tonic note.

Parameters:

Name Type Description Default
tonic_note str

The tonic note representing the root note of the chord. It should specify the chord type:

  • No suffix for major
  • m for minor
  • dim for diminished
  • + for augmented
  • m+ for minor augmented
required

Returns:

Type Description
dict[str, list[str]]

A dictionary containing the notes and their corresponding degrees.

Examples:

>>> chord("C")
{'notes': ['C', 'E', 'G'], 'degrees': ['I', 'III', 'V']}
>>> chord("Cm")
{'notes': ['C', 'D#', 'G'], 'degrees': ['I', 'III-', 'V']}
>>> chord("Cdim")
{'notes': ['C', 'D#', 'F#'], 'degrees': ['I', 'III-', 'V-']}
>>> chord("C+")
{'notes': ['C', 'E', 'G#'], 'degrees': ['I', 'III', 'V+']}
>>> chord("Cm+")
{'notes': ['C', 'D#', 'G#'], 'degrees': ['I', 'III-', 'V+']}
Source code in melodica_notes/chords.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def chord(tonic_note: str) -> dict[str, list[str]]:
    """
    Generate the notes and degrees for a chord based on a tonic note.

    Args:
        tonic_note (str): The tonic note representing the root note of the
            chord. It should specify the chord type:

            - No suffix for major
            - `m` for minor
            - `dim` for diminished
            - `+` for augmented
            - `m+` for minor augmented

    Returns:
        A dictionary containing the notes and their corresponding degrees.

    Examples:
        >>> chord("C")
        {'notes': ['C', 'E', 'G'], 'degrees': ['I', 'III', 'V']}

        >>> chord("Cm")
        {'notes': ['C', 'D#', 'G'], 'degrees': ['I', 'III-', 'V']}

        >>> chord("Cdim")
        {'notes': ['C', 'D#', 'F#'], 'degrees': ['I', 'III-', 'V-']}

        >>> chord("C+")
        {'notes': ['C', 'E', 'G#'], 'degrees': ['I', 'III', 'V+']}

        >>> chord("Cm+")
        {'notes': ['C', 'D#', 'G#'], 'degrees': ['I', 'III-', 'V+']}
    """

    if "dim" in tonic_note:
        key, _ = tonic_note.split("dim")
        tonic, third, fifth = triad(key, "minor")
        keys = [tonic, third, semitone(fifth, interval=-1)]
        degrees = ["I", "III-", "V-"]

    elif "m" in tonic_note:
        keys, degrees = minor(tonic_note)

    elif "+" in tonic_note:
        key, _ = tonic_note.split("+")
        tonic, third, fifth = triad(key, "major")
        keys = [tonic, third, semitone(fifth, interval=+1)]
        degrees = ["I", "III", "V+"]

    else:
        keys = triad(tonic_note, "major")
        degrees = ["I", "III", "V"]

    return {"notes": keys, "degrees": degrees}

minor(tonic_note)

Generates the minor triad based on the given tonic note.

Parameters:

Name Type Description Default
tonic_note str

The tonic note representing the root of the minor triad.

required

Returns:

Type Description
tuple[list[str], list[str]]

tuple[list[str], list[str]]: A tuple containing the notes and degrees of the minor triad.

Examples:

>>> minor("Cm")
(['C', 'D#', 'G'], ['I', 'III-', 'V'])
>>> minor("Cm+")
(['C', 'D#', 'G#'], ['I', 'III-', 'V+'])
Source code in melodica_notes/chords.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def minor(tonic_note: str) -> tuple[list[str], list[str]]:
    """
    Generates the minor triad based on the given tonic note.

    Args:
        tonic_note (str): The tonic note representing the root of the minor
            triad.

    Returns:
        tuple[list[str], list[str]]: A tuple containing the notes and degrees
            of the minor triad.

    Examples:
        >>> minor("Cm")
        (['C', 'D#', 'G'], ['I', 'III-', 'V'])

        >>> minor("Cm+")
        (['C', 'D#', 'G#'], ['I', 'III-', 'V+'])
    """
    key, _ = tonic_note.split("m")

    if "+" in tonic_note:
        tonic, third, fifth = triad(key, "minor")
        keys = [tonic, third, semitone(fifth, interval=1)]
        degrees = ["I", "III-", "V+"]
    else:
        keys = triad(key, "minor")
        degrees = ["I", "III-", "V"]

    return keys, degrees

semitone(tonic_note, *, interval)

Returns the note that is a semitone away from the given tonic note.

Parameters:

Name Type Description Default
tonic_note str

The starting note or key.

required
interval int

The number of semitones to move from the tonic note. Positive values move upward, and negative values move downward.

required

Returns:

Name Type Description
str str

The note that is a semitone away from the tonic note.

Examples:

>>> semitone('C', interval=1)
'C#'
>>> semitone('A', interval=-1)
'G#'
Source code in melodica_notes/chords.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def semitone(tonic_note: str, *, interval: int) -> str:
    """
    Returns the note that is a semitone away from the given tonic note.

    Args:
        tonic_note (str): The starting note or key.
        interval (int): The number of semitones to move from the tonic note.
            Positive values move upward, and negative values move downward.

    Returns:
        str: The note that is a semitone away from the tonic note.

    Examples:
        >>> semitone('C', interval=1)
        'C#'
        >>> semitone('A', interval=-1)
        'G#'
    """
    if len(tonic_note) == 1:
        tonic_note = tonic_note.upper()
    else:
        tonic_note = tonic_note[0].upper() + tonic_note[1].lower()

    try:
        position = SHARP_NOTES.index(tonic_note) + interval
        notes = SHARP_NOTES
    except ValueError:
        position = FLAT_NOTES.index(tonic_note) + interval
        notes = FLAT_NOTES

    return notes[position % 12]

triad(tonic_note, scale_type)

Generate the triad based on a given tonic note and scale mode.

Parameters:

Name Type Description Default
tonic_note str

The tonic note or the root note of the scale for which you want to determine the triad type.

required
scale_type str

The type of the scale. Currently supports only "major".

required

Returns:

Type Description
list[str]

list[str]: A list containing the notes of the triad.

Raises:

Type Description
ValueError

If the tonic note is invalid.

KeyError

If the scale mode does not exist or has not been implemented.

Examples:

>>> triad("C", "major")
['C', 'E', 'G']
>>> triad("A", "minor")
['A', 'C', 'E']
Source code in melodica_notes/chords.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def triad(tonic_note: str, scale_type: str) -> list[str]:
    """
    Generate the triad based on a given tonic note and scale mode.

    Args:
        tonic_note (str): The tonic note or the root note of the scale for
            which you want to determine the triad type.
        scale_type (str): The type of the scale. Currently supports only
            "major".

    Returns:
        list[str]: A list containing the notes of the triad.

    Raises:
        ValueError: If the tonic note is invalid.
        KeyError: If the scale mode does not exist or has not been implemented.

    Examples:
        >>> triad("C", "major")
        ['C', 'E', 'G']

        >>> triad("A", "minor")
        ['A', 'C', 'E']
    """
    degrees = (0, 2, 4)
    scale_notes, _ = scale(tonic_note, scale_type).values()

    return [scale_notes[degree] for degree in degrees]