简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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