]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/musicxml2ly.py
Lilypond-book: Check for dblatex and pdflatex for the regtests
[lilypond.git] / scripts / musicxml2ly.py
index aa91e9fc6857373799bfb02a63e5a7ccc1a17202..88058b17d01eea5341de741ffed52300c1c50252 100644 (file)
@@ -401,6 +401,8 @@ def staff_attributes_to_lily_staff (mxl_attr):
         if staff_lines:
             lines = string.atoi (staff_lines.get_text ())
 
+    # TODO: Handle other staff attributes like staff-space, etc.
+
     staff = None
     if clef_sign == "percussion" and lines == 1:
         staff = musicexp.RhythmicStaff ()
@@ -412,8 +414,11 @@ def staff_attributes_to_lily_staff (mxl_attr):
         staff.string_tunings = staff_attributes_to_string_tunings (attributes)
         # staff.tablature_format = ???
     else:
-        # TODO: Handle case with lines <> 5!
         staff = musicexp.Staff ()
+        # TODO: Handle case with lines <> 5!
+        if (lines != 5):
+            staff.add_context_modification ("\\override StaffSymbol #'line-count = #%s" % lines)
+
 
     return staff
 
@@ -439,9 +444,20 @@ def extract_score_structure (part_list, staffinfo):
         # Finale gives unnamed parts the name "MusicXML Part" automatically!
         if partname and partname.get_text() != "MusicXML Part":
             staff.instrument_name = partname.get_text ()
-        if el.get_maybe_exist_named_child ('part-abbreviation'):
-            staff.short_instrument_name = el.get_maybe_exist_named_child ('part-abbreviation').get_text ()
+        # part-name-display overrides part-name!
+        partname = el.get_maybe_exist_named_child ("part-name-display")
+        if partname:
+            staff.instrument_name = extract_display_text (partname)
+
+        partdisplay = el.get_maybe_exist_named_child ('part-abbreviation')
+        if partdisplay:
+            staff.short_instrument_name = partdisplay.get_text ()
+        # part-abbreviation-display overrides part-abbreviation!
+        partdisplay = el.get_maybe_exist_named_child ("part-abbreviation-display")
+        if partdisplay:
+            staff.short_instrument_name = extract_display_text (partdisplay)
         # TODO: Read in the MIDI device / instrument
+
         return staff
 
     def read_score_group (el):
@@ -580,10 +596,11 @@ def rational_to_lily_duration (rational_len):
     d_log = {1: 0, 2: 1, 4:2, 8:3, 16:4, 32:5, 64:6, 128:7, 256:8, 512:9}.get (rational_len.denominator (), -1)
 
     # Duration of the form 1/2^n or 3/2^n can be converted to a simple lilypond duration
-    if (d_log >= 0 and rational_len.numerator() in (1,3,5,7) ):
+    dots = {1: 0, 3: 1, 7: 2, 15: 3, 31: 4, 63: 5, 127: 6}.get (rational_len.numerator(), -1)
+    if ( d_log >= dots >= 0 ):
         # account for the dots!
-        d.dots = (rational_len.numerator()-1)/2
-        d.duration_log = d_log - d.dots
+        d.duration_log = d_log - dots
+        d.dots = dots
     elif (d_log >= 0):
         d.duration_log = d_log
         d.factor = Rational (rational_len.numerator ())
@@ -914,6 +931,22 @@ def musicxml_transpose_to_lily (attributes):
     transposition.pitch = musicexp.Pitch ().transposed (shift)
     return transposition
 
+def musicxml_staff_details_to_lily (attributes):
+    details = attributes.get_maybe_exist_named_child ('staff-details')
+    if not details:
+        return None
+
+    ## TODO: Handle staff-type, staff-lines, staff-tuning, capo, staff-size
+    ret = []
+
+    stafflines = details.get_maybe_exist_named_child ('staff-lines')
+    if stafflines:
+        lines = string.atoi (stafflines.get_text ());
+        lines_event = musicexp.StaffLinesEvent (lines);
+        ret.append (lines_event);
+
+    return ret;
+
 
 def musicxml_attributes_to_lily (attrs):
     elts = []
@@ -922,16 +955,28 @@ def musicxml_attributes_to_lily (attrs):
         'time': musicxml_time_to_lily,
         'key': musicxml_key_to_lily,
         'transpose': musicxml_transpose_to_lily,
+        'staff-details': musicxml_staff_details_to_lily,
     }
     for (k, func) in attr_dispatch.items ():
         children = attrs.get_named_children (k)
         if children:
             ev = func (attrs)
-            if ev:
+            if isinstance (ev, list):
+              for e in ev:
+                elts.append (e)
+            elif ev:
                 elts.append (ev)
 
     return elts
 
+def extract_display_text (el):
+    child = el.get_maybe_exist_named_child ("display-text")
+    if child:
+        return child.get_text ()
+    else:
+        return False
+
+
 def musicxml_print_to_lily (el):
     # TODO: Implement other print attributes
     #  <!ELEMENT print (page-layout?, system-layout?, staff-layout*,
@@ -953,6 +998,14 @@ def musicxml_print_to_lily (el):
         val = getattr (el, "new-page")
         if (val == "yes"):
             elts.append (musicexp.Break ("pageBreak"))
+    child = el.get_maybe_exist_named_child ("part-name-display")
+    if child:
+        elts.append (musicexp.SetEvent ("Staff.instrumentName",
+                                        "\"%s\"" % extract_display_text (child)))
+    child = el.get_maybe_exist_named_child ("part-abbreviation-display")
+    if child:
+        elts.append (musicexp.SetEvent ("Staff.shortInstrumentName",
+                                        "\"%s\"" % extract_display_text (child)))
     return elts
 
 
@@ -1546,6 +1599,7 @@ def musicxml_direction_to_lily (n):
         if ev:
             # TODO: set the correct direction! Unfortunately, \mark in ly does
             #       not seem to support directions!
+            ev.force_direction = dir
             res.append (ev)
             continue
 
@@ -1814,7 +1868,9 @@ def musicxml_note_to_lily_main_event (n):
         acc = n.get_maybe_exist_named_child ('accidental')
         if acc:
             # let's not force accs everywhere.
-            event.cautionary = acc.editorial
+            event.cautionary = acc.cautionary
+            # TODO: Handle editorial accidentals
+            # TODO: Handle the level-display setting for displaying brackets/parentheses
 
     elif n.get_maybe_exist_typed_child (musicxml.Unpitched):
        # Unpitched elements have display-step and can also have
@@ -2604,7 +2660,7 @@ If the given filename is -, musicxml2ly reads from the command line.
 
     p.version = ('''%prog (LilyPond) @TOPLEVEL_VERSION@\n\n'''
 +
-_ ("""Copyright (c) 2005--2009 by
+_ ("""Copyright (c) 2005--2010 by
     Han-Wen Nienhuys <hanwen@xs4all.nl>,
     Jan Nieuwenhuizen <janneke@gnu.org> and
     Reinhold Kainhofer <reinhold@kainhofer.com>
@@ -2951,7 +3007,7 @@ def main ():
     # Allow the user to leave out the .xml or xml on the filename
     basefilename = args[0].decode('utf-8')
     if basefilename == "-": # Read from stdin
-        basefilename = "-"
+        filename = "-"
     else:
         filename = get_existing_filename_with_extension (basefilename, "xml")
         if not filename: