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} += $1;
112 elsif(/^channels (.*)/)
114 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
118 $super = $currentbot->{init};
119 $currentbot->{init} = $appendref = [];
121 elsif(/^intermission$/)
123 $super = $currentbot->{intermission};
124 $currentbot->{intermission} = $appendref = [];
128 $super = $currentbot->{done};
129 $currentbot->{done} = $appendref = [];
131 elsif(/^note on (-?\d+)/)
133 $super = $currentbot->{notes_on}->{$1};
134 $currentbot->{notes_on}->{$1} = $appendref = [];
136 elsif(/^note off (-?\d+)/)
138 $super = $currentbot->{notes_off}->{$1};
139 $currentbot->{notes_off}->{$1} = $appendref = [];
141 elsif(/^percussion (\d+)/)
143 $super = $currentbot->{percussion}->{$1};
144 $currentbot->{percussion}->{$1} = $appendref = [];
148 print "unknown command: $_\n";
153 $currentbot = ($bots{$1} ||= {count => 0, transpose => 0});
157 $precommands .= "$1\n";
161 print "unknown command: $_\n";
167 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
169 my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
170 $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
176 my $busybots_orig = botconfig_read $config;
179 sub busybot_cmd_bot_test($$@)
181 my ($bot, $time, @commands) = @_;
183 my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
184 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
187 if $time < $botbusytime;
189 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
192 if $time + $mintime < $bottime;
197 sub busybot_cmd_bot_execute($$@)
199 my ($bot, $time, @commands) = @_;
203 if($_->[0] eq 'time')
205 $commands .= sprintf "sv_cmd bot_cmd %d wait_until %f\n", $bot->{id}, $time + $_->[1];
206 $bot->{timer} = $time + $_->[1];
208 elsif($_->[0] eq 'busy')
210 $bot->{busytimer} = $time + $_->[1];
212 elsif($_->[0] eq 'buttons')
214 my %buttons_release = %{$bot->{buttons} ||= {}};
218 delete $buttons_release{$1};
220 for(keys %buttons_release)
222 $commands .= sprintf "sv_cmd bot_cmd %d releasekey %s\n", $bot->{id}, $_;
223 delete $bot->{buttons}->{$_};
229 $commands .= sprintf "sv_cmd bot_cmd %d presskey %s\n", $bot->{id}, $_;
230 $bot->{buttons}->{$_} = 1;
233 elsif($_->[0] eq 'cmd')
235 $commands .= sprintf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
237 elsif($_->[0] eq 'barrier')
239 $commands .= sprintf "sv_cmd bot_cmd %d barrier\n", $bot->{id};
240 $bot->{timer} = $bot->{busytimer} = 0;
242 elsif($_->[0] eq 'raw')
244 $commands .= sprintf "%s\n", join " ", @{$_}[1..@$_-1];
251 my $intermissions = 0;
253 sub busybot_intermission_bot($)
256 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preintermission];
257 busybot_cmd_bot_execute $bot, 0, ['barrier'];
258 if($bot->{intermission})
260 busybot_cmd_bot_execute $bot, 0, @{$bot->{intermission}};
262 busybot_cmd_bot_execute $bot, 0, ['barrier'];
263 $notetime = $timeoffset_postintermission - $lowestnotestart;
267 sub busybot_note_off_bot($$$$)
269 my ($bot, $time, $channel, $note) = @_;
270 #print STDERR "note off $bot:$time:$channel:$note\n";
273 my $cmds = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
275 if not defined $cmds; # note off cannot fail
278 #print STDERR "BUSY: $busy bots (OFF)\n";
279 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
283 sub busybot_note_on_bot($$$$$)
285 my ($bot, $time, $channel, $note, $init) = @_;
286 return -1 # I won't play on this channel
287 if defined $bot->{channels} and not $bot->{channels}->{$channel};
292 $cmds = $bot->{percussion}->{$note};
297 $cmds = $bot->{notes_on}->{$note - $bot->{transpose} - $transpose};
298 $cmds_off = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
300 return -1 # I won't play this note
301 if not defined $cmds;
304 #print STDERR "note on $bot:$time:$channel:$note\n";
308 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
309 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
310 busybot_cmd_bot_execute $bot, 0, ['barrier'];
311 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
313 busybot_cmd_bot_execute $bot, 0, ['barrier'];
314 for(1..$intermissions)
316 busybot_intermission_bot $bot;
318 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
323 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
324 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
326 if(defined $cmds and defined $cmds_off)
330 #print STDERR "BUSY: $busy bots (ON)\n";
337 $busybots = Storable::dclone $busybots_orig;
338 @busybots_allocated = ();
339 %notechannelbots = ();
341 $notetime = $timeoffset_postinit - $lowestnotestart;
344 sub busybot_note_off($$$)
346 my ($time, $channel, $note) = @_;
348 #print STDERR "note off $time:$channel:$note\n";
353 if(my $bot = $notechannelbots{$channel}{$note})
355 busybot_note_off_bot $bot, $time, $channel, $note;
356 delete $notechannelbots{$channel}{$note};
363 sub busybot_note_on($$$)
365 my ($time, $channel, $note) = @_;
367 if($notechannelbots{$channel}{$note})
369 busybot_note_off $time, $channel, $note;
372 #print STDERR "note on $time:$channel:$note\n";
376 for(unsort @busybots_allocated)
378 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
381 $notechannelbots{$channel}{$note} = $_;
389 for(unsort keys %$busybots)
391 next if $busybots->{$_}->{count} <= 0;
392 my $bot = Storable::dclone $busybots->{$_};
393 $bot->{id} = @busybots_allocated + 1;
394 $bot->{classname} = $_;
395 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
400 --$busybots->{$_}->{count};
401 $notechannelbots{$channel}{$note} = $bot;
402 push @busybots_allocated, $bot;
405 die "Fresh bot cannot play stuff"
411 warn "Not enough bots to play this (when playing $channel:$note)";
415 warn "Note $channel:$note cannot be played by any bot";
423 my (@preallocate) = @_;
427 die "Cannot preallocate any more $_ bots"
428 if $busybots->{$_}->{count} <= 0;
429 my $bot = Storable::dclone $busybots->{$_};
430 $bot->{id} = @busybots_allocated + 1;
431 $bot->{classname} = $_;
432 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
433 busybot_cmd_bot_execute $bot, 0, ['barrier'];
434 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
436 busybot_cmd_bot_execute $bot, 0, ['barrier'];
437 --$busybots->{$_}->{count};
438 push @busybots_allocated, $bot;
444 my ($filename, $trans) = @_;
447 my $opus = MIDI::Opus->new({from_file => $filename});
448 my $ticksperquarter = $opus->ticks();
449 my $tracks = $opus->tracks_r();
450 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
454 for($tracks->[0]->events())
457 if($_->[0] eq 'set_tempo')
459 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
466 my $curtempo = [0, 0.5 / $ticksperquarter];
471 # this event is in the past
472 # we add the full time since the last one then
473 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
477 # if this event is in the future, we break
482 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
486 # merge all to a single track
487 my @allmidievents = ();
489 for my $track(0..@$tracks-1)
492 for($tracks->[$track]->events())
494 my ($command, $delta, @data) = @$_;
495 $command = 'note_off' if $command eq 'note_on' and $data[2] == 0;
497 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
500 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
503 my $note_min = undef;
504 my $note_max = undef;
509 $t = $tick2sec->($_->[1]);
511 if($_->[0] eq 'note_on')
513 my $chan = $_->[4] + 1;
515 if not defined $note_min or $_->[5] < $note_min and $chan != 10;
517 if not defined $note_max or $_->[5] > $note_max and $chan != 10;
518 if($midinotes{$chan}{$_->[5]})
521 busybot_note_off($t, $chan, $_->[5]);
523 busybot_note_on($t, $chan, $_->[5]);
525 $midinotes{$chan}{$_->[5]} = 1;
527 elsif($_->[0] eq 'note_off')
529 my $chan = $_->[4] + 1;
530 if($midinotes{$chan}{$_->[5]})
533 busybot_note_off($t, $chan, $_->[5]);
535 $midinotes{$chan}{$_->[5]} = 0;
539 print STDERR "For file $filename:\n";
540 print STDERR " Range of notes: $note_min .. $note_max\n";
541 print STDERR " Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
542 print STDERR " Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
543 print STDERR " Stuck notes: $notes_stuck\n";
545 while(my ($k1, $v1) = each %midinotes)
547 while(my ($k2, $v2) = each %$v1)
549 busybot_note_off($t, $k1, $k2);
553 for(@busybots_allocated)
555 busybot_intermission_bot $_;
562 print STDERR "Bots allocated:\n";
563 for(@busybots_allocated)
565 print STDERR "$_->{id} is a $_->{classname}\n";
567 for(@busybots_allocated)
569 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_predone];
570 busybot_cmd_bot_execute $_, 0, ['barrier'];
573 busybot_cmd_bot_execute $_, 0, @{$_->{done}};
575 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_postdone];
576 busybot_cmd_bot_execute $_, 0, ['barrier'];
580 my @preallocate = ();
587 Preallocate(@preallocate);
591 my $filename = shift @l;
592 my $transpose = shift @l;
593 ConvertMIDI($filename, $transpose);
596 my @preallocate_new = map { $_->{classname} } @busybots_allocated;
597 if(@preallocate_new == @preallocate)
599 print "$precommands$commands";
602 @preallocate = @preallocate_new;
607 unless $@ eq "noalloc\n";