Skip to content

Instantly share code, notes, and snippets.

@marcan
Created May 30, 2020 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcan/9fa02db56d86b854b799c57e1bf3ad60 to your computer and use it in GitHub Desktop.
Save marcan/9fa02db56d86b854b799c57e1bf3ad60 to your computer and use it in GitHub Desktop.
#!/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