Music21:根据频道号删除打击乐

Music21: remove percussion based on channel number

我有一些 MIDI 文件 [sample],我想从中删除打击乐器。

这是我一直用来读取 midi 文件然后将 midi 保存回磁盘的方法。产生的声音很棒:

path = 'lld.midi'
score = music21.converter.parse(path, 
    forceSource=False, 
    quantizePost=False,
).stripTies(inPlace=True)  
score.write('midi', 'score.midi')

由于打击乐存储在 midi 的通道 10 上,我想我可以用类似的东西去除打击乐:

m = music21.midi.MidiFile()
m.open(path)
m.read()

tracks = []
for track in m.tracks:
    keep = True
    for event in track.events:
        if event.channel == 10:
            keep = False
    if keep: 
        tracks.append(track)
            
s = music21.midi.translate.midiTracksToStreams(tracks, quantizePost=False)
s.write('midi', 'no-percussion.midi')

这确实去除了打击乐,但它似乎也弄乱了音符时序:

我错过了什么?如果其他人可以就如何更正 MidiFile 方法的时间提供建议,我将不胜感激!

上帝保佑,我还需要将 forceSource=False 传递到 midiTracksToStreams 调用中:

m = music21.midi.MidiFile()
m.open(path)
m.read()

tracks = [t for t in m.tracks if not any([e.channel == 10 for e in t.events])]        
score = music21.stream.Score()
music21.midi.translate.midiTracksToStreams(tracks,                                            
    inputM21=score,
    forceSource=False, 
    quantizePost=False,
    ticksPerQuarter=m.ticksPerQuarterNote,
    quarterLengthDivisors=(4,3),
)       
score.write('midi', fp='out.midi')