-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.py
1252 lines (931 loc) · 33.7 KB
/
midi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import stderr
import logging
from collections import OrderedDict, defaultdict
import numpy as np
from scipy.interpolate import interp1d
logging.basicConfig()
LOGGER = logging.getLogger(__name__)
from midi_backend.MidiOutStream import MidiOutStream
from midi_backend.MidiInFile import MidiInFile
from midi_backend.MidiOutFile import MidiOutFile
def partition(func, iterable):
"""
Return a dictionary containing the equivalence classes (actually bags)
of `iterable`, partioned according to `func`. The value of a key k is the
list of all elements e from iterable such that `k = func(e)`
"""
result = defaultdict(list)
for v in iterable:
result[func(v)].append(v)
return result
def tic2ms(t, tempo, div):
"""
Convert `t` from midi tics to milliseconds, given `tempo`
(specified as period in microseconds), and beat division `div`
Parameters
----------
t :
tempo :
div :
Returns
-------
"""
return tempo * t / (1000.0 * div)
def tic2beat(t, div):
"""
Convert `t` from midi tics to beats given beat division `div`
Parameters
----------
t : number
div : number
Returns
-------
number
"""
return t / float(div)
# Midi Ontology
class MidiTimeEvent(object):
"""
abstract class for Midi events that have a time stamp.
Parameters
----------
time : number
the time in MIDI units.
"""
def __init__(self, time):
self.time = time
# def __str__(self):
# return 'asdf'
def _send(self, midi):
pass
class MidiMetaEvent(MidiTimeEvent):
"""
abstract class for Midi meta events
"""
def __init__(self, time):
MidiTimeEvent.__init__(self, time)
class EndOfTrackEvent(MidiMetaEvent):
def __init__(self, time):
MidiMetaEvent.__init__(self, time)
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.end_of_track()
class TextEvent(MidiMetaEvent):
"""
Parameters
----------
time :
text :
"""
def __init__(self, time, text):
MidiMetaEvent.__init__(self, time)
self.text = text
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.text(self.text.encode("utf8"))
def __str__(self):
return u'{0} Meta Text "{1}"'.format(self.time, self.text)
class MarkerEvent(MidiMetaEvent):
def __init__(self, time, text):
MidiMetaEvent.__init__(self, time)
self.text = text
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.marker(self.text.encode("utf8"))
def __str__(self):
return u'{0} Meta Marker "{1}"'.format(self.time, self.text)
class TrackNameEvent(MidiMetaEvent):
"""
Parameters
----------
time :
name : str
"""
def __init__(self, time, name):
MidiMetaEvent.__init__(self, time)
self.name = name
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.sequence_name(self.name.encode("utf8"))
def __str__(self):
return u'{0} Meta TrkName "{1}"'.format(self.time, self.name)
class InstrumentNameEvent(MidiMetaEvent):
"""
Parameters
----------
time :
name :
"""
def __init__(self, time, name):
MidiMetaEvent.__init__(self, time)
self.name = name
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.instrument_name(self.name.encode("utf8"))
def __str__(self):
return u'{0} Meta InstrName "{1}"'.format(self.time, self.name)
class TempoEvent(MidiMetaEvent):
def __init__(self, time, val):
MidiMetaEvent.__init__(self, time)
self.tempo = val
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.tempo(self.tempo)
class KeySigEvent(MidiMetaEvent):
def __init__(self, time, key, scale):
MidiMetaEvent.__init__(self, time)
self.key = key
self.scale = scale
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.key_signature(self.key, self.scale)
class TimeSigEvent(MidiMetaEvent):
"""
Parameters
----------
TO DO: check the docstring!
time : number
num : number
the numerator of the time signature
den : number
the denominator of the time signature. CHECK: Is this simply
the note value?! Could be power of two thing where e.g. 3 means
eigth note because 8 = 2^3.
metro : number, optional. Default: 24
the number of MIDI Clock ticks in a metronome click.
thirtysecs : number, optional. Default: 8
the number of 32nd notes in a "MIDI quarter note", i.e. in 24 MIDI
clock ticks. CHECK THIS!
"""
def __init__(self, time, num, den, metro=24, thirtysecs=8):
MidiMetaEvent.__init__(self, time)
self.num = num
self.den = den
self.metro = metro
self.thirtysecs = thirtysecs
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.time_signature(self.num, self.den, self.metro, self.thirtysecs)
class MidiEvent(MidiTimeEvent):
"""
abstract class for Midi events, having a time stamp and a channel
TO DO: check and fix docstring; check for channel number bug ?
Parameters
----------
time : number
channel : number
the MIDI channel number. Should be in the range 0-15. ????
"""
def __init__(self, time, channel):
MidiTimeEvent.__init__(self, time)
self.channel = channel
def _getChannelForSend(self):
"""
Ugly workaround for apparent bug in mxm code:
interprets channels as one lower as supposed to be
"""
# DEBUGGING
# import pdb
#
# if type(self.channel) != int:
# pdb.set_trace()
# NOTE: the problem seems to occur on a PatchChangeEvent!
# There seems to be something wrong with the setting of its
# channel number? CHECK this!
return self.channel - 1
class PatchChangeEvent(MidiEvent):
"""
switch to a certain MIDI patch. A patch is basically equivalent to
one particular instrument (one particular sound).
Parameters
----------
time : number
channel : number
the MIDI channel number.
patch : number
the MIDI patch (program) number to change to.
"""
def __init__(self, time, channel, patch):
MidiEvent.__init__(self, time, channel)
self.patch = patch
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.patch_change(self._getChannelForSend(), self.patch)
def __str__(self):
return '{0} PrCh ch={1} p={2}'.format(self.time, self.channel,
self.patch)
class PitchBendEvent(MidiEvent):
"""
"""
def __init__(self, time, channel, value):
MidiEvent.__init__(self, time, channel)
self.value = value
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.pitch_bend(self._getChannelForSend(), self.value)
class NoteOnOffEvent(MidiEvent):
"""
super class for `NoteOnEvent` and `NoteOffEvent`.
Parameters
----------
time :
ch :
note :
velocity :
MIDI velocity value. Should be in the range 0-127.
"""
def __init__(self, time, ch, note, velocity):
MidiEvent.__init__(self, time, ch)
self.note = note
self.velocity = velocity
class NoteOnEvent(NoteOnOffEvent):
"""
represents a 'note on' command.
"""
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.note_on(channel=self._getChannelForSend(),
note=self.note,
velocity=self.velocity)
def __str__(self):
return '{0} On ch={1} n={2} v={3}'.format(self.time, self.channel,
self.note, self.velocity)
class NoteOffEvent(NoteOnOffEvent):
"""
represents a 'note off' command.
"""
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.note_off(channel=self._getChannelForSend(),
note=self.note,
velocity=self.velocity)
def __str__(self):
return '{0} Off ch={1} n={2} v={3}'.format(self.time, self.channel,
self.note, self.velocity)
class AftertouchEvent(NoteOnOffEvent):
"""
"""
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.aftertouch(channel=self._getChannelForSend(),
note=self.note,
velocity=self.velocity)
class ControllerEvent(MidiEvent):
"""
Parameters
----------
time :
ch :
controller :
value :
"""
def __init__(self, time, ch, controller, value):
MidiEvent.__init__(self, time, ch)
self.controller = controller
self.value = value
def __str__(self):
return '%s Par %s %s %s' % (self.time, self.channel,
self.controller, self.value)
def _send(self, midi):
midi.update_time(self.time, relative=0)
midi.continuous_controller(channel=self._getChannelForSend(),
controller=self.controller,
value=self.value)
class MidiNote(object):
"""
Class that bundles midi on and off events, mostly to have a
convenient way of getting note durations from midi data
Parameters
----------
onset_event :
offset_event :
"""
def __init__(self, onset_event, offset_event):
self._onset = onset_event
self._offset = offset_event
@classmethod
def from_data(cls, onset, note, duration, channel, velocity):
"Initialize MidiNote from note information"
return cls(
NoteOnEvent(time=onset, ch=channel, note=note, velocity=velocity),
NoteOffEvent(time=onset + duration, ch=channel, note=note, velocity=0))
@property
def onset(self):
"""
the onset time of the note
"""
return self._onset.time
@onset.setter
def onset(self, time):
self._onset.time = time
@property
def offset(self):
"""
the onset time of the note
"""
return self._offset.time
@offset.setter
def offset(self, time):
self._offset.time = time
@property
def duration(self):
"""
the onset time of the note
"""
return self.offset - self.onset
@property
def channel(self):
"""
the Midi channel of the note
"""
return self._onset.channel
@channel.setter
def channel(self, ch):
self._onset.channel = ch
self._offset.channel = ch
@property
def note(self):
"""
the Midi pitch of the note
"""
return self._onset.note
@note.setter
def note(self, note):
self._onset.note = note
self._offset.note = note
@property
def velocity(self):
"""
the Midi velocity of the note
"""
return self._onset.velocity
@velocity.setter
def velocity(self, vel):
self._onset.velocity = vel
def __str__(self):
return '%s %s %s %s %s' % (self.onset, self.offset,
self.channel, self.note, self.velocity)
# some events still missing, like aftertouch etc
class MidiFile(object):
"""
TO DO: fix docstring
Parameters
----------
in_file : None OR str, optional. Default: None
zero_vel_on_is_off : optional. Default: None
Attributes
----------
"""
def __init__(self, in_file=None, zero_vel_on_is_off=False):
self.header = None
self.tracks = []
self.zero_vel_on_is_off = zero_vel_on_is_off
if in_file is not None:
self.read_file(in_file)
def summarize(self):
"""
summarize the contents of the MidiFile object, by printing
header information, and summarizing information about each
track
"""
out = [self.header.summarize()]
for t in self.tracks:
out.append(t.summarize())
return '\n'.join(out)
def midi_ticks2seconds(self, times, default_bpm=120):
"""
Convert a sequence of positions expressed in MIDI ticks to
their corresponding times in seconds, taking into account the
tempo events in the MIDI file. If the first tempo event occurs
later than time 0, that tempo event is duplicated at time
0. If the file contains no tempo events at all, the specified
`default_bpm` value is assumed from time 0.
Parameters
----------
times : iterable
A sequence of positions, in MIDI ticks
default_bpm : float (default: 120)
The tempo value to use when no tempo events are present in
the file
Returns
-------
ndarray
An array containing the positions in seconds
"""
return interp1d(*self._time_map(default_bpm), bounds_error=False)(times)
def seconds2midi_ticks(self, times, default_bpm=120):
"""
Convert a sequence of positions expressed in seconds to their
corresponding MIDI ticks, taking into account the tempo events
in the MIDI file. If the first tempo event occurs later than
time 0, that tempo event is duplicated at time 0. If the file
contains no tempo events at all, the specified `default_bpm`
value is assumed from time 0.
Parameters
----------
times : iterable
A sequence of positions, in seconds
default_bpm : float (default: 120)
The tempo value to use when no tempo events are present in
the file
Returns
-------
ndarray
An array containing the positions in MIDI ticks
default_tempo : number, optional. Default: 1000000
the default tempo. The default value is 10^6 microseconds,
i.e. 1 second.
Returns
-------
numpy array
timestamps in seconds. The array should be as long as `times`,
i.e. the same number of timestamps should be returned as were
given.
"""
time_map = self._time_map(default_bpm)
ticks = time_map[0]
seconds = time_map[1]
return interp1d(seconds, ticks, bounds_error=True)(times)
def _time_map(self, default_bpm=100):
"""
Return a map of midi tick times and second times for each
tempo event in the file
"""
first_time = 0
last_time = self.last_time_of(MidiTimeEvent) # self.last_off
events = [(e.time, e.tempo) for t in self.tracks
for e in t.get_events(TempoEvent) if first_time <= e.time < last_time]
if len(events) == 0:
init_tempo = 10**6 * (60. / default_bpm)
last_tempo = init_tempo
else:
init_tempo = events[0][1]
last_tempo = events[-1][1]
events.insert(0, (first_time, init_tempo))
events.append((last_time, last_tempo))
time_tempo = np.array(OrderedDict([e for e in events]).items())
return (time_tempo[:, 0],
(np.r_[0, np.cumsum(time_tempo[:-1, 1] * np.diff(time_tempo[:, 0]))] /
(10**6 * float(self.header.time_division))))
def first_time_of(self, cls):
try:
return np.min([t.first_time_of(cls) for t in self.tracks])
except ValueError:
return np.inf
def last_time_of(self, cls):
try:
return np.max([t.last_time_of(cls) for t in self.tracks])
except ValueError:
return -np.inf
@property
def first_on(self):
return self.first_time_of(NoteOnEvent)
@property
def last_off(self):
return self.last_time_of(NoteOffEvent)
def get_track(self, i=0):
"""
get a specific track, or the first, if no index is specified
Parameters
----------
i : number, optional. Default: 0
the index of the track (a MidiTrack object) to return
Returns
-------
MidiTrack object
# :param i: the index of the track to return
#
# :returns: a MidiTrack object
"""
return self.tracks[i]
def replace_track(self, i, track):
"""
replace an existing track with the given track
Parameters
----------
i : number
the index of the track to be replaced
track : MidiTrack object
a MidiTrack object to replace the i-th track
"""
# self.tracks[n] = track
self.tracks[i] = track
def add_track(self, track):
"""
add a track after (possibly already) existing tracks. A MidiTrack
object is added to `tracks`, which is a list.
Parameters
----------
track: MidiTrack object
track to be added to `tracks` (list).
"""
self.tracks.append(track)
def read_file(self, filename):
"""
instantiate the MidiObject with data read from a file
Parameters
----------
filename : str
the file to read from
# :param filename: the file to read from
"""
self.midi_in = MidiInFile(
_MidiHandler(self, self.zero_vel_on_is_off), filename)
# header and tracks get instantiated through the midi event handler
self.midi_in.read()
def correct_header(self):
n = len(self.tracks)
if n > 1:
if self.header.format == 0:
LOGGER.warning(
'Multiple tracks found, changing Midi file format from type 0 to type 1')
self.header.format = 1
if self.header.number_of_tracks is not n:
if self.header.number_of_tracks is not None:
# wrong nr of tracks specified, warn
LOGGER.warning(('Header specification does not match '
'number of tracks ({0} vs. {1}), correcting header'
'').format(self.header.number_of_tracks, n))
self.header.number_of_tracks = n
def write_file(self, filename):
"""
write the MidiObject to a file
Parameters
----------
filename : str
the file to write the data to (may include the path to
a folder/directory).
"""
# DEBUGGING
count = 0
for track in self.tracks:
# track.summarize() # this doesn't really do anything?
myTrack = self.get_track()
myTrack.summarize()
# print(('info on track number {0}').format(count))
count = count + 1
midi = MidiOutFile(filename)
self.correct_header()
self.header._send(midi)
# `self.tracks` is a list of MidiTrack objects that were
# previously added (via the 'add_track()' method)
# DEBUGGING
count = 0
for track in self.tracks:
track.summarize()
track._send(midi)
# print(('successfully wrote track number {0}').format(count))
count = count + 1
# DEBUGGING TG
# [track._send(midi) for track in self.tracks] # SOMEWHERE HERE is the problem
midi.eof() # NOTE: where is this defined?
class MidiHeader(object):
"""
Class representing MIDI header information.
Parameters
----------
format : specify MIDI format 0, 1, or 2
number_of_tracks : number of tracks, optional. Default: 0
time_division : number, optional. Default: 480
"""
def __init__(self, format, number_of_tracks=0, time_division=480):
self.format = format
self.number_of_tracks = number_of_tracks
self.time_division = time_division
def __str__(self):
return 'MFile %d %d %d\n' % (self.format, self.number_of_tracks,
self.time_division)
def _send(self, midi):
midi.header(format=self.format,
nTracks=self.number_of_tracks,
division=self.time_division)
def summarize(self):
out = "Midi Header\n"
out += " Format: %s\n" % self.format
out += " Nr. of Tracks: %s\n" % self.number_of_tracks
out += " Division: %s\n" % self.time_division
return out
class MidiTrack(object):
"""
Class representing MIDI track information. Midi events are in a stored in a
list called `events`. This class offers several convenience methods, for
example filtering events by type, summarizing the events in the track,
combining MidiOn/MidiOff events into a list of notes, homophonic slicing,
etc.
Parameters
----------
events : list, optional. Default: empty list []
"""
def __init__(self, events=[]):
self.events = events
def summarize(self):
"""
print information about the track, such as its name, the
number of events/metaevents, channels, and the types of events
"""
events = partition(
lambda x: isinstance(x, MidiMetaEvent), self.get_events())
ev = list(set([e.__class__.__name__ for e in events.get(False, [])]))
mev = list(set([e.__class__.__name__ for e in events.get(True, [])]))
midiChannels = list(set([e.channel for e in events.get(False, [])]))
tname = self.get_events(TrackNameEvent)
out = "Midi Track\n"
if len(tname) > 0:
out += " Track Name: %s\n" % tname[0].name
out += " Nr. of Events: %s\n" % len(events.get(False, []))
out += " Nr. of MetaEvents: %s\n" % len(events.get(True, []))
out += " Event Channels: %s\n" % midiChannels
out += " Events: %s\n" % ev
out += " Meta Events: %s\n" % mev
return out
def add_event(self, event):
"""
add `event` to the track
"""
self.events.append(event)
def close(self):
"""
sort events and add an EndOfTrack event to the track if it's missing
"""
self.sort_events()
endTime = int(
self.get_events()[-1].time if len(self.get_events()) > 0 else 0)
if len(self.get_events()) == 0 or not isinstance(self.get_events()[-1], EndOfTrackEvent):
self.add_event(EndOfTrackEvent(endTime))
def sort_events(self):
"""
Sort the events in the track according to their time stamp
"""
self.events.sort(key=lambda x: x.time)
def get_events(self, event_type=None, filters=None):
"""get all events of type event_type from track that
return True on all filter predicates
Parameters
----------
event_type : (sub) class of MidiEvent to be retrieved: None
filters : list of predicates P to test. An event e is returned when p(e)
is True for all p in P: None
Returns
-------
result :
"""
result = self.events
if not event_type is None:
result = [e for e in result if isinstance(e, event_type)]
if not filters is None:
result = [e for e in result if all([f(e) for f in filters])]
return result
def first_time_of(self, cls):
"""Return the time of the first occurrence of an event of type `cls`, or np.inf
if there is none.
"""
try:
return np.min([x.time for x in self.get_events(cls)])
except ValueError:
return np.inf
def last_time_of(self, cls):
"""Return the time of the last occurrence of an event of type `cls`, or -np.inf
if there is none.
"""
try:
return np.max([x.time for x in self.get_events(cls)])
except ValueError:
return -np.inf
@property
def first_on(self):
"""The time of the first NoteOnEvent
"""
return self.first_time_of(NoteOnEvent)
@property
def last_off(self):
"""The time of the last NoteOffEvent
"""
return self.first_time_of(NoteOffEvent)
def _send(self, midi):
midi.start_of_track()
self.close()
[e._send(midi) for e in self.get_events()]
@property
def on_offs(self):
"""
all NoteOnEvents and NoteOffEvents from track
"""
onoffs = self.get_events(NoteOffEvent) + self.get_events(NoteOnEvent)
onoffs.sort(key=lambda x: x.time)
return onoffs
@property
def on_off_eq_classes(self):
"""
all NoteOnEvents and NoteOffEvents from track, grouped by time
"""
return partition(lambda x: x.time, self.on_offs)
@property
def notes(self):
"""
a list of MidiNotes