Skip to content

Commit ee3b75e

Browse files
committed
Addition and subtraction for Time.
1 parent 9c76ac7 commit ee3b75e

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

midi.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -440,18 +440,34 @@ def _comparison(self, other, comparison):
440440
return comparison(self.note, other.note)
441441
elif isinstance(other, collections.Iterable):
442442
if len(other) == 3:
443+
for item in other:
444+
if not isinstance(other, numbers.Number):
445+
return NotImplemented
443446
time = Time(specification=self.specification)
444447
time.triple = other
445448
return comparison(self.note, time.note)
446-
elif isinstance(other, numbers.Number):
447-
return comparison(self.note, other)
448449
elif other == None:
449450
if comparison == operator.eq:
450451
return False
451452
elif comparison == operator.ne:
452453
return True
453454
return NotImplemented
454455

456+
def _operation(self, other, operation):
457+
time = Time(specification=self.specification)
458+
if isinstance(other, Time):
459+
time.note = operation(self.note, other.note)
460+
elif isinstance(other, collections.Iterable):
461+
if len(other) == 3:
462+
for item in other:
463+
if not isinstance(other, numbers.Number):
464+
return NotImplemented
465+
time.triple = other
466+
time.note = operatrion(self.note, other.note)
467+
else:
468+
return NotImplemented
469+
return time
470+
455471
def __lt__(self, other):
456472
return self._comparison(other, operator.lt)
457473

@@ -470,6 +486,12 @@ def __ge__(self, other):
470486
def __gt__(self, other):
471487
return self._comparison(other, operator.gt)
472488

489+
def __add__(self, other):
490+
return self._operation(other, operator.add)
491+
492+
def __sub__(self, other):
493+
return self._operation(other, operator.sub)
494+
473495
def __repr__(self):
474496
return str(self)
475497

@@ -549,13 +571,7 @@ def division(self):
549571
@division.setter
550572
def division(self, value):
551573
self._division = value
552-
if self._division != None:
553-
note = 0.0
554-
cumulative = 0
555-
for node in self:
556-
node.cumulative = cumulative + (node.note - note) * node.ppn
557-
cumulative = node.cumulative
558-
note = node.note
574+
self._update_cumulative()
559575

560576
def time(self, time_object):
561577
return self._lookup(time_object.note, 'note')
@@ -566,12 +582,6 @@ def note(self, value):
566582
def cumulative(self, value):
567583
return self._lookup(value, 'cumulative')
568584

569-
def _lookup(self, value, key):
570-
for node in reversed(self):
571-
if node.__dict__[key] <= value:
572-
return node
573-
return None
574-
575585
def events(self, *, track=None):
576586
self.sort()
577587
tempo = None
@@ -613,6 +623,21 @@ def note(node):
613623
key = note
614624
super().sort(key=key, reverse=reverse)
615625

626+
def _lookup(self, value, key):
627+
for node in reversed(self):
628+
if node.__dict__[key] <= value:
629+
return node
630+
return None
631+
632+
def _update_cumulative(self):
633+
if self._division != None:
634+
note = 0.0
635+
cumulative = 0
636+
for node in self:
637+
node.cumulative = cumulative + (node.note - note) * node.ppn
638+
cumulative = node.cumulative
639+
note = node.note
640+
616641

617642
class Event:
618643
"""Base class for MIDI events."""

0 commit comments

Comments
 (0)