]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/etf2ly.py
patch::: 1.3.96.jcn9
[lilypond.git] / scripts / etf2ly.py
index 1835fd340be29a25cf771c6152ff2c5ec6cf7c0c..563e0308dcb55dae5a4a54d3fb659cfeac1e3a0f 100644 (file)
 #  * beams (better use autobeam?)
 #  * more robust: try entertainer.etf (freenote)
 #  * dynamics
+#  * automatic `deletion' of invalid items
 #
 
+
 program_name = 'etf2ly'
 version = '@TOPLEVEL_VERSION@'
 if version == '@' + 'TOPLEVEL_VERSION' + '@':
@@ -36,10 +38,17 @@ import re
 import string
 import os
 
-finale_clefs= ['treble', 'alto', 'tenor', 'bass', 'percussion', 'treble8vb', 'bass8vb', 'baritone']
+finale_clefs= ['treble', 'alto', 'tenor', 'bass', 'percussion', 'treble_8', 'bass_8', 'baritone']
 
 def lily_clef (fin):
-       return finale_clefs[fin]
+       try:
+               return finale_clefs[fin]
+       except IndexError:
+               sys.stderr.write ( '\nHuh? Found clef number %d\n' % fin)
+
+       return 'treble'
+       
+       
 
 def gulp_file(f):
        return open (f).read ()
@@ -87,6 +96,36 @@ def find_scale (transposition):
        trscale = map(lambda x, k=transposition: transpose(x, k), cscale)
 
        return trscale
+def EDU_to_duration (edu):
+       log = 1
+       d = 4096
+       while d > edu:
+               d = d >> 1
+               log = log << 1
+
+       edu = edu - d
+       dots = 0
+       if edu == d /2:
+               dots = 1
+       elif edu == d*3/4:
+               dots = 2
+       return (log, dots)      
+
+def rat_to_lily_duration (rat):
+       (n,d) = rat
+
+       basedur = 1
+       while d and  d % 2 == 0:
+               basedur = basedur << 1
+               d = d >> 1
+
+       str = 's%d' % basedur
+       if n <> 1:
+               str = str + '*%d' % n
+       if d <> 1:
+               str = str + '/%d' % d
+
+       return str
 
 def gcd (a,b):
        if b == 0:
@@ -126,6 +165,8 @@ def rat_neg (a):
        (p,q) = a
        return (-p,q)
 
+
+
 def rat_subtract (a,b ):
        return rat_add (a, rat_neg (b))
 
@@ -255,6 +296,7 @@ class Articulation:
                        a = articulation_dict[self.type]
                except KeyError:
                        sys.stderr.write ("\nUnknown articulation no. %d on note no. %d" % (self.type, self.notenumber))
+                       sys.stderr.write ("\nPlease add an entry to articulation_dict in the Python source")
                        a = '"art"'
                        
                c.note_suffix = '-' + a + c.note_suffix
@@ -311,24 +353,29 @@ class Measure:
                self.finale = []
                self.global_measure = None
                self.staff = None
+               self.valid = 1
                
        def add_finale_entry (self, entry):
                self.finale.append (entry)
 
+       def valid (self):
+               return self.valid
        def calculate (self):
+               fs = []
+
                if len (self.finale) < 2:
-                       sys.stderr.write ("Measure %d in staff %d  has incomplete information.\n" % (self.number, self.staff.number))
-                       
-                       return 
+                       fs = self.finale[0]
+                       fs = map (string.atoi, list (fs))
+                       self.clef = fs[1]
+                       self.frames = [fs[0]]
+               else:
+                       fs = self.finale[0] + self.finale[1]
                        
-               f0 = self.finale[0]
-               f1 = self.finale[1]
-               
-               self.clef = string.atoi (f0[0])
-               self.flags = string.atoi (f0[1])
-               fs = map (string.atoi, list (f0[2:]) + [f1[0]])
+                       fs = map (string.atoi, list (fs))
+                       self.clef = fs[0]
+                       self.flags = fs[1]
+                       self.frames = fs[2:]
 
-               self.frames = fs
 
 class Frame:
        def __init__ (self, finale):
@@ -413,27 +460,30 @@ class Staff:
                last_clef = None
                gap = (0,1)
                for m in self.measures[1:]:
-                       if not m :
+                       if not m or not m.valid:
                                continue # ugh.
                        
                        g = m.global_measure
                        e = ''
-                       if last_key <> g.keysignature:
+                       if g and last_key <> g.keysignature:
                                e = e + "\\key %s \\major; " % lily_notename (g.keysignature)
                                last_key = g.keysignature
