# While loops and wait() in multiscripts.



## Ozymandias (Oct 11, 2012)

Hi all,

In a multiscript I'm working on, I've noticed that some while loops - and especially nested loops - will only execute properly if there's a wait() in the code.

Using the callback below as an example, if I were to remove the wait command, the *receiving* instrument scripts beyond channel 5 or 6 stop behaving properly. Once the command is reintroduced everything works fine.

I'm baffled by this. Can anyone explain why it happens, and if there are any "rules of thumb" for using wait() in this sort of context?

Thanks!

Ben



*on ui_control*($all_mics)
````*if* ($disable=0)
````````*if* ($all_mics=0)
````````````set_control_par(get_ui_id($all_mics),$CONTROL_PAR_VALUE,$antipurge)
````````*end* *if*
````````$n := 0
````````*while* ($n<16)
````````````*if* (get_control_par(%m[$n],$CONTROL_PAR_VALUE)=1)
````````````````$count := 0
````````````````*while* ($count<4)
````````````````````set_midi($n,$MIDI_COMMAND_CC,%cc_array[$count],$all_mics)
````````````````````wait(1500)
````````````````````inc($count)
````````````````*end while*
````````````*end* *if*
````````````inc($n)
````````*end while*````````
````````$n := 0
````````*while* ($n<=3)
````````````set_control_par(%mics[$n],$CONTROL_PAR_VALUE,$all_mics)
````````````inc($n)
````````*end while*
````*else* *if* ($disable=1)
````*end* *if*
*end* on


----------



## Big Bob (Oct 11, 2012)

'tis a little puzzling. :roll: 

I know that while loops with no wait statement have a 'watchdog' counter that limit the number of passes to 49999. But in your case you would be no where near that upper limit.

So, there must be yet some different mechanism at work here. Perhaps it's related to the size of the MIDI queue used by Kontakt? Maybe the MIDI send rate is throttled down to something rather more like actual MIDI cable speed (ie one event per millisecond)? In that case, if you don't use a wait, there could be as many as 64 MIDI events piled up in the MIDI queue and maybe that queue is smaller?

You probably need to run some detailed tests to get a handle on this. If you do, we would appreciate you posting your findings.

Rejoice,

Bob


----------



## mk282 (Oct 11, 2012)

If you're sending MIDI CC to change some MIDI learned controls in the instrument, Kontakt has a strict limit of only 16 MIDI learned callbacks RUNNING CONCURRENTLY. No way around that. So if you have more than 16 parameters MIDI learned to the same CC, only the first 16 will work. Others will receive the CC value, but will NOT carry out their callbacks.


Not sure if this is related, but I bumped on this issue on this last project I'm working on, so now we're at a standstill, because the feature is kinda important. So we're waiting NI to respond on how they plan to deal with this issue. Probably gonna take a while... :roll:


----------



## Ozymandias (Oct 11, 2012)

Hi Bob & mk282,

Thanks for your replies. That definitely sounds like the problem I'm experiencing, mk282.

I spent a good couple of hours rewriting various callbacks because I assumed I was doing something wrong. (o)


----------



## Big Bob (Oct 11, 2012)

Hey Mario,



> Kontakt has a strict limit of only 16 MIDI learned callbacks RUNNING CONCURRENTLY.



I must be a little dense this morning (not too unusual with me lately :lol: ), but isn't each script restricted to only one CC callback at a time? Or are you referring to other callbacks triggered somehow via an assigned CC? Could you elaborate a little on what you mean by MIDI-learned callbacks? :? I thought that MIDI-learned just referred to how the user assigns the CC to control something. Boy am I confused, huh? :oops: 

Rejoice,

Bob

EDIT: To clarify my confusion :lol: If we assign a CC to control one or more knobs/sliders, etc, are you referring to the knob/slider callbacks that will be triggered when the CC changes? Is that what you are calling MIDI-Learned callbacks? If so, what does the 16 limit apply to, the number of total knobs/sliders assigned to the same CC or the combined number of knob/slider callbacks that can run as a consequence of all assigned CC changes? For example, if I assign CC1 to control 16 knobs and CC2 to control 16 knobs, and both CC1 and CC2 change, there will be a total of 32 callbacks triggered. Is that OK? Whereas if we assign 17 knobs to CC1 and CC1 changes, that won't work properly? Or won't the first scenario work either?


----------



## mk282 (Oct 11, 2012)

Big Bob @ 11.10.2012 said:


> EDIT: To clarify my confusion :lol: If we assign a CC to control one or more knobs/sliders, etc, are you referring to the knob/slider callbacks that will be triggered when the CC changes? Is that what you are calling MIDI-Learned callbacks?



Yes. When you right-click on a control (scripted one) and MIDI learn it, that is what I meant.



Big Bob @ 11.10.2012 said:


> If so, what does the 16 limit apply to, the number of total knobs/sliders assigned to the same CC or the combined number of knob/slider callbacks that can run as a consequence of all assigned CC changes



I think it's the latter, because it doesn't matter if you change the CC or even MIDI channel, things won't quite work well.

Please download the attached NKI (Kontakt 4.2.4) and see what I'm talking about. You will notice that moving the knob in the multiscript will indeed move all knobs linked to it (all 20 knobs across 5 NKIs), but only for the first 4 NKIs the callback (calling the Text function) is going to be carried out.

It doesn't seem that CC number or MIDI channel have any matter here.


----------



## Big Bob (Oct 12, 2012)

How interesting and as usual for NI, how strange :lol: 

Take a look at the attached variation. I extended each of the 5 instruments to 6 knobs where the last two knobs are automated by CC126 instead of CC127.

Note that the first four instruments work OK (even though there are 24 callbacks being triggered). However, once the magic limit of 16 callbacks for CC127 is reached, then even the CC127 callbacks cease (in instrument 5).

This might mean that this can be worked around by using more CCs working in parallel. :roll: 

Oh well, what's one more unpublished KSP limitation in the big scheme of things :lol: 

Rejoice,

Bob


----------



## Big Bob (Oct 12, 2012)

> This might mean that this can be worked around by using more CCs working in parallel.



Apparently the foregoing statement is more or less correct. Here's another variant using CC125, CC126, and CC127 automating knob pairs for each instrument script. Note that there are 8 total instruments for a total of 48 callbacks being triggered.

Of course if we add a 9th instrument, the knob callbacks for that instrument will not be triggered. Thus it would appear that the limit is not the number of callbacks triggered but rather that no one CC can be allowed to trigger more than 15 callbacks. So I guess if we divide the workload (by burning up more CC #s), 
we can trigger a max of 16*(#of CCs used) callbacks per multi?

Rejoice,

Bob


----------

