Skip to content

Dropshortnotes

dropshortnotes

dropshortnotes(score: Score, threshold: float) -> Score

Removes notes whose total tied duration is strictly less than a threshold.

For each note that starts a tied chain, the total tied duration is compared against the threshold. If the total duration is strictly less than the threshold, all notes in the chain are removed.

Zero is a special case: passing threshold = 0 removes only notes whose tied duration is exactly zero.

Parameters:

  • score (Score) –

    The score to filter.

  • threshold (float) –

    Duration threshold in beats. Notes with (tied) duration < threshold are removed. Pass 0 to remove only zero-duration grace notes.

Returns:

  • Score

    A copy of the score with short notes removed.

Source code in amads/algorithms/dropshortnotes.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def dropshortnotes(score: Score, threshold: float) -> Score:
    """
    Removes notes whose total tied duration is strictly less than a threshold.

    For each note that starts a tied chain, the total tied duration is compared
    against the threshold. If the total duration is strictly less than the
    threshold, all notes in the chain are removed.

    Zero is a special case: passing `threshold = 0` removes only notes whose
    tied duration is exactly zero.

    Parameters
    ----------
    score : Score
        The score to filter.
    threshold : float
        Duration threshold in beats. Notes with (tied) `duration < threshold`
        are removed. Pass `0` to remove only zero-duration grace notes.

    Returns
    -------
    Score
        A copy of the score with short notes removed.
    """
    # 1. Copy the score to avoid modifying the original
    score_copy = cast(Score, score.copy())
    all_notes = list(score_copy.find_all(Note))

    # 2. Find notes to remove, starting only from chain heads
    tied_to_ids = {id(n.tie) for n in all_notes if n.tie}

    to_remove = []
    for note in all_notes:
        if id(note) in tied_to_ids:
            continue
        if threshold == 0:
            should_drop = note.duration == 0
        else:
            should_drop = note.duration < threshold
        if should_drop:
            node = note
            while isinstance(node, Note):
                to_remove.append(node)
                if node.tie is None:
                    break
                node = node.tie

    # 3. Remove all collected notes
    for note in to_remove:
        if note.parent:
            note.parent.remove(note)

    return score_copy