-                       if last_time <> g.timesig :
+                       if g and last_time <> g.timesig :
                                e = e + "\\time %d/%d; " % g.timesig
                                last_time = g.timesig
+
+                       
                        if last_clef <> m.clef :
-                               e = e + '\\clef %s;' % lily_clef (m.clef)
+                               e = e + '\\clef "%s";' % lily_clef (m.clef)
                                last_clef = m.clef
                        if e:
                                if gap <> (0,1):
                                        k = k +' ' + rat_to_lily_duration (gap) + '\n'
                                gap = (0,1)
                                k = k + e
-                       
-                       gap = rat_add (gap, g.length ())
+                               
+                       if g:
+                               gap = rat_add (gap, g.length ())
 
                                
                k = '%sglobal = \\notes  { %s }\n\n ' % (self.staffid (), k)
@@ -450,11 +500,18 @@ class Staff:
                        first_frame = None
                        gap = (0,1)
                        for m in self.measures[1:]:
-                               if not m:
+                               if not m or not m.valid:
+                                       sys.stderr.write ("Skipping non-existant measure")
                                        continue
-                               
-                               
-                               fr = m.frames[x]
+
+                               fr = None
+                               try:
+                                       fr = m.frames[x]
+                               except IndexError:
+                                       
+                                       sys.stderr.write ("Skipping nonexistent frame")
+                                       laystr = laystr + "% FOOBAR ! \n"
+                                       print laystr
                                if fr:
                                        first_frame = fr
                                        if gap <> (0,1):
@@ -462,8 +519,12 @@ class Staff:
                                                gap = (0,1)
                                        laystr = laystr + fr.dump ()
                                else:
-                                       gap = rat_add (gap, m.global_measure.length ())
-
+                                       if m.global_measure :
+                                               gap = rat_add (gap, m.global_measure.length ())
+                                       else:
+                                               sys.stderr.write ( \
+                                                       "No global measure for staff %d measure %d\n"
+                                                       % (self.number, m.number))
                        if first_frame:
                                l = self.layerid (x)
                                laystr = '%s =  \\notes { { %s } }\n\n' % (l, laystr)
@@ -481,37 +542,7 @@ class Staff:
                return str
 
                                
-def EDU_to_duration (edu):
-       log = 1
-       d = 4096
-       while d > edu:
-               d = d >> 1
-               log = log << 1
-
-       edu = edu - d
-       dots = 0
-       if edu == d /2:
-               dots = 1
-       elif edu == d*3/4:
-               dots = 2
-       return (log, dots)      
-
-def rat_to_lily_duration (rat):
-       (n,d) = rat
-
-       basedur = 1
-       while d and  d % 2 == 0:
-               basedur = basedur << 1
-               d = d >> 1
-
-       str = 's%d' % basedur
-       if n <> 1:
-               str = str + '*%d' % n
-       if d <> 1:
-               str = str + '/%d' % d
 
-       return str
-               
 
 class Chord:
        def __init__ (self, finale_entry):
@@ -655,7 +686,7 @@ class Etf_file:
                self.frames = [None]
                self.tuplets = [None]
                self.staffs = [None]
-               self.slurs = [None]
+               self.slur_dict = {}
                self.articulations = [None]
                self.syllables = [None]
                self.verses = [None]
@@ -743,14 +774,13 @@ class Etf_file:
                          = tuple (map (string.atoi, [no,prev,next,dur,pos,extended,follow]))
 
                        entryflag = string.atol (entryflag,16)
-                       if no > len (self.entries):
-                               sys.stderr.write ("\nHuh? Entry number to large,\nexpected %d got %d. Filling with void entries.\n" % (len(self.entries), no  ))
-                               while len (self.entries) <> no:
-                                       c = ((len (self.entries), 0, 0, 0, 0, 0L, 0, 0), [])
-                                       self.entries.append (c)
+                       if len (self.entries) <= no:
+                               # missing entries seem to be quite common.
+                               # we fill'em up with None.
+                               self.entries = self.entries + [None] * (no - len (self.entries) + 1)
                                        
                        current_entry = ((no, prev, next, dur, pos, entryflag, extended, follow), [])
-                       self.entries.append (current_entry)
+                       self.entries[no] = current_entry
                return m
 
        def try_Sx(self,l):
@@ -758,12 +788,16 @@ class Etf_file:
                if m:
                        slurno = string.atoi (m.group (1))
 
-                       if len (self.slurs) == slurno:
-                               self.slurs.append (Slur (slurno))
+                       sl = None
+                       try:
+                               sl = self.slur_dict[slurno]
+                       except KeyError:
+                               sl = Slur (slurno)
+                               self.slur_dict[slurno] = sl
 
                        params = list (m.groups ()[1:])
                        params = map (string.atoi, params)
