2

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.

pc101
  • 66
  • 5

1 Answers1

1

In General MIDI, all percussion events are sent on channel 9. So you have to search for track(s) with events on channel 9. (The stream object probably is not helpful for this).

In that file, it's the track named "Drums". (I don't know why music21 did not pick up the name.)

To extract the acoustic bass drum events, delete all notes that do not use note number 35. (Which is all of them in that file; it actually uses the other bass drum with note number 36.)

CL.
  • 173,858
  • 17
  • 217
  • 259