3 # once upon a rainy monday afternoon.
10 program_name = 'abc-to-ly'
20 global_voice_stuff = []
22 global_key = [0] * 7 # UGH
26 def dump_header (hdr):
29 print '%s = "%s";\n'% (k,hdr[k])
32 def set_default_length (s):
33 m = re.search ('1/([0-9]+)', s)
35 __main__.default_len = string.atoi ( m.group (1))
44 print 'can\'t open file: ' + f + '\n'
48 print 'gulped empty file: ' + f + '\n'
53 # pitch manipulation. Tuples are (name, alteration).
54 # 0 is (central) C. Alteration -1 is a flat, Alteration +1 is a sharp
56 def semitone_pitch (tup):
69 def fifth_above_pitch (tup):
70 (n, a) = (tup[0] + 4, tup[1])
72 difference = 7 - (semitone_pitch ((n,a)) - semitone_pitch (tup))
83 (t,a) = fifth_above_pitch (p)
84 if semitone_pitch((t,a)) % 12 == 0:
96 (t,a) = quart_above_pitch (p)
97 if semitone_pitch((t,a)) % 12 == 0:
103 def quart_above_pitch (tup):
104 (n, a) = (tup[0] + 3, tup[1])
106 difference = 5 - (semitone_pitch ((n,a)) - semitone_pitch (tup))
114 intkey = (ord (k[0]) - ord('a') + 5) % 7
118 if k and k[0] == 'b':
121 elif k and k[0] == '#':
125 keytup = (intkey, intkeyacc)
127 sharp_key_seq = sharp_keys ()
128 flat_key_seq = flat_keys ()
132 if keytup in sharp_key_seq:
134 key_count = sharp_key_seq.index (keytup)
135 accseq = map (lambda x: (4*x -1 ) % 7, range (1, key_count + 1))
137 elif keytup in flat_key_seq:
139 key_count = flat_key_seq.index (keytup)
140 accseq = map (lambda x: (3*x + 3 ) % 7, range (1, key_count + 1))
146 key_table[a] = key_table[a] + accsign
151 def try_parse_header_line (ln):
152 m = re.match ('^(.): *(.*)$', ln)
160 global_voice_stuff.append ('\\time %s;' % a)
162 __main__.global_key =compute_key (a)# ugh.
164 global_voice_stuff.append ('\\key %s;' % a)
166 header ['origin'] = a
168 header ['crossRefNumber'] = a
173 header ['history'] = a
177 header ['subtitle'] = a
179 set_default_length (ln)
184 def pitch_to_mudela_name (name, acc):
194 return chr (name + ord('c')) + s * acc
196 def octave_to_mudela_quotes (o):
208 while str[0] in "1234567890":
209 durstr = durstr + str[0]
214 n =string.atoi (durstr)
218 def duration_to_mudela_duration (multiply_tup, defaultlen):
221 # WAT IS ABC EEN ONTZETTENDE PROGRAMMEERPOEP !
222 def try_parse_note (str):
242 if str[0] in "ABCDEFG":
243 str = string.lower (str[0]) + str[1:]
248 if str[0] in "abcdefg":
249 notename = (ord(str[0]) - ord('a') + 5)%7
252 return str # failed; not a note!
257 while str[0] == '\'':
264 (str, num) = parse_num (str)
271 (str, den) =parse_num (str)
275 print duration_to_mudela_duration ((num,den), default_len)
276 print '%s%s%d' % (pitch_to_mudela_name(notename, acc + global_key[notename]) , octave_to_mudela_quotes (octave), duration_mult)
286 def junk_space (str):
287 while str and str[0] in '\t\n ':
292 def try_parse_bar (str):
297 def try_parse_body_line (ln):
299 while ln and ln != prev_ln:
301 ln = try_parse_note (ln)
302 ln = try_parse_bar (ln)
314 if re.match ('^[\t ]*(%.*)?$', l):
318 m = try_parse_header_line (l)
323 m = try_parse_body_line (l)
327 print '%s %s' % (program_name, version)
331 This is a disfunctional ABC to mudela convertor. It only gulps input, and
332 says huh when confused. Does not do chords. Go ahead and fix me.
334 -h, --help this help.
340 (options, files) = getopt.getopt (sys.argv[1:], 'h', ['help'])
345 if o== '--help' or o == '-h':
358 print global_voice_stuff, 1