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)
5 # perl midi2cfg.pl filename.mid basenote walktime "x y z" "x y z" "x y z" ... "/" "x y z" "x y z" ... > filename.cfg
13 use constant MIDI_FIRST_NONCHANNEL => 17;
14 use constant MIDI_DRUMS_CHANNEL => 10;
16 die "Usage: $0 filename.mid transpose timeoffset timeoffset2 timeoffset3 preallocatedbots..."
18 my ($filename, $transpose, $timeoffset, $timeoffset2, $timeoffset3, @preallocate) = @ARGV;
20 my $opus = MIDI::Opus->new({from_file => $filename});
21 #$opus->write_to_file("/tmp/y.mid");
22 my $ticksperquarter = $opus->ticks();
23 my $tracks = $opus->tracks_r();
24 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
28 for($tracks->[0]->events())
31 if($_->[0] eq 'set_tempo')
33 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
40 my $curtempo = [0, 0.5 / $ticksperquarter];
45 # this event is in the past
46 # we add the full time since the last one then
47 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
51 # if this event is in the future, we break
56 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
60 # merge all to a single track
61 my @allmidievents = ();
63 for my $track(0..@$tracks-1)
66 for($tracks->[$track]->events())
68 my ($command, $delta, @data) = @$_;
70 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
73 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
78 return map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, rand] } @_;
94 my $currentbot = undef;
95 my $appendref = undef;
104 my @cmd = split /\s+/, $_;
105 if($cmd[0] eq 'super')
107 push @$appendref, @$super
110 elsif($cmd[0] eq 'percussion') # simple import
112 push @$appendref, @{$currentbot->{percussion}->{$cmd[1]}};
116 push @$appendref, \@cmd;
123 my $base = $bots{$1};
128 $currentbot->{$_} = Storable::dclone $base->{$_}; # copy array items as new array
132 $currentbot->{$_} = $base->{$_};
135 # better: do some merging TODO
137 elsif(/^count (\d+)/)
139 $currentbot->{count} = $1;
141 elsif(/^transpose (\d+)/)
143 $currentbot->{transpose} += $1;
145 elsif(/^channels (.*)/)
147 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
151 $super = $currentbot->{init};
152 $currentbot->{init} = $appendref = [];
156 $super = $currentbot->{done};
157 $currentbot->{done} = $appendref = [];
159 elsif(/^note on (-?\d+)/)
161 $super = $currentbot->{notes_on}->{$1};
162 $currentbot->{notes_on}->{$1} = $appendref = [];
164 elsif(/^note off (-?\d+)/)
166 $super = $currentbot->{notes_off}->{$1};
167 $currentbot->{notes_off}->{$1} = $appendref = [];
169 elsif(/^percussion (\d+)/)
171 $super = $currentbot->{percussion}->{$1};
172 $currentbot->{percussion}->{$1} = $appendref = [];
176 print "unknown command: $_\n";
181 $currentbot = ($bots{$1} ||= {count => 0, transpose => 0});
185 print "unknown command: $_\n";
189 my $lowesttime = undef;
190 my $highesttime = undef;
191 my $highestbusytime = undef;
192 my $lowestnotestart = undef;
196 next unless defined $l;
197 my $t = $l->[0]->[0] eq 'time' ? $l->[0]->[1] : 0;
198 $lowesttime = $t if not defined $lowesttime or $t < $lowesttime;
201 if($_->[0] eq 'time')
203 $highesttime = $_->[1] if not defined $highesttime or $_->[1] > $highesttime;
205 if($_->[0] eq 'busy')
207 $highestbusytime = $_->[1] if not defined $highestbusytime or $_->[1] > $highestbusytime;
210 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
212 my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
213 $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
217 my $initdelta = $highesttime - $lowesttime - $lowestnotestart;
218 if(defined $highestbusytime)
220 my $initdelta2 = $highestbusytime - $lowesttime;
221 $initdelta = $initdelta2
222 if $initdelta2 > $initdelta;
225 # init shall take place at $timeoffset
226 # note playing shall take place at $timeoffset + $initdelta + $timeoffset2
228 $inittime = $timeoffset - $lowesttime;
229 $notetime = $timeoffset + $initdelta + $timeoffset2;
231 print STDERR "Initialization offset: $inittime (start: @{[$inittime + $lowesttime]}, end: @{[$inittime + $highesttime]})\n";
232 print STDERR "Note offset: $notetime (start: @{[$notetime + $lowestnotestart]})\n";
237 sub busybot_cmd_bot_test($$@)
239 my ($bot, $time, @commands) = @_;
241 my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
242 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
245 if $time < $botbusytime;
247 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
250 if $time + $mintime < $bottime;
255 sub busybot_cmd_bot_execute($$@)
257 my ($bot, $time, @commands) = @_;
261 if($_->[0] eq 'time')
263 printf "w %d %f\n", $bot->{id}, $time + $_->[1];
264 $bot->{timer} = $time + $_->[1];
266 elsif($_->[0] eq 'busy')
268 $bot->{busytimer} = $time + $_->[1];
270 elsif($_->[0] eq 'buttons')
272 my %buttons_release = %{$bot->{buttons} ||= {}};
276 delete $buttons_release{$1};
278 for(keys %buttons_release)
280 printf "r %d %s\n", $bot->{id}, $_;
281 delete $bot->{buttons}->{$_};
287 printf "p %d %s\n", $bot->{id}, $_;
288 $bot->{buttons}->{$_} = 1;
291 elsif($_->[0] eq 'cmd')
293 printf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
295 elsif($_->[0] eq 'raw')
297 printf join " ", @{$_}[1..@$_-1];
304 sub busybot_note_off_bot($$$$)
306 my ($bot, $time, $channel, $note) = @_;
307 my $cmds = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
309 if not defined $cmds; # note off cannot fail
311 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
315 sub busybot_note_on_bot($$$$$)
317 my ($bot, $time, $channel, $note, $init) = @_;
318 return -1 # I won't play on this channel
319 if defined $bot->{channels} and not $bot->{channels}->{$channel};
325 $cmds = $bot->{percussion}->{$note};
329 $cmds = $bot->{notes_on}->{$note - $bot->{transpose} - $transpose};
330 my $cmds_off = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
331 if(defined $cmds and defined $cmds_off)
336 return -1 # I won't play this note
337 if not defined $cmds;
338 if($init && $bot->{init})
341 if not busybot_cmd_bot_test $bot, $inittime, @{$bot->{init}};
343 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
344 busybot_cmd_bot_execute $bot, $inittime, @{$bot->{init}};
345 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
350 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
351 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
356 my $busybots = botconfig_read "midi2cfg-ng.conf";
357 my @busybots_allocated;
360 sub busybot_note_off($$$)
362 my ($time, $channel, $note) = @_;
367 if(my $bot = $notechannelbots{$channel}{$note})
369 busybot_note_off_bot $bot, $time, $channel, $note;
370 delete $notechannelbots{$channel}{$note};
377 sub busybot_note_on($$$)
379 my ($time, $channel, $note) = @_;
381 if($notechannelbots{$channel}{$note})
383 busybot_note_off $time, $channel, $note;
388 for(unsort @busybots_allocated)
390 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
393 $notechannelbots{$channel}{$note} = $_;
401 for(unsort keys %$busybots)
403 next if $busybots->{$_}->{count} <= 0;
404 my $bot = Storable::dclone $busybots->{$_};
405 $bot->{id} = @busybots_allocated + 1;
406 $bot->{classname} = $_;
407 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
410 --$busybots->{$_}->{count};
411 $notechannelbots{$channel}{$note} = $bot;
412 push @busybots_allocated, $bot;
421 warn "Not enough bots to play this (when playing $channel:$note)";
425 warn "Note $channel:$note cannot be played by any bot"
431 print 'alias p "sv_cmd bot_cmd $1 presskey $2"' . "\n";
432 print 'alias r "sv_cmd bot_cmd $1 releasekey $2"' . "\n";
433 print 'alias w "sv_cmd bot_cmd $1 wait_until $2"' . "\n";
434 print 'alias m "sv_cmd bot_cmd $1 moveto \"$2 $3 $4\""' . "\n";
438 die "Cannot preallocate any more $_ bots"
439 if $busybots->{$_}->{count} <= 0;
440 my $bot = Storable::dclone $busybots->{$_};
441 $bot->{id} = @busybots_allocated + 1;
442 $bot->{classname} = $_;
443 busybot_cmd_bot_execute $bot, $inittime, @{$bot->{init}};
444 --$busybots->{$_}->{count};
445 push @busybots_allocated, $bot;
449 my $note_min = undef;
450 my $note_max = undef;
455 $t = tick2sec $_->[1];
457 if($_->[0] eq 'note_on')
459 my $chan = $_->[4] + 1;
461 if not defined $note_min or $_->[5] < $note_min and $chan != 10;
463 if not defined $note_max or $_->[5] > $note_max and $chan != 10;
464 if($midinotes{$chan}{$_->[5]})
467 busybot_note_off($t, $chan, $_->[5]);
469 busybot_note_on($t, $chan, $_->[5]);
471 $midinotes{$chan}{$_->[5]} = 1;
473 elsif($_->[0] eq 'note_off')
475 my $chan = $_->[4] + 1;
476 if($midinotes{$chan}{$_->[5]})
479 busybot_note_off($t, $chan, $_->[5]);
481 $midinotes{$chan}{$_->[5]} = 0;
485 for(@busybots_allocated)
489 busybot_cmd_bot_execute $_, $notetime + $t + $timeoffset3, @{$_->{done}};
493 print STDERR "Range of notes: $note_min .. $note_max\n";
494 print STDERR "Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
495 print STDERR "Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
496 print STDERR "Stuck notes: $notes_stuck\n";
497 print STDERR "Bots allocated:\n";
498 for(@busybots_allocated)
500 print STDERR "$_->{id} is a $_->{classname}\n";