#!@TARGET_PYTHON@
# -*- coding: utf-8 -*-
+
# once upon a rainy monday afternoon.
+
+# This file is part of LilyPond, the GNU music typesetter.
+#
+# LilyPond is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# LilyPond is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
+
#
# ...
#
# (not finished.)
-# ABC standard v1.6: http://www.gre.ac.uk/~c.walshaw/abc2mtex/abc.txt
+# ABC standard v1.6: http://www.walshaw.plus.com/abc/
#
# Enhancements (Roy R. Rankin)
#
#TODO:
#
+# * coding style
# * lilylib
# * GNU style messages: warning:FILE:LINE:
# * l10n
import getopt
import sys
import re
-import string
import os
program_name = sys.argv[0]
-for d in ['@lilypond_datadir@',
- '@lilypond_libdir@']:
- sys.path.insert (0, os.path.join (d, 'python'))
-
-# dynamic relocation, for GUB binaries.
-bindir = os.path.abspath (os.path.split (sys.argv[0])[0])
-for p in ['share', 'lib']:
- datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
- sys.path.insert (0, datadir)
-
+"""
+@relocate-preamble@
+"""
import lilylib as ly
global _;_=ly._
sys.stderr.write (msg)
if global_options.strict:
sys.exit (1)
-
+
def alphabet (i):
return chr (i + ord('A'))
def check_clef(s):
+ # the number gives the base_octave
+ clefs = [("treble", "treble", 0),
+ ("treble1", "french", 0),
+ ("bass3", "varbaritone", 0),
+ ("bass", "bass", 0),
+ ("alto4", "tenor", 0),
+ ("alto2", "mezzosoprano", 0),
+ ("alto1", "soprano", 0),
+ ("alto", "alto", 0),
+ ("perc", "percussion", 0)]
+ modifier = [("-8va", "_8", -1),
+ ("-8", "_8", -1),
+ ("\+8", "^8", +1),
+ ("8", "_8", -1)]
+
if not s:
return ''
- if re.match('-8va', s) or re.match('treble8', s):
- # treble8 is used by abctab2ps; -8va is used by barfly,
- # and by my patch to abc2ps. If there's ever a standard
- # about this we'll support that.
- s = s[4:]
- state.base_octave = -1
- voices_append("\\clef \"G_8\"\n")
- elif re.match('^treble', s):
- s = s[6:]
- if re.match ('^-8', s):
- s = s[2:]
- state.base_octave = -2
- voices_append("\\clef \"G_8\"\n")
- else:
- state.base_octave = 0
- voices_append("\\clef treble\n")
- elif re.match('^alto', s):
- s = s[4:]
- state.base_octave = -1
- voices_append ("\\clef alto\n" )
- elif re.match('^bass',s ):
- s = s[4:]
- state.base_octave = -2
- voices_append ("\\clef bass\n" )
+ clef = None;
+ octave = 0;
+ for c in clefs:
+ m = re.match('^'+c[0], s)
+ if m:
+ (clef, octave) = (c[1], c[2])
+ s = s[m.end():]
+ break;
+ if not clef:
+ return s
+
+ mod = "";
+ for md in modifier:
+ m = re.match('^'+md[0], s)
+ if m:
+ mod = md[1];
+ octave += md[2];
+ s = s[m.end():]
+ break;
+
+ state.base_octave = octave
+ voices_append ("\\clef \""+clef+mod+"\"\n")
return s
def select_voice (name, rol):
ks.sort ()
for k in ks:
if re.match('[1-9]', k):
- m = alphabet(string.atoi(k))
+ m = alphabet (int (k))
else:
m = k
for i in range (len(slyrics[voice_idx_dict[k]])):
l= alphabet(i)
- outf.write ("\nwords%sV%s = \lyricmode {" % (m, l))
+ outf.write ("\nwords%sV%s = \\lyricmode {" % (m, l))
outf.write ("\n" + slyrics [voice_idx_dict[k]][i])
outf.write ("\n}")
ks.sort ()
for k in ks:
if re.match ('[1-9]', k):
- m = alphabet(string.atoi(k))
+ m = alphabet (int (k))
else:
m = k
outf.write ("\nvoice%s = {" % m)
outf.write ("\n}")
def try_parse_q(a):
- global midi_specs
- #assume that Q takes the form "Q:1/4=120"
+ #assume that Q takes the form "Q:'opt. description' 1/4=120"
#There are other possibilities, but they are deprecated
- if string.count(a, '/') == 1:
- array=string.split(a,'/')
- numerator=array[0]
- if int(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.atoi(denominator)/string.atoi(numerator))
- midi_specs=string.join([" \n\t\t\context {\n\t\t \Score tempoWholesPerMinute = #(ly:make-moment ", perminute, " ", duration, ")\n\t\t }\n"])
+ r = re.compile ('^(.*) *([0-9]+) */ *([0-9]+) *=* *([0-9]+)\s*')
+ m = r.match (a)
+ if m:
+ descr = m.group(1) # possibly empty
+ numerator = int(m.group (2))
+ denominator = int(m.group (3))
+ tempo = m.group (4)
+ dur = duration_to_lilypond_duration ((numerator,denominator), 1, 0)
+ voices_append ("\\tempo " + descr + " " + dur + "=" + tempo + "\n")
else:
sys.stderr.write("abc2ly: Warning, unable to parse Q specification: %s\n" % a)
-
+
def dump_score (outf):
outf.write (r"""
ks.sort ()
for k in ks:
if re.match('[1-9]', k):
- m = alphabet (string.atoi(k))
+ m = alphabet (int (k))
else:
m = k
if k == 'default' and len (voice_idx_dict) > 1:
l = ord( 'A' )
for lyrics in slyrics [voice_idx_dict[k]]:
- outf.write ("\n\t\\addlyrics { \n")
+ outf.write ("\n\t\\addlyrics {\n")
if re.match('[1-9]',k):
- m = alphabet (string.atoi(k))
+ m = alphabet (int (k))
else:
m = k
global length_specified
m = re.search ('1/([0-9]+)', s)
if m:
- __main__.default_len = string.atoi ( m.group (1))
+ __main__.default_len = int ( m.group (1))
length_specified = 1
def set_default_len_from_time_sig (s):
m = re.search ('([0-9]+)/([0-9]+)', s)
if m:
- n = string.atoi (m.group (1))
- d = string.atoi (m.group (2))
+ n = int (m.group (1))
+ d = int (m.group (2))
if (n * 1.0 )/(d * 1.0) < 0.75:
__main__.default_len = 16
else:
def lily_key (k):
orig = "" + k
# UGR
- k = string.lower (k)
+ k = k.lower ()
key = k[0]
#UGH
k = k[1:]
'dorian' : -2
}
def compute_key (k):
- k = string.lower (k)
+ k = k.lower ()
intkey = (ord (k[0]) - ord('a') + 5) % 7
intkeyacc =0
k = k[1:]
accseq = map (lambda x: (3*x + 3 ) % 7, range (1, key_count + 1))
else:
error ("Huh?")
- raise "Huh"
+ raise Exception ("Huh")
key_table = [0] * 7
for a in accseq:
dig = str[1]
str = str[2:]
prev_tuplet_state = state.parsing_tuplet
- state.parsing_tuplet = string.atoi (dig[0])
+ state.parsing_tuplet = int (dig[0])
if prev_tuplet_state:
voices_append ("}")
voices_append ("\\times %s {" % tup_lookup[dig])
header [key] = s + a
def wordwrap(a, v):
- linelen = len (v) - string.rfind(v, '\n')
+ linelen = len (v) - v.rfind ('\n')
if linelen + len (a) > 80:
v = v + '\n'
return v + a + ' '
if a == 'C':
if not state.common_time:
state.common_time = 1
- voices_append (" \\override Staff.TimeSignature #\'style = #'C\n")
+ voices_append (" \\override Staff.TimeSignature #'style = #'C\n")
a = '4/4'
if a == 'C|':
if not state.common_time:
state.common_time = 1
- voices_append ("\\override Staff.TimeSignature #\'style = #'C\n")
+ voices_append ("\\override Staff.TimeSignature #'style = #'C\n")
a = '2/2'
if not length_specified:
set_default_len_from_time_sig (a)
if g == 'K': # KEY
a = check_clef(a)
if a:
- m = re.match ('^([^ \t]*) *(.*)$', a) # seperate clef info
+ m = re.match ('^([^ \t]*) *([^ ]*)( *)(.*)$', a) # seperate clef info
if m:
# there may or may not be a space
# between the key letter and the mode
- if key_lookup.has_key(m.group(2)[0:3]):
- key_info = m.group(1) + m.group(2)[0:3]
- clef_info = m.group(2)[4:]
+ # convert the mode to lower-case before comparing
+ mode = m.group(2)[0:3].lower();
+ if key_lookup.has_key(mode):
+ # use the full mode, not only the first three letters
+ key_info = m.group(1) + m.group(2).lower()
+ clef_info = a[m.start(4):]
else:
key_info = m.group(1)
- clef_info = m.group(2)
+ clef_info = a[m.start(2):]
__main__.global_key = compute_key (key_info)
k = lily_key (key_info)
if k:
n = None
if durstr:
- n =string.atoi (durstr)
+ n = int (durstr)
return (str,n)
base = '\\breve'
dots = 1
if (multiply_tup[0] / multiply_tup[1]) == 4:
- base = '\longa'
+ base = '\\longa'
return '%s%s' % ( base, '.'* dots)
class Parser_state:
# s7m2 input doesnt care about spaces
if re.match('[ \t]*\(', str):
- str = string.lstrip (str)
+ str = str.lstrip ()
slur_begin =0
while str[:1] =='(' and str[1] not in DIGITS:
return(UNDEF)
def clear_bar_acc(state):
- for k in state.in_acc.keys():
- del state.in_acc[k]
-
+ state.in_acc = {}
+
# if we are parsing a beam, close it off
def close_beam_state(state):
octave = parser_state.base_octave
if str[0] in "ABCDEFG":
- str = string.lower (str[0]) + str[1:]
+ str = str[0].lower () + str[1:]
octave = octave - 1
(str, num,den,current_dots) = parse_duration (str, parser_state)
if re.match('[ \t]*\)', str):
- str = string.lstrip (str)
+ str = str.lstrip ()
slur_end =0
while str[:1] ==')':
if 'nobarlines' in str:
nobarlines = 1
elif str[0:3] == '%LY':
- p = string.find(str, 'voices')
+ p = str.find ('voices')
if (p > -1):
voices_append(str[p+7:])
voices_append("\n")
- p = string.find(str, 'slyrics')
+ p = str.find ('slyrics')
if (p > -1):
slyrics_append(str[p+8:])
def get_option_parser ():
p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'abc2ly',
- version="abc2ly (LilyPond) @TOPLEVEL_VERSION@",
description=_ ('''abc2ly converts ABC music files (see
-%s) to LilyPond input.''') % 'http://www.gre.ac.uk/~c.walshaw/abc2mtex/abc.txt')
-
+%s) to LilyPond input.
+''') % 'http://www.gre.ac.uk/~c.walshaw/abc2mtex/abc.txt',
+ add_help_option=False)
+
+ p.version = "abc2ly (LilyPond) @TOPLEVEL_VERSION@"
+ p.add_option("--version",
+ action="version",
+ help=_ ("show version number and exit"))
+
+ p.add_option("-h", "--help",
+ action="help",
+ help=_ ("show this help and exit"))
p.add_option ('-o', '--output', metavar='FILE',
help=_ ("write output to FILE"),
action='store')
- p.add_option ('-s', '--strict', help=_ ("be strict about succes"),
+ p.add_option ('-s', '--strict', help=_ ("be strict about success"),
action='store_true')
- p.add_option ('-b', '--beams', help=_ ("preserve ABC's notion of beams"))
- p.add_option_group ('bugs',
- description=(_ ('Report bugs via')
- + ''' http://post.gmane.org/post.php'''
- '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
+ p.add_option ('-b', '--beams', help=_ ("preserve ABC's notion of beams"), action="store_true")
+ p.add_option_group ('',
+ description=(
+ _ ('Report bugs via %s')
+ % 'http://post.gmane.org/post.php'
+ '?group=gmane.comp.gnu.lilypond.bugs') + '\n')
return p