]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/tools/midi2cfg.pl
use the new notes ;)
[divverent/nexuiz.git] / misc / tools / midi2cfg.pl
1 #!/usr/bin/perl
2
3 # converter from Type 1 MIDI files to CFG files that control bots with the Tuba and other weapons for percussion (requires g_weaponarena all)
4 # usage:
5 #   perl midi2cfg.pl filename.mid basenote walktime "x y z" "x y z" "x y z" ... "/" "x y z" "x y z" ... > filename.cfg
6
7 use strict;
8 use warnings;
9 use MIDI;
10 use MIDI::Opus;
11
12 my ($filename, $transpose, $walktime, @coords) = @ARGV;
13 my @coords_percussion = ();
14 my @coords_tuba = ();
15 my $l = \@coords_tuba;
16 for(@coords)
17 {
18         if($_ eq '/')
19         {
20                 $l = \@coords_percussion;
21         }
22         else
23         {
24                 push @$l, [split /\s+/, $_];
25         }
26 }
27
28 my $opus = MIDI::Opus->new({from_file => $filename});
29 my $ticksperquarter = $opus->ticks();
30 my $tracks = $opus->tracks_r();
31 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
32 my $tick;
33
34 $tick = 0;
35 for($tracks->[0]->events())
36 {   
37     $tick += $_->[1];
38     if($_->[0] eq 'set_tempo')
39     {   
40         push @tempi, [$_->[1], $_->[2] * 0.000001 / $ticksperquarter];
41     }
42 }
43 sub tick2sec($)
44 {
45     my ($tick) = @_;
46     my $sec = 0;
47     my $curtempo = [0, 0.5 / $ticksperquarter];
48     for(@tempi)
49     {
50         if($_->[0] < $tick)
51         {
52                         # this event is in the past
53                         # we add the full time since the last one then
54                         $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
55         }   
56         else
57         {
58                         # if this event is in the future, we break
59                         last;
60         }
61                 $curtempo = $_;
62     }
63         $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
64         return $sec;
65 }
66
67 # merge all to a single track
68 my @allmidievents = ();
69 my $sequence = 0;
70 for my $track(0..@$tracks-1)
71 {
72         $tick = 0;
73         for($tracks->[$track]->events())
74         {
75                 my ($command, $delta, @data) = @$_;
76                 $tick += $delta;
77                 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
78         }
79 }
80 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
81
82
83
84
85
86 my @busybots_percussion = map { undef } @coords_percussion;
87 my @busybots_tuba       = map { undef } @coords_tuba;
88
89 my $notes = 0;
90 sub busybot_findfree($$$)
91 {
92         my ($time, $vchannel, $note) = @_;
93         my $l = ($vchannel < 16) ? \@busybots_tuba : \@busybots_percussion;
94         for(0..@$l-1)
95         {
96                 if(!$l->[$_])
97                 {
98                         my $bot = {id => $_ + 1, busy => 0, busytime => 0, channel => $vchannel, curtime => -$walktime, curbuttons => 0, noteoffset => 0};
99                         $l->[$_] = $bot;
100                         return $bot;
101                 }
102                 return $l->[$_] if
103                         $l->[$_]{channel} == $vchannel
104                         &&
105                         !$l->[$_]{busy}
106                         &&
107                         $time > $l->[$_]{busytime};
108         }
109         die "No free channel found ($notes notes active)\n";
110 }
111
112 sub busybot_find($$)
113 {
114         my ($vchannel, $note) = @_;
115         my $l = ($vchannel < 16) ? \@busybots_tuba : \@busybots_percussion;
116         for(0..@$l-1)
117         {
118                 return $l->[$_] if
119                         $l->[$_]
120                         &&
121                         $l->[$_]{busy}
122                         &&
123                         $l->[$_]{channel} == $vchannel
124                         &&
125                         defined $l->[$_]{note}
126                         &&
127                         $l->[$_]{note} == $note;
128         }
129         return undef;
130 }
131
132 sub busybot_advance($$)
133 {
134         my ($bot, $t) = @_;
135         my $t0 = $bot->{curtime};
136         if($t != $t0)
137         {
138                 #print "sv_cmd bot_cmd $bot->{id} wait @{[$t - $t0]}\n";
139                 print "sv_cmd bot_cmd $bot->{id} wait_until $t\n";
140         }
141         $bot->{curtime} = $t;
142 }
143
144 sub busybot_setbuttons($$)
145 {
146         my ($bot, $b) = @_;
147         my $b0 = $bot->{curbuttons};
148         my $press = $b & ~$b0;
149         my $release = $b0 & ~$b;
150         print "sv_cmd bot_cmd $bot->{id} releasekey attack1\n" if $release & 32;
151         print "sv_cmd bot_cmd $bot->{id} releasekey attack2\n" if $release & 64;
152         print "sv_cmd bot_cmd $bot->{id} releasekey forward\n" if $release & 1;
153         print "sv_cmd bot_cmd $bot->{id} releasekey backward\n" if $release & 2;
154         print "sv_cmd bot_cmd $bot->{id} releasekey left\n" if $release & 4;
155         print "sv_cmd bot_cmd $bot->{id} releasekey right\n" if $release & 8;
156         print "sv_cmd bot_cmd $bot->{id} releasekey crouch\n" if $release & 16;
157         print "sv_cmd bot_cmd $bot->{id} releasekey jump\n" if $release & 128;
158         print "sv_cmd bot_cmd $bot->{id} presskey forward\n" if $press & 1;
159         print "sv_cmd bot_cmd $bot->{id} presskey backward\n" if $press & 2;
160         print "sv_cmd bot_cmd $bot->{id} presskey left\n" if $press & 4;
161         print "sv_cmd bot_cmd $bot->{id} presskey right\n" if $press & 8;
162         print "sv_cmd bot_cmd $bot->{id} presskey crouch\n" if $press & 16;
163         print "sv_cmd bot_cmd $bot->{id} presskey jump\n" if $press & 128;
164         print "sv_cmd bot_cmd $bot->{id} presskey attack1\n" if $press & 32;
165         print "sv_cmd bot_cmd $bot->{id} presskey attack2\n" if $press & 64;
166         $bot->{curbuttons} = $b;
167 }
168
169 my %notes = (
170         -18 => '1lbc',
171         -17 => '1bc',
172         -16 => '1brc',
173         -13 => '1frc',
174         -12 => '1c',
175         -11 => '2lbc',
176         -10 => '1rc',
177         -9 => '1flc',
178         -8 => '1fc',
179         -7 => '1lc',
180         -6 => '1lb',
181         -5 => '1b',
182         -4 => '1br',
183         -3 => '2rc',
184         -2 => '2flc',
185         -1 => '1fl',
186         0 => '1',
187         1 => '2lb',
188         2 => '1r',
189         3 => '1fl',
190         4 => '1f',
191         5 => '1l',
192         6 => '2fr',
193         7 => '2',
194         8 => '1brj',
195         9 => '2r',
196         10 => '2fl',
197         11 => '2f',
198         12 => '2l',
199         13 => '2lbj',
200         14 => '1rj',
201         15 => '1flj',
202         16 => '1fj',
203         17 => '1lj',
204         18 => '2frj',
205         19 => '2j',
206         21 => '2rj',
207         22 => '2flj',
208         23 => '2fj',
209         24 => '2lj'
210 );
211
212 my $note_min = +99;
213 my $note_max = -99;
214 sub getnote($$)
215 {
216         my ($bot, $note) = @_;
217         $note_max = $note if $note_max < $note;
218         $note_min = $note if $note_min > $note;
219         $note -= $transpose;
220         $note -= $bot->{noteoffset};
221         my $s = $notes{$note};
222         return $s;
223 }
224
225 sub busybot_playnote($$)
226 {
227         my ($bot, $note) = @_;
228         my $s = getnote $bot => $note;
229         return (warn("note $note not found"), 0)
230                 unless defined $s;
231         my $buttons = 0;
232         $buttons |= 1 if $s =~ /f/;
233         $buttons |= 2 if $s =~ /b/;
234         $buttons |= 4 if $s =~ /l/;
235         $buttons |= 8 if $s =~ /r/;
236         $buttons |= 16 if $s =~ /c/;
237         $buttons |= 32 if $s =~ /1/;
238         $buttons |= 64 if $s =~ /2/;
239         $buttons |= 128 if $s =~ /j/;
240         busybot_setbuttons $bot => $buttons;
241         return 1;
242 }
243
244 sub busybot_stopnote($$)
245 {
246         my ($bot, $note) = @_;
247         my $s = getnote $bot => $note;
248         return 0
249                 unless defined $s;
250         my $buttons = $bot->{curbuttons};
251         $buttons &= ~(32 | 64);
252         #$buttons = 0;
253         busybot_setbuttons $bot => $buttons;
254         return 1;
255 }
256
257 sub note_on($$$)
258 {
259         my ($t, $channel, $note) = @_;
260         if(busybot_find($channel, $note))
261         {
262                 note_off($t, $channel, $note); # MIDI allows redoing a note-on for the same note
263         }
264         ++$notes;
265         if($channel == 10)
266         {
267                 $channel = 16 + $note; # percussion
268         }
269         my $bot = busybot_findfree($t, $channel, $note);
270         if($channel < 16)
271         {
272                 busybot_advance $bot => $t if getnote $bot => $note;
273                 if(busybot_playnote $bot => $note)
274                 {
275                         $bot->{busy} = 1;
276                         $bot->{note} = $note;
277                         $bot->{busytime} = $t + 0.25;
278                         busybot_stopnote $bot => $note;
279                 }
280         }
281         if($channel >= 16)
282         {
283                 print "sv_cmd bot_cmd $bot->{id} presskey attack1\n";
284                 print "sv_cmd bot_cmd $bot->{id} releasekey attack1\n";
285                 $bot->{busy} = 1;
286                 $bot->{note} = $note;
287                 $bot->{busytime} = $t + 1.5;
288         }
289 }
290
291 sub note_off($$$)
292 {
293         my ($t, $channel, $note) = @_;
294         --$notes;
295         if($channel == 10)
296         {
297                 $channel = 16 + $note; # percussion
298         }
299         my $bot = busybot_find($channel, $note)
300                 or return;
301         $bot->{busy} = 0;
302         if($channel < 16)
303         {
304                 busybot_advance $bot => $t;
305                 busybot_stopnote $bot => $note;
306                 $bot->{busytime} = $t + 0.25;
307         }
308 }
309
310 for(@allmidievents)
311 {
312         my $t = tick2sec $_->[1];
313         my $track = $_->[3];
314         if($_->[0] eq 'note_on')
315         {
316                 my $chan = $_->[4];
317                 note_on($t, $chan, $_->[5]);
318         }
319         elsif($_->[0] eq 'note_off')
320         {
321                 my $chan = $_->[4];
322                 note_off($t, $chan, $_->[5]);
323         }
324 }
325
326 print STDERR "Range of notes: $note_min .. $note_max\n";
327 print STDERR "Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
328 print STDERR "Unsafe transpose range: @{[$note_max - 24]} .. @{[$note_min + 18]}\n";
329 printf STDERR "%d bots allocated for tuba, %d for percussion\n", int scalar grep { defined $_ } @busybots_tuba, int scalar grep { defined $_ } @busybots_percussion;
330
331 my $n = 0;
332 for(@busybots_percussion, @busybots_tuba)
333 {
334         ++$n if $_ && $_->{busy};
335 }
336 if($n)
337 {
338         die "$n channels blocked ($notes MIDI notes)";
339 }