Skip to content

Commit

Permalink
catching exception when decoding velocity values in MIDILike
Browse files Browse the repository at this point in the history
  • Loading branch information
Natooz committed Nov 30, 2024
1 parent b53111a commit 4c1cd28
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
13 changes: 13 additions & 0 deletions miditok/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,19 @@ def __init__(
# Additional params
self.additional_params = kwargs

# Using dataclass overly complicates all the checks performed after init and reduces
# the types flexibility (sequence etc...).
# Freezing the class could be done, but special cases (MMM) should be handled.
"""def __setattr__(self, name, value):
if getattr(self, "_is_frozen", False) and name != "_is_frozen":
raise AttributeError(
f"Cannot modify frozen instance of {self.__class__.__name__}"
)
super().__setattr__(name, value)
def freeze(self):
object.__setattr__(self, "_is_frozen", True)"""

@property
def max_num_pos_per_beat(self) -> int:
"""
Expand Down
2 changes: 1 addition & 1 deletion miditok/midi_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(

# Tweak the tokenizer's configuration and / or attributes before creating the
# vocabulary. This method is intended to be overridden by inheriting tokenizer
# classes
# classes.
self._tweak_config_before_creating_voc()

# Determines whether the tokenizer will produce a single sequence of tokens for
Expand Down
5 changes: 4 additions & 1 deletion miditok/tokenizations/midi_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,10 @@ def is_track_empty(track: Track) -> bool:
if not self.config.use_velocities:
vel = DEFAULT_VELOCITY
elif ti + 1 < len(seq):
vel = int(seq[ti + 1].split("_")[1])
try:
vel = int(seq[ti + 1].split("_")[1])
except ValueError: # invalid token succession
continue
else:
break # last token

Expand Down
2 changes: 1 addition & 1 deletion tests/test_attribute_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
@pytest.mark.parametrize(
"random_bars_idx", [False, True], ids=lambda r: "rand_bars" if r else "all_bars"
)
def test_controller_controls_computation(
def test_attribute_controls_computation(
file_path: Path,
tokenization: str,
random_tracks_idx: bool,
Expand Down

0 comments on commit 4c1cd28

Please sign in to comment.