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