X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scripts%2Fabc2ly.py;h=b4295e933f597fa6db6b0a7ca854b1c298d1b074;hb=5e963da4031a9efceda94f4969785bf6fa13048b;hp=a64607dbd84bb06c72fab9dde03930a7cc4a4cc0;hpb=2a0f240bdfac5cec93adc010d3c5eff4986746e5;p=lilypond.git diff --git a/scripts/abc2ly.py b/scripts/abc2ly.py index a64607dbd8..b4295e933f 100644 --- a/scripts/abc2ly.py +++ b/scripts/abc2ly.py @@ -87,7 +87,7 @@ names = ["One", "Two", "Three"] DIGITS='0123456789' alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ" HSPACE=' \t' - +midi_specs = '' def check_clef(s): if not s: @@ -174,9 +174,13 @@ def dump_slyrics (outf): ks = voice_idx_dict.keys() ks.sort () for k in ks: + if re.match('[1-9]', k): + m = alphabet[string.atoi(k)] + else: + m = k for i in range (len(slyrics[voice_idx_dict[k]])): l=alphabet[i] - outf.write ("\nwords%sV%s = \\lyrics {" % (k, l)) + outf.write ("\nwords%sV%s = \\lyrics {" % (m, l)) outf.write ("\n" + slyrics [voice_idx_dict[k]][i]) outf.write ("\n}") @@ -185,7 +189,11 @@ def dump_voices (outf): ks = voice_idx_dict.keys() ks.sort () for k in ks: - outf.write ("\nvoice%s = \\notes {" % k) + if re.match ('[1-9]', k): + m = alphabet[string.atoi(k)] + else: + m = k + outf.write ("\nvoice%s = \\notes {" % m) dump_default_bar(outf) if repeat_state[voice_idx_dict[k]]: outf.write("\n\\repeat volta 2 {") @@ -197,6 +205,23 @@ def dump_voices (outf): outf.write("}") outf.write ("\n}") +def try_parse_q(a): + global midi_specs + #assume that Q takes the form "Q:1/4=120" + #There are other possibilities, but they are deprecated + if string.count(a, '/') == 1: + array=string.split(a,'/') + numerator=array[0] + if numerator != 1: + sys.stderr.write("abc2ly: Warning, unable to translate a Q specification with a numerator of %s: %s\n" % (numerator, a)) + array2=string.split(array[1],'=') + denominator=array2[0] + perminute=array2[1] + duration=str(string.atof(denominator)/string.atoi(numerator)) + midi_specs=string.join(["\\tempo", duration, "=", perminute]) + else: + sys.stderr.write("abc2ly: Warning, unable to parse Q specification: %s\n" % a) + def dump_score (outf): outf.write (r"""\score{ \notes < @@ -205,6 +230,10 @@ def dump_score (outf): ks = voice_idx_dict.keys (); ks.sort () for k in ks: + if re.match('[1-9]', k): + m = alphabet[string.atoi(k)] + else: + m = k if k == 'default' and len (voice_idx_dict) > 1: break if len ( slyrics [voice_idx_dict[k]] ): @@ -212,22 +241,26 @@ def dump_score (outf): outf.write ("\n\t\\context Staff=\"%s\"\n\t{\n" %k ) if k != 'default': outf.write ("\t \\voicedefault\n") - outf.write ("\t \\voice%s " % k) + outf.write ("\t \\voice%s " % m) outf.write ("\n\t}\n") if len ( slyrics [voice_idx_dict[k]] ): outf.write ("\n\t\\context Lyrics=\"%s\" \n\t<\t" % k) + if re.match('[1-9]',k): + m = alphabet[string.atoi(k)] + else: + m = k for i in range (len(slyrics[voice_idx_dict[k]])): l=alphabet[i] - outf.write("\n\t { \\words%sV%s }" % ( k, l) ) + outf.write("\n\t { \\words%sV%s }" % ( m, l) ) outf.write ( "\n\t>\n" ) outf.write ("\n >") outf.write ("\n\t\\paper {\n") if part_names: outf.write ("\t \\translator \n\t {\n") outf.write ("\t\t\\StaffContext\n") - outf.write ("\t\t\\consists Staff_margin_engraver\n") +# outf.write ("\t\t\\consists Staff_margin_engraver\n") outf.write ("\t }\n") - outf.write ("\t}\n\t\\midi {}\n}\n") + outf.write ("\t}\n\t\\midi {%s}\n}\n" % midi_specs) @@ -255,11 +288,11 @@ def gulp_file(f): n = i.tell () i.seek (0,0) except: - sys.stderr.write ("can't open file: %s\n" % f) + sys.stderr.write ("can't open file: `%s'\n" % f) return '' s = i.read (n) if len (s) <= 0: - sys.stderr.write ("gulped empty file: %s\n" % f) + sys.stderr.write ("gulped empty file: `%s'\n" % f) i.close () return s @@ -354,7 +387,7 @@ def lily_key (k): type = k[0:3] if key_lookup.has_key(type): return("%s \\%s" % ( key, key_lookup[type])) - sys.stderr.write("Unknown key type %s ignored\n" % type) + sys.stderr.write("Unknown key type `%s' ignored\n" % type) return key def shift_key (note, acc , shift): @@ -615,7 +648,8 @@ def try_parse_header_line (ln, state): lyrics_append(a) if g == 'w': # vocals slyrics_append (a) - + if g == 'Q': #tempo + try_parse_q (a) return '' return ln @@ -696,15 +730,16 @@ def parse_duration (str, parser_state): (str, num) = parse_num (str) if not num: num = 1 - - if str[0] == '/': - while str[:1] == '/': - str= str[1:] - d = 2 - if str[0] in DIGITS: - (str, d) =parse_num (str) + if len(str): + if str[0] == '/': + if len(str[0]): + while str[:1] == '/': + str= str[1:] + d = 2 + if str[0] in DIGITS: + (str, d) =parse_num (str) - den = den * d + den = den * d den = den * default_len @@ -779,7 +814,7 @@ def try_parse_articulation (str, state): while str and artic_tbl.has_key(str[:1]): state.next_articulation = state.next_articulation + artic_tbl[str[:1]] if not artic_tbl[str[:1]]: - sys.stderr.write("Warning: ignoring %s\n" % str[:1] ) + sys.stderr.write("Warning: ignoring `%s'\n" % str[:1] ) str = str[1:] @@ -1247,12 +1282,12 @@ for f in files: if f == '-': f = '' - sys.stderr.write ('Parsing... [%s]\n' % f) + sys.stderr.write ('Parsing `%s\'...\n' % f) parse_file (f) if not out_filename: out_filename = os.path.basename (os.path.splitext (f)[0]) + ".ly" - sys.stderr.write ('Ly output to: %s...' % out_filename) + sys.stderr.write ('lilypond output to: `%s\'...' % out_filename) outf = open (out_filename, 'w') # dump_global (outf)