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)
11 use constant MIDI_FIRST_NONCHANNEL => 17;
12 use constant MIDI_DRUMS_CHANNEL => 10;
14 die "Usage: $0 filename.conf timeoffset_preinit timeoffset_postinit timeoffset_predone timeoffset_postdone timeoffset_preintermission timeoffset_postintermission midifile1 transpose1 midifile2 transpose2 ..."
15 unless @ARGV > 7 and @ARGV % 2;
16 my ($config, $timeoffset_preinit, $timeoffset_postinit, $timeoffset_predone, $timeoffset_postdone, $timeoffset_preintermission, $timeoffset_postintermission, @midilist) = @ARGV;
20 return map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, rand] } @_;
26 my ($dest, $src) = @_;
27 if(ref $src eq 'HASH')
33 $dest->{$_} = override $dest->{$_}, $src->{$_};
36 elsif(ref $src eq 'ARRAY')
42 push @$dest, override undef, $_;
47 $dest = Storable::dclone $src;
59 my @busybots_allocated;
63 my $lowestnotestart = undef;
72 my $currentbot = undef;
73 my $appendref = undef;
82 my @cmd = split /\s+/, $_;
83 if($cmd[0] eq 'super')
85 push @$appendref, @$super
88 elsif($cmd[0] eq 'percussion') # simple import
90 push @$appendref, @{$currentbot->{percussion}->{$cmd[1]}};
94 push @$appendref, \@cmd;
101 my $base = $bots{$1};
102 $currentbot = override $currentbot, $base;
104 elsif(/^count (\d+)/)
106 $currentbot->{count} = $1;
108 elsif(/^transpose (\d+)/)
110 $currentbot->{transpose} ||= 0;
111 $currentbot->{transpose} += $1;
113 elsif(/^channels (.*)/)
115 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
119 $super = $currentbot->{init};
120 $currentbot->{init} = $appendref = [];
122 elsif(/^intermission$/)
124 $super = $currentbot->{intermission};
125 $currentbot->{intermission} = $appendref = [];
129 $super = $currentbot->{done};
130 $currentbot->{done} = $appendref = [];
132 elsif(/^note on (-?\d+)/)
134 $super = $currentbot->{notes_on}->{$1};
135 $currentbot->{notes_on}->{$1} = $appendref = [];
137 elsif(/^note off (-?\d+)/)
139 $super = $currentbot->{notes_off}->{$1};
140 $currentbot->{notes_off}->{$1} = $appendref = [];
142 elsif(/^percussion (\d+)/)
144 $super = $currentbot->{percussion}->{$1};
145 $currentbot->{percussion}->{$1} = $appendref = [];
149 print "unknown command: $_\n";
154 $currentbot = ($bots{$1} ||= {count => 0});
158 $precommands .= "$1\n";
162 print "unknown command: $_\n";
168 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
170 my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
171 $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
177 my $busybots_orig = botconfig_read $config;
180 sub busybot_cmd_bot_test($$@)
182 my ($bot, $time, @commands) = @_;
184 my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
185 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
188 if $time < $botbusytime;
190 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
193 if $time + $mintime < $bottime;
198 sub busybot_cmd_bot_execute($$@)
200 my ($bot, $time, @commands) = @_;
204 if($_->[0] eq 'time')
206 $commands .= sprintf "sv_cmd bot_cmd %d wait_until %f\n", $bot->{id}, $time + $_->[1];
207 $bot->{timer} = $time + $_->[1];
209 elsif($_->[0] eq 'busy')
211 $bot->{busytimer} = $time + $_->[1];
213 elsif($_->[0] eq 'buttons')
215 my %buttons_release = %{$bot->{buttons} ||= {}};
219 delete $buttons_release{$1};
221 for(keys %buttons_release)
223 $commands .= sprintf "sv_cmd bot_cmd %d releasekey %s\n", $bot->{id}, $_;
224 delete $bot->{buttons}->{$_};
230 $commands .= sprintf "sv_cmd bot_cmd %d presskey %s\n", $bot->{id}, $_;
231 $bot->{buttons}->{$_} = 1;
234 elsif($_->[0] eq 'cmd')
236 $commands .= sprintf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
238 elsif($_->[0] eq 'barrier')
240 $commands .= sprintf "sv_cmd bot_cmd %d barrier\n", $bot->{id};
241 $bot->{timer} = $bot->{busytimer} = 0;
243 elsif($_->[0] eq 'raw')
245 $commands .= sprintf "%s\n", join " ", @{$_}[1..@$_-1];
252 my $intermissions = 0;
254 sub busybot_intermission_bot($)
257 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preintermission];
258 busybot_cmd_bot_execute $bot, 0, ['barrier'];
259 if($bot->{intermission})
261 busybot_cmd_bot_execute $bot, 0, @{$bot->{intermission}};
263 busybot_cmd_bot_execute $bot, 0, ['barrier'];
264 $notetime = $timeoffset_postintermission - $lowestnotestart;
268 sub busybot_note_off_bot($$$$)
270 my ($bot, $time, $channel, $note) = @_;
271 #print STDERR "note off $bot:$time:$channel:$note\n";
274 my $cmds = $bot->{notes_off}->{$note - ($bot->{transpose} || 0) - $transpose};
276 if not defined $cmds; # note off cannot fail
279 #print STDERR "BUSY: $busy bots (OFF)\n";
280 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
284 sub busybot_note_on_bot($$$$$)
286 my ($bot, $time, $channel, $note, $init) = @_;
287 return -1 # I won't play on this channel
288 if defined $bot->{channels} and not $bot->{channels}->{$channel};
293 $cmds = $bot->{percussion}->{$note};
298 $cmds = $bot->{notes_on}->{$note - ($bot->{transpose} || 0) - $transpose};
299 $cmds_off = $bot->{notes_off}->{$note - ($bot->{transpose} || 0) - $transpose};
301 return -1 # I won't play this note
302 if not defined $cmds;
305 #print STDERR "note on $bot:$time:$channel:$note\n";
309 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
310 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
311 busybot_cmd_bot_execute $bot, 0, ['barrier'];
312 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
314 busybot_cmd_bot_execute $bot, 0, ['barrier'];
315 for(1..$intermissions)
317 busybot_intermission_bot $bot;
319 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
324 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
325 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
327 if(defined $cmds and defined $cmds_off)
331 #print STDERR "BUSY: $busy bots (ON)\n";
338 $busybots = Storable::dclone $busybots_orig;
339 @busybots_allocated = ();
340 %notechannelbots = ();
342 $notetime = $timeoffset_postinit - $lowestnotestart;
345 sub busybot_note_off($$$)
347 my ($time, $channel, $note) = @_;
349 #print STDERR "note off $time:$channel:$note\n";
354 if(my $bot = $notechannelbots{$channel}{$note})
356 busybot_note_off_bot $bot, $time, $channel, $note;
357 delete $notechannelbots{$channel}{$note};
364 sub busybot_note_on($$$)
366 my ($time, $channel, $note) = @_;
368 if($notechannelbots{$channel}{$note})
370 busybot_note_off $time, $channel, $note;
373 #print STDERR "note on $time:$channel:$note\n";
377 for(unsort @busybots_allocated)
379 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
382 $notechannelbots{$channel}{$note} = $_;
390 for(unsort keys %$busybots)
392 next if $busybots->{$_}->{count} <= 0;
393 my $bot = Storable::dclone $busybots->{$_};
394 $bot->{id} = @busybots_allocated + 1;
395 $bot->{classname} = $_;
396 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
401 --$busybots->{$_}->{count};
402 $notechannelbots{$channel}{$note} = $bot;
403 push @busybots_allocated, $bot;
406 die "Fresh bot cannot play stuff"
412 warn "Not enough bots to play this (when playing $channel:$note)";
416 warn "Note $channel:$note cannot be played by any bot";
424 my (@preallocate) = @_;
428 die "Cannot preallocate any more $_ bots"
429 if $busybots->{$_}->{count} <= 0;
430 my $bot = Storable::dclone $busybots->{$_};
431 $bot->{id} = @busybots_allocated + 1;
432 $bot->{classname} = $_;
433 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
434 busybot_cmd_bot_execute $bot, 0, ['barrier'];
435 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
437 busybot_cmd_bot_execute $bot, 0, ['barrier'];
438 --$busybots->{$_}->{count};
439 push @busybots_allocated, $bot;
445 my ($filename, $trans) = @_;
448 my $opus = MIDI::Opus->new({from_file => $filename});
449 my $ticksperquarter = $opus->ticks();
450 my $tracks = $opus->tracks_r();
451 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
455 for($tracks->[0]->events())
458 if($_->[0] eq 'set_tempo')
460 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
467 my $curtempo = [0, 0.5 / $ticksperquarter];
472 # this event is in the past
473 # we add the full time since the last one then
474 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
478 # if this event is in the future, we break
483 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
487 # merge all to a single track
488 my @allmidievents = ();
490 for my $track(0..@$tracks-1)
493 for($tracks->[$track]->events())
495 my ($command, $delta, @data) = @$_;
496 $command = 'note_off' if $command eq 'note_on' and $data[2] == 0;
498 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
501 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
504 my $note_min = undef;
505 my $note_max = undef;
510 $t = $tick2sec->($_->[1]);
512 if($_->[0] eq 'note_on')
514 my $chan = $_->[4] + 1;
516 if not defined $note_min or $_->[5] < $note_min and $chan != 10;
518 if not defined $note_max or $_->[5] > $note_max and $chan != 10;
519 if($midinotes{$chan}{$_->[5]})
522 busybot_note_off($t, $chan, $_->[5]);
524 busybot_note_on($t, $chan, $_->[5]);
526 $midinotes{$chan}{$_->[5]} = 1;
528 elsif($_->[0] eq 'note_off')
530 my $chan = $_->[4] + 1;
531 if($midinotes{$chan}{$_->[5]})
534 busybot_note_off($t, $chan, $_->[5]);
536 $midinotes{$chan}{$_->[5]} = 0;
540 print STDERR "For file $filename:\n";
541 print STDERR " Range of notes: $note_min .. $note_max\n";
542 print STDERR " Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
543 print STDERR " Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
544 print STDERR " Stuck notes: $notes_stuck\n";
546 while(my ($k1, $v1) = each %midinotes)
548 while(my ($k2, $v2) = each %$v1)
550 busybot_note_off($t, $k1, $k2);
554 for(@busybots_allocated)
556 busybot_intermission_bot $_;
563 print STDERR "Bots allocated:\n";
564 for(@busybots_allocated)
566 print STDERR "$_->{id} is a $_->{classname}\n";
568 for(@busybots_allocated)
570 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_predone];
571 busybot_cmd_bot_execute $_, 0, ['barrier'];
574 busybot_cmd_bot_execute $_, 0, @{$_->{done}};
576 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_postdone];
577 busybot_cmd_bot_execute $_, 0, ['barrier'];
581 my @preallocate = ();
588 Preallocate(@preallocate);
592 my $filename = shift @l;
593 my $transpose = shift @l;
594 ConvertMIDI($filename, $transpose);
597 my @preallocate_new = map { $_->{classname} } @busybots_allocated;
598 if(@preallocate_new == @preallocate)
600 print "$precommands$commands";
603 @preallocate = @preallocate_new;
608 unless $@ eq "noalloc\n";