Skip to content

Commit

Permalink
Merge pull request #1725 from cuthbertLab/jtw/stem-style-without-dire…
Browse files Browse the repository at this point in the history
…ction

Export stem styles on notes with implicit stem directions
  • Loading branch information
mscuthbert authored Sep 17, 2024
2 parents bbcd2f2 + 08cd3db commit 7726e66
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
24 changes: 17 additions & 7 deletions music21/musicxml/m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4103,17 +4103,27 @@ def noteToXml(self, n: note.GeneralNote, noteIndexInChord=0, chordParent=None):
n = t.cast(note.NotRest, n)
stemDirection = n.stemDirection

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

if stemDirection is not None or stem_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_style:
self.setColor(mxStem, stem_style)
self.setPosition(mxStem, stem_style)

# 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 7726e66

Please sign in to comment.