]> git.donarmstrong.com Git - lilypond.git/blob - scripts/etf2ly.py
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / scripts / etf2ly.py
1 #!@TARGET_PYTHON@
2
3 # info mostly taken from looking at files. See also
4 # http://lilypond.org/wiki/?EnigmaTransportFormat
5
6 # This supports
7 #
8 #  * notes
9 #  * rests
10 #  * ties
11 #  * slurs
12 #  * lyrics
13 #  * articulation
14 #  * grace notes
15 #  * tuplets
16 #
17
18 # todo:
19 #  * slur/stem directions
20 #  * voices (2nd half of frame?)
21 #  * more intelligent lyrics
22 #  * beams (better use autobeam?)
23 #  * more robust: try entertainer.etf (freenote)
24 #  * dynamics
25 #  * empty measures (eg. twopt03.etf from freenote)
26 #
27
28
29 import __main__
30 import getopt
31 import sys
32 import re
33 import string
34 import os
35
36 program_name = sys.argv[0]
37
38 version = '@TOPLEVEL_VERSION@'
39 if version == '@' + 'TOPLEVEL_VERSION' + '@':
40     version = '(unknown version)'           # uGUHGUHGHGUGH
41
42 """
43 @relocate-preamble@
44 """
45
46 ################################################################
47
48 import lilylib as ly
49 _ = ly._
50  
51 finale_clefs= ['treble', 'alto', 'tenor', 'bass', 'percussion', 'treble_8', 'bass_8', 'baritone']
52
53 def lily_clef (fin):
54     try:
55         return finale_clefs[fin]
56     except IndexError:
57         sys.stderr.write ( '\nHuh? Found clef number %d\n' % fin)
58
59     return 'treble'
60     
61     
62
63 def gulp_file(f):
64     return open (f).read ()
65
66 # notename 0 == central C
67 distances = [0, 2, 4, 5, 7, 9, 11, 12]
68 def semitones (name, acc):
69     return (name / 7 ) * 12 + distances[name % 7] + acc
70
71 # represent pitches as (notename, alteration), relative to C-major scale
72 def transpose(orig, delta):
73     (oname, oacc) = orig
74     (dname, dacc) = delta
75     
76     old_pitch =semitones (oname, oacc)
77     delta_pitch = semitones (dname, dacc)
78     nname = (oname + dname) 
79     nacc = oacc
80     new_pitch = semitones (nname, nacc) 
81
82     nacc = nacc - (new_pitch - old_pitch - delta_pitch)
83
84     return (nname, nacc)
85
86
87
88 def interpret_finale_key_sig (finale_id):
89     """
90 find the transposition of C-major scale that belongs here.
91
92 we are not going to insert the correct major/minor, we only want to
93 have the correct number of accidentals
94 """
95
96     p = (0,0)
97
98     
99     bank_number = finale_id >> 8
100     accidental_bits = finale_id & 0xff
101
102     if 0 <= accidental_bits < 7:
103         while accidental_bits > 0:
104             p = transpose (p, (4,0)) # a fifth up
105             accidental_bits = accidental_bits - 1
106     elif 248 < accidental_bits <= 255:
107         while accidental_bits < 256:
108             p = transpose (p, (3,0))
109             accidental_bits = accidental_bits + 1
110
111     if bank_number == 1:
112         # minor scale
113         p = transpose (p, (5, 0))
114     p  = (p[0] % 7, p[1])
115
116     return KeySignature (p, bank_number)
117
118 # should cache this.
119 def find_scale (keysig):
120     cscale = map (lambda x: (x,0), range (0,7))
121 #        print "cscale: ", cscale
122     ascale = map (lambda x: (x,0), range (-2,5))
123 #        print "ascale: ", ascale
124     transposition = keysig.pitch
125     if keysig.sig_type == 1:
126         transposition = transpose(transposition, (2, -1))
127         transposition = (transposition[0] % 7, transposition[1])
128         trscale = map(lambda x, k=transposition: transpose(x, k), ascale)
129     else:
130         trscale = map(lambda x, k=transposition: transpose(x, k), cscale)
131 #        print "trscale: ", trscale
132     return trscale
133
134 def EDU_to_duration (edu):
135     log = 1
136     d = 4096
137     while d > edu:
138         d = d >> 1
139         log = log << 1
140
141     edu = edu - d
142     dots = 0
143     if edu == d /2:
144         dots = 1
145     elif edu == d*3/4:
146         dots = 2
147     return (log, dots)        
148
149 def rational_to_lily_skip (rat):
150     (n,d) = rat
151
152     basedur = 1
153     while d and  d % 2 == 0:
154         basedur = basedur << 1
155         d = d >> 1
156
157     str = 's%d' % basedur
158     if n <> 1:
159         str = str + '*%d' % n
160     if d <> 1:
161         str = str + '/%d' % d
162
163     return str
164
165 def gcd (a,b):
166     if b == 0:
167         return a
168     c = a
169     while c: 
170         c = a % b
171         a = b
172         b = c
173     return a
174     
175
176 def rat_simplify (r):
177     (n,d) = r
178     if d < 0:
179         d = -d
180         n = -n
181     if n == 0:
182         return (0,1)
183     else:
184         g = gcd (n, d)
185         return (n/g, d/g)
186     
187 def rat_multiply (a,b):
188     (x,y) = a
189     (p,q) = b
190
191     return rat_simplify ((x*p, y*q))
192
193 def rat_add (a,b):
194     (x,y) = a
195     (p,q) = b
196
197     return rat_simplify ((x*q + p*y, y*q))
198
199 def rat_neg (a):
200     (p,q) = a
201     return (-p,q)
202
203
204
205 def rat_subtract (a,b ):
206     return rat_add (a, rat_neg (b))
207
208 def lily_notename (tuple2):
209     (n, a) = tuple2
210     nn = chr ((n+ 2)%7 + ord ('a'))
211
212     return nn + {-2:'eses', -1:'es', 0:'', 1:'is', 2:'isis'}[a]
213
214
215 class Tuplet:
216     def __init__ (self, number):
217         self.start_note = number
218         self.finale = []
219
220     def append_finale (self, fin):
221         self.finale.append (fin)
222
223     def factor (self):
224         n = self.finale[0][2]*self.finale[0][3]
225         d = self.finale[0][0]*self.finale[0][1]
226         return rat_simplify( (n, d))
227     
228     def dump_start (self):
229         return '\\times %d/%d { ' % self.factor ()
230     
231     def dump_end (self):
232         return ' }'
233
234     def calculate (self, chords):
235         edu_left = self.finale[0][0] * self.finale[0][1]
236
237         startch = chords[self.start_note]
238         c = startch
239         while c and edu_left:
240             c.tuplet = self
241             if c == startch:
242                 c.chord_prefix = self.dump_start () + c.chord_prefix 
243
244             if not c.grace:
245                 edu_left = edu_left - c.EDU_duration ()
246             if edu_left == 0:
247                 c.chord_suffix = c.chord_suffix+ self.dump_end ()
248             c = c.next
249
250         if edu_left:
251             sys.stderr.write ("\nHuh? Tuplet starting at entry %d was too short." % self.start_note)
252         
253 class Slur:
254     def __init__ (self, number, params):
255         self.number = number
256         self.finale = params
257
258     def append_entry (self, finale_e):
259         self.finale.append (finale_e)
260
261     def calculate (self, chords):
262         startnote = self.finale[5]
263         endnote = self.finale[3*6 + 2]
264         try:
265             cs = chords[startnote]
266             ce = chords[endnote]
267
268             if not cs or not ce:
269                 raise IndexError
270             
271             cs.note_suffix = '-(' + cs.note_suffix 
272             ce.note_suffix = ce.note_suffix + '-)'
273             
274         except IndexError:
275             sys.stderr.write ("""\nHuh? Slur no %d between (%d,%d), with %d notes""" % (self.number,  startnote, endnote, len (chords)))
276                     
277         
278 class Global_measure:
279     def __init__ (self, number):
280         self.timesig = ''
281         self.number = number
282         self.key_signature = None
283         self.scale = None
284         self.force_break = 0
285         
286         self.repeats = []
287         self.finale = []
288
289     def __str__ (self):
290         return `self.finale `
291     
292     def set_timesig (self, finale):
293         (beats, fdur) = finale
294         (log, dots) = EDU_to_duration (fdur)
295
296         if dots == 1:
297             beats = beats * 3
298             log = log * 2
299             dots = 0
300
301         if dots <> 0:
302             sys.stderr.write ("\nHuh? Beat duration has  dots? (EDU Duration = %d)" % fdur) 
303         self.timesig = (beats, log)
304
305     def length (self):
306         return self.timesig
307     
308     def set_key_sig (self, finale):
309         k = interpret_finale_key_sig (finale)
310         self.key_signature = k
311         self.scale = find_scale (k)
312
313     def set_flags (self,flag1, flag2):
314         
315         # flag1 isn't all that interesting.
316         if flag2 & 0x8000:
317             self.force_break = 1
318             
319         if flag2 & 0x0008:
320             self.repeats.append ('start')
321         if flag2 & 0x0004:
322             self.repeats.append ('stop')
323             
324         if flag2 & 0x0002:
325             if flag2 & 0x0004:
326                 self.repeats.append ('bracket')
327
328 articulation_dict ={
329     94: '^',
330     109: '\\prall',
331     84: '\\turn',
332     62: '\\mordent',
333     85: '\\fermata',
334     46: '.',
335 #        3: '>',
336 #        18: '\arpeggio' ,
337 }
338
339 class Articulation_def:
340     def __init__ (self, n, a, b):
341         self.finale_glyph = a & 0xff
342         self.number = n
343
344     def dump (self):
345         try:
346             return articulation_dict[self.finale_glyph]
347         except KeyError:
348             sys.stderr.write ("\nUnknown articulation no. %d" % self.finale_glyph)
349             sys.stderr.write ("\nPlease add an entry to articulation_dict in the Python source")                        
350             return None
351     
352 class Articulation:
353     def __init__ (self, a,b, finale):
354         self.definition = finale[0]
355         self.notenumber = b
356         
357     def calculate (self, chords, defs):
358         c = chords[self.notenumber]
359
360         adef = defs[self.definition]
361         lystr =adef.dump()
362         if lystr == None:
363             lystr = '"art"'
364             sys.stderr.write ("\nThis happened on note %d" % self.notenumber)
365
366         c.note_suffix = '-' + lystr
367
368 class Syllable:
369     def __init__ (self, a,b , finale):
370         self.chordnum = b
371         self.syllable = finale[1]
372         self.verse = finale[0]
373     def calculate (self, chords, lyrics):
374         self.chord = chords[self.chordnum]
375
376 class Verse:
377     def __init__ (self, number, body):
378         self.body = body
379         self.number = number
380         self.split_syllables ()
381     def split_syllables (self):
382         ss = re.split ('(-| +)', self.body)
383
384         sep = 0
385         syls = [None]
386         for s in ss:
387             if sep:
388                 septor = re.sub (" +", "", s)
389                 septor = re.sub ("-", " -- ", septor) 
390                 syls[-1] = syls[-1] + septor
391             else:
392                 syls.append (s)
393             
394             sep = not sep 
395
396         self.syllables = syls
397
398     def dump (self):
399         str = ''
400         line = ''
401         for s in self.syllables[1:]:
402             line = line + ' ' + s
403             if len (line) > 72:
404                 str = str + ' ' * 4 + line + '\n'
405                 line = ''
406             
407         str = """\nverse%s = \\lyricmode {\n %s }\n""" %  (encodeint (self.number - 1) ,str)
408         return str
409
410 class KeySignature:
411     def __init__(self, pitch, sig_type = 0):
412         self.pitch = pitch
413         self.sig_type = sig_type
414     
415     def signature_type (self):
416         if self.sig_type == 1:
417             return "\\minor"
418         else:
419             # really only for 0, but we only know about 0 and 1
420             return "\\major"
421     
422     def equal (self, other):
423         if other and other.pitch == self.pitch and other.sig_type == self.sig_type:
424             return 1
425         else:
426             return 0
427     
428
429 class Measure:
430     def __init__(self, no):
431         self.number = no
432         self.frames = [0] * 4
433         self.flags = 0
434         self.clef = 0
435         self.finale = []
436         self.global_measure = None
437         self.staff = None
438         self.valid = 1
439         
440
441     def valid (self):
442         return self.valid
443     def calculate (self):
444         fs = []
445
446         if len (self.finale) < 2:
447             fs = self.finale[0]
448
449             self.clef = fs[1]
450             self.frames = [fs[0]]
451         else:
452             fs = self.finale
453             self.clef = fs[0]
454             self.flags = fs[1]
455             self.frames = fs[2:]
456
457
458 class Frame:
459     def __init__ (self, finale):
460         self.measure = None
461         self.finale = finale
462         (number, start, end ) = finale
463         self.number = number
464         self.start = start
465         self.end = end
466         self.chords  = []
467
468     def set_measure (self, m):
469         self.measure = m
470
471     def calculate (self):
472
473         # do grace notes.
474         lastch = None
475         in_grace = 0
476         for c in self.chords:
477             if c.grace and (lastch == None or (not lastch.grace)):
478                 c.chord_prefix = r'\grace {' + c.chord_prefix
479                 in_grace = 1
480             elif not c.grace and lastch and lastch.grace:
481                 lastch.chord_suffix = lastch.chord_suffix + ' } '
482                 in_grace = 0
483                 
484             lastch = c
485
486         if lastch and in_grace:
487             lastch.chord_suffix += '}' 
488
489         
490     def dump (self):
491         str = '%% FR(%d)\n' % self.number
492         left = self.measure.global_measure.length ()
493
494         
495         ln = ''
496         for c in self.chords:
497             add = c.ly_string () + ' '
498             if len (ln) + len(add) > 72:
499                 str = str + ln + '\n'
500                 ln = ''
501             ln = ln + add
502             left = rat_subtract (left, c.length ())
503
504         str = str + ln 
505         
506         if left[0] < 0:
507             sys.stderr.write ("""\nHuh? Going backwards in frame no %d, start/end (%d,%d)""" % (self.number, self.start, self.end))
508             left = (0,1)
509         if left[0]:
510             str = str + rational_to_lily_skip (left)
511
512         str = str + '  | \n'
513         return str
514         
515 def encodeint (i):
516     return chr ( i  + ord ('A'))
517
518 class Staff:
519     def __init__ (self, number):
520         self.number = number
521         self.measures = []
522
523     def get_measure (self, no):
524         fill_list_to (self.measures, no)
525
526         if self.measures[no] == None:
527             m = Measure (no)
528             self.measures [no] =m
529             m.staff = self
530
531         return self.measures[no]
532     def staffid (self):
533         return 'staff' + encodeint (self.number - 1)
534     def layerid (self, l):
535         return self.staffid() +  'layer%s' % chr (l -1 + ord ('A'))
536     
537     def dump_time_key_sigs (self):
538         k  = ''
539         last_key = None
540         last_time = None
541         last_clef = None
542         gap = (0,1)
543         for m in self.measures[1:]:
544             if not m or not m.valid:
545                 continue # ugh.
546             
547             g = m.global_measure
548             e = ''
549             
550             if g:
551                 if g.key_signature and not g.key_signature.equal(last_key):
552                     pitch= g.key_signature.pitch
553                     e = e + "\\key %s %s " % (lily_notename (pitch),
554                                  g.key_signature.signature_type())
555                     
556                     last_key = g.key_signature
557                 if last_time <> g.timesig :
558                     e = e + "\\time %d/%d " % g.timesig
559                     last_time = g.timesig
560
561                 if 'start' in g.repeats:
562                     e = e + ' \\bar "|:" ' 
563
564
565                 # we don't attempt voltas since they fail easily.
566                 if 0 : # and g.repeat_bar == '|:' or g.repeat_bar == ':|:' or g.bracket:
567                     strs = []
568                     if g.repeat_bar == '|:' or g.repeat_bar == ':|:' or g.bracket == 'end':
569                         strs.append ('#f')
570
571                     
572                     if g.bracket == 'start':
573                         strs.append ('"0."')
574
575                     str = string.join (map (lambda x: '(volta %s)' % x, strs))
576                     
577                     e = e + ' \\set Score.repeatCommands =  #\'(%s) ' % str
578
579                 if g.force_break:
580                     e = e + ' \\break '  
581             
582             if last_clef <> m.clef :
583                 e = e + '\\clef "%s"' % lily_clef (m.clef)
584                 last_clef = m.clef
585             if e:
586                 if gap <> (0,1):
587                     k = k +' ' + rational_to_lily_skip (gap) + '\n'
588                 gap = (0,1)
589                 k = k + e
590                 
591             if g:
592                 gap = rat_add (gap, g.length ())
593                 if 'stop' in g.repeats:
594                     k = k + ' \\bar ":|" '
595                 
596         k = '%sglobal = { %s }\n\n ' % (self.staffid (), k)
597         return k
598     
599     def dump (self):
600         str = ''
601
602
603         layerids = []
604         for x in range (1,5): # 4 layers.
605             laystr =  ''
606             last_frame = None
607             first_frame = None
608             gap = (0,1)
609             for m in self.measures[1:]:
610                 if not m or not m.valid:
611                     sys.stderr.write ("Skipping non-existant or invalid measure\n")
612                     continue
613
614                 fr = None
615                 try:
616                     fr = m.frames[x]
617                 except IndexError:
618                     sys.stderr.write ("Skipping nonexistent frame %d\n" % x)
619                     laystr = laystr + "%% non existent frame %d (skipped) \n" % x
620                 if fr:
621                     first_frame = fr
622                     if gap <> (0,1):
623                         laystr = laystr +'} %s {\n ' % rational_to_lily_skip (gap)
624                         gap = (0,1)
625                     laystr = laystr + fr.dump ()
626                 else:
627                     if m.global_measure :
628                         gap = rat_add (gap, m.global_measure.length ())
629                     else:
630                         sys.stderr.write ( \
631                             "No global measure for staff %d measure %d\n"
632                             % (self.number, m.number))
633             if first_frame:
634                 l = self.layerid (x)
635                 laystr = '%s = { {  %s } }\n\n' % (l, laystr)
636                 str = str  + laystr
637                 layerids.append (l)
638
639         str = str +  self.dump_time_key_sigs ()                
640         stafdef = '\\%sglobal' % self.staffid ()
641         for i in layerids:
642             stafdef = stafdef + ' \\' + i
643             
644
645         str = str + '%s = \\context Staff = %s <<\n %s\n >>\n' % \
646            (self.staffid (), self.staffid (), stafdef)
647         return str
648
649                 
650
651 def ziplist (l):
652     if len (l) < 2:
653         return []
654     else:
655         return [(l[0], l[1])] + ziplist (l[2:])
656
657
658 class Chord:
659     def __init__ (self, number, contents):
660         self.pitches = []
661         self.frame = None
662         self.finale = contents[:7]
663
664         self.notelist = ziplist (contents[7:])
665         self.duration  = None
666         self.next = None
667         self.prev = None
668         self.number = number
669         self.note_prefix= ''
670         self.note_suffix = ''
671         self.chord_suffix = ''
672         self.chord_prefix = ''
673         self.tuplet = None
674         self.grace = 0
675         
676     def measure (self):
677         if not self.frame:
678             return None
679         return self.frame.measure
680
681     def length (self):
682         if self.grace:
683             return (0,1)
684         
685         l = (1, self.duration[0])
686
687         d = 1 << self.duration[1]
688
689         dotfact = rat_subtract ((2,1), (1,d))
690         mylen =  rat_multiply (dotfact, l)
691
692         if self.tuplet:
693             mylen = rat_multiply (mylen, self.tuplet.factor())
694         return mylen
695         
696
697     def EDU_duration (self):
698         return self.finale[2]
699     def set_duration (self):
700         self.duration = EDU_to_duration(self.EDU_duration ())
701         
702     def calculate (self):
703         self.find_realpitch ()
704         self.set_duration ()
705
706         flag = self.finale[4]
707         if Chord.GRACE_MASK & flag:
708             self.grace = 1
709         
710     
711     def find_realpitch (self):
712
713         meas = self.measure ()
714         tiestart = 0
715         if not meas or not meas.global_measure  :
716             sys.stderr.write ('note %d not in measure\n' % self.number)
717         elif not meas.global_measure.scale:
718             sys.stderr.write ('note %d: no scale in this measure.' % self.number)
719         else:
720             
721             for p in self.notelist:
722                 (pitch, flag) = p
723
724
725                 nib1 = pitch & 0x0f
726                 
727                 if nib1 > 8:
728                     nib1 = -(nib1 - 8)
729                 rest = pitch / 16
730
731                 scale =  meas.global_measure.scale 
732                 (sn, sa) =scale[rest % 7]
733                 sn = sn + (rest - (rest%7)) + 7
734                 acc = sa + nib1
735                 self.pitches.append ((sn, acc))
736                 tiestart =  tiestart or (flag & Chord.TIE_START_MASK)
737         if tiestart :
738             self.chord_suffix = self.chord_suffix + ' ~ '
739         
740     REST_MASK = 0x40000000L
741     TIE_START_MASK = 0x40000000L
742     GRACE_MASK = 0x00800000L
743     
744     def ly_string (self):
745         s = ''
746
747         rest = ''
748
749
750         if not (self.finale[4] & Chord.REST_MASK):
751             rest = 'r'
752         
753         for p in self.pitches:
754             (n,a) =  p
755             o = n/ 7
756             n = n % 7
757
758             nn = lily_notename ((n,a))
759
760             if o < 0:
761                 nn = nn + (',' * -o)
762             elif o > 0:
763                 nn = nn + ('\'' * o)
764                 
765             if s:
766                 s = s + ' '
767
768             if rest:
769                 nn = rest
770                 
771             s = s + nn 
772
773         if not self.pitches:
774             s  = 'r'
775         if len (self.pitches) > 1:
776             s = '<%s>' % s
777
778         s = s + '%d%s' % (self.duration[0], '.'* self.duration[1])
779         s = self.note_prefix + s + self.note_suffix
780         
781         s = self.chord_prefix + s + self.chord_suffix
782
783         return s
784
785
786 def fill_list_to (list, no):
787     """
788 Add None to LIST until it contains entry number NO.
789     """
790     while len (list) <= no:
791         list.extend ([None] * (no - len(list) + 1))
792     return list
793
794 def read_finale_value (str):
795     """
796 Pry off one value from STR. The value may be $hex, decimal, or "string".
797 Return: (value, rest-of-STR)
798     """
799     while str and str[0] in ' \t\n':
800         str = str[1:]
801
802     if not str:
803         return (None,str)
804     
805     if str[0] == '$':
806         str = str [1:]
807
808         hex = ''
809         while str and str[0] in '0123456789ABCDEF':
810             hex = hex  + str[0]
811             str = str[1:]
812
813         
814         return (string.atol (hex, 16), str)
815     elif str[0] == '"':
816         str = str[1:]
817         s = ''
818         while str and str[0] <> '"':
819             s = s + str[0]
820             str = str[1:]
821
822         return (s,str)
823     elif str[0] in '-0123456789':
824         dec = ''
825         while str and str[0] in '-0123456789':
826             dec = dec  + str[0]
827             str = str[1:]
828             
829         return (string.atoi (dec), str)
830     else:
831         sys.stderr.write ("cannot convert `%s'\n" % str)
832         return (None, str)
833
834
835
836     
837 def parse_etf_file (fn, tag_dict):
838
839     """ Read FN, putting ETF info into
840     a giant dictionary.  The keys of TAG_DICT indicate which tags
841     to put into the dict.
842     """
843     
844     sys.stderr.write ('parsing ... ' )
845     f = open (fn)
846     
847     gulp = re.sub ('[\n\r]+', '\n',  f.read ())
848     ls = string.split (gulp, '\n^')
849
850     etf_file_dict = {}
851     for k in tag_dict.keys (): 
852         etf_file_dict[k] = {}
853
854     last_tag = None
855     last_numbers = None
856
857
858     for l in  ls:
859         m = re.match ('^([a-zA-Z0-9&]+)\(([^)]+)\)', l)
860         if m and tag_dict.has_key (m.group (1)):
861             tag = m.group (1)
862
863             indices = tuple (map (string.atoi, string.split (m.group (2), ',')))
864             content = l[m.end (2)+1:]
865
866
867             tdict = etf_file_dict[tag]
868             if not tdict.has_key (indices):
869                 tdict[indices] = []
870
871
872             parsed = []
873
874             if tag == 'verse' or tag == 'block':
875                 m2 = re.match ('(.*)\^end', content)
876                 if m2:
877                     parsed = [m2.group (1)]
878             else:
879                 while content:
880                     (v, content) = read_finale_value (content)
881                     if v <> None:
882                         parsed.append (v)
883
884             tdict [indices].extend (parsed)
885
886             last_indices = indices
887             last_tag = tag
888
889             continue
890
891 # let's not do this: this really confuses when eE happens to be before  a ^text.
892 #                if last_tag and last_indices:
893 #                        etf_file_dict[last_tag][last_indices].append (l)
894             
895     sys.stderr.write ('\n') 
896     return etf_file_dict
897
898     
899
900
901
902 class Etf_file:
903     def __init__ (self, name):
904         self.measures = [None]
905         self.chords = [None]
906         self.frames = [None]
907         self.tuplets = [None]
908         self.staffs = [None]
909         self.slurs = [None]
910         self.articulations = [None]
911         self.syllables = [None]
912         self.verses = [None]
913         self.articulation_defs = [None]
914
915         ## do it
916         self.parse (name)
917
918     def get_global_measure (self, no):
919         fill_list_to (self.measures, no)
920         if self.measures[no] == None:
921             self.measures [no] = Global_measure (no)
922
923         return self.measures[no]
924
925         
926     def get_staff(self,staffno):
927         fill_list_to (self.staffs, staffno)
928         if self.staffs[staffno] == None:
929             self.staffs[staffno] = Staff (staffno)
930
931         return self.staffs[staffno]
932
933     # staff-spec
934     def try_IS (self, indices, contents):
935         pass
936
937     def try_BC (self, indices, contents):
938         bn = indices[0]
939         where = contents[0] / 1024.0
940     def try_TP(self,  indices, contents):
941         (nil, num) = indices
942
943         if self.tuplets[-1] == None or num <> self.tuplets[-1].start_note:
944             self.tuplets.append (Tuplet (num))
945
946         self.tuplets[-1].append_finale (contents)
947
948     def try_IM (self, indices, contents):
949         (a,b) = indices
950         fin = contents
951         self.articulations.append (Articulation (a,b,fin))
952     def try_verse (self, indices, contents):
953         a = indices[0]
954         body = contents[0]
955
956         body = re.sub (r"""\^[a-z]+\([^)]+\)""", "", body)
957         body = re.sub ("\^[a-z]+", "", body)
958         self.verses.append (Verse (a, body))
959     def try_ve (self,indices, contents):
960         (a,b) = indices
961         self.syllables.append (Syllable (a,b,contents))
962
963     def try_eE (self,indices, contents):
964         no = indices[0]
965         (prev, next, dur, pos, entryflag, extended, follow) = contents[:7]
966
967         fill_list_to (self.chords, no)
968         self.chords[no]  =Chord (no, contents)
969
970     def try_Sx(self,indices, contents):
971         slurno = indices[0]
972         fill_list_to (self.slurs, slurno)
973         self.slurs[slurno] = Slur(slurno, contents)
974
975     def try_IX (self, indices, contents):
976         n = indices[0]
977         a = contents[0]
978         b = contents[1]
979
980         ix= None
981         try:
982             ix = self.articulation_defs[n]
983         except IndexError:
984             ix = Articulation_def (n,a,b)
985             self.articulation_defs.append (Articulation_def (n, a, b))
986
987     def try_GF(self, indices, contents):
988         (staffno,measno) = indices
989
990         st = self.get_staff (staffno)
991         meas = st.get_measure (measno)
992         meas.finale = contents
993         
994     def try_FR(self, indices, contents):
995         frameno = indices [0]
996         
997         startnote = contents[0]
998         endnote = contents[1]
999
1000         fill_list_to (self.frames, frameno)
1001     
1002         self.frames[frameno] = Frame ((frameno, startnote, endnote))
1003     
1004     def try_MS (self, indices, contents):
1005         measno = indices[0]
1006         keynum = contents[1]
1007         meas =self. get_global_measure (measno)
1008
1009         meas.set_key_sig (keynum)
1010
1011         beats = contents[2]
1012         beatlen = contents[3]
1013         meas.set_timesig ((beats, beatlen))
1014
1015         meas_flag1 = contents[4]
1016         meas_flag2 = contents[5]
1017
1018         meas.set_flags (meas_flag1, meas_flag2);
1019
1020
1021     routine_dict = {
1022         'MS': try_MS,
1023         'FR': try_FR,
1024         'GF': try_GF,
1025         'IX': try_IX,
1026         'Sx' : try_Sx,
1027         'eE' : try_eE,
1028         'verse' : try_verse,
1029         've' : try_ve,
1030         'IM' : try_IM,
1031         'TP' : try_TP,
1032         'BC' : try_BC,
1033         'IS' : try_IS,
1034         }
1035     
1036     def parse (self, etf_dict):
1037         sys.stderr.write ('reconstructing ...')
1038         sys.stderr.flush ()
1039
1040         for (tag,routine) in Etf_file.routine_dict.items ():
1041             ks = etf_dict[tag].keys ()
1042             ks.sort ()
1043             for k in ks:
1044                 routine (self, k, etf_dict[tag][k])
1045             
1046         sys.stderr.write ('processing ...')
1047         sys.stderr.flush ()
1048
1049         self.unthread_entries ()
1050
1051         for st in self.staffs[1:]:
1052             if not st:
1053                 continue
1054             mno = 1
1055             for m in st.measures[1:]:
1056                 if not m:
1057                     continue
1058                 
1059                 m.calculate()
1060                 try:
1061                     m.global_measure = self.measures[mno]
1062                 except IndexError:
1063                     sys.stderr.write ("Non-existent global measure %d" % mno)
1064                     continue
1065                 
1066                 frame_obj_list = [None]
1067                 for frno in m.frames:
1068                     try:
1069                         fr = self.frames[frno]
1070                         frame_obj_list.append (fr)
1071                     except IndexError:
1072                         sys.stderr.write ("\nNon-existent frame %d"  % frno)
1073
1074                 m.frames = frame_obj_list
1075                 for fr in frame_obj_list[1:]:
1076                     if not fr:
1077                         continue
1078                     
1079                     fr.set_measure (m)
1080                     
1081                     fr.chords = self.get_thread (fr.start, fr.end)
1082                     for c in fr.chords:
1083                         c.frame = fr
1084                 mno = mno + 1
1085
1086         for c in self.chords[1:]:
1087             if c:
1088                 c.calculate()
1089
1090         for f in self.frames[1:]:
1091             if f:
1092                 f.calculate ()
1093             
1094         for t in self.tuplets[1:]:
1095             t.calculate (self.chords)
1096             
1097         for s in self.slurs[1:]:
1098             if s:
1099                 s.calculate (self.chords)
1100             
1101         for s in self.articulations[1:]:
1102             s.calculate (self.chords, self.articulation_defs)
1103             
1104     def get_thread (self, startno, endno):
1105
1106         thread = []
1107
1108         c = None
1109         try:
1110             c = self.chords[startno]
1111         except IndexError:
1112             sys.stderr.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno, endno))
1113             return []
1114
1115         
1116         while c and c.number <> endno:
1117             thread.append (c)
1118             c = c.next
1119
1120         if c: 
1121             thread.append (c)
1122         
1123         return thread
1124
1125     def dump (self):
1126         str = ''
1127         staffs = []
1128         for s in self.staffs[1:]:
1129             if s:
1130                 str = str + '\n\n' + s.dump () 
1131                 staffs.append ('\\' + s.staffid ())
1132
1133
1134         # should use \addlyrics ?
1135
1136         for v in self.verses[1:]:
1137             str = str + v.dump()
1138
1139         if len (self.verses) > 1:
1140             sys.stderr.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
1141             
1142         if staffs:
1143             str += '\\version "2.3.25"\n'
1144             str = str + '<<\n  %s\n>> } ' % string.join (staffs)
1145             
1146         return str
1147
1148
1149     def __str__ (self):
1150         return 'ETF FILE %s %s' % (self.measures,  self.entries)
1151     
1152     def unthread_entries (self):
1153         for e in self.chords[1:]:
1154             if not e:
1155                 continue
1156
1157             e.prev = self.chords[e.finale[0]]
1158             e.next = self.chords[e.finale[1]]
1159
1160 def identify():
1161     sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
1162
1163 def warranty ():
1164     identify ()
1165     sys.stdout.write ('''
1166 Copyright (c) %s by
1167
1168  Han-Wen Nienhuys
1169  Jan Nieuwenhuizen
1170
1171 %s
1172 %s
1173 ''' % ( '2001--2006',
1174    _('Distributed under terms of the GNU General Public License.'),
1175    _('It comes with NO WARRANTY.')))
1176
1177
1178
1179 def get_option_parser ():
1180     p = ly.get_option_parser (usage=_ ("%s [OPTION]... ETF-FILE") % 'etf2ly',
1181                  description=_ ("""Enigma Transport Format is a format used by Coda Music Technology's
1182 Finale product.  etf2ly converts a subset of ETF to a ready-to-use LilyPond file."""),
1183                  add_help_option=False)
1184     p.add_option("-h", "--help",
1185                  action="help",
1186                  help=_ ("show this help and exit"))
1187     p.version = "etf2ly (LilyPond) @TOPLEVEL_VERSION@"
1188     p.add_option("--version",
1189                  action="version",
1190                  help=_ ("show version number and exit"))
1191     p.add_option ('-o', '--output', help=_ ("write output to FILE"),
1192            metavar=_("FILE"),
1193            action='store')
1194     p.add_option ('-w', '--warranty', help=_ ("show warranty and copyright"),
1195            action='store_true',
1196            ),
1197
1198     p.add_option_group (ly.display_encode (_ ('Bugs')),
1199                         description=(_ ('Report bugs via')
1200                                      + ''' http://post.gmane.org/post.php'''
1201                                      '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
1202     return p
1203
1204 def do_options ():
1205     opt_parser = get_option_parser()
1206     (options,args) = opt_parser.parse_args ()
1207     if options.warranty:
1208         warranty ()
1209         sys.exit (0)
1210
1211     return (options,args)
1212
1213 (options, files) = do_options()
1214 identify()
1215
1216 out_filename = options.output
1217
1218 e = None
1219 for f in files:
1220     if f == '-':
1221         f = ''
1222
1223     sys.stderr.write ('Processing `%s\'\n' % f)
1224
1225     dict = parse_etf_file (f, Etf_file.routine_dict)
1226     e = Etf_file(dict)
1227     if not out_filename:
1228         out_filename = os.path.basename (re.sub ('(?i).etf$', '.ly', f))
1229         
1230     if out_filename == f:
1231         out_filename = os.path.basename (f + '.ly')
1232         
1233     sys.stderr.write ('Writing `%s\'' % out_filename)
1234     ly = e.dump()
1235
1236     fo = open (out_filename, 'w')
1237     fo.write ('%% lily was here -- automatically converted by etf2ly from %s\n' % f)
1238     fo.write(ly)
1239     fo.close ()
1240