Skip to content

Commit

Permalink
Export stem styles on notes with implicit stem directions
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Aug 3, 2024
1 parent 204e9d0 commit b9861ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
23 changes: 16 additions & 7 deletions music21/musicxml/m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4103,17 +4103,26 @@ def noteToXml(self, n: note.GeneralNote, noteIndexInChord=0, chordParent=None):
n = t.cast(note.NotRest, n)
stemDirection = n.stemDirection

if stemDirection is not None:
if stem_has_style := (chordOrN.hasStyleInformation
and isinstance(chordOrN.style, style.NoteStyle)
and chordOrN.style.stemStyle is not None):
note_style = chordOrN.style

if stemDirection is not None or stem_has_style:
mxStem = SubElement(mxNote, 'stem')
sdText = stemDirection
if stemDirection is None:
if closest_clef := chordOrN.getContextByClass(clef.Clef):
sdText = closest_clef.getStemDirectionForPitches(chordOrN.pitches)
else:
sdText = 'up'
else:
sdText = stemDirection
if sdText == 'noStem':
sdText = 'none'
mxStem.text = sdText
if (chordOrN.hasStyleInformation
and isinstance(chordOrN.style, style.NoteStyle)
and chordOrN.style.stemStyle is not None):
self.setColor(mxStem, chordOrN.style.stemStyle)
self.setPosition(mxStem, chordOrN.style.stemStyle)
if stem_has_style:
self.setColor(mxStem, note_style.stemStyle)
self.setPosition(mxStem, note_style.stemStyle)

# end Stem

Expand Down
8 changes: 8 additions & 0 deletions music21/musicxml/test_m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from music21 import repeat
from music21 import spanner
from music21 import stream
from music21 import style
from music21 import tempo

from music21.musicxml import helpers
Expand Down Expand Up @@ -921,6 +922,13 @@ def test_roman_musicxml_two_kinds(self):
self.assertIn('<rest', xmlOut)
self.assertNotIn('<forward>', xmlOut)

def test_stem_style_without_direction(self):
one_note_tune = converter.parse('tinyNotation: 2/4 c2')
half_note = one_note_tune.recurse().notes.first()
half_note.style.stemStyle = style.Style()
half_note.style.stemStyle.color = 'red'
xmlOut = GeneralObjectExporter().parse(one_note_tune).decode('utf-8')
self.assertIn('<stem color="#FF0000">up</stem>', xmlOut)


class TestExternal(unittest.TestCase):
Expand Down

0 comments on commit b9861ad

Please sign in to comment.