-                       self.slurs[-1].append_entry (params)
+                       sl.append_entry (params)
 
                return m        
        def try_GF(self, l):
@@ -784,14 +818,13 @@ class Etf_file:
                if m:
                        (frameno, startnote, endnote, foo, bar) = m.groups ()
                        (frameno, startnote, endnote)  = tuple (map (string.atoi, [frameno, startnote, endnote]))
-                       if frameno > len (self.frames):
-                               sys.stderr.write ("Frame no %d missing, filling up to %d\n" % (len(self.frames), frameno))
-                               while frameno <> len (self.frames):
-                                       self.frames.append (Frame ((len (self.frames), 0,0) ))
+                       if len (self.frames) <= frameno:
+                               self.frames = self.frames + [None]  * (frameno - len(self.frames) + 1)
                        
-                       self.frames.append (Frame ((frameno, startnote, endnote)))
+                       self.frames[frameno] = Frame ((frameno, startnote, endnote))
                        
                return m
+       
        def try_MS (self, l):
                m = MSre.match (l)
                if m:
@@ -857,9 +890,13 @@ class Etf_file:
                                if not m:
                                        continue
                                
-                               m.global_measure = self.measures[mno]
                                m.calculate()
-
+                               try:
+                                       m.global_measure = self.measures[mno]
+                               except IndexError:
+                                       sys.stderr.write ("Non-existent global measure %d" % mno)
+                                       continue
+                               
                                frame_obj_list = [None]
                                for frno in m.frames:
                                        fr = self.frames[frno]
@@ -878,15 +915,17 @@ class Etf_file:
                                mno = mno + 1
 
                for c in self.chords[1:]:
-                       c.calculate()
+                       if c:
+                               c.calculate()
 
                for f in self.frames[1:]:
-                       f.calculate ()
+                       if f:
+                               f.calculate ()
                        
                for t in self.tuplets[1:]:
                        t.calculate (self.chords)
                        
-               for s in self.slurs [1:]:
+               for s in self.slur_dict.values():
                        s.calculate (self.chords)
                for s in self.articulations[1:]:
                        s.calculate (self.chords)
@@ -894,7 +933,15 @@ class Etf_file:
        def get_thread (self, startno, endno):
 
                thread = []
-               c = self.chords[startno]
+
+               c = None
+               try:
+                       c = self.chords[startno]
+               except IndexError:
+                       sys.stderr.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno, endno))
+                       return []
+
+               
                while c and c.number () <> endno:
                        thread.append (c)
                        c = c.next
@@ -933,40 +980,48 @@ class Etf_file:
        def unthread_entries (self):
                self.chords = [None]
                for e in self.entries[1:]:
-                       self.chords.append (Chord (e))
-
+                       ch = None
+                       if e:           
+                               ch = Chord (e)
+                       self.chords.append (ch)
+                               
                for e in self.chords[1:]:
+                       if not e:
+                               continue
                        e.prev = self.chords[e.finale[0][1]]
                        e.next = self.chords[e.finale[0][2]]
 
-
-        
-
-
-
 def identify():
        sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
 
 def help ():
-       print r"""
-Convert ETF to LilyPond.
+       print """Usage: etf2ly [OPTION]... ETF-FILE
 
-Usage: etf2ly [OPTION]... ETF-FILE
+Convert ETF to LilyPond.
 
 Options:
-  -h, --help          this help
-  -o, --output=FILE   set output filename to FILE
-  -v, --version       version information
+  -h,--help          this help
+  -o,--output=FILE   set output filename to FILE
+  -v,--version       version information
 
 Enigma Transport Format is a format used by Coda Music Technology's
 Finale product. This program will convert a subset of ETF to a
 ready-to-use lilypond file.
 
+Report bugs to bug-gnu-music@gnu.org
 
+Written by  Han-Wen Nienhuys <hanwen@cs.uu.nl>
 """
 
 def print_version ():
-       print r"""etf2ly (GNU lilypond) %s""" % version
+       print r"""etf2ly (GNU lilypond) %s
+
+This is free software.  It is covered by the GNU General Public License,
+and you are welcome to change it and/or distribute copies of it under
+certain conditions.  Invoke as `midi2ly --warranty' for more information.
+
+Copyright (c) 2000 by Han-Wen Nienhuys <hanwen@cs.uu.nl>
+""" % version
 
 
 
@@ -991,7 +1046,7 @@ for opt in options:
 
 identify()
 
-# header['tagline'] = 'Lily was here %s -- automatically converted from ABC' % version
+e = None
 for f in files:
        if f == '-':
                f = ''