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
106 def find_scale (transposition):
107 cscale = map (lambda x: (x,0), range (0,7))
108 trscale = map(lambda x, k=transposition: transpose(x, k), cscale)
111 def EDU_to_duration (edu):
126 def rat_to_lily_duration (rat):
130 while d and d % 2 == 0:
131 basedur = basedur << 1
134 str = 's%d' % basedur
136 str = str + '*%d' % n
138 str = str + '/%d' % d
153 def rat_simplify (r):
164 def rat_multiply (a,b):
168 return rat_simplify ((x*p, y*q))
174 return rat_simplify ((x*q + p*y, y*q))
182 def rat_subtract (a,b ):
183 return rat_add (a, rat_neg (b))
185 def lily_notename (tuple2):
187 nn = chr ((n+ 2)%7 + ord ('a'))
189 return nn + {-2:'eses', -1:'es', 0:'', 1:'is', 2:'isis'}[a]
193 def __init__ (self, number):
194 self.start_note = number
197 def append_finale (self, fin):
198 self.finale.append (fin)
201 n = self.finale[0][2]*self.finale[0][3]
202 d = self.finale[0][0]*self.finale[0][1]
203 return rat_simplify( (n, d))
205 def dump_start (self):
206 return '\\times %d/%d { ' % self.factor ()
211 def calculate (self, chords):
212 edu_left = self.finale[0][0] * self.finale[0][1]
214 startch = chords[self.start_note]
216 while c and edu_left:
219 c.chord_prefix = self.dump_start () + c.chord_prefix
222 edu_left = edu_left - c.EDU_duration ()
224 c.chord_suffix = c.chord_suffix+ self.dump_end ()
228 sys.stderr.write ("\nHuh? Tuplet starting at entry %d was too short." % self.start_note)
231 def __init__ (self, number, params):
235 def append_entry (self, finale_e):
236 self.finale.append (finale_e)
238 def calculate (self, chords):
239 startnote = self.finale[5]
240 endnote = self.finale[3*6 + 2]
242 cs = chords[startnote]
248 cs.note_suffix = '-(' + cs.note_suffix
249 ce.note_suffix = ce.note_suffix + '-)'
252 sys.stderr.write ("""\nHuh? Slur no %d between (%d,%d), with %d notes""" % (self.number, startnote, endnote, len (chords)))
255 class Global_measure:
256 def __init__ (self, number):
259 self.key_signature = None
267 return `self.finale `
269 def set_timesig (self, finale):
270 (beats, fdur) = finale
271 (log, dots) = EDU_to_duration (fdur)
279 sys.stderr.write ("\nHuh? Beat duration has dots? (EDU Duration = %d)" % fdur)
280 self.timesig = (beats, log)
285 def set_key_sig (self, finale):
286 k = interpret_finale_key_sig (finale)
287 self.key_signature = k
288 self.scale = find_scale (k)
290 def set_flags (self,flag1, flag2):
292 # flag1 isn't all that interesting.
297 self.repeats.append ('start')
299 self.repeats.append ('stop')
303 self.repeats.append ('bracket')
316 class Articulation_def:
317 def __init__ (self, n, a, b):
318 self.finale_glyph = a & 0xff
323 return articulation_dict[self.finale_glyph]
325 sys.stderr.write ("\nUnknown articulation no. %d" % self.finale_glyph)
326 sys.stderr.write ("\nPlease add an entry to articulation_dict in the Python source")
330 def __init__ (self, a,b, finale):
331 self.definition = finale[0]
334 def calculate (self, chords, defs):
335 c = chords[self.notenumber]
337 adef = defs[self.definition]
341 sys.stderr.write ("\nThis happened on note %d" % self.notenumber)
343 c.note_suffix = '-' + lystr
346 def __init__ (self, a,b , finale):
348 self.syllable = finale[1]
349 self.verse = finale[0]
350 def calculate (self, chords, lyrics):
351 self.chord = chords[self.chordnum]
354 def __init__ (self, number, body):
357 self.split_syllables ()
358 def split_syllables (self):
359 ss = re.split ('(-| +)', self.body)
365 septor = re.sub (" +", "", s)
366 septor = re.sub ("-", " -- ", septor)
367 syls[-1] = syls[-1] + septor
373 self.syllables = syls
378 for s in self.syllables[1:]:
379 line = line + ' ' + s
381 str = str + ' ' * 4 + line + '\n'
384 str = """\nverse%s = \\lyrics {\n %s}\n""" % (encodeint (self.number - 1) ,str)
389 def __init__(self, no):
391 self.frames = [0] * 4
395 self.global_measure = None
402 def calculate (self):
405 if len (self.finale) < 2:
409 self.frames = [fs[0]]
418 def __init__ (self, finale):
421 (number, start, end ) = finale
427 def set_measure (self, m):
430 def calculate (self):
434 for c in self.chords:
435 if c.grace and (lastch == None or (not lastch.grace)):
436 c.chord_prefix = r'\grace {' + c.chord_prefix
437 elif not c.grace and lastch and lastch.grace:
438 lastch.chord_suffix = lastch.chord_suffix + ' } '
445 str = '%% FR(%d)\n' % self.number
446 left = self.measure.global_measure.length ()
450 for c in self.chords:
451 add = c.ly_string () + ' '
452 if len (ln) + len(add) > 72:
453 str = str + ln + '\n'
456 left = rat_subtract (left, c.length ())
461 sys.stderr.write ("""\nHuh? Going backwards in frame no %d, start/end (%d,%d)""" % (self.number, self.start, self.end))
464 str = str + rat_to_lily_duration (left)
470 return chr ( i + ord ('A'))
473 def __init__ (self, number):
477 def get_measure (self, no):
478 fill_list_to (self.measures, no)
480 if self.measures[no] == None:
482 self.measures [no] =m
485 return self.measures[no]
487 return 'staff' + encodeint (self.number - 1)
488 def layerid (self, l):
489 return self.staffid() + 'layer%s' % chr (l -1 + ord ('A'))
491 def dump_time_key_sigs (self):
497 for m in self.measures[1:]:
498 if not m or not m.valid:
505 if last_key <> g.key_signature:
506 pitch= g.key_signature
507 e = e + "\\key %s \\major " % (lily_notename (pitch))
509 last_key = g.key_signature
510 if last_time <> g.timesig :
511 e = e + "\\time %d/%d " % g.timesig
512 last_time = g.timesig
514 if 'start' in g.repeats:
515 e = e + ' \\bar "|:" '
518 # we don't attempt voltas since they fail easily.
519 if 0 : # and g.repeat_bar == '|:' or g.repeat_bar == ':|:' or g.bracket:
521 if g.repeat_bar == '|:' or g.repeat_bar == ':|:' or g.bracket == 'end':
525 if g.bracket == 'start':
528 str = string.join (map (lambda x: '(volta %s)' % x, strs))
530 e = e + ' \\property Score.repeatCommands = #\'(%s) ' % str
535 if last_clef <> m.clef :
536 e = e + '\\clef "%s"' % lily_clef (m.clef)
540 k = k +' ' + rat_to_lily_duration (gap) + '\n'
545 gap = rat_add (gap, g.length ())
546 if 'stop' in g.repeats:
547 k = k + ' \\bar ":|" '
549 k = '%sglobal = \\notes { %s }\n\n ' % (self.staffid (), k)
557 for x in range (1,5): # 4 layers.
562 for m in self.measures[1:]:
563 if not m or not m.valid:
564 sys.stderr.write ("Skipping non-existant or invalid measure\n")
571 sys.stderr.write ("Skipping nonexistent frame %d\n" % x)
572 laystr = laystr + "%% non existent frame %d (skipped) \n" % x
576 laystr = laystr +'} %s {\n ' % rat_to_lily_duration (gap)
578 laystr = laystr + fr.dump ()
580 if m.global_measure :
581 gap = rat_add (gap, m.global_measure.length ())
584 "No global measure for staff %d measure %d\n"
585 % (self.number, m.number))
588 laystr = '%s = \\notes { { %s } }\n\n' % (l, laystr)
592 str = str + self.dump_time_key_sigs ()
593 stafdef = '\\%sglobal' % self.staffid ()
595 stafdef = stafdef + ' \\' + i
598 str = str + '%s = \\context Staff = %s <<\n %s\n >>\n' % \
599 (self.staffid (), self.staffid (), stafdef)
608 return [(l[0], l[1])] + ziplist (l[2:])
612 def __init__ (self, number, contents):
615 self.finale = contents[:7]
617 self.notelist = ziplist (contents[7:])
623 self.note_suffix = ''
624 self.chord_suffix = ''
625 self.chord_prefix = ''
632 return self.frame.measure
638 l = (1, self.duration[0])
640 d = 1 << self.duration[1]
642 dotfact = rat_subtract ((2,1), (1,d))
643 mylen = rat_multiply (dotfact, l)
646 mylen = rat_multiply (mylen, self.tuplet.factor())
650 def EDU_duration (self):
651 return self.finale[2]
652 def set_duration (self):
653 self.duration = EDU_to_duration(self.EDU_duration ())
655 def calculate (self):
656 self.find_realpitch ()
659 flag = self.finale[4]
660 if Chord.GRACE_MASK & flag:
664 def find_realpitch (self):
666 meas = self.measure ()
668 if not meas or not meas.global_measure :
669 sys.stderr.write ('note %d not in measure\n' % self.number)
670 elif not meas.global_measure.scale:
671 sys.stderr.write ('note %d: no scale in this measure.' % self.number)
674 for p in self.notelist:
684 scale = meas.global_measure.scale
685 (sn, sa) =scale[rest % 7]
686 sn = sn + (rest - (rest%7)) + 7
688 self.pitches.append ((sn, acc))
689 tiestart = tiestart or (flag & Chord.TIE_START_MASK)
691 self.chord_suffix = self.chord_suffix + ' ~ '
693 REST_MASK = 0x40000000L
694 TIE_START_MASK = 0x40000000L
695 GRACE_MASK = 0x00800000L
697 def ly_string (self):
703 if not (self.finale[4] & Chord.REST_MASK):
706 for p in self.pitches:
711 nn = lily_notename ((n,a))
728 if len (self.pitches) > 1:
731 s = s + '%d%s' % (self.duration[0], '.'* self.duration[1])
732 s = self.note_prefix + s + self.note_suffix
734 s = self.chord_prefix + s + self.chord_suffix
739 def fill_list_to (list, no):
741 Add None to LIST until it contains entry number NO.
743 while len (list) <= no:
744 list.extend ([None] * (no - len(list) + 1))
747 def read_finale_value (str):
749 Pry off one value from STR. The value may be $hex, decimal, or "string".
750 Return: (value, rest-of-STR)
752 while str and str[0] in ' \t\n':
762 while str and str[0] in '0123456789ABCDEF':
767 return (string.atol (hex, 16), str)
771 while str and str[0] <> '"':
776 elif str[0] in '-0123456789':
778 while str and str[0] in '-0123456789':
782 return (string.atoi (dec), str)
784 sys.stderr.write ("Can't convert `%s'\n" % str)
790 def parse_etf_file (fn, tag_dict):
792 """ Read FN, putting ETF info into
793 a giant dictionary. The keys of TAG_DICT indicate which tags
794 to put into the dict.
797 sys.stderr.write ('parsing ... ' )
800 gulp = re.sub ('[\n\r]+', '\n', f.read ())
801 ls = string.split (gulp, '\n^')
804 for k in tag_dict.keys ():
805 etf_file_dict[k] = {}
812 m = re.match ('^([a-zA-Z0-9&]+)\(([^)]+)\)', l)
813 if m and tag_dict.has_key (m.group (1)):
816 indices = tuple (map (string.atoi, string.split (m.group (2), ',')))
817 content = l[m.end (2)+1:]
820 tdict = etf_file_dict[tag]
821 if not tdict.has_key (indices):
827 if tag == 'verse' or tag == 'block':
828 m2 = re.match ('(.*)\^end', content)
830 parsed = [m2.group (1)]
833 (v, content) = read_finale_value (content)
837 tdict [indices].extend (parsed)
839 last_indices = indices
844 # let's not do this: this really confuses when eE happens to be before a ^text.
845 # if last_tag and last_indices:
846 # etf_file_dict[last_tag][last_indices].append (l)
848 sys.stderr.write ('\n')
856 def __init__ (self, name):
857 self.measures = [None]
860 self.tuplets = [None]
863 self.articulations = [None]
864 self.syllables = [None]
866 self.articulation_defs = [None]
871 def get_global_measure (self, no):
872 fill_list_to (self.measures, no)
873 if self.measures[no] == None:
874 self.measures [no] = Global_measure (no)
876 return self.measures[no]
879 def get_staff(self,staffno):
880 fill_list_to (self.staffs, staffno)
881 if self.staffs[staffno] == None:
882 self.staffs[staffno] = Staff (staffno)
884 return self.staffs[staffno]
887 def try_IS (self, indices, contents):
890 def try_BC (self, indices, contents):
892 where = contents[0] / 1024.0
893 def try_TP(self, indices, contents):
896 if self.tuplets[-1] == None or num <> self.tuplets[-1].start_note:
897 self.tuplets.append (Tuplet (num))
899 self.tuplets[-1].append_finale (contents)
901 def try_IM (self, indices, contents):
904 self.articulations.append (Articulation (a,b,fin))
905 def try_verse (self, indices, contents):
909 body = re.sub (r"""\^[a-z]+\([^)]+\)""", "", body)
910 body = re.sub ("\^[a-z]+", "", body)
911 self.verses.append (Verse (a, body))
912 def try_ve (self,indices, contents):
914 self.syllables.append (Syllable (a,b,contents))
916 def try_eE (self,indices, contents):
918 (prev, next, dur, pos, entryflag, extended, follow) = contents[:7]
920 fill_list_to (self.chords, no)
921 self.chords[no] =Chord (no, contents)
923 def try_Sx(self,indices, contents):
925 fill_list_to (self.slurs, slurno)
926 self.slurs[slurno] = Slur(slurno, contents)
928 def try_IX (self, indices, contents):
935 ix = self.articulation_defs[n]
937 ix = Articulation_def (n,a,b)
938 self.articulation_defs.append (Articulation_def (n, a, b))
940 def try_GF(self, indices, contents):
941 (staffno,measno) = indices
943 st = self.get_staff (staffno)
944 meas = st.get_measure (measno)
945 meas.finale = contents
947 def try_FR(self, indices, contents):
948 frameno = indices [0]
950 startnote = contents[0]
951 endnote = contents[1]
953 fill_list_to (self.frames, frameno)
955 self.frames[frameno] = Frame ((frameno, startnote, endnote))
957 def try_MS (self, indices, contents):
960 meas =self. get_global_measure (measno)
962 meas.set_key_sig (keynum)
965 beatlen = contents[3]
966 meas.set_timesig ((beats, beatlen))
968 meas_flag1 = contents[4]
969 meas_flag2 = contents[5]
971 meas.set_flags (meas_flag1, meas_flag2);
989 def parse (self, etf_dict):
990 sys.stderr.write ('reconstructing ...')
993 for (tag,routine) in Etf_file.routine_dict.items ():
994 ks = etf_dict[tag].keys ()
997 routine (self, k, etf_dict[tag][k])
999 sys.stderr.write ('processing ...')
1002 self.unthread_entries ()
1004 for st in self.staffs[1:]:
1008 for m in st.measures[1:]:
1014 m.global_measure = self.measures[mno]
1016 sys.stderr.write ("Non-existent global measure %d" % mno)
1019 frame_obj_list = [None]
1020 for frno in m.frames:
1022 fr = self.frames[frno]
1023 frame_obj_list.append (fr)
1025 sys.stderr.write ("\nNon-existent frame %d" % frno)
1027 m.frames = frame_obj_list
1028 for fr in frame_obj_list[1:]:
1034 fr.chords = self.get_thread (fr.start, fr.end)
1039 for c in self.chords[1:]:
1043 for f in self.frames[1:]:
1047 for t in self.tuplets[1:]:
1048 t.calculate (self.chords)
1050 for s in self.slurs[1:]:
1052 s.calculate (self.chords)
1054 for s in self.articulations[1:]:
1055 s.calculate (self.chords, self.articulation_defs)
1057 def get_thread (self, startno, endno):
1063 c = self.chords[startno]
1065 sys.stderr.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno, endno))
1069 while c and c.number <> endno:
1081 for s in self.staffs[1:]:
1083 str = str + '\n\n' + s.dump ()
1084 staffs.append ('\\' + s.staffid ())
1087 # should use \addlyrics ?
1089 for v in self.verses[1:]:
1090 str = str + v.dump()
1092 if len (self.verses) > 1:
1093 sys.stderr.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
1096 str = str + '\\score { < %s > } ' % string.join (staffs)
1102 return 'ETF FILE %s %s' % (self.measures, self.entries)
1104 def unthread_entries (self):
1105 for e in self.chords[1:]:
1109 e.prev = self.chords[e.finale[0]]
1110 e.next = self.chords[e.finale[1]]
1113 sys.stderr.write ("%s from LilyPond %s\n" % (program_name, version))
1116 sys.stdout.write("""Usage: etf2ly [OPTIONS]... ETF-FILE
1118 Convert ETF to LilyPond.
1121 -h, --help print this help
1122 -o, --output=FILE set output filename to FILE
1123 -v, --version show version information
1125 Enigma Transport Format is a format used by Coda Music Technology's
1126 Finale product. This program will convert a subset of ETF to a
1127 ready-to-use lilypond file.
1129 Report bugs to bug-lilypond@gnu.org.
1131 Written by Han-Wen Nienhuys <hanwen@cs.uu.nl>.
1135 def print_version ():
1136 sys.stdout.write (r"""etf2ly (GNU lilypond) %s
1138 This is free software. It is covered by the GNU General Public License,
1139 and you are welcome to change it and/or distribute copies of it under
1140 certain conditions. Invoke as `midi2ly --warranty' for more information.
1142 Copyright (c) 2000--2003 by Han-Wen Nienhuys <hanwen@cs.uu.nl>
1147 (options, files) = getopt.getopt (sys.argv[1:], 'vo:h', ['help','version', 'output='])
1153 if o== '--help' or o == '-h':
1156 if o == '--version' or o == '-v':
1160 if o == '--output' or o == '-o':
1173 sys.stderr.write ('Processing `%s\'\n' % f)
1175 dict = parse_etf_file (f, Etf_file.routine_dict)
1177 if not out_filename:
1178 out_filename = os.path.basename (re.sub ('(?i).etf$', '.ly', f))
1180 if out_filename == f:
1181 out_filename = os.path.basename (f + '.ly')
1183 sys.stderr.write ('Writing `%s\'' % out_filename)
1188 fo = open (out_filename, 'w')
1189 fo.write ('%% lily was here -- automatically converted by etf2ly from %s\n' % f)