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 # workaround for possible refire time problems
12 use constant SYS_TICRATE => 0.033333;
13 #use constant SYS_TICRATE => 0;
15 use constant MIDI_FIRST_NONCHANNEL => 17;
16 use constant MIDI_DRUMS_CHANNEL => 10;
18 die "Usage: $0 filename.conf timeoffset_preinit timeoffset_postinit timeoffset_predone timeoffset_postdone timeoffset_preintermission timeoffset_postintermission midifile1 transpose1 midifile2 transpose2 ..."
19 unless @ARGV > 7 and @ARGV % 2;
20 my ($config, $timeoffset_preinit, $timeoffset_postinit, $timeoffset_predone, $timeoffset_postdone, $timeoffset_preintermission, $timeoffset_postintermission, @midilist) = @ARGV;
24 return map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, rand] } @_;
30 my ($dest, $src) = @_;
31 if(ref $src eq 'HASH')
37 $dest->{$_} = override $dest->{$_}, $src->{$_};
40 elsif(ref $src eq 'ARRAY')
46 push @$dest, override undef, $_;
51 $dest = Storable::dclone $src;
63 my @busybots_allocated;
67 my $lowestnotestart = undef;
76 my $currentbot = undef;
77 my $appendref = undef;
86 my @cmd = split /\s+/, $_;
87 if($cmd[0] eq 'super')
89 push @$appendref, @$super
92 elsif($cmd[0] eq 'percussion') # simple import
94 push @$appendref, @{$currentbot->{percussion}->{$cmd[1]}};
98 push @$appendref, \@cmd;
105 my $base = $bots{$1};
106 $currentbot = override $currentbot, $base;
108 elsif(/^count (\d+)/)
110 $currentbot->{count} = $1;
112 elsif(/^transpose (\d+)/)
114 $currentbot->{transpose} ||= 0;
115 $currentbot->{transpose} += $1;
117 elsif(/^channels (.*)/)
119 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
123 $super = $currentbot->{init};
124 $currentbot->{init} = $appendref = [];
126 elsif(/^intermission$/)
128 $super = $currentbot->{intermission};
129 $currentbot->{intermission} = $appendref = [];
133 $super = $currentbot->{done};
134 $currentbot->{done} = $appendref = [];
136 elsif(/^note on (-?\d+)/)
138 $super = $currentbot->{notes_on}->{$1};
139 $currentbot->{notes_on}->{$1} = $appendref = [];
141 elsif(/^note off (-?\d+)/)
143 $super = $currentbot->{notes_off}->{$1};
144 $currentbot->{notes_off}->{$1} = $appendref = [];
146 elsif(/^percussion (\d+)/)
148 $super = $currentbot->{percussion}->{$1};
149 $currentbot->{percussion}->{$1} = $appendref = [];
153 $super = $currentbot->{vocals};
154 $currentbot->{vocals} = $appendref = [];
158 print "unknown command: $_\n";
163 $currentbot = ($bots{$1} ||= {count => 0});
167 $precommands .= "$1\n";
171 print "unknown command: $_\n";
177 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
179 my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
180 $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
186 my $busybots_orig = botconfig_read $config;
189 sub busybot_cmd_bot_test($$@)
191 my ($bot, $time, @commands) = @_;
193 my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
194 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
197 if $time < $botbusytime + SYS_TICRATE;
199 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
202 if $time + $mintime < $bottime + SYS_TICRATE;
207 sub busybot_cmd_bot_execute($$@)
209 my ($bot, $time, @commands) = @_;
213 if($_->[0] eq 'time')
215 $commands .= sprintf "sv_cmd bot_cmd %d wait_until %f\n", $bot->{id}, $time + $_->[1];
216 $bot->{timer} = $time + $_->[1];
218 elsif($_->[0] eq 'busy')
220 $bot->{busytimer} = $time + $_->[1];
222 elsif($_->[0] eq 'buttons')
224 my %buttons_release = %{$bot->{buttons} ||= {}};
228 delete $buttons_release{$1};
230 for(keys %buttons_release)
232 $commands .= sprintf "sv_cmd bot_cmd %d releasekey %s\n", $bot->{id}, $_;
233 delete $bot->{buttons}->{$_};
239 $commands .= sprintf "sv_cmd bot_cmd %d presskey %s\n", $bot->{id}, $_;
240 $bot->{buttons}->{$_} = 1;
243 elsif($_->[0] eq 'cmd')
245 $commands .= sprintf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
247 elsif($_->[0] eq 'barrier')
249 $commands .= sprintf "sv_cmd bot_cmd %d barrier\n", $bot->{id};
250 $bot->{timer} = $bot->{busytimer} = 0;
252 elsif($_->[0] eq 'raw')
254 $commands .= sprintf "%s\n", join " ", @{$_}[1..@$_-1];
261 my $intermissions = 0;
263 sub busybot_intermission_bot($)
266 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preintermission];
267 busybot_cmd_bot_execute $bot, 0, ['barrier'];
268 if($bot->{intermission})
270 busybot_cmd_bot_execute $bot, 0, @{$bot->{intermission}};
272 busybot_cmd_bot_execute $bot, 0, ['barrier'];
273 $notetime = $timeoffset_postintermission - $lowestnotestart;
277 sub busybot_note_off_bot($$$$)
279 my ($bot, $time, $channel, $note) = @_;
280 #print STDERR "note off $bot:$time:$channel:$note\n";
283 my $cmds = $bot->{notes_off}->{$note - ($bot->{transpose} || 0) - $transpose};
285 if not defined $cmds; # note off cannot fail
288 #print STDERR "BUSY: $busy bots (OFF)\n";
289 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
293 sub busybot_note_on_bot($$$$$)
295 my ($bot, $time, $channel, $note, $init) = @_;
296 return -1 # I won't play on this channel
297 if defined $bot->{channels} and not $bot->{channels}->{$channel};
305 $cmds = $bot->{vocals};
309 $cmds = [ map { [ map { $_ eq '%s' ? $note : $_ } @$_ ] } @$cmds ];
314 elsif($channel == 10)
317 $cmds = $bot->{percussion}->{$note};
325 $cmds = $bot->{notes_on}->{$note - ($bot->{transpose} || 0) - $transpose};
326 $cmds_off = $bot->{notes_off}->{$note - ($bot->{transpose} || 0) - $transpose};
328 $k1 = $note - ($bot->{transpose} || 0) - $transpose;
330 return -1 # I won't play this note
331 if not defined $cmds;
334 #print STDERR "note on $bot:$time:$channel:$note\n";
338 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
339 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
340 busybot_cmd_bot_execute $bot, 0, ['barrier'];
341 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
343 busybot_cmd_bot_execute $bot, 0, ['barrier'];
344 for(1..$intermissions)
346 busybot_intermission_bot $bot;
348 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
353 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
354 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
356 if(defined $cmds and defined $cmds_off)
360 #print STDERR "BUSY: $busy bots (ON)\n";
362 ++$bot->{seen}{$k0}{$k1};
368 $busybots = Storable::dclone $busybots_orig;
369 @busybots_allocated = ();
370 %notechannelbots = ();
372 $notetime = $timeoffset_postinit - $lowestnotestart;
375 sub busybot_note_off($$$)
377 my ($time, $channel, $note) = @_;
379 #print STDERR "note off $time:$channel:$note\n";
386 if(my $bot = $notechannelbots{$channel}{$note})
388 busybot_note_off_bot $bot, $time, $channel, $note;
389 delete $notechannelbots{$channel}{$note};
396 sub busybot_note_on($$$)
398 my ($time, $channel, $note) = @_;
400 if($notechannelbots{$channel}{$note})
402 busybot_note_off $time, $channel, $note;
405 #print STDERR "note on $time:$channel:$note\n";
409 for(unsort @busybots_allocated)
411 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
414 $notechannelbots{$channel}{$note} = $_;
422 for(unsort keys %$busybots)
424 next if $busybots->{$_}->{count} <= 0;
425 my $bot = Storable::dclone $busybots->{$_};
426 $bot->{id} = @busybots_allocated + 1;
427 $bot->{classname} = $_;
428 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
433 --$busybots->{$_}->{count};
434 $notechannelbots{$channel}{$note} = $bot;
435 push @busybots_allocated, $bot;
438 die "Fresh bot cannot play stuff"
444 warn "Not enough bots to play this (when playing $channel:$note)";
448 warn "Note $channel:$note cannot be played by any bot";
456 my (@preallocate) = @_;
460 die "Cannot preallocate any more $_ bots"
461 if $busybots->{$_}->{count} <= 0;
462 my $bot = Storable::dclone $busybots->{$_};
463 $bot->{id} = @busybots_allocated + 1;
464 $bot->{classname} = $_;
465 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
466 busybot_cmd_bot_execute $bot, 0, ['barrier'];
467 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
469 busybot_cmd_bot_execute $bot, 0, ['barrier'];
470 --$busybots->{$_}->{count};
471 push @busybots_allocated, $bot;
477 my ($filename, $trans) = @_;
480 my $opus = MIDI::Opus->new({from_file => $filename});
481 my $ticksperquarter = $opus->ticks();
482 my $tracks = $opus->tracks_r();
483 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
487 for($tracks->[0]->events())
490 if($_->[0] eq 'set_tempo')
492 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
499 my $curtempo = [0, 0.5 / $ticksperquarter];
504 # this event is in the past
505 # we add the full time since the last one then
506 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
510 # if this event is in the future, we break
515 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
519 # merge all to a single track
520 my @allmidievents = ();
522 for my $track(0..@$tracks-1)
525 for($tracks->[$track]->events())
527 my ($command, $delta, @data) = @$_;
528 $command = 'note_off' if $command eq 'note_on' and $data[2] == 0;
530 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
534 if(open my $fh, "$filename.vocals")
541 my ($tick, $file) = split /\s+/, $_;
546 elsif($tick eq 'shift')
552 push @allmidievents, ['note_on', $tick * $scale + $shift, $sequence++, -1, -1, $file];
553 push @allmidievents, ['note_off', $tick * $scale + $shift, $sequence++, -1, -1, $file];
558 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
561 my $note_min = undef;
562 my $note_max = undef;
567 $t = $tick2sec->($_->[1]);
569 if($_->[0] eq 'note_on')
571 my $chan = $_->[4] + 1;
573 if $chan != 10 and $chan > 0 and (not defined $note_min or $_->[5] < $note_min);
575 if $chan != 10 and $chan > 0 and (not defined $note_max or $_->[5] > $note_max);
576 if($midinotes{$chan}{$_->[5]})
579 busybot_note_off($t, $chan, $_->[5]);
581 busybot_note_on($t, $chan, $_->[5]);
583 $midinotes{$chan}{$_->[5]} = 1;
585 elsif($_->[0] eq 'note_off')
587 my $chan = $_->[4] + 1;
588 if($midinotes{$chan}{$_->[5]})
591 busybot_note_off($t, $chan, $_->[5]);
593 $midinotes{$chan}{$_->[5]} = 0;
597 print STDERR "For file $filename:\n";
598 print STDERR " Range of notes: $note_min .. $note_max\n";
599 print STDERR " Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
600 print STDERR " Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
601 print STDERR " Stuck notes: $notes_stuck\n";
603 while(my ($k1, $v1) = each %midinotes)
605 while(my ($k2, $v2) = each %$v1)
607 busybot_note_off($t, $k1, $k2);
611 for(@busybots_allocated)
613 busybot_intermission_bot $_;
620 print STDERR "Bots allocated:\n";
623 for(@busybots_allocated)
625 print STDERR "$_->{id} is a $_->{classname}\n";
626 ++$counthash{$_->{classname}};
627 while(my ($type, $notehash) = each %{$_->{seen}})
629 while(my ($k, $v) = each %$notehash)
631 $notehash{$_->{classname}}{$type}{$k} += $v;
635 for my $cn(sort keys %counthash)
637 print STDERR "$counthash{$cn} bots of $cn have played:\n";
638 for my $type(sort keys %{$notehash{$cn}})
640 for my $note(sort { $a <=> $b } keys %{$notehash{$cn}{$type}})
642 my $cnt = $notehash{$cn}{$type}{$note};
643 print STDERR " $type $note ($cnt times)\n";
647 for(@busybots_allocated)
649 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_predone];
650 busybot_cmd_bot_execute $_, 0, ['barrier'];
653 busybot_cmd_bot_execute $_, 0, @{$_->{done}};
655 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_postdone];
656 busybot_cmd_bot_execute $_, 0, ['barrier'];
660 my @preallocate = ();
667 Preallocate(@preallocate);
671 my $filename = shift @l;
672 my $transpose = shift @l;
673 ConvertMIDI($filename, $transpose);
676 my @preallocate_new = map { $_->{classname} } @busybots_allocated;
677 if(@preallocate_new == @preallocate)
679 print "$precommands$commands";
682 @preallocate = @preallocate_new;
687 unless $@ eq "noalloc\n";