3 # info mostly taken from looking at files. See also
4 # http://www.cs.uu.nl/~hanwen/lily-devel/etf.html
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)
25 # * automatic `deletion' of invalid items
29 program_name = 'etf2ly'
30 version = '@TOPLEVEL_VERSION@'
31 if version == '@' + 'TOPLEVEL_VERSION' + '@':
32 version = '(unknown version)' # uGUHGUHGHGUGH
41 finale_clefs= ['treble', 'alto', 'tenor', 'bass', 'percussion', 'treble8vb', 'bass8vb', 'baritone']
44 return finale_clefs[fin]
47 return open (f).read ()
49 # notename 0 == central C
50 distances = [0, 2, 4, 5, 7, 9, 11, 12]
51 def semitones (name, acc):
52 return (name / 7 ) * 12 + distances[name % 7] + acc
54 # represent pitches as (notename, alteration), relative to C-major scale
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)
92 def EDU_to_duration (edu):
107 def rat_to_lily_duration (rat):
111 while d and d % 2 == 0:
112 basedur = basedur << 1
115 str = 's%d' % basedur
117 str = str + '*%d' % n
119 str = str + '/%d' % d
134 def rat_simplify (r):
145 def rat_multiply (a,b):
149 return rat_simplify ((x*p, y*q))
155 return rat_simplify ((x*q + p*y, y*q))
163 def rat_subtract (a,b ):
164 return rat_add (a, rat_neg (b))
166 def lily_notename (tuple2):
168 nn = chr ((n+ 2)%7 + ord ('a'))
183 def __init__ (self, number):
184 self.start_note = number
187 def append_finale (self, fin):
188 self.finale.append (fin)
191 n = self.finale[0][2]*self.finale[0][3]
192 d = self.finale[0][0]*self.finale[0][1]
193 return rat_simplify( (n, d))
195 def dump_start (self):
196 return '\\times %d/%d { ' % self.factor ()
201 def calculate (self, chords):
202 edu_left = self.finale[0][0] * self.finale[0][1]
204 startch = chords[self.start_note]
206 while c and edu_left:
209 c.chord_prefix = self.dump_start () + c.chord_prefix
212 edu_left = edu_left - c.EDU_duration ()
214 c.chord_suffix = c.chord_suffix+ self.dump_end ()
218 sys.stderr.write ("\nHuh? Tuplet starting at entry %d was too short." % self.start_note)
221 def __init__ (self, number):
225 def append_entry (self, finale_e):
226 self.finale.append (finale_e)
228 def calculate (self, chords):
229 startnote = self.finale[0][5]
230 endnote = self.finale[3][2]
232 cs = chords[startnote]
238 cs.note_suffix = '(' + cs.note_suffix
239 ce.note_prefix = ce.note_prefix + ')'
241 sys.stderr.write ("""\nHuh? Slur no %d between (%d,%d), with %d notes""" % (self.number, startnote, endnote, len (chords)))
244 class Global_measure:
245 def __init__ (self, number):
248 self.keysignature = None
254 return `self.finale `
256 def set_timesig (self, finale):
257 (beats, fdur) = finale
258 (log, dots) = EDU_to_duration (fdur)
260 self.timesig = (beats, log)
265 def set_keysig (self, finale):
266 k = interpret_finale_key_sig (finale)
267 self.keysignature = k
268 self.scale = find_scale (k)
278 18: '"arp"' , # arpeggio
282 def __init__ (self, a,b, finale):
283 self.type = finale[0]
285 def calculate (self, chords):
286 c = chords[self.notenumber]
289 a = articulation_dict[self.type]
291 sys.stderr.write ("\nUnknown articulation no. %d on note no. %d" % (self.type, self.notenumber))
294 c.note_suffix = '-' + a + c.note_suffix
297 def __init__ (self, a,b , finale):
299 self.syllable = finale[1]
300 self.verse = finale[0]
301 def calculate (self, chords, lyrics):
302 self.chord = chords[self.chordnum]
305 def __init__ (self, number, body):
308 self.split_syllables ()
309 def split_syllables (self):
310 ss = re.split ('(-| +)', self.body)
316 septor = re.sub (" +", "", s)
317 septor = re.sub ("-", " -- ", septor)
318 syls[-1] = syls[-1] + septor
324 self.syllables = syls
329 for s in self.syllables[1:]:
330 line = line + ' ' + s
332 str = str + ' ' * 4 + line + '\n'
335 str = """\nverse%s = \\lyrics {\n %s}\n""" % (encodeint (self.number - 1) ,str)
340 def __init__(self, no):
342 self.frames = [0] * 4
346 self.global_measure = None
350 def add_finale_entry (self, entry):
351 self.finale.append (entry)
355 def calculate (self):
356 if len (self.finale) < 2:
357 sys.stderr.write ("Measure %d in staff %d has incomplete information.\n" % (self.number, self.staff.number))
364 self.clef = string.atoi (f0[0])
365 self.flags = string.atoi (f0[1])
366 fs = map (string.atoi, list (f0[2:]) + [f1[0]])
371 def __init__ (self, finale):
374 (number, start, end ) = finale
380 def set_measure (self, m):
383 def calculate (self):
387 for c in self.chords:
388 if c.grace and (lastch == None or (not lastch.grace)):
389 c.chord_prefix = r'\grace {' + c.chord_prefix
390 elif not c.grace and lastch and lastch.grace:
391 lastch.chord_suffix = lastch.chord_suffix + ' } '
398 str = '%% FR(%d)\n' % self.number
399 left = self.measure.global_measure.length ()
403 for c in self.chords:
404 add = c.ly_string () + ' '
405 if len (ln) + len(add) > 72:
406 str = str + ln + '\n'
409 left = rat_subtract (left, c.length ())
414 sys.stderr.write ("""\nHuh? Going backwards in frame no %d, start/end (%d,%d)""" % (self.number, self.start, self.end))
417 str = str + rat_to_lily_duration (left)
423 return chr ( i + ord ('A'))
426 def __init__ (self, number):
430 def get_measure (self, no):
431 if len (self.measures) <= no:
432 self.measures = self.measures + [None]* (1 + no - len (self.measures))
434 if self.measures[no] == None:
436 self.measures [no] =m
440 return self.measures[no]
442 return 'staff' + encodeint (self.number - 1)
443 def layerid (self, l):
444 return self.staffid() + 'layer%s' % chr (l -1 + ord ('A'))
446 def dump_time_key_sigs (self):
452 for m in self.measures[1:]:
453 if not m or not m.valid:
458 if last_key <> g.keysignature:
459 e = e + "\\key %s \\major; " % lily_notename (g.keysignature)
460 last_key = g.keysignature
461 if last_time <> g.timesig :
462 e = e + "\\time %d/%d; " % g.timesig
463 last_time = g.timesig
464 if last_clef <> m.clef :
465 e = e + '\\clef %s;' % lily_clef (m.clef)
469 k = k +' ' + rat_to_lily_duration (gap) + '\n'
473 gap = rat_add (gap, g.length ())
476 k = '%sglobal = \\notes { %s }\n\n ' % (self.staffid (), k)
484 for x in range (1,5): # 4 layers.
489 for m in self.measures[1:]:
490 if not m or not m.valid:
498 laystr = laystr +'} %s {\n ' % rat_to_lily_duration (gap)
500 laystr = laystr + fr.dump ()
502 gap = rat_add (gap, m.global_measure.length ())
506 laystr = '%s = \\notes { { %s } }\n\n' % (l, laystr)
510 str = str + self.dump_time_key_sigs ()
511 stafdef = '\\%sglobal' % self.staffid ()
513 stafdef = stafdef + ' \\' + i
516 str = str + '%s = \\context Staff = %s <\n %s\n >\n' % \
517 (self.staffid (), self.staffid (), stafdef)
524 def __init__ (self, finale_entry):
527 self.finale = finale_entry
532 self.note_suffix = ''
533 self.chord_suffix = ''
534 self.chord_prefix = ''
541 return self.frame.measure
547 l = (1, self.duration[0])
549 d = 1 << self.duration[1]
551 dotfact = rat_subtract ((2,1), (1,d))
552 mylen = rat_multiply (dotfact, l)
555 mylen = rat_multiply (mylen, self.tuplet.factor())
559 return self.finale[0][0]
561 def EDU_duration (self):
562 return self.finale[0][3]
563 def set_duration (self):
564 self.duration = EDU_to_duration(self.EDU_duration ())
565 def calculate (self):
566 self.find_realpitch ()
569 flag = self.finale[0][5]
570 if Chord.GRACE_MASK & flag:
574 def find_realpitch (self):
576 ((no, prev, next, dur, pos, entryflag, extended, follow), notelist) = self.finale
578 meas = self.measure ()
580 if not meas or not meas.global_measure :
581 print 'note %d not in measure' % self.number ()
582 elif not meas.global_measure.scale:
583 print 'note %d: no scale in this measure.' % self.number ()
593 scale = meas.global_measure.scale
594 (sn, sa) =scale[rest % 7]
595 sn = sn + (rest - (rest%7)) + 7
597 self.pitches.append ((sn, acc))
598 tiestart = tiestart or (flag & Chord.TIE_START_MASK)
600 self.chord_suffix = self.chord_suffix + ' ~ '
602 REST_MASK = 0x40000000L
603 TIE_START_MASK = 0x40000000L
604 GRACE_MASK = 0x00800000L
606 def ly_string (self):
611 if not (self.finale[0][5] & Chord.REST_MASK):
614 for p in self.pitches:
619 nn = lily_notename ((n,a))
632 s = s + '%s%d%s' % (nn, self.duration[0], '.'* self.duration[1])
635 s = 'r%d%s' % (self.duration[0] , '.'* self.duration[1])
636 s = self.note_prefix + s + self.note_suffix
637 if len (self.pitches) > 1:
640 s = self.chord_prefix + s + self.chord_suffix
643 GFre = re.compile(r"""^\^GF\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
644 BCre = re.compile (r"""^\^BC\(([0-9-]+)\) ([0-9-]+) .*$""")
645 eEre = re.compile(r"""^\^eE\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) \$([0-9A-Fa-f]+) ([0-9-]+) ([0-9-]+)""")
646 FRre = re.compile (r"""^\^FR\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
647 MSre = re.compile (r"""^\^MS\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
648 note_re = re.compile (r"""^ +([0-9-]+) \$([A-Fa-f0-9]+)""")
649 Sxre = re.compile (r"""^\^Sx\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
650 IMre = re.compile (r"""^\^IM\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
651 vere = re.compile(r"""^\^(ve|ch|se)\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
652 versere = re.compile(r"""^\^verse\(([0-9]+)\)(.*)\^end""")
654 TPre = re.compile(r"""^\^TP\(([0-9]+),([0-9]+)\) *([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
658 def __init__ (self, name):
659 self.measures = [None]
660 self.entries = [None]
663 self.tuplets = [None]
666 self.articulations = [None]
667 self.syllables = [None]
673 def get_global_measure (self, no):
674 if len (self.measures) <= no:
675 self.measures = self.measures + [None]* (1 + no - len (self.measures))
677 if self.measures[no] == None:
678 self.measures [no] = Global_measure (no)
680 return self.measures[no]
683 def get_staff(self,staffno):
684 if len (self.staffs) <= staffno:
685 self.staffs = self.staffs + [None] * (1 + staffno - len (self.staffs))
687 if self.staffs[staffno] == None:
688 self.staffs[staffno] = Staff (staffno)
690 return self.staffs[staffno]
693 def try_IS (self, l):
696 def try_BC (self, l):
699 bn = string.atoi (m.group (1))
700 where = string.atoi (m.group (2)) / 1024.0
705 (nil, num) = map (string.atoi, (m.groups ()[0:2]))
706 entries = map (string.atoi, (m.groups ()[2:]))
708 if self.tuplets[-1] == None or num <> self.tuplets[-1].start_note:
709 self.tuplets.append (Tuplet (num))
711 self.tuplets[-1].append_finale (entries)
713 def try_IM (self, l):
716 a = string.atoi (m.group (1))
717 b = string.atoi (m.group (2))
719 fin = map (string.atoi, m.groups ()[2:])
721 self.articulations.append (Articulation (a,b,fin))
723 def try_verse (self,l):
724 m = versere .match (l)
726 a = string.atoi (m.group (1))
729 body = re.sub (r"""\^[a-z]+\([^)]+\)""", "", body)
730 body = re.sub ("\^[a-z]+", "", body)
731 self.verses.append (Verse (a, body))
737 a = string.atoi (m.group (1))
738 b = string.atoi (m.group (2))
740 fin = map (string.atoi, m.groups ()[2:])
742 self.syllables.append (Syllable (a,b,fin))
744 def try_eE (self, l):
748 (no, prev, next, dur, pos, entryflag, extended, follow) = tup
749 (no, prev, next, dur, pos,extended, follow) \
750 = tuple (map (string.atoi, [no,prev,next,dur,pos,extended,follow]))
752 entryflag = string.atol (entryflag,16)
753 if no > len (self.entries):
754 sys.stderr.write ("\nHuh? Entry number to large,\nexpected %d got %d. Filling with void entries.\n" % (len(self.entries), no ))
755 while len (self.entries) <> no:
756 c = ((len (self.entries), 0, 0, 0, 0, 0L, 0, 0), [])
757 self.entries.append (c)
759 current_entry = ((no, prev, next, dur, pos, entryflag, extended, follow), [])
760 self.entries.append (current_entry)
766 slurno = string.atoi (m.group (1))
770 sl = self.slur_dict[slurno]
773 self.slur_dict[slurno] = sl
775 params = list (m.groups ()[1:])
776 params = map (string.atoi, params)
777 sl.append_entry (params)
783 (staffno,measno) = m.groups ()[0:2]
784 s = string.atoi (staffno)
785 me = string.atoi (measno)
787 entry = m.groups () [2:]
788 st = self.get_staff (s)
789 meas = st.get_measure (me)
790 meas.add_finale_entry (entry)
796 (frameno, startnote, endnote, foo, bar) = m.groups ()
797 (frameno, startnote, endnote) = tuple (map (string.atoi, [frameno, startnote, endnote]))
798 if frameno > len (self.frames):
799 sys.stderr.write ("Frame no %d missing, filling up to %d\n" % (len(self.frames), frameno))
800 while frameno <> len (self.frames):
801 self.frames.append (Frame ((len (self.frames), 0,0) ))
803 self.frames.append (Frame ((frameno, startnote, endnote)))
806 def try_MS (self, l):
809 measno = string.atoi (m.group (1))
810 keynum = string.atoi (m.group (3))
811 meas =self. get_global_measure (measno)
812 meas.set_keysig (keynum)
814 beats = string.atoi (m.group (4))
815 beatlen = string.atoi (m.group (5))
816 meas.set_timesig ((beats, beatlen))
820 def try_note (self, l):
821 m = note_re.match (l)
823 (pitch, flag) = m.groups ()
824 pitch = string.atoi (pitch)
825 flag = string.atol (flag,16)
826 self.entries[-1][1].append ((pitch,flag))
828 def parse (self, name):
829 sys.stderr.write ('parsing ...')
832 gulp = open (name).read ()
834 gulp = re.sub ('[\n\r]+', '\n', gulp)
835 ls = string.split (gulp, '\n')
846 m = self.try_note (l)
856 m = self.try_verse (l)
858 sys.stderr.write ('processing ...')
861 self.unthread_entries ()
863 for st in self.staffs[1:]:
867 for m in st.measures[1:]:
873 m.global_measure = self.measures[mno]
875 sys.stderr.write ("non-existent global measure %d" % mno)
878 frame_obj_list = [None]
879 for frno in m.frames:
880 fr = self.frames[frno]
881 frame_obj_list.append (fr)
883 m.frames = frame_obj_list
884 for fr in frame_obj_list[1:]:
890 fr.chords = self.get_thread (fr.start, fr.end)
895 for c in self.chords[1:]:
898 for f in self.frames[1:]:
901 for t in self.tuplets[1:]:
902 t.calculate (self.chords)
904 for s in self.slur_dict.values():
905 s.calculate (self.chords)
906 for s in self.articulations[1:]:
907 s.calculate (self.chords)
909 def get_thread (self, startno, endno):
915 c = self.chords[startno]
917 sys.stderr.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno, endno))
921 while c and c.number () <> endno:
933 for s in self.staffs[1:]:
935 str = str + '\n\n' + s.dump ()
936 staffs.append ('\\' + s.staffid ())
939 # should use \addlyrics ?
941 for v in self.verses[1:]:
944 if len (self.verses) > 1:
945 sys.stderr.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
948 str = str + '\\score { < %s > } ' % string.join (staffs)
954 return 'ETF FILE %s %s' % (self.measures, self.entries)
956 def unthread_entries (self):
958 for e in self.entries[1:]:
959 self.chords.append (Chord (e))
961 for e in self.chords[1:]:
962 e.prev = self.chords[e.finale[0][1]]
963 e.next = self.chords[e.finale[0][2]]
966 sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
969 print """Usage: etf2ly [OPTION]... ETF-FILE
971 Convert ETF to LilyPond.
975 -o,--output=FILE set output filename to FILE
976 -v,--version version information
978 Enigma Transport Format is a format used by Coda Music Technology's
979 Finale product. This program will convert a subset of ETF to a
980 ready-to-use lilypond file.
982 Report bugs to bug-gnu-music@gnu.org
984 Written by Han-Wen Nienhuys <hanwen@cs.uu.nl>
987 def print_version ():
988 print r"""etf2ly (GNU lilypond) %s
990 This is free software. It is covered by the GNU General Public License,
991 and you are welcome to change it and/or distribute copies of it under
992 certain conditions. Invoke as `midi2ly --warranty' for more information.
994 Copyright (c) 2000 by Han-Wen Nienhuys <hanwen@cs.uu.nl>
999 (options, files) = getopt.getopt (sys.argv[1:], 'vo:h', ['help','version', 'output='])
1005 if o== '--help' or o == '-h':
1008 if o == '--version' or o == '-v':
1012 if o == '--output' or o == '-o':
1024 sys.stderr.write ('Processing `%s\'\n' % f)
1026 if not out_filename:
1027 out_filename = os.path.basename (re.sub ('(?i).etf$', '.ly', f))
1029 if out_filename == f:
1030 out_filename = os.path.basename (f + '.ly')
1032 sys.stderr.write ('Writing `%s\'' % out_filename)
1037 fo = open (out_filename, 'w')
1038 fo.write ('%% lily was here -- automatically converted by etf2ly from %s\n' % f)