@@ -440,18 +440,34 @@ def _comparison(self, other, comparison):
440
440
return comparison (self .note , other .note )
441
441
elif isinstance (other , collections .Iterable ):
442
442
if len (other ) == 3 :
443
+ for item in other :
444
+ if not isinstance (other , numbers .Number ):
445
+ return NotImplemented
443
446
time = Time (specification = self .specification )
444
447
time .triple = other
445
448
return comparison (self .note , time .note )
446
- elif isinstance (other , numbers .Number ):
447
- return comparison (self .note , other )
448
449
elif other == None :
449
450
if comparison == operator .eq :
450
451
return False
451
452
elif comparison == operator .ne :
452
453
return True
453
454
return NotImplemented
454
455
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
+
455
471
def __lt__ (self , other ):
456
472
return self ._comparison (other , operator .lt )
457
473
@@ -470,6 +486,12 @@ def __ge__(self, other):
470
486
def __gt__ (self , other ):
471
487
return self ._comparison (other , operator .gt )
472
488
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
+
473
495
def __repr__ (self ):
474
496
return str (self )
475
497
@@ -549,13 +571,7 @@ def division(self):
549
571
@division .setter
550
572
def division (self , value ):
551
573
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 ()
559
575
560
576
def time (self , time_object ):
561
577
return self ._lookup (time_object .note , 'note' )
@@ -566,12 +582,6 @@ def note(self, value):
566
582
def cumulative (self , value ):
567
583
return self ._lookup (value , 'cumulative' )
568
584
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
-
575
585
def events (self , * , track = None ):
576
586
self .sort ()
577
587
tempo = None
@@ -613,6 +623,21 @@ def note(node):
613
623
key = note
614
624
super ().sort (key = key , reverse = reverse )
615
625
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
+
616
641
617
642
class Event :
618
643
"""Base class for MIDI events."""
0 commit comments