I am trying to read a midi file and generate another midi with the drum notes only using python midi. The code is the following:
pattern = midi.read_midifile(IN_PATH+file)
out_p = midi.Pattern()
out_t = midi.Track()
out_p.append(out_t)
for track in pattern:
for e in track:
if not(isinstance(e, midi.NoteEvent) and e.channel!=9):
out_t.append(e)
eot = midi.EndOfTrackEvent(tick=1)
out_t.append(eot)
midi.write_midifile(OUT_PATH+file, out_p)
Basically, I am appending just the drum notes and other MIDI events. However, rmoving other notes causes some timing issues because the drum notes appear to be unaligned with the grid when I load them on a DAW. I tried with pattern.make_ticks_abs
but it did not work.
How can I remove undesired notes without timing issues?
Had the opposite problem, of removing all drum lines, but the solution is (using pretty midi)
import pretty_midi as pm
sound = pm.PrettyMIDI("MyMidiSong.midi")
# check all songs, remove the not in case you want to remove all drums
# instead of including all drums
drum_instruments_index = [i for i, inst in enumerate(sound.instruments) if not inst.is_drum]
# remove all non drums, from the sorted such that no conflicting indexes
for i in sorted(drum_instruments_index, reverse=True):
del sound.instruments[i]
sound.write('MyDrumOnlyMidi.midi')
hope this is an acceptabel solution for someone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With