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 my ($filename, $transpose, $timeoffset) = @ARGV;
18 my $opus = MIDI::Opus->new({from_file => $filename});
19 #$opus->write_to_file("/tmp/y.mid");
20 my $ticksperquarter = $opus->ticks();
21 my $tracks = $opus->tracks_r();
22 my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
26 for($tracks->[0]->events())
29 if($_->[0] eq 'set_tempo')
31 push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
38 my $curtempo = [0, 0.5 / $ticksperquarter];
43 # this event is in the past
44 # we add the full time since the last one then
45 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
49 # if this event is in the future, we break
54 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
58 # merge all to a single track
59 my @allmidievents = ();
61 for my $track(0..@$tracks-1)
64 for($tracks->[$track]->events())
66 my ($command, $delta, @data) = @$_;
68 push @allmidievents, [$command, $tick, $sequence++, $track, @data];
71 @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
85 my $currentbot = undef;
86 my $appendref = undef;
95 my @cmd = split /\s+/, $_;
96 if($cmd[0] eq 'super')
98 push @$appendref, @$super;
102 push @$appendref, \@cmd;
109 my $base = $bots{$1};
114 $currentbot->{$_} = Storable::dclone $base->{$_}; # copy array items as new array
118 $currentbot->{$_} = $base->{$_};
121 # better: do some merging TODO
123 elsif(/^count (\d+)/)
125 $currentbot->{count} = $1;
127 elsif(/^transpose (\d+)/)
129 $currentbot->{transpose} += $1;
131 elsif(/^channels (.*)/)
133 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
137 $super = $currentbot->{init};
138 $currentbot->{init} = $appendref = [];
140 elsif(/^note on (-?\d+)/)
142 $super = $currentbot->{notes_on}->{$1};
143 $currentbot->{notes_on}->{$1} = $appendref = [];
145 elsif(/^note off (-?\d+)/)
147 $super = $currentbot->{notes_off}->{$1};
148 $currentbot->{notes_off}->{$1} = $appendref = [];
150 elsif(/^percussion (\d+)/)
152 $super = $currentbot->{percussion}->{$1};
153 $currentbot->{percussion}->{$1} = $appendref = [];
157 print "unknown command: $_\n";
162 $currentbot = ($bots{$1} ||= {});
166 print "unknown command: $_\n";
170 my $lowesttimeoffset = 0;
174 next unless defined $l;
175 next unless $l->[0]->[0] eq 'time';
176 my $t = $l->[0]->[1];
177 $lowesttimeoffset = $t
178 if $t < $lowesttimeoffset;
180 print STDERR "Using a time adjustment of $lowesttimeoffset\n";
181 $timeoffset -= $lowesttimeoffset;
186 sub busybot_cmd_bot_test($$@)
188 my ($bot, $time, @commands) = @_;
190 my $bottime = defined $bot->{timer} ? $bot->{timer} : -$timeoffset-1;
191 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -$timeoffset-1;
194 if $time < $botbusytime;
196 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
199 if $time + $mintime < $bottime;
204 sub busybot_cmd_bot_execute($$@)
206 my ($bot, $time, @commands) = @_;
210 if($_->[0] eq 'time')
212 printf "w %d %f\n", $bot->{id}, $time + $_->[1] + $timeoffset;
213 $bot->{timer} = $time + $_->[1];
215 elsif($_->[0] eq 'busy')
217 $bot->{busytimer} = $time + $_->[1];
219 elsif($_->[0] eq 'buttons')
221 my %buttons_release = %{$bot->{buttons} ||= {}};
225 delete $buttons_release{$1};
227 for(keys %buttons_release)
229 printf "r %d %s\n", $bot->{id}, $_;
230 delete $bot->{buttons}->{$_};
236 printf "p %d %s\n", $bot->{id}, $_;
237 $bot->{buttons}->{$_} = 1;
240 elsif($_->[0] eq 'cmd')
242 printf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
249 sub busybot_note_off_bot($$$$)
251 my ($bot, $time, $channel, $note) = @_;
252 my $cmds = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
254 if not defined $cmds; # note off cannot fail
255 busybot_cmd_bot_execute $bot, $time, @$cmds;
259 sub busybot_note_on_bot($$$$$)
261 my ($bot, $time, $channel, $note, $init) = @_;
262 return -1 # I won't play on this channel
263 if defined $bot->{channels} and not grep { $_ == $channel } $bot->{channels};
264 my $cmds = $bot->{notes_on}->{$note - $bot->{transpose} - $transpose};
265 if(not defined $cmds)
267 $cmds = $bot->{percussion}->{$note};
268 return -1 # I won't play this note
269 if not defined $cmds;
271 if($init && $bot->{init})
274 if not busybot_cmd_bot_test $bot, 0, @{$bot->{init}};
276 if not busybot_cmd_bot_test $bot, $time, @$cmds;
277 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}};
278 busybot_cmd_bot_execute $bot, $time, @$cmds;
283 if not busybot_cmd_bot_test $bot, $time, @$cmds;
284 busybot_cmd_bot_execute $bot, $time, @$cmds;
289 my $busybots = botconfig_read "midi2cfg-ng.conf";
290 my @busybots_allocated;
293 sub busybot_note_off($$$)
295 my ($time, $channel, $note) = @_;
297 if(my $bot = $notechannelbots{$channel}{$note})
299 busybot_note_off_bot $bot, $time, $channel, $note;
300 delete $notechannelbots{$channel}{$note};
307 sub busybot_note_on($$$)
309 my ($time, $channel, $note) = @_;
311 if($notechannelbots{$channel}{$note})
313 busybot_note_off $time, $channel, $note;
318 for(@busybots_allocated)
320 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
323 $notechannelbots{$channel}{$note} = $_;
331 for(map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, rand] } keys %$busybots)
333 next if $busybots->{$_}->{count} <= 0;
334 my $bot = Storable::dclone $busybots->{$_};
335 $bot->{id} = @busybots_allocated + 1;
336 $bot->{classname} = $_;
337 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
340 --$busybots->{$_}->{count};
341 $notechannelbots{$channel}{$note} = $bot;
342 push @busybots_allocated, $bot;
351 warn "Not enough bots to play this";
353 print STDERR Dumper \@busybots_allocated;
357 warn "Note $channel:$note cannot be played by any bot"
363 print 'alias p "sv_cmd bot_cmd $1 presskey $2"' . "\n";
364 print 'alias r "sv_cmd bot_cmd $1 releasekey $2"' . "\n";
365 print 'alias w "sv_cmd bot_cmd $1 wait_until $2"' . "\n";
366 print 'alias m "sv_cmd bot_cmd $1 moveto \"$2 $3 $4\""' . "\n";
369 my $note_min = undef;
370 my $note_max = undef;
374 my $t = tick2sec $_->[1];
376 if($_->[0] eq 'note_on')
378 my $chan = $_->[4] + 1;
380 if not defined $note_min or $_->[5] < $note_min;
382 if not defined $note_max or $_->[5] > $note_max;
383 if($midinotes{$chan}{$_->[5]})
386 busybot_note_off($t, $chan, $_->[5]);
388 busybot_note_on($t, $chan, $_->[5]);
390 $midinotes{$chan}{$_->[5]} = 1;
392 elsif($_->[0] eq 'note_off')
394 my $chan = $_->[4] + 1;
395 if($midinotes{$chan}{$_->[5]})
398 busybot_note_off($t, $chan, $_->[5]);
400 $midinotes{$chan}{$_->[5]} = 0;
404 print STDERR "Range of notes: $note_min .. $note_max\n";
405 print STDERR "Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
406 print STDERR "Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
407 print STDERR "Stuck notes: $notes_stuck\n";
408 print STDERR "Bots allocated:\n";
409 for(@busybots_allocated)
411 print STDERR "$_->{id} is a $_->{classname}\n";