Created
May 30, 2020 11:28
-
-
Save marcan/9fa02db56d86b854b799c57e1bf3ad60 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/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