]> git.donarmstrong.com Git - lilypond.git/commitdiff
MusicXML: Reset measure position on backup elements, print => sys.stderr.write
authorReinhold Kainhofer <reinhold@kainhofer.com>
Fri, 14 Sep 2007 20:59:12 +0000 (22:59 +0200)
committerReinhold Kainhofer <reinhold@kainhofer.com>
Wed, 3 Oct 2007 16:39:14 +0000 (18:39 +0200)
-) Replace all relevant (i.e. non-testing outputs; these are still in there
   in the main() functions for testing) occurrences of print by
   sys.stderr.write
-) If a <backup> element is encountered (e.g. when there are two voices in
   the measure), correctly reset the measure position to the desired
   position after the backup.

Signed-off-by: Reinhold Kainhofer <reinhold@kainhofer.com>
python/musicxml.py
scripts/musicxml2ly.py

index 6234459187c71c7556e7f1e3cf6ae3537c2556f8..b3306f7939e6b435fd655248c1bffded700af785 100644 (file)
@@ -2,6 +2,7 @@ import new
 import string
 from rational import *
 import re
+import sys
 
 def escape_ly_output_string (input_string):
     return_string = input_string
@@ -41,11 +42,11 @@ class Xml_node:
        return ''.join ([c.get_text () for c in self._children])
 
     def message (self, msg):
-        print msg
+        sys.stderr.write (msg)
 
         p = self
         while p:
-            print '  In: <%s %s>' % (p._name, ' '.join (['%s=%s' % item for item in p._attribute_dict.items()]))
+            sys.stderr.write ('  In: <%s %s>' % (p._name, ' '.join (['%s=%s' % item for item in p._attribute_dict.items()])))
             p = p.get_parent ()
         
     def get_typed_children (self, klass):
@@ -81,7 +82,7 @@ class Xml_node:
     def get_unique_typed_child (self, klass):
        cn = self.get_typed_children(klass)
        if len (cn) <> 1:
-           print self.__dict__ 
+           sys.stderr.write (self.__dict__)
            raise 'Child is not unique for', (klass, 'found', cn)
 
        return cn[0]
@@ -231,7 +232,7 @@ class Attributes (Measure_element):
             return (int (beats.get_text ()),
                     int (type.get_text ()))
         except KeyError:
-            print 'error: requested time signature, but time sig unknown'
+            sys.stderr.write ('error: requested time signature, but time sig unknown')
             return (4, 4)
 
     # returns clef information in the form ("cleftype", position, octave-shift)
@@ -320,7 +321,7 @@ class Part_list (Music_xml_node):
         if instrument_name:
             return instrument_name
         else:
-            print "Opps, couldn't find instrument for ID=", id
+            sys.stderr.write ("Opps, couldn't find instrument for ID=%s" % id)
             return "Grand Piano"
         
 class Measure (Music_xml_node):
@@ -477,6 +478,16 @@ class Part (Music_xml_node):
                     last_measure_position = measure_position
                     now += dur
                     measure_position += dur
+                elif dur < Rational (0):
+                    # backup element, reset measure position
+                    now += dur
+                    measure_position += dur
+                    if measure_position < 0:
+                        # backup went beyond the measure start => reset to 0
+                        now -= measure_position
+                        measure_position = 0
+                    last_moment = now
+                    last_measure_position = measure_position
                 if n._name == 'note':
                     instrument = n.get_maybe_exist_named_child ('instrument')
                     if instrument:
index 454c3330fa4b338d6dac0279b2ae966a999757b1..cd9357afb84777094f96f37e814f7a0e5fbb77e8 100644 (file)
@@ -22,7 +22,10 @@ from rational import Rational
 def progress (str):
     sys.stderr.write (str + '\n')
     sys.stderr.flush ()
-    
+
+def error_message (str):
+    sys.stderr.write (str + '\n')
+    sys.stderr.flush ()
 
 # score information is contained in the <work>, <identification> or <movement-title> tags
 # extract those into a hash, indexed by proper lilypond header attributes
@@ -149,7 +152,7 @@ def musicxml_key_to_lily (attributes):
         start_pitch.step = n
         start_pitch.alteration = a
     except  KeyError:
-        print 'unknown mode', mode
+        error_message ('unknown mode %s' % mode)
 
     fifth = musicexp.Pitch()
     fifth.step = 4
@@ -214,7 +217,7 @@ def musicxml_spanner_to_lily_event (mxl_event):
     if func:
         ev = func()
     else:
-        print 'unknown span event ', mxl_event
+        error_message ('unknown span event %s' % mxl_event)
 
 
     type = mxl_event.get_type ()
@@ -224,7 +227,7 @@ def musicxml_spanner_to_lily_event (mxl_event):
     if span_direction != None:
         ev.span_direction = span_direction
     else:
-        print 'unknown span type', type, 'for', name
+        error_message ('unknown span type %s for %s' % (type, name))
 
     ev.set_span_type (type)
     ev.line_type = getattr (mxl_event, 'line-type', 'solid')
@@ -261,7 +264,6 @@ def musicxml_bend_to_lily_event (mxl_event):
     return ev
 
 
-# TODO: Some translations are missing!
 short_articulations_dict = {
   "staccato": ".",
   "tenuto": "-",
@@ -272,6 +274,7 @@ short_articulations_dict = {
   #"portato": "_", # does not exist in MusicXML
     #"fingering": "", # fingering is special cased, as get_text() will be the event's name
 }
+# TODO: Some translations are missing!
 articulations_dict = { 
     ##### ORNAMENTS
     "trill-mark": "trill", 
@@ -433,7 +436,7 @@ def musicxml_note_to_lily_main_event (n):
     return event
 
 
-## todo
+## TODO
 class NegativeSkip:
     def __init__ (self, here, dest):
         self.here = here
@@ -497,7 +500,7 @@ class LilyPondVoiceBuilder:
         diff = moment - current_end
         
         if diff < Rational (0):
-            print 'Negative skip', diff
+            error_message ('Negative skip %s' % diff)
             diff = Rational (0)
 
         if diff > Rational (0):
@@ -578,7 +581,7 @@ def musicxml_voice_to_lily_voice (voice):
             continue
 
         if not n.__class__.__name__ == 'Note':
-            print 'not a Note or Attributes?', n
+            error_message ('not a Note or Attributes? %s' % n)
             continue
 
         rest = n.get_maybe_exist_typed_child (musicxml.Rest)
@@ -631,7 +634,7 @@ def musicxml_voice_to_lily_voice (voice):
                 if s.get_type () in ('start','stop')]
             if slurs:
                 if len (slurs) > 1:
-                    print 'more than 1 slur?'
+                    error_message ('more than 1 slur?')
 
                 lily_ev = musicxml_spanner_to_lily_event (slurs[0])
                 ev_chord.append (lily_ev)
@@ -746,7 +749,7 @@ def musicxml_voice_to_lily_voice (voice):
     
     
     if len (modes_found) > 1:
-       print 'Too many modes found', modes_found.keys ()
+       error_message ('Too many modes found %s' % modes_found.keys ())
 
     return_value = seq_music
     for mode in modes_found.keys ():
@@ -885,10 +888,9 @@ def print_score_setup (printer, part_list, voices):
         part_name = part_definition.id
         part = part_dict.get (part_name)
         if not part:
-            print 'unknown part in part-list:', part_name
+            error_message ('unknown part in part-list: %s' % part_name)
             continue
 
-        # TODO: Apparently this is broken! There is always only one staff...
         nv_dict = voices.get (part)
         staves = reduce (lambda x,y: x+ y,
                 [mxlvoice._staves.keys ()