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', '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 # find transposition of C-major scale that belongs here.
79 def interpret_finale_key_sig (finale_id
):
81 if 0 <= finale_id
< 7:
83 p
= transpose (p
, (4,0)) # a fifth up
84 finale_id
= finale_id
- 1
85 elif 248 < finale_id
<= 255:
86 while finale_id
< 256:
87 p
= transpose (p
, (3,0))
88 finale_id
= finale_id
+ 1
94 def find_scale (transposition
):
95 cscale
= map (lambda x
: (x
,0), range (0,7))
96 trscale
= map(lambda x
, k
=transposition
: transpose(x
, k
), cscale
)
99 def EDU_to_duration (edu
):
114 def rat_to_lily_duration (rat
):
118 while d
and d
% 2 == 0:
119 basedur
= basedur
<< 1
122 str = 's%d' % basedur
124 str = str + '*%d' % n
126 str = str + '/%d' % d
141 def rat_simplify (r
):
152 def rat_multiply (a
,b
):
156 return rat_simplify ((x
*p
, y
*q
))
162 return rat_simplify ((x
*q
+ p
*y
, y
*q
))
170 def rat_subtract (a
,b
):
171 return rat_add (a
, rat_neg (b
))
173 def lily_notename (tuple2
):
175 nn
= chr ((n
+ 2)%7 + ord ('a'))
190 def __init__ (self
, number
):
191 self
.start_note
= number
194 def append_finale (self
, fin
):
195 self
.finale
.append (fin
)
198 n
= self
.finale
[0][2]*self
.finale
[0][3]
199 d
= self
.finale
[0][0]*self
.finale
[0][1]
200 return rat_simplify( (n
, d
))
202 def dump_start (self
):
203 return '\\times %d/%d { ' % self
.factor ()
208 def calculate (self
, chords
):
209 edu_left
= self
.finale
[0][0] * self
.finale
[0][1]
211 startch
= chords
[self
.start_note
]
213 while c
and edu_left
:
216 c
.chord_prefix
= self
.dump_start () + c
.chord_prefix
219 edu_left
= edu_left
- c
.EDU_duration ()
221 c
.chord_suffix
= c
.chord_suffix
+ self
.dump_end ()
225 sys
.stderr
.write ("\nHuh? Tuplet starting at entry %d was too short." % self
.start_note
)
228 def __init__ (self
, number
):
232 def append_entry (self
, finale_e
):
233 self
.finale
.append (finale_e
)
235 def calculate (self
, chords
):
236 startnote
= self
.finale
[0][5]
237 endnote
= self
.finale
[3][2]
239 cs
= chords
[startnote
]
245 cs
.note_suffix
= '(' + cs
.note_suffix
246 ce
.note_prefix
= ce
.note_prefix
+ ')'
248 sys
.stderr
.write ("""\nHuh? Slur no %d between (%d,%d), with %d notes""" % (self
.number
, startnote
, endnote
, len (chords
)))
251 class Global_measure
:
252 def __init__ (self
, number
):
255 self
.keysignature
= None
261 return `self
.finale `
263 def set_timesig (self
, finale
):
264 (beats
, fdur
) = finale
265 (log
, dots
) = EDU_to_duration (fdur
)
267 self
.timesig
= (beats
, log
)
272 def set_keysig (self
, finale
):
273 k
= interpret_finale_key_sig (finale
)
274 self
.keysignature
= k
275 self
.scale
= find_scale (k
)
285 18: '"arp"' , # arpeggio
289 def __init__ (self
, a
,b
, finale
):
290 self
.type = finale
[0]
292 def calculate (self
, chords
):
293 c
= chords
[self
.notenumber
]
296 a
= articulation_dict
[self
.type]
298 sys
.stderr
.write ("\nUnknown articulation no. %d on note no. %d" % (self
.type, self
.notenumber
))
299 sys
.stderr
.write ("\nPlease add an entry to articulation_dict in the Python source")
302 c
.note_suffix
= '-' + a
+ c
.note_suffix
305 def __init__ (self
, a
,b
, finale
):
307 self
.syllable
= finale
[1]
308 self
.verse
= finale
[0]
309 def calculate (self
, chords
, lyrics
):
310 self
.chord
= chords
[self
.chordnum
]
313 def __init__ (self
, number
, body
):
316 self
.split_syllables ()
317 def split_syllables (self
):
318 ss
= re
.split ('(-| +)', self
.body
)
324 septor
= re
.sub (" +", "", s
)
325 septor
= re
.sub ("-", " -- ", septor
)
326 syls
[-1] = syls
[-1] + septor
332 self
.syllables
= syls
337 for s
in self
.syllables
[1:]:
338 line
= line
+ ' ' + s
340 str = str + ' ' * 4 + line
+ '\n'
343 str = """\nverse%s = \\lyrics {\n %s}\n""" % (encodeint (self
.number
- 1) ,str)
348 def __init__(self
, no
):
350 self
.frames
= [0] * 4
354 self
.global_measure
= None
358 def add_finale_entry (self
, entry
):
359 self
.finale
.append (entry
)
363 def calculate (self
):
366 if len (self
.finale
) < 2:
368 fs
= map (string
.atoi
, list (fs
))
370 self
.frames
= [fs
[0]]
372 fs
= self
.finale
[0] + self
.finale
[1]
374 fs
= map (string
.atoi
, list (fs
))
381 def __init__ (self
, finale
):
384 (number
, start
, end
) = finale
390 def set_measure (self
, m
):
393 def calculate (self
):
397 for c
in self
.chords
:
398 if c
.grace
and (lastch
== None or (not lastch
.grace
)):
399 c
.chord_prefix
= r
'\grace {' + c
.chord_prefix
400 elif not c
.grace
and lastch
and lastch
.grace
:
401 lastch
.chord_suffix
= lastch
.chord_suffix
+ ' } '
408 str = '%% FR(%d)\n' % self
.number
409 left
= self
.measure
.global_measure
.length ()
413 for c
in self
.chords
:
414 add
= c
.ly_string () + ' '
415 if len (ln
) + len(add
) > 72:
416 str = str + ln
+ '\n'
419 left
= rat_subtract (left
, c
.length ())
424 sys
.stderr
.write ("""\nHuh? Going backwards in frame no %d, start/end (%d,%d)""" % (self
.number
, self
.start
, self
.end
))
427 str = str + rat_to_lily_duration (left
)
433 return chr ( i
+ ord ('A'))
436 def __init__ (self
, number
):
440 def get_measure (self
, no
):
441 if len (self
.measures
) <= no
:
442 self
.measures
= self
.measures
+ [None]* (1 + no
- len (self
.measures
))
444 if self
.measures
[no
] == None:
446 self
.measures
[no
] =m
450 return self
.measures
[no
]
452 return 'staff' + encodeint (self
.number
- 1)
453 def layerid (self
, l
):
454 return self
.staffid() + 'layer%s' % chr (l
-1 + ord ('A'))
456 def dump_time_key_sigs (self
):
462 for m
in self
.measures
[1:]:
463 if not m
or not m
.valid
:
468 if g
and last_key
<> g
.keysignature
:
469 e
= e
+ "\\key %s \\major; " % lily_notename (g
.keysignature
)
470 last_key
= g
.keysignature
471 if g
and last_time
<> g
.timesig
:
472 e
= e
+ "\\time %d/%d; " % g
.timesig
473 last_time
= g
.timesig
476 if last_clef
<> m
.clef
:
477 e
= e
+ '\\clef "%s";' % lily_clef (m
.clef
)
481 k
= k
+' ' + rat_to_lily_duration (gap
) + '\n'
486 gap
= rat_add (gap
, g
.length ())
489 k
= '%sglobal = \\notes { %s }\n\n ' % (self
.staffid (), k
)
497 for x
in range (1,5): # 4 layers.
502 for m
in self
.measures
[1:]:
503 if not m
or not m
.valid
:
504 sys
.stderr
.write ("Skipping non-existant measure")
512 sys
.stderr
.write ("Skipping nonexistent frame")
513 laystr
= laystr
+ "% FOOBAR ! \n"
518 laystr
= laystr
+'} %s {\n ' % rat_to_lily_duration (gap
)
520 laystr
= laystr
+ fr
.dump ()
522 if m
.global_measure
:
523 gap
= rat_add (gap
, m
.global_measure
.length ())
526 "No global measure for staff %d measure %d\n"
527 % (self
.number
, m
.number
))
530 laystr
= '%s = \\notes { { %s } }\n\n' % (l
, laystr
)
534 str = str + self
.dump_time_key_sigs ()
535 stafdef
= '\\%sglobal' % self
.staffid ()
537 stafdef
= stafdef
+ ' \\' + i
540 str = str + '%s = \\context Staff = %s <\n %s\n >\n' % \
541 (self
.staffid (), self
.staffid (), stafdef
)
548 def __init__ (self
, finale_entry
):
551 self
.finale
= finale_entry
556 self
.note_suffix
= ''
557 self
.chord_suffix
= ''
558 self
.chord_prefix
= ''
565 return self
.frame
.measure
571 l
= (1, self
.duration
[0])
573 d
= 1 << self
.duration
[1]
575 dotfact
= rat_subtract ((2,1), (1,d
))
576 mylen
= rat_multiply (dotfact
, l
)
579 mylen
= rat_multiply (mylen
, self
.tuplet
.factor())
583 return self
.finale
[0][0]
585 def EDU_duration (self
):
586 return self
.finale
[0][3]
587 def set_duration (self
):
588 self
.duration
= EDU_to_duration(self
.EDU_duration ())
589 def calculate (self
):
590 self
.find_realpitch ()
593 flag
= self
.finale
[0][5]
594 if Chord
.GRACE_MASK
& flag
:
598 def find_realpitch (self
):
600 ((no
, prev
, next
, dur
, pos
, entryflag
, extended
, follow
), notelist
) = self
.finale
602 meas
= self
.measure ()
604 if not meas
or not meas
.global_measure
:
605 print 'note %d not in measure' % self
.number ()
606 elif not meas
.global_measure
.scale
:
607 print 'note %d: no scale in this measure.' % self
.number ()
617 scale
= meas
.global_measure
.scale
618 (sn
, sa
) =scale
[rest
% 7]
619 sn
= sn
+ (rest
- (rest
%7)) + 7
621 self
.pitches
.append ((sn
, acc
))
622 tiestart
= tiestart
or (flag
& Chord
.TIE_START_MASK
)
624 self
.chord_suffix
= self
.chord_suffix
+ ' ~ '
626 REST_MASK
= 0x40000000L
627 TIE_START_MASK
= 0x40000000L
628 GRACE_MASK
= 0x00800000L
630 def ly_string (self
):
635 if not (self
.finale
[0][5] & Chord
.REST_MASK
):
638 for p
in self
.pitches
:
643 nn
= lily_notename ((n
,a
))
656 s
= s
+ '%s%d%s' % (nn
, self
.duration
[0], '.'* self
.duration
[1])
659 s
= 'r%d%s' % (self
.duration
[0] , '.'* self
.duration
[1])
660 s
= self
.note_prefix
+ s
+ self
.note_suffix
661 if len (self
.pitches
) > 1:
664 s
= self
.chord_prefix
+ s
+ self
.chord_suffix
667 GFre
= re
.compile(r
"""^\^GF\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
668 BCre
= re
.compile (r
"""^\^BC\(([0-9-]+)\) ([0-9-]+) .*$""")
669 eEre
= re
.compile(r
"""^\^eE\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) \$([0-9A-Fa-f]+) ([0-9-]+) ([0-9-]+)""")
670 FRre
= re
.compile (r
"""^\^FR\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
671 MSre
= re
.compile (r
"""^\^MS\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
672 note_re
= re
.compile (r
"""^ +([0-9-]+) \$([A-Fa-f0-9]+)""")
673 Sxre
= re
.compile (r
"""^\^Sx\(([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
674 IMre
= re
.compile (r
"""^\^IM\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
675 vere
= re
.compile(r
"""^\^(ve|ch|se)\(([0-9-]+),([0-9-]+)\) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
676 versere
= re
.compile(r
"""^\^verse\(([0-9]+)\)(.*)\^end""")
678 TPre
= re
.compile(r
"""^\^TP\(([0-9]+),([0-9]+)\) *([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)""")
682 def __init__ (self
, name
):
683 self
.measures
= [None]
684 self
.entries
= [None]
687 self
.tuplets
= [None]
690 self
.articulations
= [None]
691 self
.syllables
= [None]
697 def get_global_measure (self
, no
):
698 if len (self
.measures
) <= no
:
699 self
.measures
= self
.measures
+ [None]* (1 + no
- len (self
.measures
))
701 if self
.measures
[no
] == None:
702 self
.measures
[no
] = Global_measure (no
)
704 return self
.measures
[no
]
707 def get_staff(self
,staffno
):
708 if len (self
.staffs
) <= staffno
:
709 self
.staffs
= self
.staffs
+ [None] * (1 + staffno
- len (self
.staffs
))
711 if self
.staffs
[staffno
] == None:
712 self
.staffs
[staffno
] = Staff (staffno
)
714 return self
.staffs
[staffno
]
717 def try_IS (self
, l
):
720 def try_BC (self
, l
):
723 bn
= string
.atoi (m
.group (1))
724 where
= string
.atoi (m
.group (2)) / 1024.0
729 (nil
, num
) = map (string
.atoi
, (m
.groups ()[0:2]))
730 entries
= map (string
.atoi
, (m
.groups ()[2:]))
732 if self
.tuplets
[-1] == None or num
<> self
.tuplets
[-1].start_note
:
733 self
.tuplets
.append (Tuplet (num
))
735 self
.tuplets
[-1].append_finale (entries
)
737 def try_IM (self
, l
):
740 a
= string
.atoi (m
.group (1))
741 b
= string
.atoi (m
.group (2))
743 fin
= map (string
.atoi
, m
.groups ()[2:])
745 self
.articulations
.append (Articulation (a
,b
,fin
))
747 def try_verse (self
,l
):
748 m
= versere
.match (l
)
750 a
= string
.atoi (m
.group (1))
753 body
= re
.sub (r
"""\^[a-z]+\([^)]+\)""", "", body
)
754 body
= re
.sub ("\^[a-z]+", "", body
)
755 self
.verses
.append (Verse (a
, body
))
761 a
= string
.atoi (m
.group (1))
762 b
= string
.atoi (m
.group (2))
764 fin
= map (string
.atoi
, m
.groups ()[2:])
766 self
.syllables
.append (Syllable (a
,b
,fin
))
768 def try_eE (self
, l
):
772 (no
, prev
, next
, dur
, pos
, entryflag
, extended
, follow
) = tup
773 (no
, prev
, next
, dur
, pos
,extended
, follow
) \
774 = tuple (map (string
.atoi
, [no
,prev
,next
,dur
,pos
,extended
,follow
]))
776 entryflag
= string
.atol (entryflag
,16)
777 if len (self
.entries
) <= no
:
778 # missing entries seem to be quite common.
779 # we fill'em up with None.
780 self
.entries
= self
.entries
+ [None] * (no
- len (self
.entries
) + 1)
782 current_entry
= ((no
, prev
, next
, dur
, pos
, entryflag
, extended
, follow
), [])
783 self
.entries
[no
] = current_entry
789 slurno
= string
.atoi (m
.group (1))
793 sl
= self
.slur_dict
[slurno
]
796 self
.slur_dict
[slurno
] = sl
798 params
= list (m
.groups ()[1:])
799 params
= map (string
.atoi
, params
)
800 sl
.append_entry (params
)
806 (staffno
,measno
) = m
.groups ()[0:2]
807 s
= string
.atoi (staffno
)
808 me
= string
.atoi (measno
)
810 entry
= m
.groups () [2:]
811 st
= self
.get_staff (s
)
812 meas
= st
.get_measure (me
)
813 meas
.add_finale_entry (entry
)
819 (frameno
, startnote
, endnote
, foo
, bar
) = m
.groups ()
820 (frameno
, startnote
, endnote
) = tuple (map (string
.atoi
, [frameno
, startnote
, endnote
]))
821 if len (self
.frames
) <= frameno
:
822 self
.frames
= self
.frames
+ [None] * (frameno
- len(self
.frames
) + 1)
824 self
.frames
[frameno
] = Frame ((frameno
, startnote
, endnote
))
828 def try_MS (self
, l
):
831 measno
= string
.atoi (m
.group (1))
832 keynum
= string
.atoi (m
.group (3))
833 meas
=self
. get_global_measure (measno
)
834 meas
.set_keysig (keynum
)
836 beats
= string
.atoi (m
.group (4))
837 beatlen
= string
.atoi (m
.group (5))
838 meas
.set_timesig ((beats
, beatlen
))
842 def try_note (self
, l
):
843 m
= note_re
.match (l
)
845 (pitch
, flag
) = m
.groups ()
846 pitch
= string
.atoi (pitch
)
847 flag
= string
.atol (flag
,16)
848 self
.entries
[-1][1].append ((pitch
,flag
))
850 def parse (self
, name
):
851 sys
.stderr
.write ('parsing ...')
854 gulp
= open (name
).read ()
856 gulp
= re
.sub ('[\n\r]+', '\n', gulp
)
857 ls
= string
.split (gulp
, '\n')
868 m
= self
.try_note (l
)
878 m
= self
.try_verse (l
)
880 sys
.stderr
.write ('processing ...')
883 self
.unthread_entries ()
885 for st
in self
.staffs
[1:]:
889 for m
in st
.measures
[1:]:
895 m
.global_measure
= self
.measures
[mno
]
897 sys
.stderr
.write ("Non-existent global measure %d" % mno
)
900 frame_obj_list
= [None]
901 for frno
in m
.frames
:
902 fr
= self
.frames
[frno
]
903 frame_obj_list
.append (fr
)
905 m
.frames
= frame_obj_list
906 for fr
in frame_obj_list
[1:]:
912 fr
.chords
= self
.get_thread (fr
.start
, fr
.end
)
917 for c
in self
.chords
[1:]:
921 for f
in self
.frames
[1:]:
925 for t
in self
.tuplets
[1:]:
926 t
.calculate (self
.chords
)
928 for s
in self
.slur_dict
.values():
929 s
.calculate (self
.chords
)
930 for s
in self
.articulations
[1:]:
931 s
.calculate (self
.chords
)
933 def get_thread (self
, startno
, endno
):
939 c
= self
.chords
[startno
]
941 sys
.stderr
.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno
, endno
))
945 while c
and c
.number () <> endno
:
957 for s
in self
.staffs
[1:]:
959 str = str + '\n\n' + s
.dump ()
960 staffs
.append ('\\' + s
.staffid ())
963 # should use \addlyrics ?
965 for v
in self
.verses
[1:]:
968 if len (self
.verses
) > 1:
969 sys
.stderr
.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
972 str = str + '\\score { < %s > } ' % string
.join (staffs
)
978 return 'ETF FILE %s %s' % (self
.measures
, self
.entries
)
980 def unthread_entries (self
):
982 for e
in self
.entries
[1:]:
986 self
.chords
.append (ch
)
988 for e
in self
.chords
[1:]:
991 e
.prev
= self
.chords
[e
.finale
[0][1]]
992 e
.next
= self
.chords
[e
.finale
[0][2]]
995 sys
.stderr
.write ("%s from LilyPond %s\n" % (program_name
, version
))
998 print """Usage: etf2ly [OPTION]... ETF-FILE
1000 Convert ETF to LilyPond.
1004 -o,--output=FILE set output filename to FILE
1005 -v,--version version information
1007 Enigma Transport Format is a format used by Coda Music Technology's
1008 Finale product. This program will convert a subset of ETF to a
1009 ready-to-use lilypond file.
1011 Report bugs to bug-gnu-music@gnu.org
1013 Written by Han-Wen Nienhuys <hanwen@cs.uu.nl>
1016 def print_version ():
1017 print r
"""etf2ly (GNU lilypond) %s
1019 This is free software. It is covered by the GNU General Public License,
1020 and you are welcome to change it and/or distribute copies of it under
1021 certain conditions. Invoke as `midi2ly --warranty' for more information.
1023 Copyright (c) 2000 by Han-Wen Nienhuys <hanwen@cs.uu.nl>
1028 (options
, files
) = getopt
.getopt (sys
.argv
[1:], 'vo:h', ['help','version', 'output='])
1034 if o
== '--help' or o
== '-h':
1037 if o
== '--version' or o
== '-v':
1041 if o
== '--output' or o
== '-o':
1054 sys
.stderr
.write ('Processing `%s\'\n' % f
)
1056 if not out_filename
:
1057 out_filename
= os
.path
.basename (re
.sub ('(?i).etf$', '.ly', f
))
1059 if out_filename
== f
:
1060 out_filename
= os
.path
.basename (f
+ '.ly')
1062 sys
.stderr
.write ('Writing `%s\'' % out_filename
)
1067 fo
= open (out_filename
, 'w')
1068 fo
.write ('%% lily was here -- automatically converted by etf2ly from %s\n' % f
)