]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/midichannels.pl
Merge branch 'master' of git://de.git.xonotic.org/xonotic/xonotic
[xonotic/xonotic.git] / misc / tools / midichannels.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use MIDI;
6 use MIDI::Opus;
7
8 my ($filename) = @ARGV;
9 my $opus = MIDI::Opus->new({from_file => $filename});
10
11 my %chanpos = (
12         note_off => 2,
13         note_on => 2,
14         key_after_touch => 2,
15         control_change => 2,
16         patch_change => 2,
17         channel_after_touch => 2,
18         pitch_wheel_change => 2
19 );
20
21 my %isclean = (
22         set_tempo => sub { 1; },
23         note_off => sub { 1; },
24         note_on => sub { 1; },
25         control_change => sub { $_[3] == 64; },
26 );
27
28 sub abstime(@)
29 {
30         my $t = 0;
31         return map { [$_->[0], $t += $_->[1], @{$_}[2..(@$_-1)]]; } @_;
32 }
33
34 sub reltime(@)
35 {
36         my $t = 0;
37         return map { my $tsave = $t; $t = $_->[1]; [$_->[0], $t - $tsave, @{$_}[2..(@$_-1)]]; } @_;
38 }
39
40 sub clean(@)
41 {
42         return reltime grep { ($isclean{$_->[0]} // sub { 0; })->(@$_) } abstime @_;
43 }
44
45 while(<STDIN>)
46 {
47         chomp;
48         my @arg = split /\s+/, $_;
49         my $cmd = shift @arg;
50         print "Executing: $cmd @arg\n";
51         if($cmd eq 'clean')
52         {
53                 my $tracks = $opus->tracks_r();
54                 $tracks->[$_]->events_r([clean($tracks->[$_]->events())])
55                         for 0..@$tracks-1;
56         }
57         elsif($cmd eq 'ticks')
58         {
59                 if(@arg)
60                 {
61                         $opus->ticks($arg[0]);
62                 }
63                 else
64                 {
65                         print "Ticks: ", $opus->ticks(), "\n";
66                 }
67         }
68         elsif($cmd eq 'tricks')
69         {
70                 print "haha, very funny\n";
71         }
72         elsif($cmd eq 'tracks')
73         {
74                 my $tracks = $opus->tracks_r();
75                 if(@arg)
76                 {
77                         my %taken = ();
78                         my @t = ();
79                         my $force = 0;
80                         for(@arg)
81                         {
82                                 if($_ eq '--force')
83                                 {
84                                         $force = 1;
85                                         next;
86                                 }
87                                 next if $taken{$_}++ and not $force;
88                                 push @t, $tracks->[$_];
89                         }
90                         $opus->tracks_r(\@t);
91                 }
92                 else
93                 {
94                         for(0..@$tracks-1)
95                         {
96                                 print "Track $_:";
97                                 my $name = undef;
98                                 my %channels = ();
99                                 my $notes = 0;
100                                 my %notehash = ();
101                                 my $t = 0;
102                                 my $events = 0;
103                                 for($tracks->[$_]->events())
104                                 {
105                                         ++$events;
106                                         $_->[0] = 'note_off' if $_->[0] eq 'note_on' and $_->[4] == 0;
107                                         $t += $_->[1];
108                                         my $p = $chanpos{$_->[0]};
109                                         if(defined $p)
110                                         {
111                                                 my $c = $_->[$p] + 1;
112                                                 ++$channels{$c};
113                                         }
114                                         ++$notes if $_->[0] eq 'note_on';
115                                         $notehash{$_->[2]}{$_->[3]} = $t if $_->[0] eq 'note_on';
116                                         $notehash{$_->[2]}{$_->[3]} = undef if $_->[0] eq 'note_off';
117                                         $name = $_->[2] if $_->[0] eq 'track_name';
118                                 }
119                                 my $channels = join " ", sort keys %channels;
120                                 my @stuck = ();
121                                 while(my ($k1, $v1) = each %notehash)
122                                 {
123                                         while(my ($k2, $v2) = each %$v1)
124                                         {
125                                                 push @stuck, sprintf "%d:%d@%.1f%%", $k1+1, $k2, $v2 * 100.0 / $t
126                                                         if defined $v2;
127                                         }
128                                 }
129                                 print " $name" if defined $name;
130                                 print " (channel $channels)" if $channels ne "";
131                                 print " ($events events)" if $events;
132                                 print " ($notes notes)" if $notes;
133                                 print " (notes @stuck stuck)" if @stuck;
134                                 print "\n";
135                         }
136                 }
137         }
138         elsif($cmd eq 'save')
139         {
140                 $opus->write_to_file($arg[0]);
141         }
142         else
143         {
144                 print "Unknown command, allowed commands: ticks, tracks, clean, save\n";
145         }
146         print "Done with: $cmd @arg\n";
147 }