-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi_pygame.py
81 lines (62 loc) · 2.29 KB
/
midi_pygame.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
import threading
import pygame.midi as midi
known_devices = [
'Launchkey Mini MIDI 1',
'Launchkey Mini LK Mini MIDI',
]
class MidiPygame:
events = {
8: 'noteoff',
9: 'noteon',
11: 'controller',
12: 'pgmchange',
}
def __init__(self, debug=False):
print('Using Pygame MIDI interface')
self.debug = debug
midi.init()
self.midi_input = None
for device_id in range(midi.get_count()):
interf, name, is_input, is_output, opened = midi.get_device_info(device_id)
interf = interf.decode('utf-8')
name = name.decode('utf-8')
imode = 'input' if is_input else 'output' if is_output else 'none'
iopen = '(open)' if opened else ''
if self.debug:
print(f'{interf} / {name} ({imode}) {iopen}')
if name in known_devices and is_input:
self.midi_input = midi.Input(device_id)
self.midi_input.name = f'{name} ({imode})'
print(f'Using midi input device {name}')
break
self.binds = {}
self.running = True
self.done = False
def bind(self, event, func):
self.binds[event] = func
def start(self, use_thread=True):
def do_loop():
while self.running:
if self.midi_input and self.midi_input.poll():
[[[status, *params], timestamp]] = self.midi_input.read(1)
etype = status >> 4
channel = status & 0xf
event = self.events[etype] if etype in self.events else etype
if self.debug:
print(f'{self.midi_input.name}: {event} <{channel}>, {params} : {timestamp}')
if 'event' in self.binds:
self.binds['event'](event, channel, *params[:2])
if event in self.binds:
self.binds[event](channel, *params[:2])
self.done = True
if use_thread:
mt = threading.Thread(target=do_loop)
mt.start()
else:
do_loop()
def end(self):
self.running = False
while not self.done:
pass
if self.midi_input:
self.midi_input.close()