# Pressed keys counter



## acabreira (Dec 10, 2008)

Hi. I´m very inexperienced with scripting, but trying to do my own for specific purposes. The problem is I´m not getting even the basics . Let´s start with this example:

___________________________________

on init

declare $how_many_notes
declare ui_label $testtext(1,1)

end on

on note

inc($how_many_notes)
set_text ($testtext,$how_many_notes)

end on

on release

dec($how_many_notes)
set_text ($testtext,$how_many_notes)

end on

____________________________________

I´m expecting the variable $how_many_notes to be my pressed keys counter. When I play a simple phrase in the keyboard, the counter seems to work fine. But as I press many simultaneous keys, the counter does not reset to zero anymore. And the keyboard is sending notes on and off consistently, as I can check in MIDI OX.

Notice I don´t want to know how many voices are being played as I could use $PLAYED_VOICES_INST instead. I want to know how many keys are being pressed. I tried checking every %KEY_DOWN(0 to 127) as well, but the results seemed to be consistent with the behavior of the above script. 

I looked for an answer in the forum, and found many issues regarding stuck notes, but no real alternative to my case. I´m using Kontakt 3.0.2.

I heard that slot 1 can be problematic, then tried in slot 2 with the same results.

The instrument has no release groups.

I know I´m probably missing something simple. Forgive me as I was reading Big Bob´s KSP Math Guides, so my brain must have lost many essential functions through the reading :D

Any hints?

Thanks.


----------



## kotori (Dec 10, 2008)

Does it work if you don't use the sustain pedal?


----------



## Nickie Fønshauge (Dec 10, 2008)

This should work - at least in some cases:


```
on init
  declare $how_many_notes
  declare ui_label $testtext(1, 1) 
end on

on note
  inc($how_many_notes)
  set_text($testtext,$how_many_notes)
end on

on release
  if (%CC[64]=0 or (num_elements(%GROUPS_AFFECTED)=0 and (%CC[64]>0)))
    dec($how_many_notes)
  end if
  set_text($testtext,$how_many_notes)
end on
```

The problem is, that releasing the sustain pedal causes an extra Note-OFF, for notes released during pedal down, to be sent - *except* when you accidentally play a note outside the instruments key-range. Hence the IF statement.


----------



## polypx (Dec 10, 2008)

This may or may not be relevant to your script, but if your script is GENERATING notes itself, then you need to be careful of the note-offs created by those. 

To keep track of held-note-count in that case you can do something like this:


```
on init
declare polyphonic $ORIGINAL_ID
declare $how_many_notes
declare ui_label $testtext(1,1)
end on

on note
$ORIGINAL_ID := $EVENT_ID
inc($how_many_notes)
set_text ($testtext,$how_many_notes)
ignore_event($EVENT_ID)
play_note ($EVENT_NOTE, $EVENT_VELOCITY, 0,-1)
end on	

on release
if($EVENT_ID = $ORIGINAL_ID)
dec($how_many_notes)
set_text ($testtext,$how_many_notes)
end if	

end on
```

As I say, this method is handy only if you are ignoring the incoming note and generating new ones. Also in this script, you don't need to watch the sustain pedal since that's affecting only the generated notes.

cheers
Dan


----------



## acabreira (Dec 10, 2008)

kotori @ December 10th 2008 said:


> Does it work if you don't use the sustain pedal?



It doesn´t, I did not even press sustain pedal for my test purposes.


----------



## acabreira (Dec 10, 2008)

Nickie Fønshauge @ December 10th 2008 said:


> This should work - at least in some cases:
> 
> 
> ```
> ...



It has the same problem of the original script, for I´was not using the sustain pedal, but thanks for the pedal tip.


----------



## acabreira (Dec 10, 2008)

polypx @ December 10th 2008 said:


> This may or may not be relevant to your script, but if your script is GENERATING notes itself, then you need to be careful of the note-offs created by those.
> 
> To keep track of held-note-count in that case you can do something like this:
> 
> ...



This method did work. But then it suscitates me two questions:

1) Should I use ignore_event always for the sake of precaution? Yes, my script is further going to generate many notes, so should I ignore the original event even in the cases I don´t need?
2) Is that because of an erratic behavior of Kontakt? I still don´t see what´s wrong with the logic of my original script.

Thanks for your answers, guys.


----------



## acabreira (Dec 10, 2008)

> That's really strange that Nicki's first script doesn't work correctly for you. Are you sure you're not combining it with another script or something like that?



Yes, it´s strange, but it doesn´t work well, it does not decrease $how_many_notes the same way mine didn´t. And no, I´m not combining with any other script for testing.




> And there's nothing wrong with the logic of your first script if you don't use the sustain pedal. It works correctly for me here, as does Nicki's.



Well, I don´t use sustain pedal, but it doesn not work either way. I mean, playing many notes in the keyboard, like 9 or 10 simultaneously (without sustain pedal) do not reset $how_many_notes to 0 when I release all the keys. In fact, I even disabled sustain pedal behavior in the instrument (tested in all three sus pedal modes). Pressing less keys - up to 7 or 8 - seems to work fine, that´s what´s confusing me.

Sorry if I´m not very clear sometimes, I´m not a native english speaker 

Thanls.


----------



## polypx (Dec 12, 2008)

Are you testing in Kontakt standalone, or within a sequencer of some kind? 

cheers
Dan

EDIT: For example, you might want to check that the note-offs are getting to Kontakt correctly. You can use the Midi Monitor script that comes with Kontakt (Presets/Factory/Utilities) to check that note-offs are coming in.


----------



## acabreira (Dec 14, 2008)

polypx @ December 12th 2008 said:


> Are you testing in Kontakt standalone, or within a sequencer of some kind?
> 
> cheers
> Dan
> ...



Hi, Dan. Both standalone and plugin. Note-offs are getting correctly, as I had already checked. I´m using a different method, anyway, as I´m developing my script. But I found out that the aforementioned happened only when voice count were exceeding max polyphony of the instrument. Even when I set ´voice stealing fade out time´ to 0. I thought those little fadeouts could be generating undesirable RCBs, but setting to 0 did not help.

Cheers


----------

