Skip to content

Commit 205a102

Browse files
Merge branch 'gregc/moreAccurateSpannerStartStop' into gregc/betterPedalMarks
2 parents 577d020 + faed972 commit 205a102

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

music21/musicxml/xmlToM21.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4142,8 +4142,8 @@ def xmlDirectionTypeToSpanners(
41424142
and ottava are encoded as MusicXML directions.
41434143
41444144
:param mxObj: the specific direction element (e.g. <wedge>).
4145-
:param staffKey: staff number (required for <pedal>)
4146-
:param totalOffset: offset in measure of this direction (required for <pedal>)
4145+
:param staffKey: staff number
4146+
:param totalOffset: offset in measure of this direction
41474147
41484148
>>> from xml.etree.ElementTree import fromstring as EL
41494149
>>> MP = musicxml.xmlToM21.MeasureParser()
@@ -4240,7 +4240,6 @@ def xmlDirectionTypeToSpanners(
42404240
(<music21.expressions.PedalBounce at 1.0>, <music21.expressions.PedalGapStart at 2.0>,
42414241
<music21.expressions.PedalGapEnd at 3.5>)
42424242
'''
4243-
targetLast = self.nLast
42444243
returnList = []
42454244

42464245
if totalOffset is not None:
@@ -4259,8 +4258,10 @@ def xmlDirectionTypeToSpanners(
42594258

42604259
if mType != 'stop':
42614260
sp = self.xmlOneSpanner(mxObj, None, spClass, allowDuplicateIds=True)
4261+
start = spanner.SpannerAnchor()
4262+
self.insertCoreAndRef(totalOffset, staffKey, start)
4263+
sp.addSpannedElements(start)
42624264
returnList.append(sp)
4263-
self.spannerBundle.setPendingSpannedElementAssignment(sp, 'GeneralNote')
42644265
else:
42654266
idFound = mxObj.get('number')
42664267
spb = self.spannerBundle.getByClassIdLocalComplete(
@@ -4269,12 +4270,12 @@ def xmlDirectionTypeToSpanners(
42694270
sp = spb[0]
42704271
except IndexError:
42714272
raise MusicXMLImportException('Error in getting DynamicWedges')
4273+
stop = spanner.SpannerAnchor()
4274+
self.insertCoreAndRef(totalOffset, staffKey, stop)
4275+
sp.addSpannedElements(stop)
42724276
sp.completeStatus = True
4273-
# will only have a target if this follows the note
4274-
if targetLast is not None:
4275-
sp.addSpannedElements(targetLast)
42764277

4277-
if mxObj.tag in ('bracket', 'dashes'):
4278+
elif mxObj.tag in ('bracket', 'dashes'):
42784279
mxType = mxObj.get('type')
42794280
idFound = mxObj.get('number')
42804281
if mxType == 'start':
@@ -4290,11 +4291,12 @@ def xmlDirectionTypeToSpanners(
42904291
sp.startTick = mxObj.get('line-end')
42914292
sp.lineType = mxObj.get('line-type') # redundant with setLineStyle()
42924293

4294+
start = spanner.SpannerAnchor()
4295+
self.insertCoreAndRef(totalOffset, staffKey, start)
4296+
sp.addSpannedElements(start)
42934297
self.spannerBundle.append(sp)
42944298
returnList.append(sp)
4295-
# define this spanner as needing component assignment from
4296-
# the next general note
4297-
self.spannerBundle.setPendingSpannedElementAssignment(sp, 'GeneralNote')
4299+
42984300
elif mxType == 'stop':
42994301
# need to retrieve an existing spanner
43004302
# try to get base class of both Crescendo and Decrescendo
@@ -4305,7 +4307,6 @@ def xmlDirectionTypeToSpanners(
43054307
except IndexError:
43064308
warnings.warn('Line <' + mxObj.tag + '> stop without start', MusicXMLWarning)
43074309
return []
4308-
sp.completeStatus = True
43094310

43104311
if mxObj.tag == 'dashes':
43114312
sp.endTick = 'none'
@@ -4317,13 +4318,15 @@ def xmlDirectionTypeToSpanners(
43174318
sp.endHeight = float(height)
43184319
sp.lineType = mxObj.get('line-type')
43194320

4320-
# will only have a target if this follows the note
4321-
if targetLast is not None:
4322-
sp.addSpannedElements(targetLast)
4321+
stop = spanner.SpannerAnchor()
4322+
self.insertCoreAndRef(totalOffset, staffKey, stop)
4323+
sp.addSpannedElements(stop)
4324+
sp.completeStatus = True
4325+
43234326
else:
43244327
raise MusicXMLImportException(f'unidentified mxType of mxBracket: {mxType}')
43254328

4326-
if mxObj.tag == 'octave-shift':
4329+
elif mxObj.tag == 'octave-shift':
43274330
mxType = mxObj.get('type')
43284331
mxSize = mxObj.get('size')
43294332
idFound = mxObj.get('number')
@@ -4342,9 +4345,12 @@ def xmlDirectionTypeToSpanners(
43424345
sp.placement = 'above'
43434346
sp.idLocal = idFound
43444347
sp.type = (mxSize or 8, m21Type)
4348+
start = spanner.SpannerAnchor()
4349+
self.insertCoreAndRef(totalOffset, staffKey, start)
4350+
sp.addSpannedElements(start)
43454351
self.spannerBundle.append(sp)
43464352
returnList.append(sp)
4347-
self.spannerBundle.setPendingSpannedElementAssignment(sp, 'GeneralNote')
4353+
43484354
elif mxType in ('continue', 'stop'):
43494355
spb = self.spannerBundle.getByClassIdLocalComplete(
43504356
'Ottava', idFound, False # get first
@@ -4354,15 +4360,20 @@ def xmlDirectionTypeToSpanners(
43544360
except IndexError:
43554361
raise MusicXMLImportException('Error in getting Ottava')
43564362
if mxType == 'continue':
4357-
self.spannerBundle.setPendingSpannedElementAssignment(sp, 'GeneralNote')
4363+
# is this actually necessary?
4364+
cont = spanner.SpannerAnchor()
4365+
self.insertCoreAndRef(totalOffset, staffKey, cont)
4366+
sp.addSpannedElements(cont)
43584367
else: # if mxType == 'stop':
4368+
stop = spanner.SpannerAnchor()
4369+
self.insertCoreAndRef(totalOffset, staffKey, stop)
4370+
sp.addSpannedElements(stop)
43594371
sp.completeStatus = True
4360-
if targetLast is not None:
4361-
sp.addSpannedElements(targetLast)
4372+
43624373
else:
43634374
raise MusicXMLImportException(f'unidentified mxType of octave-shift: {mxType}')
43644375

4365-
if mxObj.tag == 'pedal':
4376+
elif mxObj.tag == 'pedal':
43664377
mxType = mxObj.get('type')
43674378
mxAbbreviated = mxObj.get('abbreviated')
43684379
mxLine = mxObj.get('line') # 'yes'/'no'
@@ -4397,9 +4408,12 @@ def xmlDirectionTypeToSpanners(
43974408
if mxAbbreviated == 'yes':
43984409
sp.abbreviated = True
43994410

4411+
start = spanner.SpannerAnchor()
4412+
self.insertCoreAndRef(totalOffset, staffKey, start)
4413+
sp.addSpannedElements(start)
44004414
self.spannerBundle.append(sp)
44014415
returnList.append(sp)
4402-
self.spannerBundle.setPendingSpannedElementAssignment(sp, 'GeneralNote')
4416+
44034417
elif mxType in ('continue', 'stop', 'discontinue', 'resume', 'change'):
44044418
spb = self.spannerBundle.getByClassIdLocalComplete(
44054419
'PedalMark', idFound, False # get first
@@ -4415,7 +4429,6 @@ def xmlDirectionTypeToSpanners(
44154429
# important, they should probably end the spanner and start
44164430
# a new one.
44174431
pass
4418-
# self.spannerBundle.setPendingSpannedElementAssignment(sp, 'GeneralNote')
44194432
elif mxType == 'discontinue':
44204433
# insert a PedalGapStart
44214434
pgStart = expressions.PedalGapStart()
@@ -4461,9 +4474,11 @@ def xmlDirectionTypeToSpanners(
44614474
sp.addSpannedElements(pb)
44624475
elif mxType == 'stop':
44634476
# TODO: here we need to see if the 'stop' is actually part of a bounce
4477+
stop = spanner.SpannerAnchor()
4478+
self.insertCoreAndRef(totalOffset, staffKey, stop)
4479+
sp.addSpannedElements(stop)
44644480
sp.completeStatus = True
4465-
if targetLast is not None:
4466-
sp.addSpannedElements(targetLast)
4481+
44674482
else:
44684483
raise MusicXMLImportException(f'unidentified mxType of pedal: {mxType}')
44694484

0 commit comments

Comments
 (0)