Replies: 1 comment 4 replies
-
Yes! The result would look something like this (Untested code written on the fly): from mido import Message, MidiFile, MidiTrack
# Adjust to taste
NOTE_OFF_DELAY = .1 # 100 ms
# Let's prepare our target file
mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)
# Parse the original file
for msg in MidiFile('orig.mid'):
if msg.type == 'note_on':
if msg.velocity !=0: # 0 velocity messages also means Note Off
# We keep the Note On message
track.append(msg)
# Let's build a Note Off just after the Note On
note_off = Message('note_off', note=msg.note, velocity=0, time=NOTE_OFF_DELAY)
track.append(note_off)
else:
pass # We omit the zero velocity Note On since they're Note Off and we build our own.
elif msg.type != 'note_off': # Let's keep all the rest that is neither Note On nor Note Off
track.append(msg)
# Finally save our modified file
mid.save('mod.mid')
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a MIDI glockenspiel. Because of the way the solenoid drivers are constructed, a note needs to be off for a certain amount of time before it can sound again. Hence, I'd like to import MIDI files and set all note lengths to the same, short value (since the glockenspiel cannot sustain notes).
My idea was to import a MIDI file, convert all message timestamps from relative to absolute, remove all note_off messages, generate all new note_off messages with an absolute timestamp some delta from its corresponding note_on message, sort all messages by absolute timestamp, and recompute the relative timestamps.
Is there a trivial way to accomplish this?
Beta Was this translation helpful? Give feedback.
All reactions