User’s Guide, Chapter 34: Instruments¶
All music is made through instruments (if we consider the human voice
and synthesis types of instruments). Music21 uses instruments in
analysis and in giving hints to output how music should sound.
Instruments live in the music21.instrument module and all
subclass Articulation.
Instruments and Streams¶
Instruments are Music21Object subclasses. Thus,
they live in streams. Generally a part represents music for one
instrument, though it can also represent a single playing performing on
multiple instruments (“doublers” or percussionists playing a kit).
You can iterate through all Instruments in a stream using the same
getElementsByClass() with recurse() filters as for any other
music21 objects, or through the [instrument.Instrument] shortcut:
from music21 import *
# weber = corpus.parse('weber')
lili = corpus.parse('aloha_oe')
for inst in lili[instrument.Instrument]:
print(repr(inst), inst.midiProgram, inst.getContextByClass(stream.Part))
<music21.instrument.Soprano 'P1: Soprano'> 53 <music21.stream.Part Soprano>
<music21.instrument.Alto 'P2: Alto'> 53 <music21.stream.Part Alto>
<music21.instrument.Tenor 'P3: Tenor'> 53 <music21.stream.Part Tenor>
<music21.instrument.Bass 'P4: Bass'> 53 <music21.stream.Part Bass>
<music21.instrument.Vocalist 'P5: Solo Voice'> 53 <music21.stream.Part Solo Voice>
<music21.instrument.Piano 'P6: Piano'> 0 <music21.stream.PartStaff P6-Staff1>
<music21.instrument.Piano 'P6: Piano'> 0 <music21.stream.PartStaff P6-Staff2>
# weber.measures(9, 13).show()
lili.measures(12, 16).show()
lili.measures(12, 16).show('midi')
This MIDI player only runs in trusted notebooks. In JupyterLab: File → Trust Notebook, then re-run this cell.
x = stream.Stream()
x.insert(0, instrument.Clarinet())
x.append(note.Note('C5', type='whole'))
x.show('midi')
This MIDI player only runs in trusted notebooks. In JupyterLab: File → Trust Notebook, then re-run this cell.