Skip to content

Commit

Permalink
MusicXML import: forward tags with voice should trigger the same beha…
Browse files Browse the repository at this point in the history
…vior as note tags with voice.

I see this quite a bit in leadsheets where these are used to position chord changes within a melody note.
  • Loading branch information
gregchapman-dev committed May 6, 2024
1 parent 3fae7a1 commit 85dcabe
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions music21/musicxml/xmlToM21.py
Original file line number Diff line number Diff line change
Expand Up @@ -6202,7 +6202,7 @@ def parseMeasureNumbers(self, mNumRaw=None):
def updateVoiceInformation(self):
# noinspection PyShadowingNames
'''
Finds all the "voice" information in <note> tags and updates the set of
Finds all the "voice" information in <note> and <forward> tags and updates the set of
`.voiceIndices` to be a set of all the voice texts, and if there is
more than one voice in the measure, sets `.useVoices` to True
and creates a voice for each.
Expand Down Expand Up @@ -6231,14 +6231,27 @@ def updateVoiceInformation(self):
2
>>> list(MP.stream.getElementsByClass(stream.Voice))
[<music21.stream.Voice 1>, <music21.stream.Voice 2>]
>>> MP = musicxml.xmlToM21.MeasureParser()
>>> MP.mxMeasure = ET.fromstring('<measure><note><voice>1</voice></note>'
... + '<forward><voice>2</voice></forward></measure>')
>>> MP.updateVoiceInformation()
>>> sorted(list(MP.voiceIndices))
['1', '2']
>>> MP.useVoices
True
>>> len(MP.stream)
2
>>> list(MP.stream.getElementsByClass(stream.Voice))
[<music21.stream.Voice 1>, <music21.stream.Voice 2>]
'''
mxm = self.mxMeasure
for mxn in mxm.findall('note'):
voice = mxn.find('voice')
if vIndex := strippedText(voice):
self.voiceIndices.add(vIndex)
# it is a set, so no need to check if already there
# additional time < 1 sec per ten million ops.
for tagSearch in ('note', 'forward'):
for mxn in mxm.findall(tagSearch):
voice = mxn.find('voice')
if vIndex := strippedText(voice):
self.voiceIndices.add(vIndex)
# it is a set, so no need to check if already there
# additional time < 1 sec per ten million ops.

if len(self.voiceIndices) > 1:
for vIndex in sorted(self.voiceIndices):
Expand Down

0 comments on commit 85dcabe

Please sign in to comment.