stwykd
stwykd

Reputation: 2884

How can I get all piano parts from a music21 score?

I'm able to get all the parts in a music21.stream.Score, using

s = music21.stream.Score()
s.getElementsByClass(music21.instrument.Instrument)

or

music21.instrument.partitionByInstrument(s)

But I cannot find a way to check whether each Part is using a Piano instrument. Is there a better way to do that?

Upvotes: 0

Views: 1588

Answers (1)

stwykd
stwykd

Reputation: 2884

To get all piano parts from a music21.stream.Score, you can do this:

from music21 import *
piano_parts = []
score = converter.parse('path/to/midi')
instr = instrument.Piano
for part in instrument.partitionByInstrument(score):
    if isinstance(part.getInstrument(), instr):
        piano_parts.append(part)

You can assign instr to any other music21.instrument to extract other instruments

Upvotes: 1

Related Questions