I downloaded the midi file from here: Taylor Swift - You Belong With Me.mid
Then I want to only extract the acoustic bass drum part, which means I want the output acoustic bass drum.mid of this song.
I used music21 library to parse the midi file, below is my code:
from music21 import *
import os
fp = os.path.join(common.getSourceFilePath(), 'midi', 'testPrimitive', 'Taylor Swift - You Belong With Me.mid')
mf = midi.MidiFile()
mf.open(fp)
mf.read()
mf.close()
s = midi.translate.midiFileToStream(mf)
partStream = s.parts.stream()
for p in partStream:
print p.partName
Then the output is
Saxophone
None
Electric Bass
None
None
Electric Guitar
None
None
None
Banjo
Banjo
Banjo
StringInstrument
Piano
I don't know which one is percussion... After I know which part is percussion, then I want to extract acoustic bass drum in that part and output it as acoustic bass drum.mid file. Can anyone tell me how to do it? Thank you
[edit] I used below code,
from music21 import *
import os
fp = os.path.join(common.getSourceFilePath(), 'midi', 'testPrimitive', 'Taylor Swift - You Belong With Me.mid')
mf = midi.MidiFile()
mf.open(fp)
mf.read()
mf.close()
for n in range(len(mf.tracks)):
for c in mf.tracks[n].getChannels():
if c == 10:
print n
Then the output is 14, which means mf.tracks[14] is the percussion, then I need to extract the part whose pitch number is 35 or 36.
Then I used below code:
for n in range(len(mf.tracks[14].events)):
if mf.tracks[14].events[n].pitch == 35 or mf.tracks[14].events[n].pitch == 36:
print n
Then there are many outputs. I am thinking about what should I do next.