Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion music21/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def __deepcopy__(self, memo=None):
new.instrumentIdRandomize()
return new

def bestName(self):
def bestName(self) -> str|None:
'''
Find a viable name, looking first at instrument, then part, then
abbreviations.
Expand Down
23 changes: 15 additions & 8 deletions music21/musicxml/m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ def __init__(self, score: stream.Score|None = None, makeNotation: bool = True):

self.instrumentList: list[instrument.Instrument] = []
self.instrumentIdList: list[str|None] = []
self.midiChannelList: list[int|None] = []
self.midiChannelList: list[int|None] = [] # TODO: just list[int]?

self.parts: list[stream.Part] = []

Expand Down Expand Up @@ -2596,6 +2596,8 @@ class PartExporter(XMLExporterBase):
in the :class:`~music21.layout.StaffGroup`, if any. (E.g. if this is
the left hand, store a reference to the right hand.)''',
}
midiChannelList: list[int|None] # TODO: just list[int]?
meterStream: stream.Stream[meter.TimeSignatureBase]

def __init__(self,
partObj: stream.Part|stream.Score|None = None,
Expand All @@ -2611,22 +2613,23 @@ def __init__(self,
self.xmlRoot = Element('part')

if parent is None:
self.meterStream: stream.Stream[meter.TimeSignatureBase] = stream.Stream()
self.meterStream = stream.Stream[meter.TimeSignatureBase]()
self.refStreamOrTimeRange = [0.0, 0.0]
self.midiChannelList = []
self.makeNotation = True
else:
# else should not be executed.
self.meterStream = (parent.meterStream
if parent.meterStream is not None
else stream.Stream()) # else should not be executed.
else stream.Stream[meter.TimeSignatureBase]())
self.refStreamOrTimeRange = parent.refStreamOrTimeRange
self.midiChannelList = parent.midiChannelList # shared list
self.makeNotation = parent.makeNotation

self.previousPartStaffInGroup: stream.PartStaff|None = None

self.instrumentStream: stream.Stream[instrument.Instrument]|None = None
self.firstInstrumentObject = None
self.firstInstrumentObject: instrument.Instrument|None = None

# keep track of this so that we only put out new attributes when something
# has changed
Expand Down Expand Up @@ -2737,16 +2740,19 @@ def instrumentSetup(self):
else:
# get a default instrument if not assigned
self.instrumentStream = self.stream.getInstruments(returnDefault=True, recurse=True)
self.firstInstrumentObject = self.instrumentStream[0] # store first, as handled differently
firstInstrumentObject = self.instrumentStream[0] # store first, as handled differently
if t.TYPE_CHECKING:
assert isinstance(firstInstrumentObject, instrument.Instrument)
self.firstInstrumentObject = firstInstrumentObject

if self.parent is not None:
instIdList = [x.partId for x in self.parent.instrumentList]
else:
instIdList = [self.stream.id]

firstInstId = self.firstInstrumentObject.partId
firstInstId = firstInstrumentObject.partId
if firstInstId in instIdList or firstInstId is None: # must have unique ids
self.firstInstrumentObject.partIdRandomize() # set new random id
firstInstrumentObject.partIdRandomize() # set new random id

should_short_circuit = self.mergeInstrumentStreamPartStaffAware()
if should_short_circuit:
Expand All @@ -2769,6 +2775,7 @@ def instrumentSetup(self):

# this is shared among all PartExporters, so long as they are created by a
# ScoreExporter
# TODO: skip if .midiChannel is None?
self.midiChannelList.append(thisInstrument.midiChannel)
# environLocal.printDebug(['midiChannel list', self.midiChannelList])

Expand All @@ -2781,7 +2788,7 @@ def instrumentSetup(self):
# add to the lists for checking on next part
if self.parent is not None:
self.parent.instrumentIdList.append(thisInstrument.instrumentId)
if thisInstrument is self.firstInstrumentObject:
if thisInstrument is firstInstrumentObject:
self.parent.instrumentList.append(thisInstrument)

def mergeInstrumentStreamPartStaffAware(self) -> bool:
Expand Down
Loading