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)
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
13 use constant MIDI_FIRST_NONCHANNEL => 17;
14 use constant MIDI_DRUMS_CHANNEL => 10;
16 my ($filename, $transpose, $timeoffset, $timeoffset2, @preallocate) = @ARGV;
18 my $opus = MIDI::Opus->new({from_file => $filename});
19 #$opus->write_to_file("/tmp/y.mid");
20 my $ticksperquarter = $opus->ticks();
21 my $tracks = $opus->tracks_r();
22 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
26 for($tracks->[0]->events())
29 if($_->[0] eq 'set_tempo')
31 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
38 my $curtempo = [0, 0.5 / $ticksperquarter];
43 # this event is in the past
44 # we add the full time since the last one then
45 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
49 # if this event is in the future, we break
54 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
58 # merge all to a single track
59 my @allmidievents = ();
61 for my $track(0..@$tracks-1)
64 for($tracks->[$track]->events())
66 my ($command, $delta, @data) = @$_;
68 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
71 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
76 return map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, rand] } @_;
92 my $currentbot = undef;
93 my $appendref = undef;
102 my @cmd = split /\s+/, $_;
103 if($cmd[0] eq 'super')
105 push @$appendref, @$super;
109 push @$appendref, \@cmd;
116 my $base = $bots{$1};
121 $currentbot->{$_} = Storable::dclone $base->{$_}; # copy array items as new array
125 $currentbot->{$_} = $base->{$_};
128 # better: do some merging TODO
130 elsif(/^count (\d+)/)
132 $currentbot->{count} = $1;
134 elsif(/^transpose (\d+)/)
136 $currentbot->{transpose} += $1;
138 elsif(/^channels (.*)/)
140 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
144 $super = $currentbot->{init};
145 $currentbot->{init} = $appendref = [];
147 elsif(/^note on (-?\d+)/)
149 $super = $currentbot->{notes_on}->{$1};
150 $currentbot->{notes_on}->{$1} = $appendref = [];
152 elsif(/^note off (-?\d+)/)
154 $super = $currentbot->{notes_off}->{$1};
155 $currentbot->{notes_off}->{$1} = $appendref = [];
157 elsif(/^percussion (\d+)/)
159 $super = $currentbot->{percussion}->{$1};
160 $currentbot->{percussion}->{$1} = $appendref = [];
164 print "unknown command: $_\n";
169 $currentbot = ($bots{$1} ||= {});
173 print "unknown command: $_\n";
177 my $lowesttime = undef;
178 my $highesttime = undef;
179 my $highestbusytime = undef;
180 my $lowestnotestart = undef;
184 next unless defined $l;
185 my $t = $l->[0]->[0] eq 'time' ? $l->[0]->[1] : 0;
186 $lowesttime = $t if not defined $lowesttime or $t < $lowesttime;
189 if($_->[0] eq 'time')
191 $highesttime = $_->[1] if not defined $highesttime or $_->[1] > $highesttime;
193 if($_->[0] eq 'busy')
195 $highestbusytime = $_->[1] if not defined $highestbusytime or $_->[1] > $highestbusytime;
198 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
200 my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
201 $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
205 my $initdelta = $highesttime - $lowesttime - $lowestnotestart;
206 if(defined $highestbusytime)
208 my $initdelta2 = $highestbusytime - $lowesttime;
209 $initdelta = $initdelta2
210 if $initdelta2 > $initdelta;
213 # init shall take place at $timeoffset
214 # note playing shall take place at $timeoffset + $initdelta + $timeoffset2
216 $inittime = $timeoffset - $lowesttime;
217 $notetime = $timeoffset + $initdelta + $timeoffset2;
219 print STDERR "Initialization offset: $inittime (start: @{[$inittime + $lowesttime]}, end: @{[$inittime + $highesttime]})\n";
220 print STDERR "Note offset: $notetime (start: @{[$notetime + $lowestnotestart]})\n";
225 sub busybot_cmd_bot_test($$@)
227 my ($bot, $time, @commands) = @_;
229 my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
230 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
233 if $time < $botbusytime;
235 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
238 if $time + $mintime < $bottime;
243 sub busybot_cmd_bot_execute($$@)
245 my ($bot, $time, @commands) = @_;
249 if($_->[0] eq 'time')
251 printf "w %d %f\n", $bot->{id}, $time + $_->[1];
252 $bot->{timer} = $time + $_->[1];
254 elsif($_->[0] eq 'busy')
256 $bot->{busytimer} = $time + $_->[1];
258 elsif($_->[0] eq 'buttons')
260 my %buttons_release = %{$bot->{buttons} ||= {}};
264 delete $buttons_release{$1};
266 for(keys %buttons_release)
268 printf "r %d %s\n", $bot->{id}, $_;
269 delete $bot->{buttons}->{$_};
275 printf "p %d %s\n", $bot->{id}, $_;
276 $bot->{buttons}->{$_} = 1;
279 elsif($_->[0] eq 'cmd')
281 printf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
288 sub busybot_note_off_bot($$$$)
290 my ($bot, $time, $channel, $note) = @_;
291 my $cmds = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
293 if not defined $cmds; # note off cannot fail
295 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
299 sub busybot_note_on_bot($$$$$)
301 my ($bot, $time, $channel, $note, $init) = @_;
302 return -1 # I won't play on this channel
303 if defined $bot->{channels} and not grep { $_ == $channel } $bot->{channels};
306 my $cmds = $bot->{notes_on}->{$note - $bot->{transpose} - $transpose};
307 my $cmds_off = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
308 if(defined $cmds_off)
312 if(not defined $cmds)
314 $cmds = $bot->{percussion}->{$note};
315 return -1 # I won't play this note
316 if not defined $cmds;
318 if($init && $bot->{init})
321 if not busybot_cmd_bot_test $bot, $inittime, @{$bot->{init}};
323 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
324 busybot_cmd_bot_execute $bot, $inittime, @{$bot->{init}};
325 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
330 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
331 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
336 my $busybots = botconfig_read "midi2cfg-ng.conf";
337 my @busybots_allocated;
340 sub busybot_note_off($$$)
342 my ($time, $channel, $note) = @_;
344 if(my $bot = $notechannelbots{$channel}{$note})
346 busybot_note_off_bot $bot, $time, $channel, $note;
347 delete $notechannelbots{$channel}{$note};
354 sub busybot_note_on($$$)
356 my ($time, $channel, $note) = @_;
358 if($notechannelbots{$channel}{$note})
360 busybot_note_off $time, $channel, $note;
365 for(unsort @busybots_allocated)
367 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
370 $notechannelbots{$channel}{$note} = $_;
378 for(unsort keys %$busybots)
380 next if $busybots->{$_}->{count} <= 0;
381 my $bot = Storable::dclone $busybots->{$_};
382 $bot->{id} = @busybots_allocated + 1;
383 $bot->{classname} = $_;
384 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
387 --$busybots->{$_}->{count};
388 $notechannelbots{$channel}{$note} = $bot;
389 push @busybots_allocated, $bot;
398 warn "Not enough bots to play this";
400 print STDERR Dumper \@busybots_allocated;
404 warn "Note $channel:$note cannot be played by any bot"
410 print 'alias p "sv_cmd bot_cmd $1 presskey $2"' . "\n";
411 print 'alias r "sv_cmd bot_cmd $1 releasekey $2"' . "\n";
412 print 'alias w "sv_cmd bot_cmd $1 wait_until $2"' . "\n";
413 print 'alias m "sv_cmd bot_cmd $1 moveto \"$2 $3 $4\""' . "\n";
417 die "Cannot preallocate any more $_ bots"
418 if $busybots->{$_}->{count} <= 0;
419 my $bot = Storable::dclone $busybots->{$_};
420 $bot->{id} = @busybots_allocated + 1;
421 $bot->{classname} = $_;
422 busybot_cmd_bot_execute $bot, $inittime, @{$bot->{init}};
423 --$busybots->{$_}->{count};
424 push @busybots_allocated, $bot;
428 my $note_min = undef;
429 my $note_max = undef;
433 my $t = tick2sec $_->[1];
435 if($_->[0] eq 'note_on')
437 my $chan = $_->[4] + 1;
439 if not defined $note_min or $_->[5] < $note_min;
441 if not defined $note_max or $_->[5] > $note_max;
442 if($midinotes{$chan}{$_->[5]})
445 busybot_note_off($t, $chan, $_->[5]);
447 busybot_note_on($t, $chan, $_->[5]);
449 $midinotes{$chan}{$_->[5]} = 1;
451 elsif($_->[0] eq 'note_off')
453 my $chan = $_->[4] + 1;
454 if($midinotes{$chan}{$_->[5]})
457 busybot_note_off($t, $chan, $_->[5]);
459 $midinotes{$chan}{$_->[5]} = 0;
463 print STDERR "Range of notes: $note_min .. $note_max\n";
464 print STDERR "Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
465 print STDERR "Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
466 print STDERR "Stuck notes: $notes_stuck\n";
467 print STDERR "Bots allocated:\n";
468 for(@busybots_allocated)
470 print STDERR "$_->{id} is a $_->{classname}\n";