Skip to content

Commit

Permalink
Merge pull request #392 from CPJKU/stem_direction
Browse files Browse the repository at this point in the history
Stem Direction Support
  • Loading branch information
manoskary authored Oct 30, 2024
2 parents 50c52ce + af0f1cb commit 07225bc
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions partitura/io/exportmei.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ def _handle_note(self, note, xml_voice_el):
elif note.tie_prev is not None:
note_el.set("tie", "t")

if note.stem_direction in ["up", "down"]:
note_el.set("stem.dir", note.stem_direction)

if note.alter is not None:
if (
note.step.lower() + ALTER_TO_MEI[note.alter]
Expand Down
4 changes: 4 additions & 0 deletions partitura/io/exportmusicxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ def make_note_el(note, dur, voice, counter, n_of_staves):
if voice not in (None, 0):
etree.SubElement(note_e, "voice").text = "{}".format(voice)

if note.stem_direction is not None:
stem_e = etree.SubElement(note_e, "stem")
stem_e.text = note.stem_direction

if note.fermata is not None:
notations.append(etree.Element("fermata"))

Expand Down
6 changes: 6 additions & 0 deletions partitura/io/importmusicxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,9 @@ def _handle_note(e, position, part, ongoing, prev_note, doc_order, prev_beam=Non
# initialize beam to None
beam = None

# get the stem direction of the note if any
stem_dir = get_value_from_tag(e, "stem", str) or None

# add support of uppercase "ID" tags
note_id = (
get_value_from_attribute(e, "id", str)
Expand Down Expand Up @@ -1270,6 +1273,7 @@ def _handle_note(e, position, part, ongoing, prev_note, doc_order, prev_beam=Non
ornaments=ornaments,
steal_proportion=steal_proportion,
doc_order=doc_order,
stem_direction=stem_dir,
)
if isinstance(prev_note, score.GraceNote) and prev_note.voice == voice:
note.grace_prev = prev_note
Expand Down Expand Up @@ -1306,6 +1310,7 @@ def _handle_note(e, position, part, ongoing, prev_note, doc_order, prev_beam=Non
articulations=articulations,
ornaments=ornaments,
doc_order=doc_order,
stem_direction=stem_dir,
)

if isinstance(prev_note, score.GraceNote) and prev_note.voice == voice:
Expand Down Expand Up @@ -1335,6 +1340,7 @@ def _handle_note(e, position, part, ongoing, prev_note, doc_order, prev_beam=Non
articulations=articulations,
symbolic_duration=symbolic_duration,
doc_order=doc_order,
stem_direction=stem_dir,
)

else:
Expand Down
6 changes: 5 additions & 1 deletion partitura/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,9 @@ class GenericNote(TimedObject):
appearance of this note (with respect to other notes) in the
document in case the Note belongs to a part that was imported
from MusicXML. Defaults to None.
stem_direction : str, optional
The stem direction of the note. Can be 'up', 'down', or None.
Defaults to None.
"""

Expand All @@ -1710,6 +1713,7 @@ def __init__(
articulations=None,
ornaments=None,
doc_order=None,
stem_direction=None,
**kwargs,
):
self._sym_dur = None
Expand All @@ -1721,7 +1725,7 @@ def __init__(
self.articulations = articulations
self.ornaments = ornaments
self.doc_order = doc_order

self.stem_direction = stem_direction if stem_direction in ("up", "down") else None
# these attributes are set after the instance is constructed
self.fermata = None
self.tie_prev = None
Expand Down
1 change: 1 addition & 0 deletions tests/data/musicxml/test_note_ties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<duration>15</duration>
<tie type="start"/>
<voice>1</voice>
<stem>up</stem>
<type>quarter</type>
<dot/>
<notations>
Expand Down
10 changes: 10 additions & 0 deletions tests/data/musicxml/test_unfold_complex_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</pitch>
<duration>1</duration>
<voice>1</voice>
<stem>up</stem>
<type>quarter</type>
</note>
<note>
Expand All @@ -66,6 +67,7 @@
</pitch>
<duration>1</duration>
<voice>1</voice>
<stem>up</stem>
<type>quarter</type>
</note>
</measure>
Expand All @@ -78,6 +80,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -102,6 +105,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -114,6 +118,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand Down Expand Up @@ -144,6 +149,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -164,6 +170,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand Down Expand Up @@ -224,6 +231,7 @@
</pitch>
<duration>1</duration>
<voice>1</voice>
<stem>up</stem>
<type>quarter</type>
</note>
<note>
Expand All @@ -233,6 +241,7 @@
</pitch>
<duration>1</duration>
<voice>1</voice>
<stem>up</stem>
<type>quarter</type>
</note>
</measure>
Expand All @@ -245,6 +254,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand Down
8 changes: 8 additions & 0 deletions tests/data/musicxml/test_unfold_dacapo_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -105,6 +106,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -129,6 +131,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -147,6 +150,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -159,6 +163,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -177,6 +182,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand Down Expand Up @@ -285,6 +291,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -309,6 +316,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand Down
11 changes: 11 additions & 0 deletions tests/data/musicxml/test_unfold_volta_numbers_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</pitch>
<duration>1</duration>
<voice>1</voice>
<stem>up</stem>
<type>quarter</type>
</note>
<note>
Expand All @@ -66,6 +67,7 @@
</pitch>
<duration>1</duration>
<voice>1</voice>
<stem>up</stem>
<type>quarter</type>
</note>
</measure>
Expand All @@ -78,6 +80,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -102,6 +105,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -114,6 +118,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand Down Expand Up @@ -144,6 +149,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -164,6 +170,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand Down Expand Up @@ -200,6 +207,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -224,6 +232,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -236,6 +245,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand All @@ -260,6 +270,7 @@
</pitch>
<duration>2</duration>
<voice>1</voice>
<stem>up</stem>
<type>half</type>
</note>
</measure>
Expand Down
5 changes: 5 additions & 0 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ def test_export_import_slur(self):
part1 = make_part_slur()
self._pretty_export_import_pretty_test(part1)

def test_stem_direction_import(self):
# test if stem direction is imported correctly for the first note of test_note_ties.xml
part = load_musicxml(MUSICXML_IMPORT_EXPORT_TESTFILES[0])[0]
self.assertEqual(part.notes_tied[0].stem_direction, "up")

def _pretty_export_import_pretty_test(self, part1):
# pretty print the part
pstring1 = part1.pretty()
Expand Down

0 comments on commit 07225bc

Please sign in to comment.