Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python3
import mido, sys
mid = mido.MidiFile(sys.argv[1])
new_track = mido.MidiTrack()
sustain = 0
last = 0
t = 0
for ev in mido.merge_tracks(mid.tracks):
t += ev.time
ev.time = t - last
if ev.type in ("time_signature", "set_tempo"):
new_track.append(ev)
last = t
elif ev.type in ("note_on", "note_off"):
new_track.append(ev)
last = t
elif ev.type == "control_change":
if ev.control == 64:
ev.value = 127 if ev.value > 64 else 0
if ev.value != sustain:
new_track.append(ev)
last = t
sustain = ev.value
new_mid = mido.MidiFile(type=0)
new_mid.ticks_per_beat = mid.ticks_per_beat
new_mid.tracks.append(new_track)
new_mid.save(sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment