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 = [];
98 elsif(/^intermission$/)
100 $super = $currentbot->{intermission};
101 $currentbot->{intermission} = $appendref = [];
105 $super = $currentbot->{done};
106 $currentbot->{done} = $appendref = [];
108 elsif(/^note on (-?\d+)/)
110 $super = $currentbot->{notes_on}->{$1};
111 $currentbot->{notes_on}->{$1} = $appendref = [];
113 elsif(/^note off (-?\d+)/)
115 $super = $currentbot->{notes_off}->{$1};
116 $currentbot->{notes_off}->{$1} = $appendref = [];
118 elsif(/^percussion (\d+)/)
120 $super = $currentbot->{percussion}->{$1};
121 $currentbot->{percussion}->{$1} = $appendref = [];
125 print "unknown command: $_\n";
130 $currentbot = ($bots{$1} ||= {count => 0, transpose => 0});
134 $precommands .= "$1\n";
138 print "unknown command: $_\n";
144 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
146 my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
147 $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
153 my $busybots_orig = botconfig_read $config;
156 sub busybot_cmd_bot_test($$@)
158 my ($bot, $time, @commands) = @_;
160 my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
161 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
164 if $time < $botbusytime;
166 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
169 if $time + $mintime < $bottime;
174 sub busybot_cmd_bot_execute($$@)
176 my ($bot, $time, @commands) = @_;
180 if($_->[0] eq 'time')
182 $commands .= sprintf "sv_cmd bot_cmd %d wait_until %f\n", $bot->{id}, $time + $_->[1];
183 $bot->{timer} = $time + $_->[1];
185 elsif($_->[0] eq 'busy')
187 $bot->{busytimer} = $time + $_->[1];
189 elsif($_->[0] eq 'buttons')
191 my %buttons_release = %{$bot->{buttons} ||= {}};
195 delete $buttons_release{$1};
197 for(keys %buttons_release)
199 $commands .= sprintf "sv_cmd bot_cmd %d releasekey %s\n", $bot->{id}, $_;
200 delete $bot->{buttons}->{$_};
206 $commands .= sprintf "sv_cmd bot_cmd %d presskey %s\n", $bot->{id}, $_;
207 $bot->{buttons}->{$_} = 1;
210 elsif($_->[0] eq 'cmd')
212 $commands .= sprintf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
214 elsif($_->[0] eq 'barrier')
216 $commands .= sprintf "sv_cmd bot_cmd %d barrier\n", $bot->{id};
217 $bot->{timer} = $bot->{busytimer} = 0;
219 elsif($_->[0] eq 'raw')
221 $commands .= sprintf "%s\n", join " ", @{$_}[1..@$_-1];
228 my $intermissions = 0;
230 sub busybot_intermission_bot($)
233 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preintermission];
234 busybot_cmd_bot_execute $bot, 0, ['barrier'];
235 if($bot->{intermission})
237 busybot_cmd_bot_execute $bot, 0, @{$bot->{intermission}};
239 busybot_cmd_bot_execute $bot, 0, ['barrier'];
240 $notetime = $timeoffset_postintermission - $lowestnotestart;
243 sub busybot_note_off_bot($$$$)
245 my ($bot, $time, $channel, $note) = @_;
248 my $cmds = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
250 if not defined $cmds; # note off cannot fail
252 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
256 sub busybot_note_on_bot($$$$$)
258 my ($bot, $time, $channel, $note, $init) = @_;
259 return -1 # I won't play on this channel
260 if defined $bot->{channels} and not $bot->{channels}->{$channel};
266 $cmds = $bot->{percussion}->{$note};
270 $cmds = $bot->{notes_on}->{$note - $bot->{transpose} - $transpose};
271 my $cmds_off = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
272 if(defined $cmds and defined $cmds_off)
277 return -1 # I won't play this note
278 if not defined $cmds;
282 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
283 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
284 busybot_cmd_bot_execute $bot, 0, ['barrier'];
285 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
287 busybot_cmd_bot_execute $bot, 0, ['barrier'];
288 for(1..$intermissions)
290 busybot_intermission_bot $bot;
292 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
297 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
298 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
305 $busybots = Storable::dclone $busybots_orig;
306 @busybots_allocated = ();
307 %notechannelbots = ();
309 $notetime = $timeoffset_postinit - $lowestnotestart;
312 sub busybot_note_off($$$)
314 my ($time, $channel, $note) = @_;
319 if(my $bot = $notechannelbots{$channel}{$note})
321 busybot_note_off_bot $bot, $time, $channel, $note;
322 delete $notechannelbots{$channel}{$note};
329 sub busybot_note_on($$$)
331 my ($time, $channel, $note) = @_;
333 if($notechannelbots{$channel}{$note})
335 busybot_note_off $time, $channel, $note;
340 for(unsort @busybots_allocated)
342 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
345 $notechannelbots{$channel}{$note} = $_;
353 for(unsort keys %$busybots)
355 next if $busybots->{$_}->{count} <= 0;
356 my $bot = Storable::dclone $busybots->{$_};
357 $bot->{id} = @busybots_allocated + 1;
358 $bot->{classname} = $_;
359 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
362 --$busybots->{$_}->{count};
363 $notechannelbots{$channel}{$note} = $bot;
364 push @busybots_allocated, $bot;
373 warn "Not enough bots to play this (when playing $channel:$note)";
377 warn "Note $channel:$note cannot be played by any bot"
385 my (@preallocate) = @_;
389 die "Cannot preallocate any more $_ bots"
390 if $busybots->{$_}->{count} <= 0;
391 my $bot = Storable::dclone $busybots->{$_};
392 $bot->{id} = @busybots_allocated + 1;
393 $bot->{classname} = $_;
394 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
395 busybot_cmd_bot_execute $bot, 0, ['barrier'];
396 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
398 busybot_cmd_bot_execute $bot, 0, ['barrier'];
399 --$busybots->{$_}->{count};
400 push @busybots_allocated, $bot;
406 my ($filename, $trans) = @_;
409 my $opus = MIDI::Opus->new({from_file => $filename});
410 my $ticksperquarter = $opus->ticks();
411 my $tracks = $opus->tracks_r();
412 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
416 for($tracks->[0]->events())
419 if($_->[0] eq 'set_tempo')
421 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
428 my $curtempo = [0, 0.5 / $ticksperquarter];
433 # this event is in the past
434 # we add the full time since the last one then
435 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
439 # if this event is in the future, we break
444 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
448 # merge all to a single track
449 my @allmidievents = ();
451 for my $track(0..@$tracks-1)
454 for($tracks->[$track]->events())
456 my ($command, $delta, @data) = @$_;
457 $command = 'note_off' if $command eq 'note_on' and $data[2] == 0;
459 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
462 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
465 my $note_min = undef;
466 my $note_max = undef;
471 $t = $tick2sec->($_->[1]);
473 if($_->[0] eq 'note_on')
475 my $chan = $_->[4] + 1;
477 if not defined $note_min or $_->[5] < $note_min and $chan != 10;
479 if not defined $note_max or $_->[5] > $note_max and $chan != 10;
480 if($midinotes{$chan}{$_->[5]})
483 busybot_note_off($t, $chan, $_->[5]);
485 busybot_note_on($t, $chan, $_->[5]);
487 $midinotes{$chan}{$_->[5]} = 1;
489 elsif($_->[0] eq 'note_off')
491 my $chan = $_->[4] + 1;
492 if($midinotes{$chan}{$_->[5]})
495 busybot_note_off($t, $chan, $_->[5]);
497 $midinotes{$chan}{$_->[5]} = 0;
501 print STDERR "For file $filename:\n";
502 print STDERR " Range of notes: $note_min .. $note_max\n";
503 print STDERR " Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
504 print STDERR " Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
505 print STDERR " Stuck notes: $notes_stuck\n";
507 while(my ($k1, $v1) = each %midinotes)
509 while(my ($k2, $v2) = each %$v1)
511 busybot_note_off($t, $k1, $k2);
515 for(@busybots_allocated)
517 busybot_intermission_bot $_;
524 print STDERR "Bots allocated:\n";
525 for(@busybots_allocated)
527 print STDERR "$_->{id} is a $_->{classname}\n";
529 for(@busybots_allocated)
531 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_predone];
532 busybot_cmd_bot_execute $_, 0, ['barrier'];
535 busybot_cmd_bot_execute $_, 0, @{$_->{done}};
537 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_postdone];
538 busybot_cmd_bot_execute $_, 0, ['barrier'];
542 my @preallocate = ();
546 Preallocate(@preallocate);
550 my $filename = shift @l;
551 my $transpose = shift @l;
552 ConvertMIDI($filename, $transpose);
555 my @preallocate_new = map { $_->{classname} } @busybots_allocated;
556 last if @preallocate_new == @preallocate;
557 @preallocate = @preallocate_new;
560 print "$precommands$commands";