]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/midi2ly.py
add spaces.
[lilypond.git] / scripts / midi2ly.py
index 7fe8b24b04157976fa1a429d7f205c7af0318129..0ab2f9203f113481522654b10370f6bee8a05329 100644 (file)
@@ -4,8 +4,8 @@
 # 
 # source file of the GNU LilyPond music typesetter
 #
-# convert MIDI to LilyPond source
-#
+# (c)  1998--2003  Han-Wen Nienhuys <hanwen@cs.uu.nl>
+#                 Jan Nieuwenhuizen <janneke@gnu.org>
 
 
 '''
@@ -22,25 +22,27 @@ TODO:
      other converters, while leaving midi specific stuff here
 '''
 
-import os
-import sys
 import getopt
-import sys
+import os
 import string
+import sys
 
 
-# do fuddling: we must load the midi module from the right directory. 
-datadir = '@local_lilypond_datadir@'
-if os.environ.has_key ('LILYPONDPREFIX'):
-       datadir = os.environ['LILYPONDPREFIX']
-else:
-       datadir = '@local_lilypond_datadir@'
+################################################################
+# Users of python modules should include this snippet.
+#
+# This soon to be removed for: import lilypond.lilylib as ly
+libdir = '@local_lilypond_libdir@'
+if not os.path.isdir (libdir):
+       libdir = '@lilypond_libdir@'
+sys.path.insert (0, os.path.join (libdir, 'python'))
 
-sys.path.append (os.path.join (datadir, 'python'))
-sys.path.append (os.path.join (datadir, 'python/out'))
+
+################################################################
 
 import midi
 
+
 ################################################################
 ################ CONSTANTS
 
@@ -89,18 +91,18 @@ verbose_p = 0
 # keep_temp_dir_p = 0
 
 
-help_summary = _ ("Convert MIDI to LilyPond source")
+help_summary = _ ("Convert MIDI to LilyPond source.")
 
 option_definitions = [
        ('', 'a', 'absolute-pitches', _ ("print absolute pitches")),
        (_ ("DUR"), 'd', 'duration-quant', _ ("quantise note durations on DUR")),
        ('', 'e', 'explicit-durations', _ ("print explicit durations")),
-       ('', 'h', 'help', _ ("this help")),
+       ('', 'h', 'help', _ ("print this help")),
        (_ ("ALT[:MINOR]"), 'k', 'key', _ ("set key: ALT=+sharps|-flats; MINOR=1")),
-       (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")),
+       (_ ("FILE"), 'o', 'output', _ ("write output to FILE")),
        (_ ("DUR"), 's', 'start-quant', _ ("quantise note starts on DUR")),
        (_ ("DUR*NUM/DEN"), 't', 'allow-tuplet', _ ("allow tuplet durations DUR*NUM/DEN")),
-       ('', 'V', 'verbose', _ ("verbose")),
+       ('', 'V', 'verbose', _ ("be verbose")),
        ('', 'v', 'version', _ ("print version number")),
        ('', 'w', 'warranty', _ ("show warranty and copyright")),
        ('', 'x', 'text-lyrics', _ ("treat every text as a lyric")),
@@ -131,7 +133,7 @@ def identify ():
 def warranty ():
        identify ()
        sys.stdout.write ('\n')
-       sys.stdout.write (_ ('Copyright (c) %s by' % ' 2001--2002'))
+       sys.stdout.write (_ ('Copyright (c) %s by' % ' 2001--2003'))
        sys.stdout.write ('\n')
        sys.stdout.write ('  Han-Wen Nienhuys')
        sys.stdout.write ('  Jan Nieuwenhuizen')
@@ -185,9 +187,9 @@ def option_help_str (o):
        if o[1]:
                sh = '-%s' % o[1]
 
-       sep = ' '
+       sep = '  '
        if o[1] and o[2]:
-               sep = ','
+               sep = ', '
                
        long = ''
        if o[2]:
@@ -219,7 +221,7 @@ def options_help_str (opts):
        return str
 
 def help ():
-       ls = [(_ ("Usage: %s [OPTION]... FILE") % program_name),
+       ls = [(_ ("Usage: %s [OPTIONS]... FILE") % program_name),
                ('\n\n'),
                (help_summary),
                ('\n\n'),
@@ -227,7 +229,7 @@ def help ():
                ('\n'),
                (options_help_str (option_definitions)),
                ('\n\n'),
-               (_ ("Report bugs to %s") % 'bug-lilypond@gnu.org'),
+               (_ ("Report bugs to %s.") % 'bug-lilypond@gnu.org'),
                ('\n')]
        map (sys.stdout.write, ls)
        
@@ -423,7 +425,8 @@ class Note:
 
                return (o, n, a)
                
-       def dump (self):
+       def dump (self, dump_dur = 1):
+               global reference_note
                s = chr ((self.notename + 2)  % 7 + ord ('a'))
                s = s + self.alteration_names[self.alteration + 2]
                if absolute_p:
@@ -443,11 +446,12 @@ class Note:
                elif commas < 0:
                        s = s + "," * -commas
 
-               if explicit_durations_p \
-                  or Duration.compare (self.duration, reference_note.duration):
+               ## FIXME: compile fix --jcn
+               if dump_dur and (explicit_durations_p \
+                  or Duration.compare (self.duration,
+                                       reference_note.duration)):
                        s = s + self.duration.dump ()
 
-               global reference_note
                reference_note = self
                
                # TODO: move space
@@ -781,12 +785,14 @@ def dump_chord (ch):
                s = s + dump (notes[0])
        elif len (notes) > 1:
                global reference_note
-               s = s + '<'
-               s = s + notes[0].dump ()
+               s = s + '<<'
+               s = s + notes[0].dump (dump_dur = 0)
                r = reference_note
                for i in notes[1:]:
-                       s = s + i.dump ()
-               s = s + '>'
+                       s = s + i.dump (dump_dur = 0 )
+               s = s + '>>'
+
+               s = s + notes[0].duration.dump()
                reference_note = r
        return s
 
@@ -1045,13 +1051,10 @@ for opt in options:
 
 
        elif o == '--absolute-pitches' or o == '-a':
-               global absolute_p
                absolute_p = 1
        elif o == '--duration-quant' or o == '-d':
-               global duration_quant
                duration_quant = string.atoi (a)
        elif o == '--explicit-durations' or o == '-e':
-               global explicit_durations_p
                explicit_durations_p = 1
        elif o == '--key' or o == '-k':
                (alterations, minor) = map (string.atoi, string.split (a + ':0', ':'))[0:2]
@@ -1061,7 +1064,6 @@ for opt in options:
                        sharps = alterations
                else:
                        flats = - alterations
-               global key
                key = Key (sharps, flats, minor)
        elif o == '--start-quant' or o == '-s':
                start_quant = string.atoi (a)