Skip to content

Scales

scale(tonic, key)

Generate a scale from a tonic note (PT-BR=Nota tônica) and a key (PT-BR=tonalidade).

Parameters:

Name Type Description Default
tonic str

The note that will be the tonic of the scale.

required
key str

Scale key (PT-BR=tonalidade).

required

Returns:

Type Description
dict[str, list[str]]

A dictionary with the scale "notes" and "degrees" (PT-BR=graus). Inside of the dictionary, the "degrees" (PT-BR=graus) is a string (str) and a value is a list of strings.

Raises:

Type Description
ValueError

When passing a non-existent note.

KeyError

When key (PT-BR=tonalidade) does not exist or not does implemented.

Examples:

>>> scale('C', 'major')
{'notes': ['C', 'D', 'E', 'F', 'G', 'A', 'B'], 'degrees': ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII']}
>>> scale('a', 'major')
{'notes': ['A', 'B', 'C#', 'D', 'E', 'F#', 'G#'], 'degrees': ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII']}
Source code in musical_notes/scales.py
 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def scale(tonic: str, key: str) -> dict[str, list[str]]:
    """
    Generate a scale from a tonic note (PT-BR=Nota tônica) and a key (PT-BR=tonalidade).

    Args:
        tonic: The note that will be the tonic of the scale.
        key: Scale key (PT-BR=tonalidade).

    Returns:
        A dictionary with the scale "notes" and "degrees" (PT-BR=graus). Inside of the dictionary, the "degrees" (PT-BR=graus) is a string (str) and a value is a list of strings.

    Raises:
        ValueError: When passing a non-existent note.
        KeyError: When key (PT-BR=tonalidade) does not exist or not does implemented.

    Examples:
        >>> scale('C', 'major')
        {'notes': ['C', 'D', 'E', 'F', 'G', 'A', 'B'], 'degrees': ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII']}

        >>> scale('a', 'major')
        {'notes': ['A', 'B', 'C#', 'D', 'E', 'F#', 'G#'], 'degrees': ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII']}
    """
    tonic = tonic.upper()
    try:
        intervals = SCALES[key]
        key_post = NOTES.index(tonic)
    except ValueError:
        raise ValueError(f'That tonic does not exist, try {NOTES}')
    except KeyError:
        raise KeyError(
            'That key (PT-BR=tonalidade) does not exist or not does implemented, '
            f'try {list(SCALES.keys())}'
        )

    temp = []

    for interval in intervals:
        note = (key_post + interval) % 12
        temp.append(NOTES[note])

    return {
        'notes': temp,
        'degrees': ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII'],
    }