3 # Copyright (C) 2006, 2007 Brailcom, o.p.s.
5 # Author: Milan Zamazal <pdm@brailcom.org>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
32 def process_options (args):
33 parser = optparse.OptionParser (version="@TOPLEVEL_VERSION@")
34 parser.add_option ('', '--filter-tracks', metavar='REGEXP', action='store', type='string', dest='regexp',
35 help="display only tracks numbers, of those track names matching REGEXP")
36 parser.add_option ('', '--prefix-tracks', metavar='PREFIX', action='store', type='string', dest='prefix',
37 help="prefix filtered track numbers with PREFIX")
38 parser.add_option ('', '--dump', action='store_true', dest='dump',
39 help="just dump parsed contents of the MIDI file")
40 parser.usage = parser.usage + " FILE"
41 options, args = parser.parse_args (args)
49 return midi.parse (open (file).read ())
51 def track_info (data):
53 def track_name (track):
55 for time, event in track:
58 if event[0] == 255 and event[1] == 3:
63 for i in range (len (tracks)):
64 track_info.append ((i, track_name (tracks[i])))
68 options, args = process_options (sys.argv[1:])
70 midi_data = read_midi (midi_file)
71 info = track_info (midi_data)
76 regexp = re.compile (options.regexp)
77 numbers = [str(n+1) for n, name in info if regexp.search (name)]
80 sys.stdout.write ('%s ' % (options.prefix,))
82 sys.stdout.write (string.join (numbers, ','))
83 sys.stdout.write ('\n')
86 sys.stdout.write ('%d %s\n' % (n+1, name,))
88 if __name__ == '__main__':