-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi.py
1316 lines (1099 loc) · 36.6 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
#==============================================================================
#
# $Id$
#
"""
MIDI sequencer and data model module.
"""
#
# Copyright (C) 1999 Michael A. Muller
#
# Permission is granted to use, modify and redistribute this code,
# providing that the following conditions are met:
#
# 1) This copyright/licensing notice must remain intact.
# 2) If the code is modified and redistributed, the modifications must
# include documentation indicating that the code has been modified.
# 3) The author(s) of this code must be indemnified and held harmless
# against any damage resulting from the use of this code.
#
# This code comes with ABSOLUTELY NO WARRANTEE, not even the implied
# warrantee of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# $Log$
# Revision 1.8 2004-05-22 16:03:43 mike
# Bunch of changes that I made a long time ago.
#
# Revision 1.7 1999/09/01 00:17:24 mike
# Made TrackCursor a SeekableEventSource
#
# Revision 1.6 1999/08/31 23:08:18 mike
# Added code to deal with when play is aborted.
#
# Revision 1.5 1999/08/31 22:42:59 mike
# Added time offsets and SeekableEventSources so that we can do random-access
# on MIDI streams.
#
# Revision 1.3 1999/08/23 17:37:59 mike
# Added SysEx events, broke out running status processing and midi stream
# input into StreamReader class, various small fixes.
#
# Revision 1.1.1.1 1999/07/31 00:04:14 mike
# MIDI processing software
#
#
#==============================================================================
import struct, os
from select import select
import fcntl
SNDCTL_SEQ_SYNC = 20737
SNDCTL_SEQ_RESET = 20736
SNDCTL_TMR_START = 21506
SNDCTL_TMR_STOP = 21507
SNDCTL_TMR_TIMEBASE = -1073458175
SNDCTL_TMR_TEMPO = -1073458171
SNDCTL_SEQ_CTRLRATE = -1073458941
# if this is true, all received midi events will be printed
_printEvents = 0
class Event:
"""
A MIDI event. An abstract base class.
Public variables:
/time/::
Absolute time of the event.
"""
def __init__(self, time):
self.time = time
def asMidiString(self, status):
"""
Used to convert the event to a string of bytes suitable for inclusion
in a midi stream of some sort. /status/ is the current, running
status byte.
This method returns a tuple consisting of the new status byte and
the string representation of the event.
"""
raise NotImplementedError()
def __eq__(self, other):
if self is other:
return True
if self.__class__ is not other.__class__:
return False
for attr, val in self.__dict__.items():
if getattr(other, attr, None) != val:
return False
return True
class ChannelEvent(Event):
"""
Abstract base class for all events that apply to a particular channel.
Public variables:
/channel/::
The channel that the event occurred on. An integer from 0-15.
"""
def __init__(self, time, channel):
Event.__init__(self, time)
self.channel = channel
class NoteEvent(ChannelEvent):
"""
Base class for midi "note on" and "note off" events, both of which have
the same public interface.
Public variables:
/note/::
Numeric note (0-127)
/velocity/::
Numeric velocity (0-127)
"""
def __init__(self, time, channel, note, velocity):
ChannelEvent.__init__(self, time, channel)
self.note = note
self.velocity = velocity
class NoteOn(NoteEvent):
"""
Midi "note on" event.
"""
def asMidiString(self, status):
if status == 0x90 | self.channel:
data = struct.pack('BB', self.note, self.velocity)
else:
status = 0x90 | self.channel
data = struct.pack('BBB', status, self.note, self.velocity)
return status, data
def __str__(self):
return 'ch: %d, note %d on, velocity %d' % \
(self.channel, self.note, self.velocity)
def __repr__(self):
return 'NoteOn(%s, %s, %s, %s)' % (self.time, self.channel, self.note,
self.velocity
)
class NoteOff(NoteEvent):
"""
Midi "note off" event.
This may be in conflict with the actual midi spec, but I am assuming
that only a note off with a velocity of 0 can be used in a running
"note on" status (as a "note on" event with a velocity of 0). Any
other velocity value will result in a change in the current status
to "note off" so that the velocity can be reflected.
"""
def asMidiString(self, status):
if status == 0x80 | self.channel:
data = struct.pack('BB', self.note, self.velocity)
elif status == 0x90 | self.channel and self.velocity == 0:
data = struct.pack('BB', self.note, 0)
else:
status = 0x80 | self.channel
data = struct.pack('BBB', status, self.note, self.velocity)
return status, data
def __str__(self):
return 'ch: %d, note %d off, velocity %d' % \
(self.channel, self.note, self.velocity)
def __repr__(self):
return 'NoteOff(%s, %s, %s, %s)' % (self.time, self.channel, self.note,
self.velocity
)
class ProgramChange(ChannelEvent):
"""
Midi "program change" event.
Public variables:
/program/::
New program number (0-127).
"""
def __init__(self, time, channel, program):
ChannelEvent.__init__(self, time, channel)
self.program = program
def asMidiString(self, status):
# since there is no reason to do running program changes on the same
# channel, we just always send a status byte.
status = 0xC0 | self.channel
return status, struct.pack('BB', status, self.program)
def __str__(self):
return 'ch: %d, change to program %d' % \
(self.channel, self.program)
def __repr__(self):
return 'ProgramChange(%s, %s, %s)' % (self.time, self.channel,
self.program
)
class PitchWheel(ChannelEvent):
"""
Midi "pitch wheel" event.
Public variables:
/value/::
Numeric value of the pitch wheel. A value of 0x2000 is centered.
Higher values transpose the pitch up, lower values transpose it down.
"""
def __init__(self, time, channel, value):
ChannelEvent.__init__(self, time, channel)
self.value = value
def asMidiString(self, status):
if status == 0xE0 | self.channel:
data = struct.pack('BB', self.value & 0x7F, self.value >> 7)
else:
status = 0xE0 | self.channel
data = struct.pack('BBB', status, self.value & 0x7F, self.value >> 7)
return status, data
def __str__(self):
return 'ch: %d, pitch wheel to %d' % \
(self.channel, self.value)
def __repr__(self):
return 'PitchWheel(%s, %s, %s)' % (self.time, self.channel, self.value)
class ControlChange(ChannelEvent):
"""
Midi "control change" event.
Public variables:
/controller/::
The controller in question.
/value/::
The new value for the controller.
"""
def __init__(self, time, channel, controller, value):
ChannelEvent.__init__(self, time, channel)
self.controller = controller
self.value = value
def asMidiString(self, status):
if status == 0xB0 | self.channel:
data = struct.pack('BB', self.controller, self.value)
else:
status = 0xB0 | self.channel
data = struct.pack('BBB', status, self.controller, self.value)
return status, data
def __str__(self):
return 'ch: %d, control %d changed to %d' % \
(self.channel, self.controller, self.value)
def __repr__(self):
return 'ControlChange(%s, %s, %s, %s)' % (self.time, self.channel,
self.controller,
self.value
)
class SysEx(Event):
"""
Midi "system exclusive" event. Just a big bag of data passed into
the system.
Public Variables:
/data/::
A string of binary data to be passed into the remote system.
The leading F0 and trailing F7 bytes should be omitted: they will
be generated by the @asMidiString() method.
"""
def __init__(self, time, data):
Event.__init__(self, time)
self.data = data
def asMidiString(self, status):
# XXX not sure if you can do running status with a SysEx event,
# and I'm not taking any chances
return 0xF0, chr(0xF0) + self.data + chr(0xF7)
def __str__(self):
val = ''
for c in self.data:
val = val + ' %0x' % c
return 'SysEx: %s' % val
def __repr__(self):
return 'SysEx(%s, %r)' % (self.time, self.data)
class SysRealtime(Event):
def __init__(self, time):
Event.__init__(self, time)
def asMidiString(self, status):
return 0xFA, chr(0xFA)
def __str__(self):
return self.__class__.__name__
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self.time)
class SysStart(SysRealtime):
_code = 0xFA
class SysContinue(SysRealtime):
_code = 0xFB
class SysStop(SysRealtime):
_code = 0xFC
# Meta-events.
class SetTempo(Event):
def __init__(self, time: int, tempo: int):
super(SetTempo, self).__init__(time)
self.tempo : int = tempo
def asMidiString(self, status):
return 0xFF, struct.unpack(b'BBBBB', 0xFF, 3, self.tempo >> 16,
(self.tempo >> 8) & 0xff,
self.tempo & 0xff
)
def __str__(self):
return f'SetTempo: {self.time} {self.tempo}us/beat'
def __repr__(self):
return f'SetTempo({self.time}, {self.tempo})'
class AllSoundOff(ControlChange):
def __init__(self, time: int, channel: int):
super(AllSoundOff, self).__init__(time, channel, 120, 0)
class AllNotesOff(ControlChange):
def __init__(self, time: int, channel: int):
super(AllNotesOff, self).__init__(time, channel, 123, 0)
# XXX Still need the following classes:
# AfterTouch
# ChannelPressure
# RealTime
class EventSource:
"""
Abstract base class for all event sources.
"""
def __init__(self):
pass
def hasMoreEvents(self):
raise NotImplementedError()
def nextEvent(self):
"""
Returns a *FullEvent* instance for the next available event, returns
*None* if no more events are available.
"""
raise NotImplementedError()
def peekNextEvent(self):
"""
Returns the next event without consuming it. If there is no
next event, returns *None*.
"""
raise NotImplementedError()
class SeekableEventSource(EventSource):
"""
Abstrack class for event sources that are random access: allowing
the user to define the position within the event stream as a midi
clock.
"""
def setPos(self, pos):
"""
Sets the position within the event stream. /pos/ is an
absolute midi clock value.
"""
raise NotImplementedError()
def getPos(self):
"""
Returns the position within the event stream as an absolute value.
"""
raise NotImplementedError()
def getEnd(self):
"""
Returns the position of the last event in the event stream.
"""
raise NotImplementedError()
class Track:
"""
A *Track* is a list of events, sorted in order of their time.
Public variables:
/name/::
Every track can have an associated name. If the tracks is part of
a "piece", use Piece.renameTrack() to rename the track instead
of setting this attribute directly.
A track name must be unique within the piece.
"""
def __init__(self, name = "", events = [], ppqn = 24):
"""
/events/ may be used to construct a track with a prefilled
event list, but the constructor _does not attempt to guarantee
that /events/ is ordered correctly_.
"""
# __events is a list of Event objects
self.__events = events
self.name = name
self.ppqn = ppqn
def add(self, event):
"""
Adds the /event/ (an instance of *Event*) to the track at the given
time.
"""
assert isinstance(event, Event)
if not self.__events:
self.__events.append(event)
elif event.time >= self.__events[-1].time:
self.__events.append(event)
else:
i = 0
for evt in self.__events:
if evt.time > event.time:
self.__events.insert(i, event)
break
i = i + 1
def reposition(self, event):
"""Move 'event' to the correct position based on its time."""
self.__events.remove(event)
self.add(event)
def remove(self, event):
"""Remove the event from the track."""
self.__events.remove(event)
def __getitem__(self, index):
"""
Returns a tuple consisting of the absolute time of the event and the
event itself at the given /index/ in the event list.
"""
return self.__events[index]
def __len__(self):
return len(self.__events)
def __iter__(self):
return iter(self.__events)
def merge(self, other):
"""
Returns a new track consisting of the events of /self/ and /other/
combined.
"""
source = TrackZipper( [self, other] )
return Track(self.name, source.getEvents())
def overwrite(self, other):
"""
Returns a new track which is the current track overwritten with
other using a @TrackOverwriter.
"""
source = TrackOverwriter( [self, other] )
return Track(self.name, source.getEvents())
def getChannel(self):
"""
Returns the channel of the first channel event on the track.
If there are no channel events on the track, returns *None*.
"""
for evt in self.__events:
if isinstance(evt, ChannelEvent):
return evt.channel
return None
class TrackZipper(SeekableEventSource):
def __init__(self, tracks):
self._events = []
tracks = map(TrackCursor, tracks)
event = 1
# build an ordered list of the events in each of the tracks
while event:
# find the track with the next available event
event = None
for track in tracks:
if track.hasMoreEvents():
if not event:
bestTrack = track
event = track.peekNextEvent()
continue
trackEvent = track.peekNextEvent()
if trackEvent.time < event.time:
bestTrack = track
event = trackEvent
if event:
self._addEvent(event, bestTrack, tracks)
bestTrack.nextEvent()
self.__index = 0
self.__pos = 0
def _addEvent(self, event, track, tracks):
self._events.append(event)
def hasMoreEvents(self):
return self.__index < len(self._events)
def nextEvent(self):
if self.__index < len(self._events):
evt = self._events[self.__index]
self.__index = self.__index + 1
try:
self.__pos = self._events[self.__index].time
except IndexError:
self.__pos = evt.time + 1
return evt
else:
return None
def peekNextEvent(self):
if self._events:
return self._events[0]
else:
return None
def getEvents(self):
"""
Returns the entire event list - this is the original, so changing
it changes the instance.
"""
return self._events
def getPos(self):
return self.__pos
def setPos(self, pos):
self.__pos = pos
# if there are no events, this is not an issue
if not self._events:
self.__index = 1
return
# see if it is later than the last event
if pos > self._events[-1].time:
self.__index = len(self._events)
print('pos is %d, index past the end (%d)' %
(pos, self._events[-1].time))
return
# look for it
for i in range(len(self._events)):
if pos <= self._events[i].time:
self.__index = i
print('setting index to %d' % i)
break
else:
# should never get here
assert 0
def getEnd(self):
return self._events[-1].time
class TrackOverwriter(TrackZipper):
"""
This currently implements a "clean-cut" overwriter. Given a list of two
tracks, removes all events from the first track which occur during the
"period of activity" of the second track. Any NoteOn events which
are left dangling are turned off at the beginning of this period,
any NoteOn events which occur during the period that are not turned off
during the period are turned on at the end of the period.
"""
def __init__(self, tracks):
assert len(tracks) == 2
self.__map = [None] * 128
self.__done = 0
TrackZipper.__init__(self, tracks)
def __mapEvent(self, event):
# store the index of the event that turned them on.
if isinstance(event, NoteOn):
self.__map[event.note] = (len(self._events), event.velocity)
print('mapping note on: %d, %d' % (event.time, event.note))
elif isinstance(event, NoteOff):
self.__map[event.note] = None
print('mapping note off: %d, %d' % (event.time, event.note))
def _addEvent(self, event, track, tracks):
if track is tracks[0]:
# keep track of which notes are on and which notes are off,
self.__mapEvent(event)
self._events.append(event)
elif not self.__done:
# turn off all notes that are currently "on"
for i in range(len(self.__map)):
if self.__map[i] is not None:
self._events.append(NoteOff(event.time, event.channel, i, 0))
print('truncating %d' % i)
self.__map[i] = None
# add the new event
self._events.append(event)
# walk through all events on the first track that occur before the
# end of the second track, build a map so that we'll know what to
# turn off.
endTime = track.getTrack()[-1].time
print('end time %d' % endTime)
firstTrack = tracks[0]
while firstTrack.hasMoreEvents():
e = firstTrack.peekNextEvent()
if e.time > endTime:
break
else:
self.__mapEvent(e)
firstTrack.nextEvent()
# add all of the rest of the events for the track
while track.hasMoreEvents():
e = track.nextEvent()
self._events.append(e)
# turn on all notes that are going to be turned off
for i in range(len(self.__map)):
if self.__map[i] is not None:
print('starting %d' % i)
self._events.append(NoteOn(endTime, event.channel, i,
self.__map[i][1]
)
)
class TrackCursor(SeekableEventSource):
"""
A *TrackCursor* is the means by which we iterate over the set of events
in a track.
"""
def __init__(self, track):
EventSource.__init__(self)
self.__track = track
self.__index = 0
self.__pos = 0
def hasMoreEvents(self):
return self.__index < len(self.__track)
def nextEvent(self):
event = self.peekNextEvent()
self.__index = self.__index + 1
if event:
self.__pos = event.time
else:
self.__pos = self.__track[-1].time + 1
return event
def peekNextEvent(self):
try:
return self.__track[self.__index]
except IndexError:
return None
def getTrack(self):
"""
Returns the associated track.
"""
return self.__track
def getPos(self):
return self.__pos
def setPos(self, pos):
self.__pos = pos
if pos > self.__track[-1].time:
self.__index = len(self.__track)
else:
for index in range(len(self.__track)):
if pos <= self.__track[index].time:
self.__index = index
break
else:
assert 0 # should never get here
def getEnd(self):
return self.__track[-1].time
class Piece:
"""
A *Piece* is a collection of tracks.
"""
def __init__(self):
self.__tracks = {}
def addTrack(self, track):
self.__tracks[track.name] = track
def getTracks(self):
return self.__tracks.values()
def getTrack(self, trackName):
return self.__tracks[trackName]
def deleteTrack(self, track):
"""
Deletes a track from the piece. /track/ can be either a @Track
instance or the name of a track.
"""
if isinstance(track, Track):
track = track.name
del self.__tracks[track]
class PieceCursor(TrackZipper):
def __init__(self, piece):
TrackZipper.__init__(self, piece.getTracks())
class StreamReader:
"""
Maintains state information for the parsing of midi commands out of
the sequencer or a midi file.
"""
# statuses
NO_STATE = 0
NOTE_ON = 1
NOTE_OFF = 2
SYS_EX = 3
AFTER_TOUCH = 4
PROGRAM_CHANGE = 5
CHANNEL_PRESSURE = 6
PITCH_WHEEL = 7
CONTROL_CHANGE = 8
def __init__(self):
self.__buf = []
self.__state = self.NO_STATE
self.__channel = 0
# list of events being input
self._inputEvents = []
# the event filter
self.__eventFilter = None
def _processCmd(self, time, cmd):
evt = None
if cmd & 0x80:
highNyb = cmd & 0xF0
if highNyb == 0x80:
self.__state = self.NOTE_OFF
elif highNyb == 0x90:
self.__state = self.NOTE_ON
elif highNyb == 0xA0:
self.__state = self.AFTER_TOUCH
elif highNyb == 0xB0:
self.__state = self.CONTROL_CHANGE
elif highNyb == 0xC0:
self.__state = self.PROGRAM_CHANGE
elif highNyb == 0xD0:
self.__state = self.CHANNEL_PRESSURE
elif highNyb == 0xE0:
self.__state = self.PITCH_WHEEL
elif cmd == 0xF0:
self.__state = self.SYS_EX
self.__buf = ''
elif cmd == 0xF7:
evt = SysEx(time, self.__buf)
self.__buf = []
# change the channel if the status byte has channel information in
# it
if highNyb != 0xF0:
self.__channel = cmd & 0xF
else:
if self.__state == self.NOTE_ON:
if self.__buf:
if cmd == 0:
evt = NoteOff(time, self.__channel, self.__buf[0], 0)
else:
evt = NoteOn(time, self.__channel, self.__buf[0], cmd)
self.__buf = []
else:
self.__buf.append(cmd)
elif self.__state == self.NOTE_OFF:
if self.__buf:
if cmd == 0:
evt = NoteOff(time, self.__channel, self.__buf[0], 0)
else:
evt = NoteOn(time, self.__channel, self.__buf[0], cmd)
self.__buf = []
else:
self.__buf.append(cmd)
elif self.__state == self.PROGRAM_CHANGE:
evt = ProgramChange(time, self.__channel, cmd)
elif self.__state == self.PITCH_WHEEL:
if self.__buf:
val = self.__buf[0] | (cmd << 7)
evt = PitchWheel(time, self.__channel, val)
self.__buf = []
else:
self.__buf.append(cmd)
elif self.__state == self.CONTROL_CHANGE:
if self.__buf:
cntrl = self.__buf[0]
evt = ControlChange(time, self.__channel, cntrl, cmd)
self.__buf = []
else:
self.__buf.append(cmd)
elif self.__state == self.SYS_EX:
self.__buf = self.__buf + chr(cmd)
else:
# XXX this should effectively ignore everything else
pass
if evt:
self._eventRead(evt)
if evt and self.__eventFilter:
oldEvt = evt
evt = self.__eventFilter(evt)
if not evt:
print('event filtered:', oldEvt)
if evt:
self._inputEvents.append(evt)
def setEventFilter(self, filter):
"""
Sets the object or function that is used to translate and filter
events that are read from the midi stream.
/filter/ should be a callable object that accepts an @Event
as a parameter.
It should return the same event, or a translated event, or *None*
if the event is to be discarded.
"""
self.__eventFilter = filter
def _eventRead(self, event):
"""
This is a hook that can be used by derived classes to receive
notification of when a complete event is read.
"""
pass
class Sequencer(EventSource, StreamReader):
"""
Wrapper class around /dev/sequencer.
"""
def __init__(self, device = '/dev/sequencer'):
StreamReader.__init__(self)
self.__src = os.open(device, os.O_RDWR | os.O_NONBLOCK, 0)
# the last status byte written
self.__status = 0
# this is the queue where we store raw data chunks that are ready to
# be written to the sequencer.
self.__outputQueue = []
# this is the current source of output events
self.__outputEvents = None
self.__timeOffset = 0
def __del__(self):
os.close(self.__src)
def hasMoreEvents(self):
pass
def nextEvent(self):
pass
def peekNextEvent(self):
pass
def record(self, control):
if hasattr(self, '__firstTimeThrough'):
self.__reset()
else:
self.__firstTimeThrough = 1
self.__resetTime()
self.__recordOnly(control)
track = Track(events = self._inputEvents)
self._inputEvents = []
return track
def __playAndRecord(self, control):
eventSource = self.__outputEvents
self.__reset()
if isinstance(eventSource, SeekableEventSource):
self.__resetTime(eventSource.getPos())
else:
self.__resetTime()
while 1:
selected = select([self.__src, control], [self.__src], [])
# first check for incoming events
if self.__src in selected[0]:
time, cmd = self.__read()
self._processCmd(time, cmd)
# check to see if the sequencer is ready to accept more events
elif (self.__outputQueue or eventSource.hasMoreEvents()) \
and self.__src in selected[1]:
self.__writeSeqEvent()
# self.__writeEvent(eventSource.nextEvent())
# otherwise, this is an event from the controller or we have
# run out of output events
else:
if isinstance(eventSource, SeekableEventSource):
eventSource.setPos(self.__getPos())
break
# if we terminated with an event from the controller, reset the
# sequencer
if control in selected[0]:
print('resetting')
self.__reset()
return 0
else:
print(selected)
return 1
def playAndRecord(self, eventSource, control):
self.__outputEvents = eventSource
if self.__playAndRecord(control):
# read any remaining events in the input queue
self.__recordOnly(control)
track = Track(events = self._inputEvents)
self._inputEvents = []
return track
def __writeSeqEvent(self):
if not self.__outputQueue:
self.__queueEvent()
if _printEvents:
print('%02x %02x %02x %02x' %
struct.unpack('BBBB', self.__outputQueue[0]))
os.write(self.__src, self.__outputQueue[0])
del self.__outputQueue[0]
def __queueEvent(self):
event = self.__outputEvents.nextEvent()
self.__status, data = event.asMidiString(self.__status)
time = self.__fixTime(event.time)
seqEvt = struct.pack('=BHB', 2, time & 0xFFFF,