- i = 0
- p = 0
- for thread in track:
- for chord in thread:
- for event in chord:
- if event[1].__class__ == Note:
- i = i + 1
- p = p + event[1].pitch
- if i and p / i <= 3*12:
- return Clef (0)
- elif i and p / i <= 5*12:
- return Clef (1)
- elif i and p / i >= 7*12:
- return Clef (3)
- else:
- return Clef (2)
-
-
-def convert_midi (f, o):
- global clocks_per_1, clocks_per_4, key
-
- str = open (f).read ()
- midi_dump = midi.parse (str)
-
- clocks_per_1 = midi_dump[0][1]
- clocks_per_4 = clocks_per_1 / 4
-
- global start_quant, start_quant_clocks
- if start_quant:
- start_quant_clocks = clocks_per_1 / start_quant
-
- global duration_quant, duration_quant_clocks
- if duration_quant:
- duration_quant_clocks = clocks_per_1 / duration_quant
-
- global allowed_tuplet_clocks
- allowed_tuplet_clocks = []
- for (dur, num, den) in allowed_tuplets:
- allowed_tuplet_clocks.append (clocks_per_1 * num / (dur * den))
-
- tracks = []
- for t in midi_dump[1]:
- key = Key (0, 0, 0)
- tracks.append (split_track (t))
-
- tag = '%% Lily was here -- automatically converted by %s from %s' % ( program_name, f)
-
- s = ''
- s = tag + '\n\n'
- for i in range (len (tracks)):
- s = s + dump_track (tracks[i], i)
-
- s = s + '\n\\score {\n <\n'
- for i in range (len (tracks)):
- track = track_name (i)
- item = track_first_item (tracks[i])
- if item and item.__class__ == Note:
- s = s + ' \\context Staff=%s \\%s\n' % (track, track)
- elif item and item.__class__ == Text:
- s = s + ' \\context Lyrics=%s \\%s\n' % (track, track)
- s = s + ' >\n}\n'
-
- progress (_ ("%s output to `%s'...") % ('LY', o))
-
- if o == '-':
- h = sys.stdout
- else:
- h = open (o, 'w')
-
- h.write (s)
- h.close ()
-
-
-(sh, long) = getopt_args (option_definitions)
-try:
- (options, files) = getopt.getopt(sys.argv[1:], sh, long)
-except getopt.error, s:
- errorport.write ('\n')
- errorport.write (_ ("error: ") + _ ("getopt says: `%s\'" % s))
- errorport.write ('\n')
- errorport.write ('\n')
- help ()
- sys.exit (2)
-
-for opt in options:
- o = opt[0]
- a = opt[1]
-
- if 0:
- pass
- elif o == '--help' or o == '-h':
- help ()
- errorport.write ('\n')
- errorport.write (_ ("Example:"))
- errorport.write (r'''
- midi2ly --key=-2:1 --duration-quant=32 \
- --allow-tuplet=4*2/3 --allow-tuplet=2*4/3 foo.midi
+ i = 0
+ p = 0
+ for thread in track:
+ for chord in thread:
+ for event in chord:
+ if event[1].__class__ == Note:
+ i = i + 1
+ p = p + event[1].pitch
+ if i and p / i <= 3*12:
+ return Clef (0)
+ elif i and p / i <= 5*12:
+ return Clef (1)
+ elif i and p / i >= 7*12:
+ return Clef (3)
+ else:
+ return Clef (2)
+
+
+def convert_midi (in_file, out_file):
+ global clocks_per_1, clocks_per_4, key
+ global start_quant_clocks
+ global duration_quant_clocks
+ global allowed_tuplet_clocks
+
+ str = open (in_file).read ()
+ midi_dump = midi.parse (str)
+
+ clocks_per_1 = midi_dump[0][1]
+ clocks_per_4 = clocks_per_1 / 4
+
+ if global_options.start_quant:
+ start_quant_clocks = clocks_per_1 / global_options.start_quant
+
+ if global_options.duration_quant:
+ duration_quant_clocks = clocks_per_1 / global_options.duration_quant
+
+ allowed_tuplet_clocks = []
+ for (dur, num, den) in global_options.allowed_tuplets:
+ allowed_tuplet_clocks.append (clocks_per_1 / den)
+
+ tracks = []
+ for t in midi_dump[1]:
+ global_options.key = Key (0, 0, 0)
+ tracks.append (split_track (t))
+
+ tag = '%% Lily was here -- automatically converted by %s from %s' % ( program_name, in_file)
+
+
+ s = ''
+ s = tag + '\n\\version "2.7.18"\n\n'
+ for i in range (len (tracks)):
+ s = s + dump_track (tracks[i], i)
+
+ s = s + '\n\\score {\n <<\n'
+
+ i = 0
+ for t in tracks:
+ track = track_name (i)
+ item = track_first_item (t)
+
+ if item and item.__class__ == Note:
+ s = s + ' \\context Staff=%s \\%s\n' % (track, track)
+ elif item and item.__class__ == Text:
+ s = s + ' \\context Lyrics=%s \\%s\n' % (track, track)
+
+ i += 1
+ s = s + ' >>\n}\n'
+
+ progress (_ ("%s output to `%s'...") % ('LY', out_file))
+
+ if out_file == '-':
+ handle = sys.stdout
+ else:
+ handle = open (out_file, 'w')
+
+ handle.write (s)
+ handle.close ()
+
+
+def get_option_parser ():
+ p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'midi2ly',
+ description=_ ("Convert %s to LilyPond input.\n") % 'MIDI',
+ add_help_option=False)
+
+ p.add_option ('-a', '--absolute-pitches',
+ action='store_true',
+ help=_ ("print absolute pitches"))
+ p.add_option ('-d', '--duration-quant',
+ metavar= _("DUR"),
+ help=_ ("quantise note durations on DUR"))
+ p.add_option ('-e', '--explicit-durations',
+ action='store_true',
+ help=_ ("print explicit durations"))
+ p.add_option("-h", "--help",
+ action="help",
+ help=_ ("show this help and exit"))
+ p.add_option('-k', '--key', help=_ ("set key: ALT=+sharps|-flats; MINOR=1"),
+ metavar=_ ("ALT[:MINOR]"),
+ default='0'),
+ p.add_option ('-o', '--output', help=_ ("write output to FILE"),
+ metavar=_("FILE"),
+ action='store')
+ p.add_option ('-s', '--start-quant',help= _ ("quantise note starts on DUR"),
+ metavar=_ ("DUR"))
+ p.add_option ('-t', '--allow-tuplet',
+ metavar=_ ("DUR*NUM/DEN"),
+ action = "append",
+ dest="allowed_tuplets",
+ help=_ ("allow tuplet durations DUR*NUM/DEN"),
+ default=[])
+ p.add_option ('-V', '--verbose', help=_ ("be verbose"),
+ action='store_true'
+ ),
+ p.version = "midi2ly (LilyPond) @TOPLEVEL_VERSION@"
+ p.add_option("--version",
+ action="version",
+ help=_ ("show version number and exit"))
+ p.add_option ('-w', '--warranty', help=_ ("show warranty and copyright"),
+ action='store_true',
+ ),
+ p.add_option ('-x', '--text-lyrics', help=_ ("treat every text as a lyric"),
+ action='store_true')
+
+ p.add_option_group (ly.display_encode (_ ("Examples")),
+ description = r'''
+ $ midi2ly --key=-2:1 --duration-quant=32 --allow-tuplet=4*2/3 --allow-tuplet=2*4/3 foo.midi