Created
September 29, 2020 04:00
-
-
Save wilm0x42/690b6f7d7699ab9979b534ba50a3ce89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import time | |
from rtmidi.midiutil import open_midiinput | |
from subprocess import call | |
import uinput | |
axis_cal = (0, 255, 0, 0) | |
cstick_cal = (0, 255, 0, 0) | |
trigger_cal = (0, 255, 0, 0) | |
events = ( | |
uinput.BTN_NORTH, | |
uinput.BTN_SOUTH, | |
uinput.BTN_EAST, | |
uinput.BTN_WEST, | |
uinput.BTN_START, | |
uinput.BTN_DPAD_UP, | |
uinput.BTN_DPAD_DOWN, | |
uinput.BTN_DPAD_LEFT, | |
uinput.BTN_DPAD_RIGHT, | |
uinput.ABS_X + axis_cal, | |
uinput.ABS_Y + axis_cal, | |
uinput.ABS_RX + cstick_cal, | |
uinput.ABS_RY + cstick_cal, | |
uinput.BTN_TL, | |
uinput.ABS_Z + trigger_cal, | |
uinput.BTN_TR, | |
uinput.ABS_RZ + trigger_cal, | |
uinput.BTN_TR2 | |
) | |
controller = uinput.Device(events, name="midi2joypad") | |
midi_in, port_name = open_midiinput(None) | |
print("Nice.") | |
digital_map = { | |
24: uinput.BTN_START, # lowest C | |
36: uinput.BTN_DPAD_RIGHT, # low C | |
31: uinput.BTN_DPAD_LEFT, # lowest G | |
76: uinput.BTN_SOUTH, # high E | |
77: uinput.BTN_DPAD_DOWN, # high F | |
72: uinput.BTN_WEST, # high C | |
88: uinput.BTN_TR2, # very high E | |
} | |
try: | |
timer = time.time() | |
while True: | |
msg = midi_in.get_message() | |
if msg: | |
message, deltatime = msg | |
timer += deltatime | |
note = message[1] | |
velocity = message[2] | |
if note in digital_map: | |
value = 1 | |
if velocity == 0: | |
value = 0 | |
print("Output: " + str(digital_map[note]) + " " + str(value)) | |
controller.emit(digital_map[note], value, syn=True) | |
print("[%s] @%0.6f %r" % (port_name, timer, message)) | |
controller.syn() | |
time.sleep(0.008) | |
except KeyboardInterrupt: | |
print("") | |
finally: | |
print("Aaight.") | |
midi_in.close_port() | |
del midi_in |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment