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)
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', 'treble_8', 'bass_8', 'baritone']
45 return finale_clefs[fin]
47 sys.stderr.write ( '\nHuh? Found clef number %d\n' % fin)
54 return open (f).read ()
56 # notename 0 == central C
57 distances = [0, 2, 4, 5, 7, 9, 11, 12]
58 def semitones (name, acc):
59 return (name / 7 ) * 12 + distances[name % 7] + acc
61 # represent pitches as (notename, alteration), relative to C-major scale
62 def transpose(orig, delta):
66 old_pitch =semitones (oname, oacc)
67 delta_pitch = semitones (dname, dacc)
68 nname = (oname + dname)
70 new_pitch = semitones (nname, nacc)
72 nacc = nacc - (new_pitch - old_pitch - delta_pitch)
78 def interpret_finale_key_sig (finale_id):
80 find the transposition of C-major scale that belongs here.
82 we are not going to insert the correct major/minor, we only want to
83 have the correct number of accidentals
89 bank_number = finale_id >> 8
90 accidental_bits = finale_id & 0xff
92 if 0 <= accidental_bits < 7:
93 while accidental_bits > 0:
94 p = transpose (p, (4,0)) # a fifth up
95 accidental_bits = accidental_bits - 1
96 elif 248 < accidental_bits <= 255:
97 while accidental_bits < 256:
98 p = transpose (p, (3,0))
99 accidental_bits = accidental_bits + 1
103 p = transpose (p, (5, 0))
106 return KeySignature (p, bank_number)
109 def find_scale (keysig):
110 cscale = map (lambda x: (x,0), range (0,7))
111 print "cscale: ", cscale
112 ascale = map (lambda x: (x,0), range (-2,5))
113 print "ascale: ", ascale
114 transposition = keysig.pitch
115 if keysig.sig_type == 1:
116 transposition = transpose(transposition, (2, -1))
117 transposition = (transposition[0] % 7, transposition[1])
118 trscale = map(lambda x, k=transposition: transpose(x, k), ascale)
120 trscale = map(lambda x, k=transposition: transpose(x, k), cscale)
121 print "trscale: ", trscale
124 def EDU_to_duration (edu):
139 def rat_to_lily_duration (rat):
143 while d and d % 2 == 0:
144 basedur = basedur << 1
147 str = 's%d' % basedur
149 str = str + '*%d' % n
151 str = str + '/%d' % d
166 def rat_simplify (r):
177 def rat_multiply (a,b):
181 return rat_simplify ((x*p, y*q))
187 return rat_simplify ((x*q + p*y, y*q))
195 def rat_subtract (a,b ):
196 return rat_add (a, rat_neg (b))
198 def lily_notename (tuple2):
200 nn = chr ((n+ 2)%7 + ord ('a'))
202 return nn + {-2:'eses', -1:'es', 0:'', 1:'is', 2:'isis'}[a]
206 def __init__ (self, number):
207 self.start_note = number
210 def append_finale (self, fin):
211 self.finale.append (fin)
214 n = self.finale[0][2]*self.finale[0][3]
215 d = self.finale[0][0]*self.finale[0][1]
216 return rat_simplify( (n, d))
218 def dump_start (self):
219 return '\\times %d/%d { ' % self.factor ()
224 def calculate (self, chords):
225 edu_left = self.finale[0][0] * self.finale[0][1]
227 startch = chords[self.start_note]
229 while c and edu_left:
232 c.chord_prefix = self.dump_start () + c.chord_prefix
235 edu_left = edu_left - c.EDU_duration ()
237 c.chord_suffix = c.chord_suffix+ self.dump_end ()
241 sys.stderr.write ("\nHuh? Tuplet starting at entry %d was too short." % self.start_note)
244 def __init__ (self, number, params):
248 def append_entry (self, finale_e):
249 self.finale.append (finale_e)
251 def calculate (self, chords):
252 startnote = self.finale[5]
253 endnote = self.finale[3*6 + 2]
255 cs = chords[startnote]
261 cs.note_suffix = '-(' + cs.note_suffix
262 ce.note_suffix = ce.note_suffix + '-)'
265 sys.stderr.write ("""\nHuh? Slur no %d between (%d,%d), with %d notes""" % (self.number, startnote, endnote, len (chords)))
268 class Global_measure:
269 def __init__ (self, number):
272 self.key_signature = None
280 return `self.finale `
282 def set_timesig (self, finale):
283 (beats, fdur) = finale
284 (log, dots) = EDU_to_duration (fdur)
292 sys.stderr.write ("\nHuh? Beat duration has dots? (EDU Duration = %d)" % fdur)
293 self.timesig = (beats, log)
298 def set_key_sig (self, finale):
299 k = interpret_finale_key_sig (finale)
300 self.key_signature = k
301 self.scale = find_scale (k)
303 def set_flags (self,flag1, flag2):
305 # flag1 isn't all that interesting.
310 self.repeats.append ('start')
312 self.repeats.append ('stop')
316 self.repeats.append ('bracket')
329 class Articulation_def:
330 def __init__ (self, n, a, b):
331 self.finale_glyph = a & 0xff
336 return articulation_dict[self.finale_glyph]
338 sys.stderr.write ("\nUnknown articulation no. %d" % self.finale_glyph)
339 sys.stderr.write ("\nPlease add an entry to articulation_dict in the Python source")
343 def __init__ (self, a,b, finale):
344 self.definition = finale[0]
347 def calculate (self, chords, defs):
348 c = chords[self.notenumber]
350 adef = defs[self.definition]
354 sys.stderr.write ("\nThis happened on note %d" % self.notenumber)
356 c.note_suffix = '-' + lystr
359 def __init__ (self, a,b , finale):
361 self.syllable = finale[1]
362 self.verse = finale[0]
363 def calculate (self, chords, lyrics):
364 self.chord = chords[self.chordnum]
367 def __init__ (self, number, body):
370 self.split_syllables ()
371 def split_syllables (self):
372 ss = re.split ('(-| +)', self.body)
378 septor = re.sub (" +", "", s)
379 septor = re.sub ("-", " -- ", septor)
380 syls[-1] = syls[-1] + septor
386 self.syllables = syls
391 for s in self.syllables[1:]:
392 line = line + ' ' + s
394 str = str + ' ' * 4 + line + '\n'
397 str = """\nverse%s = \\lyrics {\n %s}\n""" % (encodeint (self.number - 1) ,str)
401 def __init__(self, pitch, sig_type = 0):
403 self.sig_type = sig_type
405 def signature_type (self):
406 if self.sig_type == 1:
409 # really only for 0, but we only know about 0 and 1
412 def equal (self, other):
413 if other and other.pitch == self.pitch and other.sig_type == self.sig_type:
420 def __init__(self, no):
422 self.frames = [0] * 4
426 self.global_measure = None
433 def calculate (self):
436 if len (self.finale) < 2:
440 self.frames = [fs[0]]
449 def __init__ (self, finale):
452 (number, start, end ) = finale
458 def set_measure (self, m):
461 def calculate (self):
465 for c in self.chords:
466 if c.grace and (lastch == None or (not lastch.grace)):
467 c.chord_prefix = r'\grace {' + c.chord_prefix
468 elif not c.grace and lastch and lastch.grace:
469 lastch.chord_suffix = lastch.chord_suffix + ' } '
476 str = '%% FR(%d)\n' % self.number
477 left = self.measure.global_measure.length ()
481 for c in self.chords:
482 add = c.ly_string () + ' '
483 if len (ln) + len(add) > 72:
484 str = str + ln + '\n'
487 left = rat_subtract (left, c.length ())
492 sys.stderr.write ("""\nHuh? Going backwards in frame no %d, start/end (%d,%d)""" % (self.number, self.start, self.end))
495 str = str + rat_to_lily_duration (left)
501 return chr ( i + ord ('A'))
504 def __init__ (self, number):
508 def get_measure (self, no):
509 fill_list_to (self.measures, no)
511 if self.measures[no] == None:
513 self.measures [no] =m
516 return self.measures[no]
518 return 'staff' + encodeint (self.number - 1)
519 def layerid (self, l):
520 return self.staffid() + 'layer%s' % chr (l -1 + ord ('A'))
522 def dump_time_key_sigs (self):
528 for m in self.measures[1:]:
529 if not m or not m.valid:
536 if g.key_signature and not g.key_signature.equal(last_key):
537 pitch= g.key_signature.pitch
538 e = e + "\\key %s %s " % (lily_notename (pitch),
539 g.key_signature.signature_type())
541 last_key = g.key_signature
542 if last_time <> g.timesig :
543 e = e + "\\time %d/%d " % g.timesig
544 last_time = g.timesig
546 if 'start' in g.repeats:
547 e = e + ' \\bar "|:" '
550 # we don't attempt voltas since they fail easily.
551 if 0 : # and g.repeat_bar == '|:' or g.repeat_bar == ':|:' or g.bracket:
553 if g.repeat_bar == '|:' or g.repeat_bar == ':|:' or g.bracket == 'end':
557 if g.bracket == 'start':
560 str = string.join (map (lambda x: '(volta %s)' % x, strs))
562 e = e + ' \\property Score.repeatCommands = #\'(%s) ' % str
567 if last_clef <> m.clef :
568 e = e + '\\clef "%s"' % lily_clef (m.clef)
572 k = k +' ' + rat_to_lily_duration (gap) + '\n'
577 gap = rat_add (gap, g.length ())
578 if 'stop' in g.repeats:
579 k = k + ' \\bar ":|" '
581 k = '%sglobal = \\notes { %s }\n\n ' % (self.staffid (), k)
589 for x in range (1,5): # 4 layers.
594 for m in self.measures[1:]:
595 if not m or not m.valid:
596 sys.stderr.write ("Skipping non-existant or invalid measure\n")
603 sys.stderr.write ("Skipping nonexistent frame %d\n" % x)
604 laystr = laystr + "%% non existent frame %d (skipped) \n" % x
608 laystr = laystr +'} %s {\n ' % rat_to_lily_duration (gap)
610 laystr = laystr + fr.dump ()
612 if m.global_measure :
613 gap = rat_add (gap, m.global_measure.length ())
616 "No global measure for staff %d measure %d\n"
617 % (self.number, m.number))
620 laystr = '%s = \\notes { { %s } }\n\n' % (l, laystr)
624 str = str + self.dump_time_key_sigs ()
625 stafdef = '\\%sglobal' % self.staffid ()
627 stafdef = stafdef + ' \\' + i
630 str = str + '%s = \\context Staff = %s <<\n %s\n >>\n' % \
631 (self.staffid (), self.staffid (), stafdef)
640 return [(l[0], l[1])] + ziplist (l[2:])
644 def __init__ (self, number, contents):
647 self.finale = contents[:7]
649 self.notelist = ziplist (contents[7:])
655 self.note_suffix = ''
656 self.chord_suffix = ''
657 self.chord_prefix = ''
664 return self.frame.measure
670 l = (1, self.duration[0])
672 d = 1 << self.duration[1]
674 dotfact = rat_subtract ((2,1), (1,d))
675 mylen = rat_multiply (dotfact, l)
678 mylen = rat_multiply (mylen, self.tuplet.factor())
682 def EDU_duration (self):
683 return self.finale[2]
684 def set_duration (self):
685 self.duration = EDU_to_duration(self.EDU_duration ())
687 def calculate (self):
688 self.find_realpitch ()
691 flag = self.finale[4]
692 if Chord.GRACE_MASK & flag:
696 def find_realpitch (self):
698 meas = self.measure ()
700 if not meas or not meas.global_measure :
701 sys.stderr.write ('note %d not in measure\n' % self.number)
702 elif not meas.global_measure.scale:
703 sys.stderr.write ('note %d: no scale in this measure.' % self.number)
706 for p in self.notelist:
716 scale = meas.global_measure.scale
717 (sn, sa) =scale[rest % 7]
718 sn = sn + (rest - (rest%7)) + 7
720 self.pitches.append ((sn, acc))
721 tiestart = tiestart or (flag & Chord.TIE_START_MASK)
723 self.chord_suffix = self.chord_suffix + ' ~ '
725 REST_MASK = 0x40000000L
726 TIE_START_MASK = 0x40000000L
727 GRACE_MASK = 0x00800000L
729 def ly_string (self):
735 if not (self.finale[4] & Chord.REST_MASK):
738 for p in self.pitches:
743 nn = lily_notename ((n,a))
760 if len (self.pitches) > 1:
763 s = s + '%d%s' % (self.duration[0], '.'* self.duration[1])
764 s = self.note_prefix + s + self.note_suffix
766 s = self.chord_prefix + s + self.chord_suffix
771 def fill_list_to (list, no):
773 Add None to LIST until it contains entry number NO.
775 while len (list) <= no:
776 list.extend ([None] * (no - len(list) + 1))
779 def read_finale_value (str):
781 Pry off one value from STR. The value may be $hex, decimal, or "string".
782 Return: (value, rest-of-STR)
784 while str and str[0] in ' \t\n':
794 while str and str[0] in '0123456789ABCDEF':
799 return (string.atol (hex, 16), str)
803 while str and str[0] <> '"':
808 elif str[0] in '-0123456789':
810 while str and str[0] in '-0123456789':
814 return (string.atoi (dec), str)
816 sys.stderr.write ("Can't convert `%s'\n" % str)
822 def parse_etf_file (fn, tag_dict):
824 """ Read FN, putting ETF info into
825 a giant dictionary. The keys of TAG_DICT indicate which tags
826 to put into the dict.
829 sys.stderr.write ('parsing ... ' )
832 gulp = re.sub ('[\n\r]+', '\n', f.read ())
833 ls = string.split (gulp, '\n^')
836 for k in tag_dict.keys ():
837 etf_file_dict[k] = {}
844 m = re.match ('^([a-zA-Z0-9&]+)\(([^)]+)\)', l)
845 if m and tag_dict.has_key (m.group (1)):
848 indices = tuple (map (string.atoi, string.split (m.group (2), ',')))
849 content = l[m.end (2)+1:]
852 tdict = etf_file_dict[tag]
853 if not tdict.has_key (indices):
859 if tag == 'verse' or tag == 'block':
860 m2 = re.match ('(.*)\^end', content)
862 parsed = [m2.group (1)]
865 (v, content) = read_finale_value (content)
869 tdict [indices].extend (parsed)
871 last_indices = indices
876 # let's not do this: this really confuses when eE happens to be before a ^text.
877 # if last_tag and last_indices:
878 # etf_file_dict[last_tag][last_indices].append (l)
880 sys.stderr.write ('\n')
888 def __init__ (self, name):
889 self.measures = [None]
892 self.tuplets = [None]
895 self.articulations = [None]
896 self.syllables = [None]
898 self.articulation_defs = [None]
903 def get_global_measure (self, no):
904 fill_list_to (self.measures, no)
905 if self.measures[no] == None:
906 self.measures [no] = Global_measure (no)
908 return self.measures[no]
911 def get_staff(self,staffno):
912 fill_list_to (self.staffs, staffno)
913 if self.staffs[staffno] == None:
914 self.staffs[staffno] = Staff (staffno)
916 return self.staffs[staffno]
919 def try_IS (self, indices, contents):
922 def try_BC (self, indices, contents):
924 where = contents[0] / 1024.0
925 def try_TP(self, indices, contents):
928 if self.tuplets[-1] == None or num <> self.tuplets[-1].start_note:
929 self.tuplets.append (Tuplet (num))
931 self.tuplets[-1].append_finale (contents)
933 def try_IM (self, indices, contents):
936 self.articulations.append (Articulation (a,b,fin))
937 def try_verse (self, indices, contents):
941 body = re.sub (r"""\^[a-z]+\([^)]+\)""", "", body)
942 body = re.sub ("\^[a-z]+", "", body)
943 self.verses.append (Verse (a, body))
944 def try_ve (self,indices, contents):
946 self.syllables.append (Syllable (a,b,contents))
948 def try_eE (self,indices, contents):
950 (prev, next, dur, pos, entryflag, extended, follow) = contents[:7]
952 fill_list_to (self.chords, no)
953 self.chords[no] =Chord (no, contents)
955 def try_Sx(self,indices, contents):
957 fill_list_to (self.slurs, slurno)
958 self.slurs[slurno] = Slur(slurno, contents)
960 def try_IX (self, indices, contents):
967 ix = self.articulation_defs[n]
969 ix = Articulation_def (n,a,b)
970 self.articulation_defs.append (Articulation_def (n, a, b))
972 def try_GF(self, indices, contents):
973 (staffno,measno) = indices
975 st = self.get_staff (staffno)
976 meas = st.get_measure (measno)
977 meas.finale = contents
979 def try_FR(self, indices, contents):
980 frameno = indices [0]
982 startnote = contents[0]
983 endnote = contents[1]
985 fill_list_to (self.frames, frameno)
987 self.frames[frameno] = Frame ((frameno, startnote, endnote))
989 def try_MS (self, indices, contents):
992 meas =self. get_global_measure (measno)
994 meas.set_key_sig (keynum)
997 beatlen = contents[3]
998 meas.set_timesig ((beats, beatlen))
1000 meas_flag1 = contents[4]
1001 meas_flag2 = contents[5]
1003 meas.set_flags (meas_flag1, meas_flag2);
1013 'verse' : try_verse,
1021 def parse (self, etf_dict):
1022 sys.stderr.write ('reconstructing ...')
1025 for (tag,routine) in Etf_file.routine_dict.items ():
1026 ks = etf_dict[tag].keys ()
1029 routine (self, k, etf_dict[tag][k])
1031 sys.stderr.write ('processing ...')
1034 self.unthread_entries ()
1036 for st in self.staffs[1:]:
1040 for m in st.measures[1:]:
1046 m.global_measure = self.measures[mno]
1048 sys.stderr.write ("Non-existent global measure %d" % mno)
1051 frame_obj_list = [None]
1052 for frno in m.frames:
1054 fr = self.frames[frno]
1055 frame_obj_list.append (fr)
1057 sys.stderr.write ("\nNon-existent frame %d" % frno)
1059 m.frames = frame_obj_list
1060 for fr in frame_obj_list[1:]:
1066 fr.chords = self.get_thread (fr.start, fr.end)
1071 for c in self.chords[1:]:
1075 for f in self.frames[1:]:
1079 for t in self.tuplets[1:]:
1080 t.calculate (self.chords)
1082 for s in self.slurs[1:]:
1084 s.calculate (self.chords)
1086 for s in self.articulations[1:]:
1087 s.calculate (self.chords, self.articulation_defs)
1089 def get_thread (self, startno, endno):
1095 c = self.chords[startno]
1097 sys.stderr.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno, endno))
1101 while c and c.number <> endno:
1113 for s in self.staffs[1:]:
1115 str = str + '\n\n' + s.dump ()
1116 staffs.append ('\\' + s.staffid ())
1119 # should use \addlyrics ?
1121 for v in self.verses[1:]:
1122 str = str + v.dump()
1124 if len (self.verses) > 1:
1125 sys.stderr.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
1128 str = str + '\\score { << %s >> } ' % string.join (staffs)
1134 return 'ETF FILE %s %s' % (self.measures, self.entries)
1136 def unthread_entries (self):
1137 for e in self.chords[1:]:
1141 e.prev = self.chords[e.finale[0]]
1142 e.next = self.chords[e.finale[1]]
1145 sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
1148 sys.stdout.write("""Usage: etf2ly [OPTIONS]... ETF-FILE
1150 Convert ETF to LilyPond.
1153 -h, --help print this help
1154 -o, --output=FILE set output filename to FILE
1155 -v, --version show version information
1157 Enigma Transport Format is a format used by Coda Music Technology's
1158 Finale product. This program will convert a subset of ETF to a
1159 ready-to-use lilypond file.
1161 Report bugs to bug-lilypond@gnu.org.
1163 Written by Han-Wen Nienhuys <hanwen@cs.uu.nl>.
1167 def print_version ():
1168 sys.stdout.write (r"""etf2ly (GNU lilypond) %s
1170 This is free software. It is covered by the GNU General Public License,
1171 and you are welcome to change it and/or distribute copies of it under
1172 certain conditions. Invoke as `midi2ly --warranty' for more information.
1174 Copyright (c) 2000--2003 by Han-Wen Nienhuys <hanwen@cs.uu.nl>
1179 (options, files) = getopt.getopt (sys.argv[1:], 'vo:h', ['help','version', 'output='])
1185 if o== '--help' or o == '-h':
1188 if o == '--version' or o == '-v':
1192 if o == '--output' or o == '-o':
1205 sys.stderr.write ('Processing `%s\'\n' % f)
1207 dict = parse_etf_file (f, Etf_file.routine_dict)
1209 if not out_filename:
1210 out_filename = os.path.basename (re.sub ('(?i).etf$', '.ly', f))
1212 if out_filename == f:
1213 out_filename = os.path.basename (f + '.ly')
1215 sys.stderr.write ('Writing `%s\'' % out_filename)
1220 fo = open (out_filename, 'w')
1221 fo.write ('%% lily was here -- automatically converted by etf2ly from %s\n' % f)