import os
import sys
-import __main__
-import getopt
import string
import re
sys.path.insert (0, datadir)
import lilylib as ly
-import fontextract
global _;_=ly._
from convertrules import *
add_version = 1
+def warning (s):
+ sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
+
+def error (s):
+ sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
+
+def identify (port=sys.stderr):
+ port.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
+
+def warranty ():
+ identify ()
+ sys.stdout.write ('''
+Copyright (c) %s by
+
+ Han-Wen Nienhuys
+ Jan Nieuwenhuizen
+
+%s
+%s
+''' ( '2001--2006',
+ _('Distributed under terms of the GNU General Public License.'),
+ _('It comes with NO WARRANTY.')))
+
+
def get_option_parser ():
p = ly.get_option_parser (usage='lilypond-book [OPTIONS] FILE',
def str_to_tuple (s):
- return tuple (map (string.atoi, string.split (s, '.')))
+ return tuple (map (int, string.split (s, '.')))
def tup_to_str (t):
return string.join (map (lambda x: '%s' % x, list (t)), '.')
def do_one_file (infile_name):
sys.stderr.write (_ ("Processing `%s\'... ") % infile_name)
sys.stderr.write ('\n')
- outfile_name = ''
- if global_options.edit:
- outfile_name = infile_name + '.NEW'
- elif global_options.outfile_name:
- outfile_name = global_options.outfile_name
+ from_version = None
+ to_version = None
if global_options.from_version:
from_version = global_options.from_version
else:
# should parse files[] to read \version?
if global_options.show_rules:
- show_rules (sys.stdout, from_version, to_version)
+ show_rules (sys.stdout, global_options.from_version, global_options.to_version)
sys.exit (0)
- ly.identify (sys.stderr)
+ identify (sys.stderr)
for f in files:
if f == '-':
f = ''
elif not os.path.isfile (f):
- ly.error (_ ("can't open file: `%s'") % f)
+ error (_ ("can't open file: `%s'") % f)
if len (files) == 1:
sys.exit (1)
continue
try:
do_one_file (f)
except UnknownVersion:
- ly.error (_ ("can't determine version for `%s'. Skipping") % f)
+ error (_ ("can't determine version for `%s'. Skipping") % f)
sys.stderr.write ('\n')
'''
TODO:
- * test on weird and unquantised midi input (lily-devel)
- * update doc and manpage
-
- * simply insert clef changes whenever too many ledger lines
- [to avoid tex capacity exceeded]
- * do not ever quant skips
- * better lyrics handling
- * [see if it is feasible to] move ly-classes to library for use in
- other converters, while leaving midi specific stuff here
+ * test on weird and unquantised midi input (lily-devel)
+ * update doc and manpage
+
+ * simply insert clef changes whenever too many ledger lines
+ [to avoid tex capacity exceeded]
+ * do not ever quant skips
+ * better lyrics handling
+ * [see if it is feasible to] move ly-classes to library for use in
+ other converters, while leaving midi specific stuff here
'''
-import getopt
import os
import string
import sys
-
+
################################################################
# Users of python modules should include this snippet.
#
if os.path.exists (os.path.join (datadir, 'lib/lilypond/@TOPLEVEL_VERSION@/')):
libdir = os.path.join (libdir, 'lib/lilypond/@TOPLEVEL_VERSION@/')
-
+
if os.path.exists (os.path.join (datadir, 'lib/lilypond/current/')):
libdir = os.path.join (libdir, 'lib/lilypond/current/')
-
+
sys.path.insert (0, os.path.join (libdir, 'python'))
# dynamic relocation, for GUB binaries.
bindir = os.path.split (sys.argv[0])[0]
-for p in ['share', 'lib']:
- datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
+
+for prefix_component in ['share', 'lib']:
+ datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % prefix_component)
sys.path.insert (0, datadir)
import midi
## FIXME: compile fix --jcn
if dump_dur and (global_options.explicit_durations \
- or Duration.compare (self.duration,
- reference_note.duration)):
+ or self.duration.compare (reference_note.duration)):
s = s + self.duration.dump ()
reference_note = self
self.minor = minor
def dump (self):
- global key
- key = self
+ global_options.key = self
s = ''
if self.sharps and self.flats:
s = '"%s"' % self.text
d = Duration (self.clocks)
if global_options.explicit_durations \
- or Duration.compare (d,
- reference_note.duration):
+ or d.compare (reference_note.duration):
s = s + Duration (self.clocks).dump ()
s = s + ' '
else:
# ugh, must set key while parsing
# because Note init uses key
# Better do Note.calc () at dump time?
- global key
- key = k
+ global_options.key = k
elif e[1][1] == midi.LYRIC \
or (global_options.text_lyrics and e[1][1] == midi.TEXT_EVENT):
def dump_skip (skip, clocks):
return skip + Duration (clocks).dump () + ' '
-def dump (self):
- return self.dump ()
+def dump (d):
+ return d.dump ()
def dump_chord (ch):
s = ''
def dump_channel (thread, skip):
- global key, reference_note, time
+ global reference_note, time
- key = Key (0, 0, 0)
+ global_options.key = Key (0, 0, 0)
time = Time (4, 4)
# urg LilyPond doesn't start at c4, but
# remembers from previous tracks!
allowed_tuplet_clocks = []
for (dur, num, den) in global_options.allowed_tuplets:
- allowed_tuplet_clocks.append (clocks_per_1 * num / (dur * den))
+ allowed_tuplet_clocks.append (clocks_per_1 / den)
tracks = []
for t in midi_dump[1]:
- key = Key (0, 0, 0)
+ global_options.key = Key (0, 0, 0)
tracks.append (split_track (t))
- tag = '%% Lily was here -- automatically converted by %s from %s' % ( program_name, f)
+ tag = '%% Lily was here -- automatically converted by %s from %s' % ( program_name, in_file)
s = ''
i += 1
s = s + ' >>\n}\n'
- progress (_ ("%s output to `%s'...") % ('LY', o))
+ progress (_ ("%s output to `%s'...") % ('LY', out_file))
- if o == '-':
+ if out_file == '-':
handle = sys.stdout
else:
handle = open (out_file, 'w')
dest="allowed_tuplets",
help=_ ("allow tuplet durations DUR*NUM/DEN"),
default=[])
- p.add_option ('-V', '--verbose', help=_ ("be verbose"),
- action='store_true',
+ p.add_option ('-V', '--verbose', help=_("be verbose"),
+ action='store_true'
),
- p.add_option ('-w', '--warranty', help=_ ("show warranty"),
+ p.add_option ('-w', '--warranty', help=_("show warranty"),
action='store_true',
),
p.add_option ('-x', '--text-lyrics', help=_("treat every text as a lyric"),
sys.exit (2)
if options.duration_quant:
- options.duration_quant = string.atoi (options.duration_quant)
+ options.duration_quant = int (options.duration_quant)
if options.warranty:
warranty ()
sys.exit (0)
if 1:
- (alterations, minor) = map (string.atoi, string.split (options.key + ':0', ':'))[0:2]
+ (alterations, minor) = map (int, string.split (options.key + ':0', ':'))[0:2]
sharps = 0
flats = 0
if alterations >= 0:
if options.start_quant:
- options.start_quant = string.atoi (a)
+ options.start_quant = int (options.start_quant)
- options.allowed_tuplets = [map (string.atoi, a.replace ('/','*').split ('*'))
+ options.allowed_tuplets = [map (int, a.replace ('/','*').split ('*'))
for a in options.allowed_tuplets]
global global_options
return args
+def main():
+ files = do_options()
+
+ for f in files:
+ g = f
+ g = strip_extension (g, '.midi')
+ g = strip_extension (g, '.mid')
+ g = strip_extension (g, '.MID')
+ (outdir, outbase) = ('','')
+
+ if not output_name:
+ outdir = '.'
+ outbase = os.path.basename (g)
+ o = os.path.join (outdir, outbase + '-midi.ly')
+ elif output_name[-1] == os.sep:
+ outdir = output_name
+ outbase = os.path.basename (g)
+ os.path.join (outdir, outbase + '-gen.ly')
+ else:
+ o = output_name
+ (outdir, outbase) = os.path.split (o)
-files = do_options()
-
-for f in files:
- g = f
- g = strip_extension (g, '.midi')
- g = strip_extension (g, '.mid')
- g = strip_extension (g, '.MID')
- (outdir, outbase) = ('','')
-
- if not output_name:
- outdir = '.'
- outbase = os.path.basename (g)
- o = os.path.join (outdir, outbase + '-midi.ly')
- elif output_name[-1] == os.sep:
- outdir = output_name
- outbase = os.path.basename (g)
- os.path.join (outdir, outbase + '-gen.ly')
- else:
- o = output_name
- (outdir, outbase) = os.path.split (o)
-
- if outdir != '.' and outdir != '':
- try:
- os.mkdir (outdir, 0777)
- except OSError:
- pass
-
- convert_midi (f, o)
+ if outdir != '.' and outdir != '':
+ try:
+ os.mkdir (outdir, 0777)
+ except OSError:
+ pass
+ convert_midi (f, o)
+if __name__ == '__main__':
+ main()