MusicXML: Convert words, assigned to staves => assign to notes in Lilypond
authorReinhold Kainhofer <reinhold@kainhofer.com>
Sun, 21 Oct 2007 17:04:02 +0000 (19:04 +0200)
committerReinhold Kainhofer <reinhold@kainhofer.com>
Sun, 21 Oct 2007 17:04:02 +0000 (19:04 +0200)
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
python/musicxml.py
scripts/musicxml2ly.py

index 369542226fc21dc9242f3ecdcac70ba0bc1a1f67..c4ef903a32e0a863616203ee89614da4f23854c8 100644 (file)
@@ -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
index 81d2e82465a744814f6c56e728233dfc7cbd3a8d..86eca63bf7b267985a5db85089a2c7f7253de8c2 100644 (file)
@@ -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,
 }
 
index 26ee3e4abf1332c3a1239af4245edb1ab3948f45..b97e1565244ea53687caeb0b1d2ab85629afba24 100644 (file)
@@ -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)