5 # * convertmusic (see sourceforge)
6 # * Margaret Cahill's thesis
7 # * http://www.codamusic.com/coda/Fin2000_pdk_download.asp (you have
8 # to register, but you don't need to have bought a code product
21 # * slur/stem directions
23 # * voices (2nd half of frame?)
25 # * beams (better use autobeam?)
27 program_name = 'etf2ly'
28 version = '@TOPLEVEL_VERSION@'
29 if version == '@' + 'TOPLEVEL_VERSION' + '@':
30 version = '(unknown version)' # uGUHGUHGHGUGH
41 finale_clefs= ['treble', 'alto', 'tenor', 'bass', 'percussion', 'treble8vb', 'bass8vb', 'baritone']
45 return finale_clefs[fin]
48 return open (f).read ()
50 # notename 0 == central C
51 distances = [0, 2, 4, 5, 7, 9, 11, 12]
52 def semitones (name, acc):
53 return (name / 7 ) * 12 + distances[name % 7] + acc
55 def transpose(orig, delta):
59 old_pitch =semitones (oname, oacc)
60 delta_pitch = semitones (dname, dacc)
61 nname = (oname + dname)
63 new_pitch = semitones (nname, nacc)
65 nacc = nacc - (new_pitch - old_pitch - delta_pitch)
71 # find transposition of C-major scale that belongs here.
72 def interpret_finale_key_sig (finale_id):
74 if 0 <= finale_id < 7:
76 p = transpose (p, (4,0)) # a fifth up
77 finale_id = finale_id - 1
78 elif 248 < finale_id <= 255:
79 while finale_id < 256:
80 p = transpose (p, (3,0))
81 finale_id = finale_id + 1
87 def find_scale (transposition):
88 cscale = map (lambda x: (x,0), range (0,7))
89 trscale = map(lambda x, k=transposition: transpose(x, k), cscale)
104 def rat_simplify (r):
115 def rat_multiply (a,b):
119 return rat_simplify ((x*p, y*q))
125 return rat_simplify ((x*q + p*y, y*q))
131 def rat_subtract (a,b ):
132 return rat_add (a, rat_neg (b))
134 def lily_notename (tuple2):
136 nn = chr ((n+ 2)%7 + ord ('a'))
150 def __init__ (self, number):
154 def append_entry (self, finale_e):
155 self.finale.append (finale_e)
157 def calculate (self, chords):
158 startnote = self.finale[0][5]
159 endnote = self.finale[3][2]
161 cs = chords[startnote]
162 cs.suffix = '(' + cs.suffix
164 ce.prefix = ce.prefix + ')'
166 class Global_measure:
167 def __init__ (self, number):
170 self.keysignature = None
175 def set_timesig (self, finale):
176 (beats, fdur) = finale
177 (log, dots) = EDU_to_duration (fdur)
179 self.timesig = (beats, log)
184 def set_keysig (self, finale):
185 k = interpret_finale_key_sig (finale)
186 self.keysignature = k
187 self.scale = find_scale (k)
192 def __init__(self, no):
198 self.global_measure = None
200 def add_finale_entry (self, entry):
201 self.finale.append (entry)
203 def calculate (self):
207 self.clef = string.atoi (f0[0])
208 self.flags = string.atoi (f0[1])
209 fs = map (string.atoi, list (f0[2:]) + [f1[0]])
214 def __init__ (self, finale):
217 (number, start, end ) = finale
223 def set_measure (self, m):
228 left = self.measure.global_measure.length ()
229 for c in self.chords:
230 str = str + c.ly_string () + ' '
231 left = rat_subtract (left, c.length ())
234 print self.start, self.end
238 str = str + 's*%d/%d' % left
244 def __init__ (self, number):
248 def get_measure (self, no):
249 if len (self.measures) <= no:
250 self.measures = self.measures + [None]* (1 + no - len (self.measures))
252 if self.measures[no] == None:
253 self.measures [no] = Measure (no)
255 return self.measures[no]
257 return 'staff%s'% chr (self.number - 1 +ord ('A'))
258 def layerid (self, l):
259 return self.staffid() + 'layer%s' % chr (l -1 + ord ('A'))
261 def dump_time_key_sigs (self):
267 for m in self.measures[1:]:
270 if last_key <> g.keysignature:
271 e = e + "\\key %s \\major; " % lily_notename (g.keysignature)
272 last_key = g.keysignature
273 if last_time <> g.timesig :
274 e = e + "\\time %d/%d; " % g.timesig
275 last_time = g.timesig
276 if last_clef <> m.clef :
277 e = e + '\\clef %s;' % lily_clef (m.clef)
281 k = k +' s1*%d/%d \n ' % gap
285 gap = rat_add (gap, g.length ())
288 k = '%sglobal = \\notes { %s }\n\n ' % (self.staffid (), k)
296 for x in range (1,5): # 4 layers.
301 for m in self.measures[1:]:
306 laystr = laystr +'} s1*%d/%d {\n ' % gap
308 laystr = laystr + fr.dump ()
310 gap = rat_add (gap, m.global_measure.length ())
314 laystr = '%s = \\notes { { %s } }\n\n' % (l, laystr)
318 str = str + self.dump_time_key_sigs ()
319 stafdef = '\\%sglobal' % self.staffid ()
321 stafdef = stafdef + ' \\' + i
324 str = str + '%s = \\context Staff = %s <\n %s\n >\n' % \
325 (self.staffid (), self.staffid (), stafdef)
329 def EDU_to_duration (edu):
345 def __init__ (self, finale_entry):
348 self.finale = finale_entry
357 return self.frame.measure
360 l = (1, self.duration[0])
362 d = 1 << self.duration[1]
364 dotfact = rat_subtract ((2,1), (1,d))
365 return rat_multiply (dotfact, l)
368 return self.finale[0][0]
369 def set_duration (self):
370 ((no, prev, next, dur, pos, entryflag, extended, follow),
371 notelist) = self.finale
372 self.duration = EDU_to_duration(dur)
373 def find_realpitch (self):
375 ((no, prev, next, dur, pos, entryflag, extended, follow), notelist) = self.finale
377 meas = self.measure ()
379 if not meas or not meas.global_measure :
380 print 'note %d not in measure' % self.number ()
381 elif not meas.global_measure.scale:
382 print 'note %d: no scale in this measure.' % self.number ()
392 scale = meas.global_measure.scale
393 (sn, sa) =scale[rest % 7]
394 sn = sn + (rest - (rest%7)) + 7
396 self.pitches.append ((sn, acc))
397 tiestart = tiestart or (flag & Chord.TIE_START_MASK)
399 self.suffix = self.suffix + ' ~ '
401 REST_MASK = 0x40000000L
402 TIE_START_MASK = 0x40000000L
403 def ly_string (self):
408 if not (self.finale[0][5] & Chord.REST_MASK):
411 for p in self.pitches:
416 nn = lily_notename ((n,a))
429 s = s + '%s%d%s' % (nn, self.duration[0], '.'* self.duration[1])
431 if len (self.pitches) > 1:
433 elif not self.pitches:
434 s = 'r%d%s' % (self.duration[0] , '.'* self.duration[1])
436 s = self.prefix + s + self.suffix
439 GFre = re.compile(r"""^\^GF\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
440 BCre = re.compile (r"""^\^BC\(([0-9-]+)\) ([0-9-]+) .*$""")
441 eEre = re.compile(r"""^\^eE\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) \$([0-9A-Fa-f]+) ([0-9-]+) ([0-9-]+)""")
442 FRre = re.compile (r"""^\^FR\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
443 MSre = re.compile (r"""^\^MS\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
444 note_re = re.compile (r"""^ +([0-9-]+) \$([A-Fa-f0-9]+)""")
445 Sxre = re.compile (r"""^\^Sx\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
448 def __init__ (self, name):
449 self.measures = [None]
450 self.entries = [None]
459 def get_global_measure (self, no):
460 if len (self.measures) <= no:
461 self.measures = self.measures + [None]* (1 + no - len (self.measures))
463 if self.measures[no] == None:
464 self.measures [no] = Global_measure (no)
466 return self.measures[no]
469 def get_staff(self,staffno):
470 if len (self.staffs) <= staffno:
471 self.staffs = self.staffs + [None] * (1 + staffno - len (self.staffs))
473 if self.staffs[staffno] == None:
474 self.staffs[staffno] = Staff (staffno)
476 return self.staffs[staffno]
479 def try_IS (self, l):
482 def try_BC (self, l):
485 bn = string.atoi (m.group (1))
486 where = string.atoi (m.group (2)) / 1024.0
489 def try_eE (self, l):
493 (no, prev, next, dur, pos, entryflag, extended, follow) = tup
494 (no, prev, next, dur, pos,extended, follow) \
495 = tuple (map (string.atoi, [no,prev,next,dur,pos,extended,follow]))
497 entryflag = string.atol (entryflag,16)
498 assert (no==len (self.entries))
499 current_entry = ((no, prev, next, dur, pos, entryflag, extended, follow), [])
500 self.entries.append (current_entry)
506 slurno = string.atoi (m.group (1))
508 if len (self.slurs) == slurno:
509 self.slurs.append (Slur (slurno))
511 params = list (m.groups ()[1:])
512 params = map (string.atoi, params)
513 self.slurs[-1].append_entry (params)
519 (staffno,measno) = m.groups ()[0:2]
520 s = string.atoi (staffno)
521 me = string.atoi (measno)
523 entry = m.groups () [2:]
524 st = self.get_staff (s)
525 meas = st.get_measure (me)
526 meas.add_finale_entry (entry)
532 (frameno, startnote, endnote, foo, bar) = m.groups ()
533 (frameno, startnote, endnote) = tuple (map (string.atoi, [frameno, startnote, endnote]))
534 self.frames.append (Frame ((frameno, startnote, endnote)))
537 def try_MS (self, l):
540 measno = string.atoi (m.group (1))
541 keynum = string.atoi (m.group (3))
542 meas =self. get_global_measure (measno)
543 meas.set_keysig (keynum)
545 beats = string.atoi (m.group (4))
546 beatlen = string.atoi (m.group (5))
547 meas.set_timesig ((beats, beatlen))
551 def try_note (self, l):
552 m = note_re.match (l)
554 (pitch, flag) = m.groups ()
555 pitch = string.atoi (pitch)
556 flag = string.atol (flag,16)
557 self.entries[-1][1].append ((pitch,flag))
559 def parse (self, name):
560 sys.stderr.write ('parsing ...')
563 ls = open (name).readlines ()
573 m = self.try_note (l)
579 sys.stderr.write ('processing ...')
582 self.unthread_entries ()
584 for st in self.staffs[1:]:
588 for m in st.measures[1:]:
589 m.global_measure = self.measures[mno]
592 frame_obj_list = [None]
593 for frno in m.frames:
594 fr = self.frames[frno]
595 frame_obj_list.append (fr)
597 m.frames = frame_obj_list
598 for fr in frame_obj_list[1:]:
604 fr.chords = self.get_thread (fr.start, fr.end)
609 for c in self.chords[1:]:
613 for s in self.slurs [1:]:
614 s.calculate (self.chords)
616 def get_thread (self, startno, endno):
619 c = self.chords[startno]
620 while c and c.number () <> endno:
632 for s in self.staffs[1:]:
634 str = str + '\n\n' + s.dump ()
635 staffs.append ('\\' + s.staffid ())
638 str = str + '\\score { < %s > } ' % string.join (staffs)
647 def unthread_entries (self):
649 for e in self.entries[1:]:
650 self.chords.append (Chord (e))
652 for e in self.chords[1:]:
653 e.prev = self.chords[e.finale[0][1]]
654 e.next = self.chords[e.finale[0][2]]
662 sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
666 Convert ETF to LilyPond.
668 Usage: etf2ly [OPTION]... ETF-FILE
672 -o, --output=FILE set output filename to FILE
673 -v, --version version information
675 Enigma Transport Format is a format used by Coda Music Technology's
676 Finale product. This program will convert a subset of ETF to a
677 ready-to-use lilypond file.
682 def print_version ():
683 print r"""etf2ly (GNU lilypond) %s""" % version
687 (options, files) = getopt.getopt (sys.argv[1:], 'vo:h', ['help','version', 'output='])
693 if o== '--help' or o == '-h':
696 if o == '--version' or o == '-v':
700 if o == '--output' or o == '-o':
708 # header['tagline'] = 'Lily was here %s -- automatically converted from ABC' % version
713 sys.stderr.write ('Processing `%s\'\n' % f)
716 out_filename = os.path.basename (re.sub ('.etf$', '.ly', f))
718 if out_filename == f:
719 out_filename = os.path.basename (f + '.ly')
721 sys.stderr.write ('Writing `%s\'' % out_filename)
726 fo = open (out_filename, 'w')
727 fo.write ('%% lily was here -- automatically converted by etf2ly from %s\n' % f)