Skip to content

Ambitus

ambitus

ambitus(score: Score) -> int | None

Returns the pitch range (ambitus) in semitones (or midi keynums) of a Score object.

Ports the "ambitus" function from miditoolbox.

Parameters:

  • score (Score) –

    A Score object containing Parts, Staves, and Notes.

Returns:

  • int | None –

    None if score is empty. Otherwise, the difference between the largest pitch and the smallest pitch (in semitones specified by midi keynums).

Source code in amads/melody/ambitus.py
 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
def ambitus(score: Score) -> int | None:
    """
    Returns the pitch range (ambitus) in semitones (or midi keynums)
    of a Score object.

    Ports the "ambitus" function from miditoolbox.

    Parameters
    ----------
    score : Score
        A Score object containing Parts, Staves, and Notes.

    Returns
    -------
    int | None
        None if score is empty.
        Otherwise, the difference between the largest pitch and the smallest
        pitch (in semitones specified by midi keynums).
    """
    if next(score.find_all(Note), None) is None:
        return None
    min_pitch = min(note.midi_num for note in score.find_all(Note))
    max_pitch = max(note.midi_num for note in score.find_all(Note))

    return max_pitch - min_pitch