lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Some fixes to musicxml2ly


From: Tuukka Verho
Subject: Some fixes to musicxml2ly
Date: Fri, 15 Sep 2006 17:07:33 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Hi!

I noticed that musicxml2ly can't handle files that don't have <voice> tags
telling which voice a note belongs to. It seems that sigle voice scores don't
necessarily have them. I made a modification that makes the script assume that
notes without <voice> tags belong to voice 1. Previously such situations caused
an exception.

Additionally, the script expects that notes in the input file have <type> tags
telling the type of the note. That isn't always the case, so I added some code
to calculate the type of the note by its duration. The number of dots is also
determined by the duration.

With these additions the script can now import files generated by PhotoScore to
some degree.

This is my first contrubution to Lilypond and actually to any open source
project in general, so be gentle :)

The changelog could be something like

2006-09-15  Tuukka Verho  <address@hidden>

   * python/musicxml.py: add support for input files without <voice> and <type>
  tags

   * scripts/musicxml2ly.py: change fuction musicxml_duration_to_ly accordingly

I'm sending from the gmane interface, so the diffs come here in the message 
body:

*************
musicxml2ly.py
*************

--- scripts/musicxml2ly.py      2006-08-26 13:22:56.000000000 +0300
+++ /home/tuukka/musicxml2ly.py 2006-09-15 19:31:59.000000000 +0300
@@ -37,12 +37,8 @@

 def musicxml_duration_to_lily (mxl_note):
     d = musicexp.Duration ()
-    if mxl_note.get_maybe_exist_typed_child (musicxml.Type):
-        d.duration_log = mxl_note.get_duration_log ()
-    else:
-        d.duration_log = 0
-
-    d.dots = len (mxl_note.get_typed_children (musicxml.Dot))
+    d.duration_log = mxl_note.get_duration_log ()
+    d.dots = mxl_note.get_num_dots()
     d.factor = mxl_note._duration / d.get_length ()

     return d


*************
musicxml.py
*************

--- python/musicxml.py  2006-08-26 01:04:08.000000000 +0300
+++ /home/tuukka/musicxml.py    2006-09-15 19:07:23.000000000 +0300
@@ -1,4 +1,5 @@
 import new
+import math
 from rational import *

 class Xml_node:
@@ -193,7 +194,16 @@
                     'long': -2,
                     'whole': 0} [log]
        else:
-           return 0
+           tolerance_exp = -4  # means that notes that are 1/32 or less shorter
than 1/4 are quarter notes etc.
+           return int (math.ceil (-math.log(self._duration) / math.log(2)  -
2**tolerance_exp))
+
+    def get_num_dots(self):
+        if self.get_maybe_exist_typed_child (class_dict['type']):
+            return len (self.get_typed_children (Dot))
+
+        else:
+            factor = self._duration / 2**(-self.get_duration_log())
+            return int (round (-math.log(2 - factor) / math.log(2)))

     def get_factor (self):
        return 1
@@ -351,27 +361,26 @@
            elements.extend (m.get_all_children ())

        start_attr = None
-       for n in elements:
-           voice_id = n.get_maybe_exist_typed_child (class_dict['voice'])
-
-           if not (voice_id or isinstance (n, Attributes)):
-               continue
-
-           if isinstance (n, Attributes) and not start_attr:
-               start_attr = n
-               continue
-
-           if isinstance (n, Attributes):
-               for v in voices.values ():
+       for n in elements:
+           if (isinstance (n, Note)):
+              voice_id = n.get_maybe_exist_typed_child (get_class('voice')) 
+              if voice_id:
+                 id = voice_id.get_text ()
+              else:
+                 id = '1'
+
+              if not voices.has_key (id):
+                voices[id] = Musicxml_voice()
+
+              voices[id].add_element (n)
+
+           elif isinstance (n, Attributes):
+              if not start_attr:
+                 start_attr = n
+
+              for v in voices.values ():
                    v.add_element (n)
-               continue
-
-           id = voice_id.get_text ()
-           if not voices.has_key (id):
-               voices[id] = Musicxml_voice()
-
-           voices[id].add_element (n)
-
+
        if start_attr:
            for (k,v) in voices.items ():
                v.insert (0, start_attr)






reply via email to

[Prev in Thread] Current Thread [Next in Thread]