summaryrefslogtreecommitdiff
path: root/midi-keyboard/midikeyboard.py
blob: 36765a601087ed7d74016454c18af2fcbb43eb04 (plain)
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
#!/usr/bin/env python
from evdev import uinput, ecodes as ec
import pygame
import pygame.midi
from pygame.locals import *
import time

pygame.init()
pygame.fastevent.init()
event_get = pygame.fastevent.get
event_post = pygame.fastevent.post
pygame.midi.init()

input_id = 2
print ("using input_id :%s:" % input_id)
i = pygame.midi.Input( input_id )

keymap = {
    84:ec.KEY_ENTER,
    83:ec.KEY_F1,
    81:ec.KEY_F2,
    79:ec.KEY_F3,
    77:ec.KEY_F4,
    76:ec.KEY_F5,
    75:ec.KEY_ESCAPE,
}

with uinput.UInput() as ui:
    going = True
    while going:
        events = event_get()
        for e in events:
            if e.type in [QUIT]:
                going = False
            if e.type in [pygame.midi.MIDIIN]:
                if e.data2>0: status = 1
                else: status = 0
                if keymap.has_key(e.data1):
                    ui.write(ec.EV_KEY, code, status)
                    ui.syn()
                else:
                    print "unknown key",e

        if i.poll():
            midi_events = i.read(10)
            midi_evs = pygame.midi.midis2events(midi_events, i.device_id)
            for m_e in midi_evs:
                event_post( m_e )
    del i

pygame.midi.quit()