Skip to content

MusicXML: recognize \break #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ly/musicxml/create_musicxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def create_part(self, name="unnamed", abbr=False, midi=False):
def create_measure(self, **bar_attrs):
"""Create new measure """
self.current_bar = etree.SubElement(self.current_part, "measure", number=str(self.bar_nr))
self.current_print = None
self.bar_nr +=1
if bar_attrs:
self.new_bar_attr(**bar_attrs)
Expand Down Expand Up @@ -627,6 +628,13 @@ def add_lyric(self, txt, syll, nr, ext=False):
if ext:
etree.SubElement(lyricnode, "extend")

def new_system(self):
if not self.current_print:
self.create_print()
self.current_print.attrib["new-system"] = "yes"

def create_print(self):
self.current_print = etree.SubElement(self.current_bar, "print")

##
# Create the XML document
Expand Down
4 changes: 4 additions & 0 deletions ly/musicxml/ly2xml_mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,10 @@ def new_lyrics_item(self, item):
elif item == '\\skip':
self.insert_into.barlist.append("skip")

def line_break(self):
if self.bar:
self.bar.line_break = True

def duration_from_tokens(self, tokens):
"""Calculate dots and multibar rests from tokens."""
dots = 0
Expand Down
2 changes: 2 additions & 0 deletions ly/musicxml/lymus2musxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ def Command(self, command):
if self.tupl_span:
self.mediator.unset_tuplspan_dur()
self.tupl_span = False
elif command.token == '\\break':
self.mediator.line_break()
else:
if command.token not in excls:
print("Unknown command:", command.token)
Expand Down
9 changes: 9 additions & 0 deletions ly/musicxml/xml_objs.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def iterate_part(self, part):
def iterate_bar(self, bar):
"""The objects in the bar are output to the xml-file."""
self.musxml.create_measure()

if bar.line_break:
self.musxml.new_system()

for obj in bar.obj_list:
if isinstance(obj, BarAttr):
self.new_xml_bar_attr(obj)
Expand Down Expand Up @@ -433,6 +437,7 @@ class Bar():
def __init__(self):
self.obj_list = []
self.list_full = False
self.line_break = False

def __repr__(self):
return '<{0} {1}>'.format(self.__class__.__name__, self.obj_list)
Expand Down Expand Up @@ -478,6 +483,10 @@ def inject_voice(self, new_voice, override=False):
""" Adding new voice to bar.
Omitting double or conflicting bar attributes as long as override is false.
Omitting also bars with only skips."""

if new_voice.line_break:
self.line_break = True

if new_voice.obj_list[0].has_attr():
if self.obj_list[0].has_attr():
self.obj_list[0].merge_attr(new_voice.obj_list[0], override)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def test_tuplet():
compare_output('tuplet')


def test_break():
compare_output('break')

def ly_to_xml(filename):
"""Read Lilypond file and return XML string."""
writer = ly.musicxml.writer()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_xml_files/break.ly
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
\score {
\relative c' {
c1 | \break
c | c | \break
c | c | c 1
}
\layout{}
}
97 changes: 97 additions & 0 deletions tests/test_xml_files/break.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN"
"http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="3.0">
<identification>
<encoding>
<software>python-ly 0.9.5</software>
<encoding-date>2017-03-25</encoding-date>
</encoding>
</identification>
<part-list>
<score-part id="P1">
<part-name />
</score-part>
</part-list>
<part id="P1">
<measure number="1">
<attributes>
<divisions>1</divisions>
<time symbol="common">
<beats>4</beats>
<beat-type>4</beat-type>
</time>
<clef>
<sign>G</sign>
<line>2</line>
</clef>
</attributes>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>whole</type>
</note>
</measure>
<measure number="2">
<print new-system="yes" />
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>whole</type>
</note>
</measure>
<measure number="3">
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>whole</type>
</note>
</measure>
<measure number="4">
<print new-system="yes" />
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>whole</type>
</note>
</measure>
<measure number="5">
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>whole</type>
</note>
</measure>
<measure number="6">
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
<voice>1</voice>
<type>whole</type>
</note>
</measure>
</part>
</score-partwise>