Avoid `blabla: none' messages in internal documentation.
[lilypond.git] / scripts / lilymidi.py
blob22eb8e61f47b7ef8b05f6a7704bc1d748bbe5ccd
1 #!@TARGET_PYTHON@
3 # Copyright (C) 2006, 2007 Brailcom, o.p.s.
5 # Author: Milan Zamazal <pdm@brailcom.org>
7 # COPYRIGHT NOTICE
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
17 # for more details.
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.
24 import optparse
25 import os
26 import sys
28 """
29 @relocate-preamble@
30 """
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)
42 if len (args) != 1:
43 parser.print_help ()
44 sys.exit (2)
45 return options, args
47 def read_midi (file):
48 import midi
49 return midi.parse (open (file).read ())
51 def track_info (data):
52 tracks = data[1]
53 def track_name (track):
54 name = ''
55 for time, event in track:
56 if time > 0:
57 break
58 if event[0] == 255 and event[1] == 3:
59 name = event[2]
60 break
61 return name
62 track_info = []
63 for i in range (len (tracks)):
64 track_info.append ((i, track_name (tracks[i])))
65 return track_info
67 def go ():
68 options, args = process_options (sys.argv[1:])
69 midi_file = args[0]
70 midi_data = read_midi (midi_file)
71 info = track_info (midi_data)
72 if options.dump:
73 print midi_data
74 elif options.regexp:
75 import re
76 regexp = re.compile (options.regexp)
77 numbers = [str(n+1) for n, name in info if regexp.search (name)]
78 if numbers:
79 if options.prefix:
80 sys.stdout.write ('%s ' % (options.prefix,))
81 import string
82 sys.stdout.write (string.join (numbers, ','))
83 sys.stdout.write ('\n')
84 else:
85 for n, name in info:
86 sys.stdout.write ('%d %s\n' % (n+1, name,))
88 if __name__ == '__main__':
89 go ()