-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi_reverser.py
37 lines (33 loc) · 1.08 KB
/
midi_reverser.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
import midi_wrapper
class MidiReverser:
def __init__(self):
self.midi_stack = []
self.msg = None
def update_msg(self):
# Delta time will be set by the next event
self.msg.time = 0.0
# If note_off, make it note_on
if self.msg.type == 'note_off' or self.msg.velocity == 0:
self.msg.velocity = 80
self.msg.type = 'note_on'
# if note_on, make it note_off
else:
self.msg.velocity = 0
self.msg.type ='note_on'
def dispatch(self, msg):
# This deltatime is written in the msg at the top of the stack
if msg.type != 'note_on' and msg.type != 'note_off':
return
self.msg = midi_wrapper.MidiMessage(
msg.type,
msg.time,
msg.note,
msg.channel,
msg.velocity)
if self.midi_stack:
self.midi_stack[-1].time = self.msg.time
self.update_msg()
self.midi_stack.append(self.msg)
def output(self):
while self.midi_stack:
yield self.midi_stack.pop()