]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/midi2cfg-ng.pl
fixes
[xonotic/xonotic.git] / misc / tools / midi2cfg-ng.pl
1 #!/usr/bin/perl
2
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)
4
5 use strict;
6 use warnings;
7 use MIDI;
8 use MIDI::Opus;
9 use Storable;
10
11 # workaround for possible refire time problems
12 use constant SYS_TICRATE => 0.033333;
13 #use constant SYS_TICRATE => 0;
14
15 use constant MIDI_FIRST_NONCHANNEL => 17;
16 use constant MIDI_DRUMS_CHANNEL => 10;
17
18 die "Usage: $0 filename.conf midifile1 transpose1 midifile2 transpose2 ..."
19         unless @ARGV > 1 and @ARGV % 2;
20
21 my $timeoffset_preinit = 2;
22 my $timeoffset_postinit = 2;
23 my $timeoffset_predone = 2;
24 my $timeoffset_postdone = 2;
25 my $timeoffset_preintermission = 2;
26 my $timeoffset_postintermission = 2;
27 my $time_forgetfulness = 1.5;
28
29 my ($config, @midilist) = @ARGV;
30
31 sub unsort(@)
32 {
33         return map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, rand] } @_;
34 }
35
36 sub override($$);
37 sub override($$)
38 {
39         my ($dest, $src) = @_;
40         if(ref $src eq 'HASH')
41         {
42                 $dest = {}
43                         if not defined $dest;
44                 for(keys %$src)
45                 {
46                         $dest->{$_} = override $dest->{$_}, $src->{$_};
47                 }
48         }
49         elsif(ref $src eq 'ARRAY')
50         {
51                 $dest = []
52                         if not defined $dest;
53                 for(@$src)
54                 {
55                         push @$dest, override undef, $_;
56                 }
57         }
58         elsif(ref $src)
59         {
60                 $dest = Storable::dclone $src;
61         }
62         else
63         {
64                 $dest = $src;
65         }
66         return $dest;
67 }
68
69 my $precommands = "";
70 my $commands = "";
71 my $busybots;
72 my @busybots_allocated;
73 my %notechannelbots;
74 my $transpose = 0;
75 my $notetime = undef;
76 my $lowestnotestart = undef;
77 my $noalloc = 0;
78 sub botconfig_read($)
79 {
80         my ($fn) = @_;
81         my %bots = ();
82         open my $fh, "<", $fn
83                 or die "<$fn: $!";
84         
85         my $currentbot = undef;
86         my $appendref = undef;
87         my $super = undef;
88         while(<$fh>)
89         {
90                 chomp;
91                 s/\s*#.*//;
92                 next if /^$/;
93                 if(s/^\t\t//)
94                 {
95                         my @cmd = split /\s+/, $_;
96                         if($cmd[0] eq 'super')
97                         {
98                                 push @$appendref, @$super
99                                         if $super;
100                         }
101                         elsif($cmd[0] eq 'percussion') # simple import
102                         {
103                                 push @$appendref, @{$currentbot->{percussion}->{$cmd[1]}};
104                         }
105                         else
106                         {
107                                 push @$appendref, \@cmd;
108                         }
109                 }
110                 elsif(s/^\t//)
111                 {
112                         if(/^include (.*)/)
113                         {
114                                 my $base = $bots{$1};
115                                 $currentbot = override $currentbot, $base;
116                         }
117                         elsif(/^count (\d+)/)
118                         {
119                                 $currentbot->{count} = $1;
120                         }
121                         elsif(/^transpose (\d+)/)
122                         {
123                                 $currentbot->{transpose} ||= 0;
124                                 $currentbot->{transpose} += $1;
125                         }
126                         elsif(/^channels (.*)/)
127                         {
128                                 $currentbot->{channels} = { map { $_ => 1 } split /\s+/, $1 };
129                         }
130                         elsif(/^programs (.*)/)
131                         {
132                                 $currentbot->{programs} = { map { $_ => 1 } split /\s+/, $1 };
133                         }
134                         elsif(/^init$/)
135                         {
136                                 $super = $currentbot->{init};
137                                 $currentbot->{init} = $appendref = [];
138                         }
139                         elsif(/^intermission$/)
140                         {
141                                 $super = $currentbot->{intermission};
142                                 $currentbot->{intermission} = $appendref = [];
143                         }
144                         elsif(/^done$/)
145                         {
146                                 $super = $currentbot->{done};
147                                 $currentbot->{done} = $appendref = [];
148                         }
149                         elsif(/^note on (-?\d+)/)
150                         {
151                                 $super = $currentbot->{notes_on}->{$1};
152                                 $currentbot->{notes_on}->{$1} = $appendref = [];
153                         }
154                         elsif(/^note off (-?\d+)/)
155                         {
156                                 $super = $currentbot->{notes_off}->{$1};
157                                 $currentbot->{notes_off}->{$1} = $appendref = [];
158                         }
159                         elsif(/^percussion (\d+)/)
160                         {
161                                 $super = $currentbot->{percussion}->{$1};
162                                 $currentbot->{percussion}->{$1} = $appendref = [];
163                         }
164                         elsif(/^vocals$/)
165                         {
166                                 $super = $currentbot->{vocals};
167                                 $currentbot->{vocals} = $appendref = [];
168                         }
169                         else
170                         {
171                                 print "unknown command: $_\n";
172                         }
173                 }
174                 elsif(/^bot (.*)/)
175                 {
176                         $currentbot = ($bots{$1} ||= {count => 0});
177                 }
178                 elsif(/^raw (.*)/)
179                 {
180                         $precommands .= "$1\n";
181                 }
182                 elsif(/^timeoffset_preinit (.*)/)
183                 {
184                         $timeoffset_preinit = $1;
185                 }
186                 elsif(/^timeoffset_postinit (.*)/)
187                 {
188                         $timeoffset_postinit = $1;
189                 }
190                 elsif(/^timeoffset_predone (.*)/)
191                 {
192                         $timeoffset_predone = $1;
193                 }
194                 elsif(/^timeoffset_postdone (.*)/)
195                 {
196                         $timeoffset_postdone = $1;
197                 }
198                 elsif(/^timeoffset_preintermission (.*)/)
199                 {
200                         $timeoffset_preintermission = $1;
201                 }
202                 elsif(/^timeoffset_postintermission (.*)/)
203                 {
204                         $timeoffset_postintermission = $1;
205                 }
206                 elsif(/^time_forgetfulness (.*)/)
207                 {
208                         $time_forgetfulness = $1;
209                 }
210                 else
211                 {
212                         print "unknown command: $_\n";
213                 }
214         }
215
216         for(values %bots)
217         {
218                 for(values %{$_->{notes_on}}, values %{$_->{percussion}})
219                 {
220                         my $t = $_->[0]->[0] eq 'time' ? $_->[0]->[1] : 0;
221                         $lowestnotestart = $t if not defined $lowestnotestart or $t < $lowestnotestart;
222                 }
223         }
224
225         return \%bots;
226 }
227 my $busybots_orig = botconfig_read $config;
228
229
230 # returns: ($mintime, $maxtime, $busytime)
231 sub busybot_cmd_bot_cmdinfo(@)
232 {
233         my (@commands) = @_;
234
235         my $mintime = undef;
236         my $maxtime = undef;
237         my $busytime = undef;
238
239         for(@commands)
240         {
241                 if($_->[0] eq 'time')
242                 {
243                         $mintime = $_->[1]
244                                 if not defined $mintime or $_->[1] < $mintime;
245                         $maxtime = $_->[1] + SYS_TICRATE
246                                 if not defined $maxtime or $_->[1] + SYS_TICRATE > $maxtime;
247                 }
248                 elsif($_->[0] eq 'busy')
249                 {
250                         $busytime = $_->[1] + SYS_TICRATE;
251                 }
252         }
253
254         return ($mintime, $maxtime, $busytime);
255 }
256
257 sub busybot_cmd_bot_matchtime($$$@)
258 {
259         my ($bot, $targettime, $targetbusytime, @commands) = @_;
260
261         # I want to execute @commands so that I am free on $targettime and $targetbusytime
262         # when do I execute it then?
263
264         my ($mintime, $maxtime, $busytime) = busybot_cmd_bot_cmdinfo @commands;
265
266         my $tstart_max = defined $maxtime ? $targettime - $maxtime : $targettime;
267         my $tstart_busy = defined $busytime ? $targetbusytime - $busytime : $targettime;
268
269         return $tstart_max < $tstart_busy ? $tstart_max : $tstart_busy;
270 }
271
272 # TODO function to find out whether, and when, to insert a command before another command to make it possible
273 # (note-off before note-on)
274
275 sub busybot_cmd_bot_test($$$@)
276 {
277         my ($bot, $time, $force, @commands) = @_;
278
279         my $bottime = defined $bot->{timer} ? $bot->{timer} : -1;
280         my $botbusytime = defined $bot->{busytimer} ? $bot->{busytimer} : -1;
281
282         my ($mintime, $maxtime, $busytime) = busybot_cmd_bot_cmdinfo @commands;
283
284         if($time < $botbusytime)
285         {
286                 warn "FORCE: $time < $botbusytime"
287                         if $force;
288                 return $force;
289         }
290         
291         if(defined $mintime and $time + $mintime < $bottime)
292         {
293                 warn "FORCE: $time + $mintime < $bottime"
294                         if $force;
295                 return $force;
296         }
297         
298         return 1;
299 }
300
301 sub busybot_cmd_bot_execute($$@)
302 {
303         my ($bot, $time, @commands) = @_;
304
305         for(@commands)
306         {
307                 if($_->[0] eq 'time')
308                 {
309                         $commands .= sprintf "sv_cmd bot_cmd %d wait_until %f\n", $bot->{id}, $time + $_->[1];
310                         if($bot->{timer} > $time + $_->[1] + SYS_TICRATE)
311                         {
312                                 #use Carp; carp "Negative wait: $bot->{timer} <= @{[$time + $_->[1] + SYS_TICRATE]}";
313                         }
314                         $bot->{timer} = $time + $_->[1] + SYS_TICRATE;
315                 }
316                 elsif($_->[0] eq 'busy')
317                 {
318                         $bot->{busytimer} = $time + $_->[1] + SYS_TICRATE;
319                 }
320                 elsif($_->[0] eq 'buttons')
321                 {
322                         my %buttons_release = %{$bot->{buttons} ||= {}};
323                         for(@{$_}[1..@$_-1])
324                         {
325                                 /(.*)\??/ or next;
326                                 delete $buttons_release{$1};
327                         }
328                         for(keys %buttons_release)
329                         {
330                                 $commands .= sprintf "sv_cmd bot_cmd %d releasekey %s\n", $bot->{id}, $_;
331                                 delete $bot->{buttons}->{$_};
332                         }
333                         for(@{$_}[1..@$_-1])
334                         {
335                                 /(.*)(\?)?/ or next;
336                                 defined $2 and next;
337                                 $commands .= sprintf "sv_cmd bot_cmd %d presskey %s\n", $bot->{id}, $_;
338                                 $bot->{buttons}->{$_} = 1;
339                         }
340                 }
341                 elsif($_->[0] eq 'cmd')
342                 {
343                         $commands .= sprintf "sv_cmd bot_cmd %d %s\n", $bot->{id}, join " ", @{$_}[1..@$_-1];
344                 }
345                 elsif($_->[0] eq 'barrier')
346                 {
347                         $commands .= sprintf "sv_cmd bot_cmd %d barrier\n", $bot->{id};
348                         $bot->{timer} = $bot->{busytimer} = 0;
349                         undef $bot->{lastuse};
350                 }
351                 elsif($_->[0] eq 'raw')
352                 {
353                         $commands .= sprintf "%s\n", join " ", @{$_}[1..@$_-1];
354                 }
355         }
356
357         return 1;
358 }
359
360 my $intermissions = 0;
361
362 sub busybot_intermission_bot($)
363 {
364         my ($bot) = @_;
365         busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preintermission];
366         busybot_cmd_bot_execute $bot, 0, ['barrier'];
367         if($bot->{intermission})
368         {
369                 busybot_cmd_bot_execute $bot, 0, @{$bot->{intermission}};
370         }
371         busybot_cmd_bot_execute $bot, 0, ['barrier'];
372         $notetime = $timeoffset_postintermission - $lowestnotestart;
373 }
374
375 #my $busy = 0;
376 sub busybot_note_off_bot($$$$)
377 {
378         my ($bot, $time, $channel, $note) = @_;
379         #print STDERR "note off $bot:$time:$channel:$note\n";
380         my ($busychannel, $busynote, $cmds) = @{$bot->{busy}};
381         return 1
382                 if not defined $cmds; # note off cannot fail
383         die "Wrong note-off?!?"
384                 if $busychannel != $channel || $busynote ne $note;
385         $bot->{busy} = undef;
386
387         my $t = $time + $notetime;
388         my ($mintime, $maxtime, $busytime) = busybot_cmd_bot_cmdinfo @$cmds;
389
390         # perform note-off "as soon as we can"
391         $t = $bot->{busytimer}
392                 if $t < $bot->{busytimer};
393         $t = $bot->{timer} - $mintime
394                 if $t < $bot->{timer} - $mintime;
395
396         busybot_cmd_bot_execute $bot, $t, @$cmds; 
397         return 1;
398 }
399
400 sub busybot_get_cmds_bot($$$)
401 {
402         my ($bot, $channel, $note) = @_;
403         my ($k0, $k1, $cmds, $cmds_off) = (undef, undef, undef, undef);
404         if($channel <= 0)
405         {
406                 # vocals
407                 $cmds = $bot->{vocals};
408                 if(defined $cmds)
409                 {
410                         $cmds = [ map { [ map { $_ eq '%s' ? $note : $_ } @$_ ] } @$cmds ];
411                 }
412                 $k0 = "vocals";
413                 $k1 = $channel;
414         }
415         elsif($channel == 10)
416         {
417                 # percussion
418                 $cmds = $bot->{percussion}->{$note};
419                 $k0 = "percussion";
420                 $k1 = $note;
421         }
422         else
423         {
424                 # music
425                 $cmds = $bot->{notes_on}->{$note - ($bot->{transpose} || 0) - $transpose};
426                 $cmds_off = $bot->{notes_off}->{$note - ($bot->{transpose} || 0) - $transpose};
427                 $k0 = "note";
428                 $k1 = $note - ($bot->{transpose} || 0) - $transpose;
429         }
430         return ($cmds, $cmds_off, $k0, $k1);
431 }
432
433 sub busybot_note_on_bot($$$$$$$)
434 {
435         my ($bot, $time, $channel, $program, $note, $init, $force) = @_;
436         return -1 # I won't play on this channel
437                 if defined $bot->{channels} and not $bot->{channels}->{$channel};
438         return -1 # I won't play this program
439                 if defined $bot->{programs} and not $bot->{programs}->{$program};
440
441         my ($cmds, $cmds_off, $k0, $k1) = busybot_get_cmds_bot($bot, $channel, $note);
442
443         return -1 # I won't play this note
444                 if not defined $cmds;
445         return 0
446                 if $bot->{busy};
447         #print STDERR "note on $bot:$time:$channel:$note\n";
448         if($init)
449         {
450                 return 0
451                         if not busybot_cmd_bot_test $bot, $time + $notetime, $force, @$cmds; 
452                 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
453                 busybot_cmd_bot_execute $bot, 0, ['barrier'];
454                 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
455                         if @{$bot->{init}};
456                 busybot_cmd_bot_execute $bot, 0, ['barrier'];
457                 for(1..$intermissions)
458                 {
459                         busybot_intermission_bot $bot;
460                 }
461                 # we always did a barrier, so we know this works
462                 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds; 
463         }
464         else
465         {
466                 return 0
467                         if not busybot_cmd_bot_test $bot, $time + $notetime, $force, @$cmds; 
468                 busybot_cmd_bot_execute $bot, $time + $notetime, @$cmds; 
469         }
470         if(defined $cmds and defined $cmds_off)
471         {
472                 $bot->{busy} = [$channel, $note, $cmds_off];
473         }
474         ++$bot->{seen}{$k0}{$k1};
475
476         $bot->{lastuse} = $time;
477         $bot->{lastchannel} = $channel;
478         $bot->{lastprogram} = $program;
479         $bot->{lastnote} = $note;
480
481         return 1;
482 }
483
484 sub busybots_reset()
485 {
486         $busybots = Storable::dclone $busybots_orig;
487         @busybots_allocated = ();
488         %notechannelbots = ();
489         $transpose = 0;
490         $notetime = $timeoffset_postinit - $lowestnotestart;
491 }
492
493 sub busybot_note_off($$$)
494 {
495         my ($time, $channel, $note) = @_;
496
497 #       print STDERR "note off $time:$channel:$note\n";
498
499         return 0
500                 if $channel <= 0;
501         return 0
502                 if $channel == 10;
503
504         if(my $bot = $notechannelbots{$channel}{$note})
505         {
506                 busybot_note_off_bot $bot, $time, $channel, $note;
507                 delete $notechannelbots{$channel}{$note};
508                 return 1;
509         }
510
511         return 0;
512 }
513
514 sub botsort($$$$@)
515 {
516         my ($time, $channel, $program, $note, @bots) = @_;
517         return
518                 map
519                 {
520                         $_->[0]
521                 }
522                 sort
523                 {
524                         $b->[1] <=> $a->[1]
525                         or
526                         ($a->[0]->{lastuse} // -666) <=> ($b->[0]->{lastuse} // -666)
527                         or
528                         $a->[2] <=> $b->[2]
529                 }
530                 map
531                 {
532                         my $q = 0;
533                         if(($_->{lastuse} // -666) >= $time - $time_forgetfulness)
534                         {
535                                 if($channel == $_->{lastchannel})
536                                 {
537                                         ++$q;
538                                         if($program == $_->{lastprogram})
539                                         {
540                                                 ++$q;
541                                                 if($note == $_->{lastnote})
542                                                 {
543                                                         ++$q;
544                                                 }
545                                         }
546                                 }
547                                 else
548                                 {
549                                         # better leave this one alone
550                                         --$q;
551                                 }
552                         }
553                         [$_, $q, rand]
554                 }
555                 @bots;
556 }
557
558 sub busybot_note_on($$$$)
559 {
560         my ($time, $channel, $program, $note) = @_;
561
562         if($notechannelbots{$channel}{$note})
563         {
564                 busybot_note_off $time, $channel, $note;
565         }
566
567 #       print STDERR "note on $time:$channel:$note\n";
568
569         my $overflow = 0;
570
571         my @epicfailbots = ();
572
573         for(botsort $time, $channel, $program, $note, @busybots_allocated)
574         {
575                 my $canplay = busybot_note_on_bot $_, $time, $channel, $program, $note, 0, 0;
576                 if($canplay > 0)
577                 {
578                         $notechannelbots{$channel}{$note} = $_;
579                         return 1;
580                 }
581                 push @epicfailbots, $_
582                         if $canplay == 0;
583                 # wrong
584         }
585
586         my $needalloc = 0;
587
588         for(unsort keys %$busybots)
589         {
590                 next if $busybots->{$_}->{count} <= 0;
591                 my $bot = Storable::dclone $busybots->{$_};
592                 $bot->{id} = @busybots_allocated + 1;
593                 $bot->{classname} = $_;
594                 my $canplay = busybot_note_on_bot $bot, $time, $channel, $program, $note, 1, 0;
595                 if($canplay > 0)
596                 {
597                         if($noalloc)
598                         {
599                                 $needalloc = 1;
600                         }
601                         else
602                         {
603                                 --$busybots->{$_}->{count};
604                                 $notechannelbots{$channel}{$note} = $bot;
605                                 push @busybots_allocated, $bot;
606                                 return 1;
607                         }
608                 }
609                 die "Fresh bot cannot play stuff"
610                         if $canplay == 0;
611         }
612
613         if(@epicfailbots)
614         {
615                 # we cannot add a new bot to play this
616                 # we could try finding a bot that could play this, and force him to stop the note!
617
618                 my @candidates = (); # contains: [$bot, $score, $offtime]
619
620                 # put in all currently busy bots that COULD play this, if they did a note-off first
621                 for my $bot(@epicfailbots)
622                 {
623                         next
624                                 if $busybots->{$bot->{classname}}->{count} != 0;
625                         next
626                                 unless $bot->{busy};
627                         my ($busy_chan, $busy_note, $busy_cmds_off) = @{$bot->{busy}};
628                         next
629                                 unless $busy_cmds_off;
630                         my ($cmds, $cmds_off, $k0, $k1) = busybot_get_cmds_bot $bot, $channel, $note;
631                         next
632                                 unless $cmds;
633                         my ($mintime, $maxtime, $busytime) = busybot_cmd_bot_cmdinfo @$cmds;
634                         my ($mintime_off, $maxtime_off, $busytime_off) = busybot_cmd_bot_cmdinfo @$busy_cmds_off;
635
636                         my $noteofftime = busybot_cmd_bot_matchtime $bot, $time + $notetime + $mintime, $time + $notetime, @$busy_cmds_off;
637                         next
638                                 if $noteofftime < $bot->{busytimer};
639                         next
640                                 if $noteofftime + $mintime_off < $bot->{timer};
641
642                         my $score = 0;
643                         # prefer turning off long notes
644                         $score +=  100 * ($noteofftime - $bot->{timer});
645                         # prefer turning off low notes
646                         $score +=    1 * (-$note);
647                         # prefer turning off notes that already play on another channel
648                         $score += 1000 * (grep { $_ != $busy_chan && $notechannelbots{$_}{$busy_note} && $notechannelbots{$_}{$busy_note}{busy} } keys %notechannelbots);
649
650                         push @candidates, [$bot, $score, $noteofftime];
651                 }
652
653                 # we found one?
654
655                 if(@candidates)
656                 {
657                         @candidates = sort { $a->[1] <=> $b->[1] } @candidates;
658                         my ($bot, $score, $offtime) = @{(pop @candidates)};
659                         my $oldchan = $bot->{busy}->[0];
660                         my $oldnote = $bot->{busy}->[1];
661                         busybot_note_off $offtime - $notetime, $oldchan, $oldnote;
662                         my $canplay = busybot_note_on_bot $bot, $time, $channel, $program, $note, 0, 1;
663                         die "Canplay but not?"
664                                 if $canplay <= 0;
665                         warn "Made $channel:$note play by stopping $oldchan:$oldnote";
666                         $notechannelbots{$channel}{$note} = $bot;
667                         return 1;
668                 }
669         }
670
671         die "noalloc\n"
672                 if $needalloc;
673
674         if(@epicfailbots)
675         {
676                 warn "Not enough bots to play this (when playing $channel:$note)";
677 #               for(@epicfailbots)
678 #               {
679 #                       my $b = $_->{busy};
680 #                       warn "$_->{classname} -> @{[$b ? qq{$b->[0]:$b->[1]} : 'none']} @{[$_->{timer} - $notetime]} ($time)\n";
681 #               }
682         }
683         else
684         {
685                 warn "Note $channel:$note cannot be played by any bot";
686         }
687
688         return 0;
689 }
690
691 sub Preallocate(@)
692 {
693         my (@preallocate) = @_;
694         busybots_reset();
695         for(@preallocate)
696         {
697                 die "Cannot preallocate any more $_ bots"
698                         if $busybots->{$_}->{count} <= 0;
699                 my $bot = Storable::dclone $busybots->{$_};
700                 $bot->{id} = @busybots_allocated + 1;
701                 $bot->{classname} = $_;
702                 busybot_cmd_bot_execute $bot, 0, ['cmd', 'wait', $timeoffset_preinit];
703                 busybot_cmd_bot_execute $bot, 0, ['barrier'];
704                 busybot_cmd_bot_execute $bot, 0, @{$bot->{init}}
705                         if @{$bot->{init}};
706                 busybot_cmd_bot_execute $bot, 0, ['barrier'];
707                 --$busybots->{$_}->{count};
708                 push @busybots_allocated, $bot;
709         }
710 }
711
712 sub ConvertMIDI($$)
713 {
714         my ($filename, $trans) = @_;
715         $transpose = $trans;
716
717         my $opus = MIDI::Opus->new({from_file => $filename});
718         my $ticksperquarter = $opus->ticks();
719         my $tracks = $opus->tracks_r();
720         my @tempi = (); # list of start tick, time per tick pairs (calculated as seconds per quarter / ticks per quarter)
721         my $tick;
722
723         $tick = 0;
724         for($tracks->[0]->events())
725         {   
726                 $tick += $_->[1];
727                 if($_->[0] eq 'set_tempo')
728                 {   
729                         push @tempi, [$tick, $_->[2] * 0.000001 / $ticksperquarter];
730                 }
731         }
732         my $tick2sec = sub
733         {
734                 my ($tick) = @_;
735                 my $sec = 0;
736                 my $curtempo = [0, 0.5 / $ticksperquarter];
737                 for(@tempi)
738                 {
739                         if($_->[0] < $tick)
740                         {
741                                 # this event is in the past
742                                 # we add the full time since the last one then
743                                 $sec += ($_->[0] - $curtempo->[0]) * $curtempo->[1];
744                         }   
745                         else
746                         {
747                                 # if this event is in the future, we break
748                                 last;
749                         }
750                         $curtempo = $_;
751                 }
752                 $sec += ($tick - $curtempo->[0]) * $curtempo->[1];
753                 return $sec;
754         };
755
756         # merge all to a single track
757         my @allmidievents = ();
758         my $sequence = 0;
759         for my $track(0..@$tracks-1)
760         {
761                 $tick = 0;
762                 for($tracks->[$track]->events())
763                 {
764                         my ($command, $delta, @data) = @$_;
765                         $command = 'note_off' if $command eq 'note_on' and $data[2] == 0;
766                         $tick += $delta;
767                         push @allmidievents, [$command, $tick, $sequence++, $track, @data];
768                 }
769         }
770
771         if(open my $fh, "$filename.vocals")
772         {
773                 my $scale = 1;
774                 my $shift = 0;
775                 for(<$fh>)
776                 {
777                         chomp;
778                         my ($tick, $file) = split /\s+/, $_;
779                         if($tick eq 'scale')
780                         {
781                                 $scale = $file;
782                         }
783                         elsif($tick eq 'shift')
784                         {
785                                 $shift = $file;
786                         }
787                         else
788                         {
789                                 push @allmidievents, ['note_on', $tick * $scale + $shift, $sequence++, -1, -1, $file];
790                                 push @allmidievents, ['note_off', $tick * $scale + $shift, $sequence++, -1, -1, $file];
791                         }
792                 }
793         }
794
795         @allmidievents = sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } @allmidievents;
796
797         my %midinotes = ();
798         my $notes_stuck = 0;
799         my %notes_seen = ();
800         my %programs = ();
801         my $t = 0;
802         for(@allmidievents)
803         {
804                 $t = $tick2sec->($_->[1]);
805                 my $track = $_->[3];
806                 if($_->[0] eq 'note_on')
807                 {
808                         my $chan = $_->[4] + 1;
809                         ++$notes_seen{$chan}{($programs{$chan} || 1)}{$_->[5]};
810                         if($midinotes{$chan}{$_->[5]})
811                         {
812                                 --$notes_stuck;
813                                 busybot_note_off($t - SYS_TICRATE - 0.001, $chan, $_->[5]);
814                         }
815                         busybot_note_on($t, $chan, $programs{$chan} || 1, $_->[5]);
816                         ++$notes_stuck;
817                         $midinotes{$chan}{$_->[5]} = 1;
818                 }
819                 elsif($_->[0] eq 'note_off')
820                 {
821                         my $chan = $_->[4] + 1;
822                         if($midinotes{$chan}{$_->[5]})
823                         {
824                                 --$notes_stuck;
825                                 busybot_note_off($t - SYS_TICRATE - 0.001, $chan, $_->[5]);
826                         }
827                         $midinotes{$chan}{$_->[5]} = 0;
828                 }
829                 elsif($_->[0] eq 'patch_change')
830                 {
831                         my $chan = $_->[4] + 1;
832                         my $program = $_->[5] + 1;
833                         $programs{$chan} = $program;
834                 }
835         }
836
837         print STDERR "For file $filename:\n";
838         print STDERR "  Stuck notes: $notes_stuck\n";
839
840         for my $testtranspose(-127..127)
841         {
842                 my $toohigh = 0;
843                 my $toolow = 0;
844                 my $good = 0;
845                 for my $channel(sort keys %notes_seen)
846                 {
847                         next if $channel == 10 or $channel < 0;
848                         for my $program(sort keys %{$notes_seen{$channel}})
849                         {
850                                 for my $note(sort keys %{$notes_seen{$channel}{$program}})
851                                 {
852                                         my $cnt = $notes_seen{$channel}{$program}{$note};
853                                         my $votehigh = 0;
854                                         my $votelow = 0;
855                                         my $votegood = 0;
856                                         for(@busybots_allocated)
857                                         {
858                                                 next # I won't play on this channel
859                                                         if defined $_->{channels} and not $_->{channels}->{$channel};
860                                                 next # I won't play this program
861                                                         if defined $_->{programs} and not $_->{programs}->{$program};
862                                                 my $transposed = $note - ($_->{transpose} || 0) - $testtranspose;
863                                                 if(exists $_->{notes_on}{$transposed})
864                                                 {
865                                                         ++$votegood;
866                                                 }
867                                                 else
868                                                 {
869                                                         ++$votehigh if $transposed >= 0;
870                                                         ++$votelow if $transposed < 0;
871                                                 }
872                                         }
873                                         if($votegood)
874                                         {
875                                                 $good += $cnt;
876                                         }
877                                         elsif($votelow >= $votehigh)
878                                         {
879                                                 $toolow += $cnt;
880                                         }
881                                         else
882                                         {
883                                                 $toohigh += $cnt;
884                                         }
885                                 }
886                         }
887                 }
888                 next if !$toohigh != !$toolow;
889                 print STDERR "  Transpose $testtranspose: $toohigh too high, $toolow too low, $good good\n";
890         }
891
892         for my $program(sort keys %{$notes_seen{10}})
893         {
894                 for my $note(sort keys %{$notes_seen{10}{$program}})
895                 {
896                         my $cnt = $notes_seen{10}{$program}{$note};
897                         my $votegood = 0;
898                         for(@busybots_allocated)
899                         {
900                                 next # I won't play on this channel
901                                         if defined $_->{channels} and not $_->{channels}->{10};
902                                 next # I won't play this program
903                                         if defined $_->{programs} and not $_->{programs}->{$program};
904                                 if(exists $_->{percussion}{$note})
905                                 {
906                                         ++$votegood;
907                                 }
908                         }
909                         if(!$votegood)
910                         {
911                                 print STDERR "Failed percussion $note ($cnt times)\n";
912                         }
913                 }
914         }
915
916         while(my ($k1, $v1) = each %midinotes)
917         {
918                 while(my ($k2, $v2) = each %$v1)
919                 {
920                         busybot_note_off($t, $k1, $k2);
921                 }
922         }
923
924         for(@busybots_allocated)
925         {
926                 busybot_intermission_bot $_;
927         }
928         ++$intermissions;
929 }
930
931 sub Deallocate()
932 {
933         print STDERR "Bots allocated:\n";
934         my %notehash;
935         my %counthash;
936         for(@busybots_allocated)
937         {
938                 print STDERR "$_->{id} is a $_->{classname}\n";
939                 ++$counthash{$_->{classname}};
940                 while(my ($type, $notehash) = each %{$_->{seen}})
941                 {
942                         while(my ($k, $v) = each %$notehash)
943                         {
944                                 $notehash{$_->{classname}}{$type}{$k} += $v;
945                         }
946                 }
947         }
948         for my $cn(sort keys %counthash)
949         {
950                 print STDERR "$counthash{$cn} bots of $cn have played:\n";
951                 for my $type(sort keys %{$notehash{$cn}})
952                 {
953                         for my $note(sort { $a <=> $b } keys %{$notehash{$cn}{$type}})
954                         {
955                                 my $cnt = $notehash{$cn}{$type}{$note};
956                                 print STDERR "  $type $note ($cnt times)\n";
957                         }
958                 }
959         }
960         for(@busybots_allocated)
961         {
962                 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_predone];
963                 busybot_cmd_bot_execute $_, 0, ['barrier'];
964                 if($_->{done})
965                 {
966                         busybot_cmd_bot_execute $_, 0, @{$_->{done}};
967                 }
968                 busybot_cmd_bot_execute $_, 0, ['cmd', 'wait', $timeoffset_postdone];
969                 busybot_cmd_bot_execute $_, 0, ['barrier'];
970         }
971 }
972
973 my @preallocate = ();
974 $noalloc = 0;
975 for(;;)
976 {
977         $commands = "";
978         eval
979         {
980                 Preallocate(@preallocate);
981                 my @l = @midilist;
982                 while(@l)
983                 {
984                         my $filename = shift @l;
985                         my $transpose = shift @l;
986                         ConvertMIDI($filename, $transpose);
987                 }
988                 Deallocate();
989                 my @preallocate_new = map { $_->{classname} } @busybots_allocated;
990                 if(@preallocate_new == @preallocate)
991                 {
992                         print "$precommands$commands";
993                         exit 0;
994                 }
995                 @preallocate = @preallocate_new;
996                 $noalloc = 1;
997                 1;
998         } or do {
999                 die "$@"
1000                         unless $@ eq "noalloc\n";
1001         };
1002 }