diff options
-rw-r--r-- | midi-keyboard/midikeyboard.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/midi-keyboard/midikeyboard.py b/midi-keyboard/midikeyboard.py new file mode 100644 index 0000000..36765a6 --- /dev/null +++ b/midi-keyboard/midikeyboard.py @@ -0,0 +1,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() |