]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/midichannels.pl
midichannels: support multiple inputs
[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, @others) = @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 for(@others)
46 {
47         my $opus2 = MIDI::Opus->new({from_file => $_});
48         if($opus2->ticks() != $opus->ticks())
49         {
50                 my $tickfactor = $opus->ticks() / $opus2->ticks();
51                 for($opus2->tracks())
52                 {
53                         $_->events(reltime map { $_->[1] = int($_->[1] * $tickfactor + 0.5); $_; } abstime $_->events());
54                 }
55         }
56         $opus->tracks($opus->tracks(), $opus2->tracks());
57 }
58
59 while(<STDIN>)
60 {
61         chomp;
62         my @arg = split /\s+/, $_;
63         my $cmd = shift @arg;
64         print "Executing: $cmd @arg\n";
65         if($cmd eq 'clean')
66         {
67                 my $tracks = $opus->tracks_r();
68                 $tracks->[$_]->events_r([clean($tracks->[$_]->events())])
69                         for 0..@$tracks-1;
70         }
71         elsif($cmd eq 'dump')
72         {
73                 print $opus->dump({ dump_tracks => 1 });
74         }
75         elsif($cmd eq 'ticks')
76         {
77                 if(@arg)
78                 {
79                         $opus->ticks($arg[0]);
80                 }
81                 else
82                 {
83                         print "Ticks: ", $opus->ticks(), "\n";
84                 }
85         }
86         elsif($cmd eq 'tricks')
87         {
88                 print "haha, very funny\n";
89         }
90         elsif($cmd eq 'tracks')
91         {
92                 my $tracks = $opus->tracks_r();
93                 if(@arg)
94                 {
95                         my %taken = ();
96                         my @t = ();
97                         my $force = 0;
98                         for(@arg)
99                         {
100                                 if($_ eq '--force')
101                                 {
102                                         $force = 1;
103                                         next;
104                                 }
105                                 next if $taken{$_}++ and not $force;
106                                 push @t, $tracks->[$_];
107                         }
108                         $opus->tracks_r(\@t);
109                 }
110                 else
111                 {
112                         for(0..@$tracks-1)
113                         {
114                                 print "Track $_:";
115                                 my $name = undef;
116                                 my %channels = ();
117                                 my $notes = 0;
118                                 my %notehash = ();
119                                 my $t = 0;
120                                 my $events = 0;
121                                 for($tracks->[$_]->events())
122                                 {
123                                         ++$events;
124                                         $_->[0] = 'note_off' if $_->[0] eq 'note_on' and $_->[4] == 0;
125                                         $t += $_->[1];
126                                         my $p = $chanpos{$_->[0]};
127                                         if(defined $p)
128                                         {
129                                                 my $c = $_->[$p] + 1;
130                                                 ++$channels{$c};
131                                         }
132                                         ++$notes if $_->[0] eq 'note_on';
133                                         $notehash{$_->[2]}{$_->[3]} = $t if $_->[0] eq 'note_on';
134                                         $notehash{$_->[2]}{$_->[3]} = undef if $_->[0] eq 'note_off';
135                                         $name = $_->[2] if $_->[0] eq 'track_name';
136                                 }
137                                 my $channels = join " ", sort keys %channels;
138                                 my @stuck = ();
139                                 while(my ($k1, $v1) = each %notehash)
140                                 {
141                                         while(my ($k2, $v2) = each %$v1)
142                                         {
143                                                 push @stuck, sprintf "%d:%d@%.1f%%", $k1+1, $k2, $v2 * 100.0 / $t
144                                                         if defined $v2;
145                                         }
146                                 }
147                                 print " $name" if defined $name;
148                                 print " (channel $channels)" if $channels ne "";
149                                 print " ($events events)" if $events;
150                                 print " ($notes notes)" if $notes;
151                                 print " (notes @stuck stuck)" if @stuck;
152                                 print "\n";
153                         }
154                 }
155         }
156         elsif($cmd eq 'save')
157         {
158                 $opus->write_to_file($arg[0]);
159         }
160         else
161         {
162                 print "Unknown command, allowed commands: ticks, tracks, clean, save\n";
163         }
164         print "Done with: $cmd @arg\n";
165 }