From 074704b5874eaaaf80cbf7618d4b40c712089a61 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer Date: Sun, 21 Oct 2007 19:04:02 +0200 Subject: [PATCH] MusicXML: Convert words, assigned to staves => assign to notes in Lilypond In MusicXML, words are represented in direction elements, in Lilypond, they are assigned to notes.... Also try to convert as many font attributes as possible (several are still missing, currently only text-size and text-wight are supported) --- python/musicexp.py | 17 +++++++++++++++++ python/musicxml.py | 3 +++ scripts/musicxml2ly.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/python/musicexp.py b/python/musicexp.py index 369542226f..c4ef903a32 100644 --- a/python/musicexp.py +++ b/python/musicexp.py @@ -740,6 +740,23 @@ class DynamicsEvent (Event): printer.dump ("-\\markup{ \\dynamic %s }" % self.type) +class TextEvent (Event): + def __init__ (self): + self.Text = None + self.force_direction = None + self.markup = '' + def wait_for_note (self): + return True + + def direction_mod (self): + return { 1: '^', -1: '_', 0: '-' }.get (self.force_direction, '-') + + def ly_expression (self): + base_string = '%s\"%s\"' + if self.markup: + base_string = '%s\markup{ ' + self.markup + ' {%s} }' + return base_string % (self.direction_mod (), self.text) + class ArticulationEvent (Event): def __init__ (self): self.type = None diff --git a/python/musicxml.py b/python/musicxml.py index 81d2e82465..86eca63bf7 100644 --- a/python/musicxml.py +++ b/python/musicxml.py @@ -780,6 +780,8 @@ class Bend (Music_xml_node): else: return 0 +class Words (Music_xml_node): + pass ## need this, not all classes are instantiated @@ -823,6 +825,7 @@ class_dict = { 'type': Type, 'wavy-line': Wavy_line, 'wedge': Wedge, + 'words': Words, 'work': Work, } diff --git a/scripts/musicxml2ly.py b/scripts/musicxml2ly.py index 26ee3e4abf..b97e156524 100644 --- a/scripts/musicxml2ly.py +++ b/scripts/musicxml2ly.py @@ -620,6 +620,32 @@ def musicxml_dynamics_to_lily_event (dynentry): event.type = dynentry.get_name () return event +def musicxml_words_to_lily_event (words): + event = musicexp.TextEvent () + text = words.get_text () + text = re.sub ('^ *\n? *', '', text) + text = re.sub (' *\n? *$', '', text) + + if hasattr (words, 'default-y'): + if getattr (words, 'default-y') > 0: + event.force_direction = 1 + else: + event.force_direction = -1 + + if hasattr (words, 'font-weight'): + font_weight = { "normal": '', "bold": '\\bold' }.get (getattr (words, 'font-weight'), '') + if font_weight: + event.markup += font_weight + if hasattr (words, 'font-size'): + font_size = { "xx-small": '\\smaller\\smaller\\smaller', "x-small": '\\smaller\\smaller', "small": '\\smaller', "medium": '', "large": '\\large', "x-large": '\\large\\large', "xx-large": '\\large\\large\\large' }.get (getattr (words, 'font-size'), '') + if font_size: + event.markup += font_size + #TODO: Convert the other attributes defined in %text-formatting in common.mod: + # color, font-family + + event.text = text #re.replace (text, "^ *\n? *(.*) *\n? *", "\1") + return event + direction_spanners = [ 'octave-shift', 'pedal', 'wedge' ] @@ -631,12 +657,18 @@ def musicxml_direction_to_lily (n): dirtype_children += dt.get_all_children () for entry in dirtype_children: + if entry.get_name () == "dynamics": for dynentry in entry.get_all_children (): ev = musicxml_dynamics_to_lily_event (dynentry) if ev: res.append (ev) + if entry.get_name () == "words": + ev = musicxml_words_to_lily_event (entry) + if ev: + res.append (ev) + # octave shifts. pedal marks, hairpins etc. are spanners: if entry.get_name() in direction_spanners: event = musicxml_spanner_to_lily_event (entry) -- 2.39.5