Skip to content

Instantly share code, notes, and snippets.

@vsTerminus
Created March 27, 2022 04:11
Show Gist options
  • Save vsTerminus/4c5fe7bf4088245c405e579541bdf52d to your computer and use it in GitHub Desktop.
Save vsTerminus/4c5fe7bf4088245c405e579541bdf52d to your computer and use it in GitHub Desktop.
MIDI to vJoy in FreePIE
import time
def update():
global midiKeysPressed
global buttonChange
global axisIncrement
diagnostics.watch(midi[0].data.channel);
diagnostics.watch(midi[0].data.status);
diagnostics.watch(midi[0].data.buffer[0]);
diagnostics.watch(midi[0].data.buffer[1]);
diagnostics.watch(midi[0].data.timestamp);
# Capture NoteOn and NoteOff events
if(midi[0].data.status == MidiStatus.NoteOn):
mButton=midi[0].data.buffer[0]
if mButton in midiToButton.keys():
# If you are using the mappng below, keep this line uncommented.
vButton=midiToButton[mButton]
# To bypass mapping and just use the raw MIDI outputs as button numbers, comment the line above this one, and uncomment the one below:
#vButton=mButton
vJoy[0].setButton(vButton, True)
# The longer the sleep, the more each hit holds the input.
# Reduce the number if you want more precision
# Increase the number if you don't want to hit the drums so many times.
# Default: 0.075
time.sleep(0.075)
vJoy[0].setButton(vButton, False)
# Optional.
# With mappings you can get away with a 16 button controller.
# If you don't use mappings you'll want to make a 64 or 128 button vJoy controller.
# Not sure if there is any advantage to doing it this way.
# Left column is the MIDI mapping from your drum brain
# Right column is the vJoy button number
if starting:
midiToButton={
38:0, # Snare
48:1, # Tom 1
45:2, # Tom 2
43:3, # Floor Tom
46:4, # Hi-Hat Cymbal
51:5, # Ride Cymbal Zone 1
59:6, # Ride Cymbal Zone 2
57:7, # Crash Cymbal 1
49:8, # Crash Cymbal 2
36:9, # Kick Pedal
44:10,# Hi-Hat Pedal
40:11,# Snare Rim
50:12,# Tom 1 Rim
47:13,# Tom 2 Rim
58:14 # Floor Tom Rim
}
midiKeysPressed=set()
buttonChange=False
lastMessageTimestamp=0
midi[0].update += update
if(not midi[0].data.timestamp == lastMessageTimestamp):
#Set status of all midi keys to buttons
if(buttonChange):
for midiKey, buttonId in midiToButton.items():
if midiKey in midiKeysPressed:
vJoy[0].setButton(buttonId,True)
else:
vJoy[0].setButton(buttonId,False)
buttonChange=False
#Set lastMessageTimeStamp to the currently processed one
lastMessageTimestamp=midi[0].data.timestamp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment