+ my %sustain = ();
+
+ my $note_on = sub
+ {
+ my ($ev) = @_;
+ my $chan = $ev->[4] + 1;
+ ++$notes_seen{$chan}{($programs{$chan} || 1)}{$ev->[5]};
+ if($midinotes{$chan}{$ev->[5]})
+ {
+ --$notes_stuck;
+ busybot_note_off($t - SYS_TICRATE - 0.001, $chan, $ev->[5]);
+ }
+ busybot_note_on($t, $chan, $programs{$chan} || 1, $ev->[5]);
+ ++$notes_stuck;
+ $midinotes{$chan}{$ev->[5]} = 1;
+ };
+
+ my $note_off = sub
+ {
+ my ($ev) = @_;
+ my $chan = $ev->[4] + 1;
+ if(exists $sustain{$chan})
+ {
+ push @{$sustain{$chan}}, $ev;
+ return;
+ }
+ if($midinotes{$chan}{$ev->[5]})
+ {
+ --$notes_stuck;
+ busybot_note_off($t - SYS_TICRATE - 0.001, $chan, $ev->[5]);
+ }
+ $midinotes{$chan}{$ev->[5]} = 0;
+ };
+
+ my $text_event = sub
+ {
+ my ($ev) = @_;
+
+ my $chan = TEXT_EVENT_CHANNEL;
+
+ busybot_note_on($t, TEXT_EVENT_CHANNEL, -1, $ev->[4]);
+ busybot_note_off($t, TEXT_EVENT_CHANNEL, $ev->[4]);
+ };
+
+ my $patch_change = sub
+ {
+ my ($ev) = @_;
+ my $chan = $ev->[4] + 1;
+ my $program = $ev->[5] + 1;
+ $programs{$chan} = $program;
+ };
+
+ my $sustain_change = sub
+ {
+ my ($ev) = @_;
+ my $chan = $ev->[4] + 1;
+ if($ev->[6] == 0)
+ {
+ # release all currently not pressed notes
+ my $s = $sustain{$chan};
+ delete $sustain{$chan};
+ for(@{($s || [])})
+ {
+ $note_off->($_);
+ }
+ }
+ else
+ {
+ # no more note-off
+ $sustain{$chan} = [];
+ }
+ };
+