# Ignore event



## Mike Greene (Oct 30, 2011)

I want to use an "ignore_event" command so that Kontakt won't play a note that's outside of a variable range that the user can specify. (In this case: 30 to 80.) For reasons too complicated to go into here, I have zones that are well outside this playable range. Lets say 20 to 120, even though I might only want to hear the samples if the note is between 30 and 80.

I also have a bunch of keyswitch duties to take care of (in this case reverb and delay switches,) which I handle before checking whether the note is inside the playable range. The keyswitches are outside the playable range, but there are samples (zones) there, so I don't want to hear them. Easy enough to do it like this:


```
on note
    if ($EVENT_NOTE = 20) 
        turn off reverb
    end if

    if ($EVENT_NOTE = 25) 
        turn off delay
    end if

    if (($EVENT_NOTE < 30) or (($EVENT_NOTE > 80) {out of range}
        ignore_event ($EVENT_ID)
    end if

    if in_range($EVENT_NOTE,30,80)
        do a bunch of note playing stuff
    end if
end on
```

Notice that I'm asking ksp to ignore any note below 30, even though if that note is 20 or 25, there were some keyswitch duties (reverb and delay) I wanted ksp to do. Of course, I'm telling ksp to ignore the note *after* I've taken care of those keyswitch duties, but for some reason, I'm feeling a little uneasy about this. There's no chance that that ignore_event command could get taken out of order, is there?


----------



## gregjazz (Oct 30, 2011)

You know, I'd just put an "exit" function after the "ignore_event". That way you don't have to have another if statement to detect the in_range.

I mean, or you could just put an "else".

It just seems cleaner than having two separate if statements.


----------



## Big Bob (Oct 31, 2011)

> Of course, I'm telling ksp to ignore the note *after* I've taken care of those keyswitch duties, but for some reason, I'm feeling a little uneasy about this. There's no chance that that ignore_event command could get taken out of order, is there?



Hi Mike,

I think you should be OK. AFAIK, Kontakt usually defers execution of callback instructions until after the callback exits, probably by queing them during the callback. Of course for your specific example, it's easy enough to verify whether it works the way you want or not. Simply put the MIDI analyzer script in the last slot and then hit note 20. If all is well, your 'turn off reverb' code should execute but the analyzer should *not *indicate that note 20 occured.

As Greg suggested, you can take advantage of the *exit* command whenever you execute code that will be the last thing you want to do in that callback. The only caveat with *exit* is if you are using KN functions. When the *exit* command is in a KN function, it may just cause the function to exit but not the callback. At least it was that way for a while, then it seemed to change but now I notice that K5 at least makes a point of saying this behavior is intended.

I'll just mention one other general simplification.


```
if (($EVENT_NOTE < 30) or (($EVENT_NOTE > 80) {out of range} 
        ignore_event ($EVENT_ID) 
    end if
```

The following code is equivalent:


```
if not in_range($EVENT_NOTE,30,80) 
        ignore_event ($EVENT_ID) 
    end if
```

Rejoice,

Bob


----------



## Mike Greene (Oct 31, 2011)

Thanks guys. The script does indeed work when I test it, I just wanted to make sure there isn't some odd condition everyone else knows about that would mess things up later. Looks like I should be okay then.

The reason I don't put use "else" instead of two separate "if's" is because this way it's easier for me to see which part of the NCB I'm in. Even with decent comments, I spend a lot of my scripting time retracing my logic, so I often sacrifice efficiency in favor of clarity. Putting in an "exit" function would probably be a good idea, though. I'd still keep the redundant "if in-range" statement afterwards, though, just for my own clarity.

By the way, I'm laughing that I didn't think of "not in-range" myself. Funny how we (okay, maybe just me) can miss completely obvious stuff. :mrgreen:


----------



## mpalenik (Nov 1, 2011)

Mike Greene @ Sun Oct 30 said:


> Notice that I'm asking ksp to ignore any note below 30, even though if that note is 20 or 25, there were some keyswitch duties (reverb and delay) I wanted ksp to do. Of course, I'm telling ksp to ignore the note *after* I've taken care of those keyswitch duties, but for some reason, I'm feeling a little uneasy about this. There's no chance that that ignore_event command could get taken out of order, is there?



Not that you need to, but there's absolutely no reason you couldn't put the ignore_event at the top, like below. You'll still have access to the variables $EVENT_NOTE and $EVENT_ID, and you'll be able to do the key switching stuff with notes 20 and 25.


```
on note 

    if (($EVENT_NOTE < 30) or (($EVENT_NOTE > 80) {out of range} 
        ignore_event ($EVENT_ID) 
    end if 

    if ($EVENT_NOTE = 20) 
        turn off reverb 
    end if 

    if ($EVENT_NOTE = 25) 
        turn off delay 
    end if 

    if in_range($EVENT_NOTE,30,80) 
        do a bunch of note playing stuff 
    end if 
end on
```

The other thing you could do, if you choose to leave the ignore_event command at the end, is instead of having two if statements, use an if and else statement. The advantage to this is that the second if statement doesn't need to be checked if the first if statement turns out true, and the else statement automatically executes if the if statement is false, rather than having to do another check, thus requiring fewer clock cycles. Granted it will make no measurable difference in how fast your code runs, but it's a more efficient coding model:


```
on note

    if ($EVENT_NOTE = 20) 
        turn off reverb 
    end if 

    if ($EVENT_NOTE = 25) 
        turn off delay 
    end if 

    if (($EVENT_NOTE < 30) or (($EVENT_NOTE > 80) {out of range} 
        ignore_event ($EVENT_ID) 
    else 
        do a bunch of note playing stuff 
    end if
```

Of course, I probably would have structured it like this:


```
on note

    if in_range($EVENT_NOTE,30,80) 
        do a bunch of note playing stuff 
    else
        ignore_event($EVENT_ID)
    end if 

    select ($EVENT_NOTE)
        case 20
            turn off reverb 
        case 25
            turn off delay 
    end select 

end on
```


----------



## Mike Greene (Nov 1, 2011)

Thanks mpalenik! o-[][]-o I hadn't thought about the fact that $EVENT_NOTE and $EVENT_ID variables still exist even after using the ignore_event command. Makes sense and it means my concerns are solved either way. I might go ahead and put the ignore condition at the top, along with an "exit" command.

Using "else" would be cleaner, but it does make the sections a little less obvious. This script is really long (obviously it's way more complex than my example,) so to make it clear what's happening where, I keep things in very discrete sections. I do use "cases" for the keyswitch section, though. (The example was just something I made up that would be simple to read.)


----------

