简体   繁体   English

使用python midi从midi中提取鼓音符

[英]Extracting drum notes from midi using python midi

I am trying to read a midi file and generate another midi with the drum notes only using python midi.我正在尝试读取一个midi文件并仅使用python midi生成另一个带有鼓音符的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.基本上,我只是附加鼓音符和其他 MIDI 事件。 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.但是,移动其他音符会导致一些时间问题,因为当我将它们加载到 DAW 时,鼓音符似乎与网格不对齐。 I tried with pattern.make_ticks_abs but it did not work.我尝试使用pattern.make_ticks_abs但它没有用。

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)有相反的问题,即删除所有鼓线,但解决方案是(使用漂亮的 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希望这是某人可接受的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM