-
Notifications
You must be signed in to change notification settings - Fork 0
/
lilymidi
95 lines (88 loc) · 2.24 KB
/
lilymidi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl
# Copyright (C) 2006 Brailcom, o.p.s.
#
# Author: Milan Zamazal <pdm@brailcom.org>
#
# COPYRIGHT NOTICE
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
use MIDI;
use Getopt::Long;
sub usage ()
{
print "usage: lilymidi [ --filter-tracks PATTERN [ --prefix-tracks PREFIX ] | --dump ] FILE\n";
exit 1;
}
$result = GetOptions ("help" => \$opt_help,
"filter-tracks=s" => \$opt_filter_tracks,
"prefix-tracks=s" => \$opt_prefix_tracks,
"dump" => \$opt_dump);
if ($opt_help || @ARGV != 1)
{
usage ();
}
$file = $ARGV[0];
$opus = MIDI::Opus->new ({'from_file' => $file});
$i = 1;
@track_names = map {
my @events = $_->events ();
my $name = "";
my $found = 0;
foreach my $e (@events)
{
if ($$e[0] eq "track_name")
{
$name = $$e[2];
$found++;
}
elsif ($$e[0] eq "patch_change")
{
$i = $$e[2] + 1;
$found++;
}
if ($found >= 2) { last; }
}
my @item = ($i++, $name);
\@item;
} $opus->tracks ();;
if ($opt_dump)
{
$opus->dump ({'dump_tracks' => '1'});
}
elsif ($opt_filter_tracks)
{
my @numbers = ();
foreach my $name (@track_names)
{
if ($$name[1] =~ $opt_filter_tracks)
{
push @numbers, ($$name[0]);
}
}
if ($opt_prefix_tracks && @numbers)
{
print $opt_prefix_tracks . " ";
}
print join ',', @numbers;
}
else
{
foreach my $name (@track_names)
{
print $$name[0] . " " . $$name[1] . "\n";
++$i;
}
}
__END__