/
midi_alsa.py
56 lines (43 loc) · 1.5 KB
/
midi_alsa.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
import alsaseq
CLIENT = 24
class MidiAlsa:
events = {
5: 'note',
6: 'noteon',
7: 'noteoff',
8: 'keypress',
10: 'controller',
11: 'pgmchange',
12: 'chanpress',
13: 'pitchbend',
}
def __init__(self):
print('Using ALSA MIDI interface')
alsaseq.client('Simple', 1, 1, False) # name, ninputports, noutputports, createqueue
alsaseq.connectfrom(0, CLIENT, 0) # inputport, src_client, src_port
self.binds = {}
def bind(self, event, func):
self.binds[event] = func
def start(self):
while True:
if alsaseq.inputpending():
(etype, # enum snd_seq_event_type
flags,
tag,
queue,
timestamp,
source,
destination,
data) = alsaseq.input()
sec, msec = timestamp
sclient, sport = source
dclient, dport = destination
event = self.events[etype] if etype in self.events else etype
if event in ['controller', 'pgmchange', 'chanpress', 'pitchbend']:
channel, _, _, _, *params = data
else:
channel, *params = data
print('{} event, channel {}, params {}, timestamp {}'
.format(event, channel, params, timestamp))
if event in self.binds:
self.binds[event](*params[:2])