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 timeoffset4 preallocatedbots..."
18 my ($filename, $transpose, $timeoffset, $timeoffset2, $timeoffset3, $timeoffset4, @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] } @_;
93 my $currentbot = undef;
94 my $appendref = undef;
103 my @cmd = split /\s+/, $_;
104 if($cmd[0] eq 'super')
106 push @$appendref, @$super
109 elsif($cmd[0] eq 'percussion') # simple import
111 push @$appendref, @{$currentbot->{percussion}->{$cmd[1]}};
115 push @$appendref, \@cmd;
122 my $base = $bots{$1};
127 $currentbot->{$_} = Storable::dclone $base->{$_}; # copy array items as new array
131 $currentbot->{$_} = $base->{$_};
134 # better: do some merging TODO
136 elsif(/^count (\d+)/)
138 $currentbot->{count} = $1;
140 elsif(/^transpose (\d+)/)
142 $currentbot->{transpose} += $1;
144 elsif(/^channels (.*)/)
146 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
150 $super = $currentbot->{init};
151 $currentbot->{init} = $appendref = [];
155 $super = $currentbot->{done};
156 $currentbot->{done} = $appendref = [];
158 elsif(/^note on (-?\d+)/)
160 $super = $currentbot->{notes_on}->{$1};
161 $currentbot->{notes_on}->{$1} = $appendref = [];
163 elsif(/^note off (-?\d+)/)
165 $super = $currentbot->{notes_off}->{$1};
166 $currentbot->{notes_off}->{$1} = $appendref = [];
168 elsif(/^percussion (\d+)/)
170 $super = $currentbot->{percussion}->{$1};
171 $currentbot->{percussion}->{$1} = $appendref = [];
175 print "unknown command: $_\n";
180 $currentbot = ($bots{$1} ||= {count => 0, transpose => 0});
184 print "unknown command: $_\n";
188 my $lowestnotestart = undef;
191 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
193 my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
194 $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
198 $notetime = $timeoffset2 - $lowestnotestart;
203 sub busybot_cmd_bot_test($$@)
205 my ($bot, $time, @commands) = @_;
207 my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
208 my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
211 if $time < $botbusytime;
213 my $mintime = (@commands && ($commands[0]->[0] eq 'time')) ? $commands[0]->[1] : 0;
216 if $time + $mintime < $bottime;
221 sub busybot_cmd_bot_execute($$@)
223 my ($bot, $time, @commands) = @_;
227 if($_->[0] eq 'time')
229 printf "sv_cmd bot_cmd %d wait_until %f\n", $bot->{id}, $time + $_->[1];
230 $bot->{timer} = $time + $_->[1];
232 elsif($_->[0] eq 'busy')
234 $bot->{busytimer} = $time + $_->[1];
236 elsif($_->[0] eq 'buttons')
238 my %buttons_release = %{$bot->{buttons} ||= {}};
242 delete $buttons_release{$1};
244 for(keys %buttons_release)
246 printf "sv_cmd bot_cmd %d releasekey %s\n", $bot->{id}, $_;
247 delete $bot->{buttons}->{$_};
253 printf "sv_cmd bot_cmd %d presskey %s\n", $bot->{id}, $_;
254 $bot->{buttons}->{$_} = 1;
257 elsif($_->[0] eq 'cmd')
259 printf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
261 elsif($_->[0] eq 'raw')
263 printf "%s\n", join " ", @{$_}[1..@$_-1];
270 sub busybot_note_off_bot($$$$)
272 my ($bot, $time, $channel, $note) = @_;
273 my $cmds = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
275 if not defined $cmds; # note off cannot fail
277 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
281 sub busybot_note_on_bot($$$$$)
283 my ($bot, $time, $channel, $note, $init) = @_;
284 return -1 # I won't play on this channel
285 if defined $bot->{channels} and not $bot->{channels}->{$channel};
291 $cmds = $bot->{percussion}->{$note};
295 $cmds = $bot->{notes_on}->{$note - $bot->{transpose} - $transpose};
296 my $cmds_off = $bot->{notes_off}->{$note - $bot->{transpose} - $transpose};
297 if(defined $cmds and defined $cmds_off)
302 return -1 # I won't play this note
303 if not defined $cmds;
307 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
308 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset];
309 busybot_cmd_bot_execute $bot, 0, ['cmd', 'barrier'];
310 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
312 busybot_cmd_bot_execute $bot, 0, ['cmd', 'barrier'];
313 $bot->{timer} = $bot->{busytimer} = 0;
314 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
319 if not busybot_cmd_bot_test $bot, $time + $notetime, @$cmds;
320 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds;
325 my $busybots = botconfig_read "midi2cfg-ng.conf";
326 my @busybots_allocated;
329 sub busybot_note_off($$$)
331 my ($time, $channel, $note) = @_;
336 if(my $bot = $notechannelbots{$channel}{$note})
338 busybot_note_off_bot $bot, $time, $channel, $note;
339 delete $notechannelbots{$channel}{$note};
346 sub busybot_note_on($$$)
348 my ($time, $channel, $note) = @_;
350 if($notechannelbots{$channel}{$note})
352 busybot_note_off $time, $channel, $note;
357 for(unsort @busybots_allocated)
359 my $canplay = busybot_note_on_bot $_, $time, $channel, $note, 0;
362 $notechannelbots{$channel}{$note} = $_;
370 for(unsort keys %$busybots)
372 next if $busybots->{$_}->{count} <= 0;
373 my $bot = Storable::dclone $busybots->{$_};
374 $bot->{id} = @busybots_allocated + 1;
375 $bot->{classname} = $_;
376 my $canplay = busybot_note_on_bot $bot, $time, $channel, $note, 1;
379 --$busybots->{$_}->{count};
380 $notechannelbots{$channel}{$note} = $bot;
381 push @busybots_allocated, $bot;
390 warn "Not enough bots to play this (when playing $channel:$note)";
394 warn "Note $channel:$note cannot be played by any bot"
402 die "Cannot preallocate any more $_ bots"
403 if $busybots->{$_}->{count} <= 0;
404 my $bot = Storable::dclone $busybots->{$_};
405 $bot->{id} = @busybots_allocated + 1;
406 $bot->{classname} = $_;
407 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset];
408 busybot_cmd_bot_execute $bot, 0, ['cmd', 'barrier'];
409 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
411 busybot_cmd_bot_execute $bot, 0, ['cmd', 'barrier'];
412 $bot->{timer} = $bot->{busytimer} = 0;
413 --$busybots->{$_}->{count};
414 push @busybots_allocated, $bot;
418 my $note_min = undef;
419 my $note_max = undef;
424 $t = tick2sec $_->[1];
426 if($_->[0] eq 'note_on')
428 my $chan = $_->[4] + 1;
430 if not defined $note_min or $_->[5] < $note_min and $chan != 10;
432 if not defined $note_max or $_->[5] > $note_max and $chan != 10;
433 if($midinotes{$chan}{$_->[5]})
436 busybot_note_off($t, $chan, $_->[5]);
438 busybot_note_on($t, $chan, $_->[5]);
440 $midinotes{$chan}{$_->[5]} = 1;
442 elsif($_->[0] eq 'note_off')
444 my $chan = $_->[4] + 1;
445 if($midinotes{$chan}{$_->[5]})
448 busybot_note_off($t, $chan, $_->[5]);
450 $midinotes{$chan}{$_->[5]} = 0;
454 for(@busybots_allocated)
456 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset3];
457 busybot_cmd_bot_execute $_, 0, ['cmd', 'barrier'];
460 busybot_cmd_bot_execute $_, 0, @{$_->{done}};
462 busybot_cmd_bot_execute $_, 0, ['cmd', 'barrier'];
463 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset4];
466 print STDERR "Range of notes: $note_min .. $note_max\n";
467 print STDERR "Safe transpose range: @{[$note_max - 19]} .. @{[$note_min + 13]}\n";
468 print STDERR "Unsafe transpose range: @{[$note_max - 27]} .. @{[$note_min + 18]}\n";
469 print STDERR "Stuck notes: $notes_stuck\n";
470 print STDERR "Bots allocated:\n";
471 for(@busybots_allocated)
473 print STDERR "$_->{id} is a $_->{classname}\n";