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 @busybots_allocated;
30 my $lowestnotestart = undef;
38 my $currentbot = undef;
39 my $appendref = undef;
48 my @cmd = split /\s+/, $_;
49 if($cmd[0] eq 'super')
51 push @$appendref, @$super
54 elsif($cmd[0] eq 'percussion') # simple import
56 push @$appendref, @{$currentbot->{percussion}->{$cmd[1]}};
60 push @$appendref, \@cmd;
72 $currentbot->{$_} = Storable::dclone $base->{$_}; # copy array items as new array
76 $currentbot->{$_} = $base->{$_};
79 # better: do some merging TODO
83 $currentbot->{count} = $1;
85 elsif(/^transpose (\d+)/)
87 $currentbot->{transpose} += $1;
89 elsif(/^channels (.*)/)
91 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
95 $super = $currentbot->{init};
96 $currentbot->{init} = $appendref = [];
100 $super = $currentbot->{done};
101 $currentbot->{done} = $appendref = [];
103 elsif(/^note on (-?\d+)/)
105 $super = $currentbot->{notes_on}->{$1};
106 $currentbot->{notes_on}->{$1} = $appendref = [];
108 elsif(/^note off (-?\d+)/)
110 $super = $currentbot->{notes_off}->{$1};
111 $currentbot->{notes_off}->{$1} = $appendref = [];
113 elsif(/^percussion (\d+)/)
115 $super = $currentbot->{percussion}->{$1};
116 $currentbot->{percussion}->{$1} = $appendref = [];
120 print "unknown command: $_\n";
125 $currentbot = ($bots{$1} ||= {count => 0, transpose => 0});
129 $precommands .= "$1\n";
133 print "unknown command: $_\n";
139 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
141 my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
142 $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
148 my $busybots_orig = botconfig_read $config;
151 sub busybot_cmd_bot_test($$@)
153 my ($bot, $time, @commands) = @_;
155 my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
156 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
159 if $time < $botbusytime;
161 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
164 if $time + $mintime < $bottime;
169 sub busybot_cmd_bot_execute($$@)
171 my ($bot, $time, @commands) = @_;
175 if($_->[0] eq 'time')
177 $commands .= sprintf "sv_cmd bot_cmd %d wait_until %f\n", $bot->{id}, $time + $_->[1];
178 $bot->{timer} = $time + $_->[1];
180 elsif($_->[0] eq 'busy')
182 $bot->{busytimer} = $time + $_->[1];
184 elsif($_->[0] eq 'buttons')
186 my %buttons_release = %{$bot->{buttons} ||= {}};
190 delete $buttons_release{$1};
192 for(keys %buttons_release)
194 $commands .= sprintf "sv_cmd bot_cmd %d releasekey %s\n", $bot->{id}, $_;
195 delete $bot->{buttons}->{$_};
201 $commands .= sprintf "sv_cmd bot_cmd %d presskey %s\n", $bot->{id}, $_;
202 $bot->{buttons}->{$_} = 1;
205 elsif($_->[0] eq 'cmd')
207 $commands .= sprintf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
209 elsif($_->[0] eq 'barrier')
211 $commands .= sprintf "sv_cmd bot_cmd %d barrier\n", $bot->{id};
212 $bot->{timer} = $bot->{busytimer} = 0;
214 elsif($_->[0] eq 'raw')
216 $commands .= sprintf "%s\n", join " ", @{$_}[1..@$_-1];
223 my $intermissions = 0;
225 sub busybot_intermission_bot($)
228 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preintermission];
229 busybot_cmd_bot_execute $bot, 0, ['barrier'];
230 if($bot->{intermission})
232 busybot_cmd_bot_execute $bot, 0, @{$bot->{intermission}};
234 busybot_cmd_bot_execute $bot, 0, ['barrier'];
235 $notetime = $timeoffset_postintermission - $lowestnotestart;
238 sub busybot_note_off_bot($$$$)
240 my ($bot, $time, $channel, $note) = @_;
243 my $cmds = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
245 if not defined $cmds; # note off cannot fail
247 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
251 sub busybot_note_on_bot($$$$$)
253 my ($bot, $time, $channel, $note, $init) = @_;
254 return -1 # I won't play on this channel
255 if defined $bot->{channels} and not $bot->{channels}->{$channel};
261 $cmds = $bot->{percussion}->{$note};
265 $cmds = $bot->{notes_on}->{$note - $bot->{transpose} - $transpose};
266 my $cmds_off = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
267 if(defined $cmds and defined $cmds_off)
272 return -1 # I won't play this note
273 if not defined $cmds;
277 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
278 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
279 busybot_cmd_bot_execute $bot, 0, ['barrier'];
280 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
282 busybot_cmd_bot_execute $bot, 0, ['barrier'];
283 for(1..$intermissions)
285 busybot_intermission_bot $bot;
287 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
292 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
293 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
300 $busybots = Storable::dclone $busybots_orig;
301 @busybots_allocated = ();
302 %notechannelbots = ();
304 $notetime = $timeoffset_postinit - $lowestnotestart;
307 sub busybot_note_off($$$)
309 my ($time, $channel, $note) = @_;
314 if(my $bot = $notechannelbots{$channel}{$note})
316 busybot_note_off_bot $bot, $time, $channel, $note;
317 delete $notechannelbots{$channel}{$note};
324 sub busybot_note_on($$$)
326 my ($time, $channel, $note) = @_;
328 if($notechannelbots{$channel}{$note})
330 busybot_note_off $time, $channel, $note;
335 for(unsort @busybots_allocated)
337 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
340 $notechannelbots{$channel}{$note} = $_;
348 for(unsort keys %$busybots)
350 next if $busybots->{$_}->{count} <= 0;
351 my $bot = Storable::dclone $busybots->{$_};
352 $bot->{id} = @busybots_allocated + 1;
353 $bot->{classname} = $_;
354 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
357 --$busybots->{$_}->{count};
358 $notechannelbots{$channel}{$note} = $bot;
359 push @busybots_allocated, $bot;
368 warn "Not enough bots to play this (when playing $channel:$note)";
372 warn "Note $channel:$note cannot be played by any bot"
380 my (@preallocate) = @_;
384 die "Cannot preallocate any more $_ bots"
385 if $busybots->{$_}->{count} <= 0;
386 my $bot = Storable::dclone $busybots->{$_};
387 $bot->{id} = @busybots_allocated + 1;
388 $bot->{classname} = $_;
389 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
390 busybot_cmd_bot_execute $bot, 0, ['barrier'];
391 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
393 busybot_cmd_bot_execute $bot, 0, ['barrier'];
394 --$busybots->{$_}->{count};
395 push @busybots_allocated, $bot;
401 my ($filename, $trans) = @_;
404 my $opus = MIDI::Opus->new({from_file => $filename});
405 my $ticksperquarter = $opus->ticks();
406 my $tracks = $opus->tracks_r();
407 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
411 for($tracks->[0]->events())
414 if($_->[0] eq 'set_tempo')
416 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
423 my $curtempo = [0, 0.5 / $ticksperquarter];
428 # this event is in the past
429 # we add the full time since the last one then
430 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
434 # if this event is in the future, we break
439 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
443 # merge all to a single track
444 my @allmidievents = ();
446 for my $track(0..@$tracks-1)
449 for($tracks->[$track]->events())
451 my ($command, $delta, @data) = @$_;
452 $command = 'note_off' if $command eq 'note_on' and $data[2] == 0;
454 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
457 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
460 my $note_min = undef;
461 my $note_max = undef;
466 $t = $tick2sec->($_->[1]);
468 if($_->[0] eq 'note_on')
470 my $chan = $_->[4] + 1;
472 if not defined $note_min or $_->[5] < $note_min and $chan != 10;
474 if not defined $note_max or $_->[5] > $note_max and $chan != 10;
475 if($midinotes{$chan}{$_->[5]})
478 busybot_note_off($t, $chan, $_->[5]);
480 busybot_note_on($t, $chan, $_->[5]);
482 $midinotes{$chan}{$_->[5]} = 1;
484 elsif($_->[0] eq 'note_off')
486 my $chan = $_->[4] + 1;
487 if($midinotes{$chan}{$_->[5]})
490 busybot_note_off($t, $chan, $_->[5]);
492 $midinotes{$chan}{$_->[5]} = 0;
496 print STDERR "For file $filename:\n";
497 print STDERR " Range of notes: $note_min .. $note_max\n";
498 print STDERR " Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
499 print STDERR " Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
500 print STDERR " Stuck notes: $notes_stuck\n";
502 while(my ($k1, $v1) = each %midinotes)
504 while(my ($k2, $v2) = each %$v1)
506 busybot_note_off($t, $k1, $k2);
510 for(@busybots_allocated)
512 busybot_intermission_bot $_;
519 print STDERR "Bots allocated:\n";
520 for(@busybots_allocated)
522 print STDERR "$_->{id} is a $_->{classname}\n";
524 for(@busybots_allocated)
526 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_predone];
527 busybot_cmd_bot_execute $_, 0, ['barrier'];
530 busybot_cmd_bot_execute $_, 0, @{$_->{done}};
532 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_postdone];
533 busybot_cmd_bot_execute $_, 0, ['barrier'];
537 my @preallocate = ();
541 Preallocate(@preallocate);
545 my $filename = shift @l;
546 my $transpose = shift @l;
547 ConvertMIDI($filename, $transpose);
550 my @preallocate_new = map { $_->{classname} } @busybots_allocated;
551 last if @preallocate_new == @preallocate;
552 @preallocate = @preallocate_new;
555 print "$precommands$commands";