Skip to content

Commit

Permalink
Corrections on validation of PerformedNotes.
Browse files Browse the repository at this point in the history
  • Loading branch information
manoskary committed Sep 25, 2023
1 parent e279f2d commit 702adf7
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions partitura/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(
super().__init__()
self.id = id
self.part_name = part_name
self.notes = list(map(lambda n: PerformedNote(n), notes))
self.notes = list(map(lambda n: n if isinstance(n, PerformedNote) else PerformedNote(n), notes))
self.controls = controls or []
self.programs = programs or []
self.ppq = ppq
Expand Down Expand Up @@ -310,7 +310,7 @@ def __init__(self, pnote_dict):
self.pnote_dict["track"] = self.pnote_dict.get("track", 0)
self.pnote_dict["channel"] = self.pnote_dict.get("channel", 1)
self.pnote_dict["velocity"] = self.pnote_dict.get("velocity", 60)
self._validate_values()
self._validate_values(pnote_dict)
self._accepted_keys = [
"id",
"pitch",
Expand Down Expand Up @@ -363,8 +363,8 @@ def __getitem__(self, key):
def __setitem__(self, key, value):
if key not in self._accepted_keys:
raise KeyError(f"Key {key} not accepted for PerformedNote")
self._validate_values((key, value))
self.pnote_dict[key] = value
self._validate_values(key)

def __delitem__(self, key):
raise KeyError("Cannot delete items from PerformedNote")
Expand All @@ -381,72 +381,77 @@ def __contains__(self, key):
def copy(self):
return PerformedNote(self.pnote_dict.copy())

def _validate_values(self, key=None):
if key is None:
keys = self.keys()
def _validate_values(self, d):
if isinstance(d, dict):
dd = d
elif isinstance(d, tuple):
dd = {d[0]: d[1]}
else:
keys = [key]
raise ValueError(f"Invalid value {d} provided for PerformedNote")

for key in keys:
for key, value in dd.items():
if key == "pitch":
self._validate_pitch()
self._validate_pitch(value)
elif key == "note_on":
self._validate_note_on()
self._validate_note_on(value)
elif key == "note_off":
self._validate_note_off()
self._validate_note_off(value)
elif key == "velocity":
self._validate_velocity()
self._validate_velocity(value)
elif key == "sound_off":
self._validate_sound_off()
self._validate_sound_off(value)

def _validate_sound_off(self):

def _validate_sound_off(self, value):
if self.get("note_off", -1) < 0:
return
if self.pnote_dict["sound_off"] < self.pnote_dict["note_off"]:
if value < 0:
raise ValueError(f"sound_off must be greater than or equal to 0")
if value < self.pnote_dict["note_off"]:
raise ValueError(f"sound_off must be greater or equal to note_off")

def _validate_note_on(self):
if self.pnote_dict["note_on"] < 0:
def _validate_note_on(self, value):
if value < 0:
raise ValueError(
f"Note on value provided is invalid, must be greater than or equal to 0"
)

def _validate_note_off(self):
def _validate_note_off(self, value):
if self.pnote_dict.get("note_on", -1) < 0:
return
if (
self.pnote_dict["note_off"] < 0
or self.pnote_dict["note_off"] < self.pnote_dict["note_on"]
value < 0
or value < self.pnote_dict["note_on"]
):
raise ValueError(
f"Note off value provided is invalid, "
f"must be greater than or equal to 0 and greater or equal to note_on"
f"must be a positive value greater than or equal to 0 and greater or equal to note_on"
)

def _validate_note_on_tick(self):
if self.pnote_dict["note_on_tick"] < 0:
def _validate_note_on_tick(self, value):
if value < 0:
raise ValueError(
f"Note on tick value provided is invalid, must be greater than or equal to 0"
)

def _validate_note_off_tick(self):
def _validate_note_off_tick(self, value):
if self.pnote_dict.get("note_on_tick", -1) < 0:
return
if (
self.pnote_dict["note_off_tick"] < 0
or self.pnote_dict["note_off_tick"] < self.pnote_dict["note_on_tick"]
value < 0
or value < self.pnote_dict["note_on_tick"]
):
raise ValueError(
f"Note off tick value provided is invalid, "
f"must be greater than or equal to 0 and greater or equal to note_on_tick"
f"must be a positive value greater than or equal to 0 and greater or equal to note_on_tick"
)

def _validate_pitch(self):
if self.pnote_dict["pitch"] > 127 or self.pnote_dict["pitch"] < 0:
def _validate_pitch(self, value):
if value > 127 or value < 0:
raise ValueError(f"pitch must be between 0 and 127")

def _validate_velocity(self):
if self.pnote_dict["velocity"] > 127 or self.pnote_dict["velocity"] < 0:
def _validate_velocity(self, value):
if value > 127 or value < 0:
raise ValueError(f"velocity must be between 0 and 127")


Expand Down

0 comments on commit 702adf7

Please sign in to comment.