3 # info mostly taken from looking at files. See also
4 # http://lilypond.org/wiki/?EnigmaTransportFormat
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 # * empty measures (eg. twopt03.etf from freenote)
36 program_name = sys.argv[0]
38 version = '@TOPLEVEL_VERSION@'
39 if version == '@' + 'TOPLEVEL_VERSION' + '@':
40 version = '(unknown version)' # uGUHGUHGHGUGH
42 finale_clefs= ['treble', 'alto', 'tenor', 'bass', 'percussion', 'treble_8', 'bass_8', 'baritone']
46 return finale_clefs[fin]
48 sys.stderr.write ( '\nHuh? Found clef number %d\n' % fin)
55 return open (f).read ()
57 # notename 0 == central C
58 distances = [0, 2, 4, 5, 7, 9, 11, 12]
59 def semitones (name, acc):
60 return (name / 7 ) * 12 + distances[name % 7] + acc
62 # represent pitches as (notename, alteration), relative to C-major scale
63 def transpose(orig, delta):
67 old_pitch =semitones (oname, oacc)
68 delta_pitch = semitones (dname, dacc)
69 nname = (oname + dname)
71 new_pitch = semitones (nname, nacc)
73 nacc = nacc - (new_pitch - old_pitch - delta_pitch)
79 def interpret_finale_key_sig (finale_id):
81 find the transposition of C-major scale that belongs here.
83 we are not going to insert the correct major/minor, we only want to
84 have the correct number of accidentals
90 bank_number = finale_id >> 8
91 accidental_bits = finale_id & 0xff
93 if 0 <= accidental_bits < 7:
94 while accidental_bits > 0:
95 p = transpose (p, (4,0)) # a fifth up
96 accidental_bits = accidental_bits - 1
97 elif 248 < accidental_bits <= 255:
98 while accidental_bits < 256:
99 p = transpose (p, (3,0))
100 accidental_bits = accidental_bits + 1
104 p = transpose (p, (5, 0))
107 return KeySignature (p, bank_number)
110 def find_scale (keysig):
111 cscale = map (lambda x: (x,0), range (0,7))
112 print "cscale: ", cscale
113 ascale = map (lambda x: (x,0), range (-2,5))
114 print "ascale: ", ascale
115 transposition = keysig.pitch
116 if keysig.sig_type == 1:
117 transposition = transpose(transposition, (2, -1))
118 transposition = (transposition[0] % 7, transposition[1])
119 trscale = map(lambda x, k=transposition: transpose(x, k), ascale)
121 trscale = map(lambda x, k=transposition: transpose(x, k), cscale)
122 print "trscale: ", trscale
125 def EDU_to_duration (edu):
140 def rat_to_lily_duration (rat):
144 while d and d % 2 == 0:
145 basedur = basedur << 1
148 str = 's%d' % basedur
150 str = str + '*%d' % n
152 str = str + '/%d' % d
167 def rat_simplify (r):
178 def rat_multiply (a,b):
182 return rat_simplify ((x*p, y*q))
188 return rat_simplify ((x*q + p*y, y*q))
196 def rat_subtract (a,b ):
197 return rat_add (a, rat_neg (b))
199 def lily_notename (tuple2):
201 nn = chr ((n+ 2)%7 + ord ('a'))
203 return nn + {-2:'eses', -1:'es', 0:'', 1:'is', 2:'isis'}[a]
207 def __init__ (self, number):
208 self.start_note = number
211 def append_finale (self, fin):
212 self.finale.append (fin)
215 n = self.finale[0][2]*self.finale[0][3]
216 d = self.finale[0][0]*self.finale[0][1]
217 return rat_simplify( (n, d))
219 def dump_start (self):
220 return '\\times %d/%d { ' % self.factor ()
225 def calculate (self, chords):
226 edu_left = self.finale[0][0] * self.finale[0][1]
228 startch = chords[self.start_note]
230 while c and edu_left:
233 c.chord_prefix = self.dump_start () + c.chord_prefix
236 edu_left = edu_left - c.EDU_duration ()
238 c.chord_suffix = c.chord_suffix+ self.dump_end ()
242 sys.stderr.write ("\nHuh? Tuplet starting at entry %d was too short." % self.start_note)
245 def __init__ (self, number, params):
249 def append_entry (self, finale_e):
250 self.finale.append (finale_e)
252 def calculate (self, chords):
253 startnote = self.finale[5]
254 endnote = self.finale[3*6 + 2]
256 cs = chords[startnote]
262 cs.note_suffix = '-(' + cs.note_suffix
263 ce.note_suffix = ce.note_suffix + '-)'
266 sys.stderr.write ("""\nHuh? Slur no %d between (%d,%d), with %d notes""" % (self.number, startnote, endnote, len (chords)))
269 class Global_measure:
270 def __init__ (self, number):
273 self.key_signature = None
281 return `self.finale `
283 def set_timesig (self, finale):
284 (beats, fdur) = finale
285 (log, dots) = EDU_to_duration (fdur)
293 sys.stderr.write ("\nHuh? Beat duration has dots? (EDU Duration = %d)" % fdur)
294 self.timesig = (beats, log)
299 def set_key_sig (self, finale):
300 k = interpret_finale_key_sig (finale)
301 self.key_signature = k
302 self.scale = find_scale (k)
304 def set_flags (self,flag1, flag2):
306 # flag1 isn't all that interesting.
311 self.repeats.append ('start')
313 self.repeats.append ('stop')
317 self.repeats.append ('bracket')
330 class Articulation_def:
331 def __init__ (self, n, a, b):
332 self.finale_glyph = a & 0xff
337 return articulation_dict[self.finale_glyph]
339 sys.stderr.write ("\nUnknown articulation no. %d" % self.finale_glyph)
340 sys.stderr.write ("\nPlease add an entry to articulation_dict in the Python source")
344 def __init__ (self, a,b, finale):
345 self.definition = finale[0]
348 def calculate (self, chords, defs):
349 c = chords[self.notenumber]
351 adef = defs[self.definition]
355 sys.stderr.write ("\nThis happened on note %d" % self.notenumber)
357 c.note_suffix = '-' + lystr
360 def __init__ (self, a,b , finale):
362 self.syllable = finale[1]
363 self.verse = finale[0]
364 def calculate (self, chords, lyrics):
365 self.chord = chords[self.chordnum]
368 def __init__ (self, number, body):
371 self.split_syllables ()
372 def split_syllables (self):
373 ss = re.split ('(-| +)', self.body)
379 septor = re.sub (" +", "", s)
380 septor = re.sub ("-", " -- ", septor)
381 syls[-1] = syls[-1] + septor
387 self.syllables = syls
392 for s in self.syllables[1:]:
393 line = line + ' ' + s
395 str = str + ' ' * 4 + line + '\n'
398 str = """\nverse%s = \\lyrics {\n %s}\n""" % (encodeint (self.number - 1) ,str)
402 def __init__(self, pitch, sig_type = 0):
404 self.sig_type = sig_type
406 def signature_type (self):
407 if self.sig_type == 1:
410 # really only for 0, but we only know about 0 and 1
413 def equal (self, other):
414 if other and other.pitch == self.pitch and other.sig_type == self.sig_type:
421 def __init__(self, no):
423 self.frames = [0] * 4
427 self.global_measure = None
434 def calculate (self):
437 if len (self.finale) < 2:
441 self.frames = [fs[0]]
450 def __init__ (self, finale):
453 (number, start, end ) = finale
459 def set_measure (self, m):
462 def calculate (self):
466 for c in self.chords:
467 if c.grace and (lastch == None or (not lastch.grace)):
468 c.chord_prefix = r'\grace {' + c.chord_prefix
469 elif not c.grace and lastch and lastch.grace:
470 lastch.chord_suffix = lastch.chord_suffix + ' } '
477 str = '%% FR(%d)\n' % self.number
478 left = self.measure.global_measure.length ()
482 for c in self.chords:
483 add = c.ly_string () + ' '
484 if len (ln) + len(add) > 72:
485 str = str + ln + '\n'
488 left = rat_subtract (left, c.length ())
493 sys.stderr.write ("""\nHuh? Going backwards in frame no %d, start/end (%d,%d)""" % (self.number, self.start, self.end))
496 str = str + rat_to_lily_duration (left)
502 return chr ( i + ord ('A'))
505 def __init__ (self, number):
509 def get_measure (self, no):
510 fill_list_to (self.measures, no)
512 if self.measures[no] == None:
514 self.measures [no] =m
517 return self.measures[no]
519 return 'staff' + encodeint (self.number - 1)
520 def layerid (self, l):
521 return self.staffid() + 'layer%s' % chr (l -1 + ord ('A'))
523 def dump_time_key_sigs (self):
529 for m in self.measures[1:]:
530 if not m or not m.valid:
537 if g.key_signature and not g.key_signature.equal(last_key):
538 pitch= g.key_signature.pitch
539 e = e + "\\key %s %s " % (lily_notename (pitch),
540 g.key_signature.signature_type())
542 last_key = g.key_signature
543 if last_time <> g.timesig :
544 e = e + "\\time %d/%d " % g.timesig
545 last_time = g.timesig
547 if 'start' in g.repeats:
548 e = e + ' \\bar "|:" '
551 # we don't attempt voltas since they fail easily.
552 if 0 : # and g.repeat_bar == '|:' or g.repeat_bar == ':|:' or g.bracket:
554 if g.repeat_bar == '|:' or g.repeat_bar == ':|:' or g.bracket == 'end':
558 if g.bracket == 'start':
561 str = string.join (map (lambda x: '(volta %s)' % x, strs))
563 e = e + ' \\set Score.repeatCommands = #\'(%s) ' % str
568 if last_clef <> m.clef :
569 e = e + '\\clef "%s"' % lily_clef (m.clef)
573 k = k +' ' + rat_to_lily_duration (gap) + '\n'
578 gap = rat_add (gap, g.length ())
579 if 'stop' in g.repeats:
580 k = k + ' \\bar ":|" '
582 k = '%sglobal = \\notes { %s }\n\n ' % (self.staffid (), k)
590 for x in range (1,5): # 4 layers.
595 for m in self.measures[1:]:
596 if not m or not m.valid:
597 sys.stderr.write ("Skipping non-existant or invalid measure\n")
604 sys.stderr.write ("Skipping nonexistent frame %d\n" % x)
605 laystr = laystr + "%% non existent frame %d (skipped) \n" % x
609 laystr = laystr +'} %s {\n ' % rat_to_lily_duration (gap)
611 laystr = laystr + fr.dump ()
613 if m.global_measure :
614 gap = rat_add (gap, m.global_measure.length ())
617 "No global measure for staff %d measure %d\n"
618 % (self.number, m.number))
621 laystr = '%s = \\notes { { %s } }\n\n' % (l, laystr)
625 str = str + self.dump_time_key_sigs ()
626 stafdef = '\\%sglobal' % self.staffid ()
628 stafdef = stafdef + ' \\' + i
631 str = str + '%s = \\context Staff = %s <<\n %s\n >>\n' % \
632 (self.staffid (), self.staffid (), stafdef)
641 return [(l[0], l[1])] + ziplist (l[2:])
645 def __init__ (self, number, contents):
648 self.finale = contents[:7]
650 self.notelist = ziplist (contents[7:])
656 self.note_suffix = ''
657 self.chord_suffix = ''
658 self.chord_prefix = ''
665 return self.frame.measure
671 l = (1, self.duration[0])
673 d = 1 << self.duration[1]
675 dotfact = rat_subtract ((2,1), (1,d))
676 mylen = rat_multiply (dotfact, l)
679 mylen = rat_multiply (mylen, self.tuplet.factor())
683 def EDU_duration (self):
684 return self.finale[2]
685 def set_duration (self):
686 self.duration = EDU_to_duration(self.EDU_duration ())
688 def calculate (self):
689 self.find_realpitch ()
692 flag = self.finale[4]
693 if Chord.GRACE_MASK & flag:
697 def find_realpitch (self):
699 meas = self.measure ()
701 if not meas or not meas.global_measure :
702 sys.stderr.write ('note %d not in measure\n' % self.number)
703 elif not meas.global_measure.scale:
704 sys.stderr.write ('note %d: no scale in this measure.' % self.number)
707 for p in self.notelist:
717 scale = meas.global_measure.scale
718 (sn, sa) =scale[rest % 7]
719 sn = sn + (rest - (rest%7)) + 7
721 self.pitches.append ((sn, acc))
722 tiestart = tiestart or (flag & Chord.TIE_START_MASK)
724 self.chord_suffix = self.chord_suffix + ' ~ '
726 REST_MASK = 0x40000000L
727 TIE_START_MASK = 0x40000000L
728 GRACE_MASK = 0x00800000L
730 def ly_string (self):
736 if not (self.finale[4] & Chord.REST_MASK):
739 for p in self.pitches:
744 nn = lily_notename ((n,a))
761 if len (self.pitches) > 1:
764 s = s + '%d%s' % (self.duration[0], '.'* self.duration[1])
765 s = self.note_prefix + s + self.note_suffix
767 s = self.chord_prefix + s + self.chord_suffix
772 def fill_list_to (list, no):
774 Add None to LIST until it contains entry number NO.
776 while len (list) <= no:
777 list.extend ([None] * (no - len(list) + 1))
780 def read_finale_value (str):
782 Pry off one value from STR. The value may be $hex, decimal, or "string".
783 Return: (value, rest-of-STR)
785 while str and str[0] in ' \t\n':
795 while str and str[0] in '0123456789ABCDEF':
800 return (string.atol (hex, 16), str)
804 while str and str[0] <> '"':
809 elif str[0] in '-0123456789':
811 while str and str[0] in '-0123456789':
815 return (string.atoi (dec), str)
817 sys.stderr.write ("Can't convert `%s'\n" % str)
823 def parse_etf_file (fn, tag_dict):
825 """ Read FN, putting ETF info into
826 a giant dictionary. The keys of TAG_DICT indicate which tags
827 to put into the dict.
830 sys.stderr.write ('parsing ... ' )
833 gulp = re.sub ('[\n\r]+', '\n', f.read ())
834 ls = string.split (gulp, '\n^')
837 for k in tag_dict.keys ():
838 etf_file_dict[k] = {}
845 m = re.match ('^([a-zA-Z0-9&]+)\(([^)]+)\)', l)
846 if m and tag_dict.has_key (m.group (1)):
849 indices = tuple (map (string.atoi, string.split (m.group (2), ',')))
850 content = l[m.end (2)+1:]
853 tdict = etf_file_dict[tag]
854 if not tdict.has_key (indices):
860 if tag == 'verse' or tag == 'block':
861 m2 = re.match ('(.*)\^end', content)
863 parsed = [m2.group (1)]
866 (v, content) = read_finale_value (content)
870 tdict [indices].extend (parsed)
872 last_indices = indices
877 # let's not do this: this really confuses when eE happens to be before a ^text.
878 # if last_tag and last_indices:
879 # etf_file_dict[last_tag][last_indices].append (l)
881 sys.stderr.write ('\n')
889 def __init__ (self, name):
890 self.measures = [None]
893 self.tuplets = [None]
896 self.articulations = [None]
897 self.syllables = [None]
899 self.articulation_defs = [None]
904 def get_global_measure (self, no):
905 fill_list_to (self.measures, no)
906 if self.measures[no] == None:
907 self.measures [no] = Global_measure (no)
909 return self.measures[no]
912 def get_staff(self,staffno):
913 fill_list_to (self.staffs, staffno)
914 if self.staffs[staffno] == None:
915 self.staffs[staffno] = Staff (staffno)
917 return self.staffs[staffno]
920 def try_IS (self, indices, contents):
923 def try_BC (self, indices, contents):
925 where = contents[0] / 1024.0
926 def try_TP(self, indices, contents):
929 if self.tuplets[-1] == None or num <> self.tuplets[-1].start_note:
930 self.tuplets.append (Tuplet (num))
932 self.tuplets[-1].append_finale (contents)
934 def try_IM (self, indices, contents):
937 self.articulations.append (Articulation (a,b,fin))
938 def try_verse (self, indices, contents):
942 body = re.sub (r"""\^[a-z]+\([^)]+\)""", "", body)
943 body = re.sub ("\^[a-z]+", "", body)
944 self.verses.append (Verse (a, body))
945 def try_ve (self,indices, contents):
947 self.syllables.append (Syllable (a,b,contents))
949 def try_eE (self,indices, contents):
951 (prev, next, dur, pos, entryflag, extended, follow) = contents[:7]
953 fill_list_to (self.chords, no)
954 self.chords[no] =Chord (no, contents)
956 def try_Sx(self,indices, contents):
958 fill_list_to (self.slurs, slurno)
959 self.slurs[slurno] = Slur(slurno, contents)
961 def try_IX (self, indices, contents):
968 ix = self.articulation_defs[n]
970 ix = Articulation_def (n,a,b)
971 self.articulation_defs.append (Articulation_def (n, a, b))
973 def try_GF(self, indices, contents):
974 (staffno,measno) = indices
976 st = self.get_staff (staffno)
977 meas = st.get_measure (measno)
978 meas.finale = contents
980 def try_FR(self, indices, contents):
981 frameno = indices [0]
983 startnote = contents[0]
984 endnote = contents[1]
986 fill_list_to (self.frames, frameno)
988 self.frames[frameno] = Frame ((frameno, startnote, endnote))
990 def try_MS (self, indices, contents):
993 meas =self. get_global_measure (measno)
995 meas.set_key_sig (keynum)
998 beatlen = contents[3]
999 meas.set_timesig ((beats, beatlen))
1001 meas_flag1 = contents[4]
1002 meas_flag2 = contents[5]
1004 meas.set_flags (meas_flag1, meas_flag2);
1014 'verse' : try_verse,
1022 def parse (self, etf_dict):
1023 sys.stderr.write ('reconstructing ...')
1026 for (tag,routine) in Etf_file.routine_dict.items ():
1027 ks = etf_dict[tag].keys ()
1030 routine (self, k, etf_dict[tag][k])
1032 sys.stderr.write ('processing ...')
1035 self.unthread_entries ()
1037 for st in self.staffs[1:]:
1041 for m in st.measures[1:]:
1047 m.global_measure = self.measures[mno]
1049 sys.stderr.write ("Non-existent global measure %d" % mno)
1052 frame_obj_list = [None]
1053 for frno in m.frames:
1055 fr = self.frames[frno]
1056 frame_obj_list.append (fr)
1058 sys.stderr.write ("\nNon-existent frame %d" % frno)
1060 m.frames = frame_obj_list
1061 for fr in frame_obj_list[1:]:
1067 fr.chords = self.get_thread (fr.start, fr.end)
1072 for c in self.chords[1:]:
1076 for f in self.frames[1:]:
1080 for t in self.tuplets[1:]:
1081 t.calculate (self.chords)
1083 for s in self.slurs[1:]:
1085 s.calculate (self.chords)
1087 for s in self.articulations[1:]:
1088 s.calculate (self.chords, self.articulation_defs)
1090 def get_thread (self, startno, endno):
1096 c = self.chords[startno]
1098 sys.stderr.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno, endno))
1102 while c and c.number <> endno:
1114 for s in self.staffs[1:]:
1116 str = str + '\n\n' + s.dump ()
1117 staffs.append ('\\' + s.staffid ())
1120 # should use \addlyrics ?
1122 for v in self.verses[1:]:
1123 str = str + v.dump()
1125 if len (self.verses) > 1:
1126 sys.stderr.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
1129 str = str + '\\score { << %s >> } ' % string.join (staffs)
1135 return 'ETF FILE %s %s' % (self.measures, self.entries)
1137 def unthread_entries (self):
1138 for e in self.chords[1:]:
1142 e.prev = self.chords[e.finale[0]]
1143 e.next = self.chords[e.finale[1]]
1146 sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
1149 sys.stdout.write("""Usage: etf2ly [OPTIONS]... ETF-FILE
1151 Convert ETF to LilyPond.
1154 -h, --help print this help
1155 -o, --output=FILE set output filename to FILE
1156 -v, --version show version information
1158 Enigma Transport Format is a format used by Coda Music Technology's
1159 Finale product. This program will convert a subset of ETF to a
1160 ready-to-use lilypond file.
1162 Report bugs to bug-lilypond@gnu.org.
1164 Written by Han-Wen Nienhuys <hanwen@cs.uu.nl>.
1168 def print_version ():
1169 sys.stdout.write (r"""etf2ly (GNU lilypond) %s
1171 This is free software. It is covered by the GNU General Public License,
1172 and you are welcome to change it and/or distribute copies of it under
1173 certain conditions. Invoke as `midi2ly --warranty' for more information.
1175 Copyright (c) 2000--2004 by Han-Wen Nienhuys <hanwen@cs.uu.nl>
1180 (options, files) = getopt.getopt (sys.argv[1:], 'vo:h', ['help','version', 'output='])
1186 if o== '--help' or o == '-h':
1189 if o == '--version' or o == '-v':
1193 if o == '--output' or o == '-o':
1206 sys.stderr.write ('Processing `%s\'\n' % f)
1208 dict = parse_etf_file (f, Etf_file.routine_dict)
1210 if not out_filename:
1211 out_filename = os.path.basename (re.sub ('(?i).etf$', '.ly', f))
1213 if out_filename == f:
1214 out_filename = os.path.basename (f + '.ly')
1216 sys.stderr.write ('Writing `%s\'' % out_filename)
1221 fo = open (out_filename, 'w')
1222 fo.write ('%% lily was here -- automatically converted by etf2ly from %s\n' % f)