3 # info mostly taken from looking at files. See also
4 # http://www.cs.uu.nl/~hanwen/lily-devel/etf.html
17 # * slur/stem directions
18 # * voices (2nd half of frame?)
19 # * more intelligent lyrics
20 # * beams (better use autobeam?)
22 program_name = 'etf2ly'
23 version = '@TOPLEVEL_VERSION@'
24 if version == '@' + 'TOPLEVEL_VERSION' + '@':
25 version = '(unknown version)' # uGUHGUHGHGUGH
34 finale_clefs= ['treble', 'alto', 'tenor', 'bass', 'percussion', 'treble8vb', 'bass8vb', 'baritone']
37 return finale_clefs[fin]
40 return open (f).read ()
42 # notename 0 == central C
43 distances = [0, 2, 4, 5, 7, 9, 11, 12]
44 def semitones (name, acc):
45 return (name / 7 ) * 12 + distances[name % 7] + acc
47 # represent pitches as (notename, alteration), relative to C-major scale
48 def transpose(orig, delta):
52 old_pitch =semitones (oname, oacc)
53 delta_pitch = semitones (dname, dacc)
54 nname = (oname + dname)
56 new_pitch = semitones (nname, nacc)
58 nacc = nacc - (new_pitch - old_pitch - delta_pitch)
64 # find transposition of C-major scale that belongs here.
65 def interpret_finale_key_sig (finale_id):
67 if 0 <= finale_id < 7:
69 p = transpose (p, (4,0)) # a fifth up
70 finale_id = finale_id - 1
71 elif 248 < finale_id <= 255:
72 while finale_id < 256:
73 p = transpose (p, (3,0))
74 finale_id = finale_id + 1
80 def find_scale (transposition):
81 cscale = map (lambda x: (x,0), range (0,7))
82 trscale = map(lambda x, k=transposition: transpose(x, k), cscale)
108 def rat_multiply (a,b):
112 return rat_simplify ((x*p, y*q))
118 return rat_simplify ((x*q + p*y, y*q))
124 def rat_subtract (a,b ):
125 return rat_add (a, rat_neg (b))
127 def lily_notename (tuple2):
129 nn = chr ((n+ 2)%7 + ord ('a'))
144 def __init__ (self, number):
148 def append_entry (self, finale_e):
149 self.finale.append (finale_e)
151 def calculate (self, chords):
152 startnote = self.finale[0][5]
153 endnote = self.finale[3][2]
155 cs = chords[startnote]
156 cs.note_suffix = '(' + cs.note_suffix
158 ce.prefix = ce.prefix + ')'
160 class Global_measure:
161 def __init__ (self, number):
164 self.keysignature = None
169 def set_timesig (self, finale):
170 (beats, fdur) = finale
171 (log, dots) = EDU_to_duration (fdur)
173 self.timesig = (beats, log)
178 def set_keysig (self, finale):
179 k = interpret_finale_key_sig (finale)
180 self.keysignature = k
181 self.scale = find_scale (k)
188 18: '"arp"' , # arpeggio
192 def __init__ (self, a,b, finale):
193 self.type = finale[0]
195 def calculate (self, chords):
196 c = chords[self.notenumber]
199 a = articulation_dict[self.type]
203 c.note_suffix = '-' + a + c.note_suffix
206 def __init__ (self, a,b , finale):
208 self.syllable = finale[1]
209 self.verse = finale[0]
210 def calculate (self, chords, lyrics):
211 self.chord = chords[self.chordnum]
214 def __init__ (self, number, body):
217 self.split_syllables ()
218 def split_syllables (self):
219 ss = re.split ('(-| +)', self.body)
225 septor = re.sub (" +", "", s)
226 septor = re.sub ("-", " -- ", septor)
227 syls[-1] = syls[-1] + septor
233 self.syllables = syls
238 for s in self.syllables[1:]:
239 line = line + ' ' + s
241 str = str + ' ' * 4 + line + '\n'
244 str = """\nverse%s = \\lyrics {\n %s}\n""" % (encodeint (self.number - 1) ,str)
249 def __init__(self, no):
255 self.global_measure = None
257 def add_finale_entry (self, entry):
258 self.finale.append (entry)
260 def calculate (self):
264 self.clef = string.atoi (f0[0])
265 self.flags = string.atoi (f0[1])
266 fs = map (string.atoi, list (f0[2:]) + [f1[0]])
271 def __init__ (self, finale):
274 (number, start, end ) = finale
280 def set_measure (self, m):
285 left = self.measure.global_measure.length ()
286 for c in self.chords:
287 str = str + c.ly_string () + ' '
288 left = rat_subtract (left, c.length ())
291 print self.start, self.end
295 str = str + 's*%d/%d' % left
301 return chr ( i + ord ('A'))
304 def __init__ (self, number):
308 def get_measure (self, no):
309 if len (self.measures) <= no:
310 self.measures = self.measures + [None]* (1 + no - len (self.measures))
312 if self.measures[no] == None:
313 self.measures [no] = Measure (no)
315 return self.measures[no]
317 return 'staff' + encodeint (self.number - 1)
318 def layerid (self, l):
319 return self.staffid() + 'layer%s' % chr (l -1 + ord ('A'))
321 def dump_time_key_sigs (self):
327 for m in self.measures[1:]:
330 if last_key <> g.keysignature:
331 e = e + "\\key %s \\major; " % lily_notename (g.keysignature)
332 last_key = g.keysignature
333 if last_time <> g.timesig :
334 e = e + "\\time %d/%d; " % g.timesig
335 last_time = g.timesig
336 if last_clef <> m.clef :
337 e = e + '\\clef %s;' % lily_clef (m.clef)
341 k = k +' s1*%d/%d \n ' % gap
345 gap = rat_add (gap, g.length ())
348 k = '%sglobal = \\notes { %s }\n\n ' % (self.staffid (), k)
356 for x in range (1,5): # 4 layers.
361 for m in self.measures[1:]:
366 laystr = laystr +'} s1*%d/%d {\n ' % gap
368 laystr = laystr + fr.dump ()
370 gap = rat_add (gap, m.global_measure.length ())
374 laystr = '%s = \\notes { { %s } }\n\n' % (l, laystr)
378 str = str + self.dump_time_key_sigs ()
379 stafdef = '\\%sglobal' % self.staffid ()
381 stafdef = stafdef + ' \\' + i
384 str = str + '%s = \\context Staff = %s <\n %s\n >\n' % \
385 (self.staffid (), self.staffid (), stafdef)
389 def EDU_to_duration (edu):
405 def __init__ (self, finale_entry):
408 self.finale = finale_entry
413 self.note_suffix = ''
414 self.chord_suffix = ''
415 self.chord_prefix = ''
420 return self.frame.measure
423 l = (1, self.duration[0])
425 d = 1 << self.duration[1]
427 dotfact = rat_subtract ((2,1), (1,d))
428 return rat_multiply (dotfact, l)
431 return self.finale[0][0]
432 def set_duration (self):
433 ((no, prev, next, dur, pos, entryflag, extended, follow),
434 notelist) = self.finale
435 self.duration = EDU_to_duration(dur)
436 def find_realpitch (self):
438 ((no, prev, next, dur, pos, entryflag, extended, follow), notelist) = self.finale
440 meas = self.measure ()
442 if not meas or not meas.global_measure :
443 print 'note %d not in measure' % self.number ()
444 elif not meas.global_measure.scale:
445 print 'note %d: no scale in this measure.' % self.number ()
455 scale = meas.global_measure.scale
456 (sn, sa) =scale[rest % 7]
457 sn = sn + (rest - (rest%7)) + 7
459 self.pitches.append ((sn, acc))
460 tiestart = tiestart or (flag & Chord.TIE_START_MASK)
462 self.chord_suffix = self.chord_suffix + ' ~ '
464 REST_MASK = 0x40000000L
465 TIE_START_MASK = 0x40000000L
466 def ly_string (self):
471 if not (self.finale[0][5] & Chord.REST_MASK):
474 for p in self.pitches:
479 nn = lily_notename ((n,a))
492 s = s + '%s%d%s' % (nn, self.duration[0], '.'* self.duration[1])
495 s = 'r%d%s' % (self.duration[0] , '.'* self.duration[1])
496 s = self.note_prefix + s + self.note_suffix
497 if len (self.pitches) > 1:
500 s = self.chord_prefix + s + self.chord_suffix
503 GFre = re.compile(r"""^\^GF\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
504 BCre = re.compile (r"""^\^BC\(([0-9-]+)\) ([0-9-]+) .*$""")
505 eEre = re.compile(r"""^\^eE\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) \$([0-9A-Fa-f]+) ([0-9-]+) ([0-9-]+)""")
506 FRre = re.compile (r"""^\^FR\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
507 MSre = re.compile (r"""^\^MS\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
508 note_re = re.compile (r"""^ +([0-9-]+) \$([A-Fa-f0-9]+)""")
509 Sxre = re.compile (r"""^\^Sx\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
510 IMre = re.compile (r"""^\^IM\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
511 vere = re.compile(r"""^\^(ve|ch|se)\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
512 versere = re.compile(r"""^\^verse\(([0-9]+)\)(.*)\^end""")
515 def __init__ (self, name):
516 self.measures = [None]
517 self.entries = [None]
522 self.articulations = [None]
523 self.syllables = [None]
529 def get_global_measure (self, no):
530 if len (self.measures) <= no:
531 self.measures = self.measures + [None]* (1 + no - len (self.measures))
533 if self.measures[no] == None:
534 self.measures [no] = Global_measure (no)
536 return self.measures[no]
539 def get_staff(self,staffno):
540 if len (self.staffs) <= staffno:
541 self.staffs = self.staffs + [None] * (1 + staffno - len (self.staffs))
543 if self.staffs[staffno] == None:
544 self.staffs[staffno] = Staff (staffno)
546 return self.staffs[staffno]
549 def try_IS (self, l):
552 def try_BC (self, l):
555 bn = string.atoi (m.group (1))
556 where = string.atoi (m.group (2)) / 1024.0
559 def try_IM (self, l):
562 a = string.atoi (m.group (1))
563 b = string.atoi (m.group (2))
565 fin = map (string.atoi, m.groups ()[2:])
567 self.articulations.append (Articulation (a,b,fin))
569 def try_verse (self,l):
570 m = versere .match (l)
572 a = string.atoi (m.group (1))
575 body = re.sub (r"""\^[a-z]+\([^)]+\)""", "", body)
576 body = re.sub ("\^[a-z]+", "", body)
577 self.verses.append (Verse (a, body))
583 a = string.atoi (m.group (1))
584 b = string.atoi (m.group (2))
586 fin = map (string.atoi, m.groups ()[2:])
588 self.syllables.append (Syllable (a,b,fin))
590 def try_eE (self, l):
594 (no, prev, next, dur, pos, entryflag, extended, follow) = tup
595 (no, prev, next, dur, pos,extended, follow) \
596 = tuple (map (string.atoi, [no,prev,next,dur,pos,extended,follow]))
598 entryflag = string.atol (entryflag,16)
599 assert (no==len (self.entries))
600 current_entry = ((no, prev, next, dur, pos, entryflag, extended, follow), [])
601 self.entries.append (current_entry)
607 slurno = string.atoi (m.group (1))
609 if len (self.slurs) == slurno:
610 self.slurs.append (Slur (slurno))
612 params = list (m.groups ()[1:])
613 params = map (string.atoi, params)
614 self.slurs[-1].append_entry (params)
620 (staffno,measno) = m.groups ()[0:2]
621 s = string.atoi (staffno)
622 me = string.atoi (measno)
624 entry = m.groups () [2:]
625 st = self.get_staff (s)
626 meas = st.get_measure (me)
627 meas.add_finale_entry (entry)
633 (frameno, startnote, endnote, foo, bar) = m.groups ()
634 (frameno, startnote, endnote) = tuple (map (string.atoi, [frameno, startnote, endnote]))
635 self.frames.append (Frame ((frameno, startnote, endnote)))
638 def try_MS (self, l):
641 measno = string.atoi (m.group (1))
642 keynum = string.atoi (m.group (3))
643 meas =self. get_global_measure (measno)
644 meas.set_keysig (keynum)
646 beats = string.atoi (m.group (4))
647 beatlen = string.atoi (m.group (5))
648 meas.set_timesig ((beats, beatlen))
652 def try_note (self, l):
653 m = note_re.match (l)
655 (pitch, flag) = m.groups ()
656 pitch = string.atoi (pitch)
657 flag = string.atol (flag,16)
658 self.entries[-1][1].append ((pitch,flag))
660 def parse (self, name):
661 sys.stderr.write ('parsing ...')
664 ls = open (name).readlines ()
674 m = self.try_note (l)
682 m = self.try_verse (l)
684 sys.stderr.write ('processing ...')
687 self.unthread_entries ()
689 for st in self.staffs[1:]:
693 for m in st.measures[1:]:
694 m.global_measure = self.measures[mno]
697 frame_obj_list = [None]
698 for frno in m.frames:
699 fr = self.frames[frno]
700 frame_obj_list.append (fr)
702 m.frames = frame_obj_list
703 for fr in frame_obj_list[1:]:
709 fr.chords = self.get_thread (fr.start, fr.end)
714 for c in self.chords[1:]:
718 for s in self.slurs [1:]:
719 s.calculate (self.chords)
720 for s in self.articulations[1:]:
721 s.calculate (self.chords)
723 def get_thread (self, startno, endno):
726 c = self.chords[startno]
727 while c and c.number () <> endno:
739 for s in self.staffs[1:]:
741 str = str + '\n\n' + s.dump ()
742 staffs.append ('\\' + s.staffid ())
745 str = str + '\\score { < %s > } ' % string.join (staffs)
747 # should use \addlyrics ?
749 for v in self.verses[1:]:
752 if len (self.verses) > 1:
753 sys.stderr.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
761 def unthread_entries (self):
763 for e in self.entries[1:]:
764 self.chords.append (Chord (e))
766 for e in self.chords[1:]:
767 e.prev = self.chords[e.finale[0][1]]
768 e.next = self.chords[e.finale[0][2]]
776 sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
780 Convert ETF to LilyPond.
782 Usage: etf2ly [OPTION]... ETF-FILE
786 -o, --output=FILE set output filename to FILE
787 -v, --version version information
789 Enigma Transport Format is a format used by Coda Music Technology's
790 Finale product. This program will convert a subset of ETF to a
791 ready-to-use lilypond file.
796 def print_version ():
797 print r"""etf2ly (GNU lilypond) %s""" % version
801 (options, files) = getopt.getopt (sys.argv[1:], 'vo:h', ['help','version', 'output='])
807 if o== '--help' or o == '-h':
810 if o == '--version' or o == '-v':
814 if o == '--output' or o == '-o':
822 # header['tagline'] = 'Lily was here %s -- automatically converted from ABC' % version
827 sys.stderr.write ('Processing `%s\'\n' % f)
830 out_filename = os.path.basename (re.sub ('.etf$', '.ly', f))
832 if out_filename == f:
833 out_filename = os.path.basename (f + '.ly')
835 sys.stderr.write ('Writing `%s\'' % out_filename)
840 fo = open (out_filename, 'w')
841 fo.write ('%% lily was here -- automatically converted by etf2ly from %s\n' % f)