-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmidi2keyinput.py
66 lines (57 loc) · 1.74 KB
/
midi2keyinput.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
import sys
import keyboard
import pygame
import pygame.midi
# 以下のキーは好みに応じて変更することが出来ます
midi_to_key = {
47: 'a',
48: 's',
50: 'd',
52: 'SPACE',
53: 'g',
54: 'y',
55: 'h',
56: 'u',
57: 'j',
58: 'i',
59: 'k',
60: 'l',
45: 'SHIFT',
49: 'w',
51: 'f',
46: 'ESCAPE',
44: 'TAB',
43: 'e',
42: 'q',
61: 'o',
63: 'n',
}
def midi_to_keyboard():
pygame.init()
pygame.midi.init()
try:
input_device_id = 1 # デバイスIDは合うものに変えてください
midi_input = pygame.midi.Input(input_device_id)
while True:
if midi_input.poll():
midi_events = midi_input.read(10)
for midi_event in midi_events:
status, note, velocity, _ = midi_event[0]
if status & 0xF0 == 0x90 and velocity > 0:
if note in midi_to_key:
key = midi_to_key[note]
keyboard.press(key)
print(f"MIDI Note On: {note}, Key Down: {key}")
elif status & 0xF0 == 0x80 or (status & 0xF0 == 0x90 and velocity == 0):
if note in midi_to_key:
key = midi_to_key[note]
keyboard.release(key)
# print(f"MIDI Note Off: {note}, Key Up: {key}")
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
finally:
pygame.midi.quit()
pygame.quit()
if __name__ == "__main__":
midi_to_keyboard()