Skip to content

Instantly share code, notes, and snippets.

@Dobby233Liu
Last active February 21, 2025 12:48
Show Gist options
  • Save Dobby233Liu/7179f9282fb3ee08007a8da5cba24a96 to your computer and use it in GitHub Desktop.
Save Dobby233Liu/7179f9282fb3ee08007a8da5cba24a96 to your computer and use it in GitHub Desktop.
import mido
import sys
def clean_pitch_bend(input_path, output_path):
mid = mido.MidiFile(input_path)
last_pb = {}
for track in mid.tracks:
new_events = []
delay_acculm = {i: 0 for i in range(16)}
for msg in track:
if isinstance(msg, mido.Message) and msg.type != "sysex":
channel = msg.channel
if msg.is_cc(10) and msg.value == 63:
# BENDY (the plugin I use to generate pitch bends in FL)
# sabotages panning like this for some reason
msg.value += 1
elif msg.type == "pitchwheel":
target_pb = last_pb.get(channel, None)
if target_pb is not None and target_pb.pitch == msg.pitch:
delay_acculm[channel] += msg.time
continue
print(msg.pitch, str(target_pb))
last_pb[channel] = msg
if delay_acculm.get(channel, None) > 0:
msg.time += delay_acculm[channel]
delay_acculm[channel] = 0
new_events.append(msg)
track[:] = new_events
mid.save(output_path)
clean_pitch_bend(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment