# How Kontakt trigger in script the host tempo changes



## anatholy (Apr 13, 2016)

Hello guys,
Is anyone knows how to trigg the Kontakt 5 on host tempo change in script?
There are tempo changes in tempo track, so my step sequensers are out of sync at some stages.
I tried to run listener on beat and recalculate interval but DURATION_QUARTER changes only at next call of the listener after the host change the tempo!

Thank you for your help


----------



## Lindon (Apr 14, 2016)

There's no on tempo_change event so on listener is as near as you might get with a small ms or large beat no. 

Of course your sequencer (I'm assuming its running a loop) can always check the duration every iteration of the loop


----------



## anatholy (Apr 14, 2016)

Thax a lot @Lindon,
Yes it runs a loop.
The problem is that DURATION_QUARTER changes with delay that you can hear.
I have tested it with checking it on every 10 microsec.


----------



## willbedford (Apr 14, 2016)

You can also use wait_ticks() (buggy in versions before K5.5) or run your tempo-synced code in the listener, using NI_SIGNAL_TYPE_BEAT.


----------



## anatholy (Apr 14, 2016)

@willbedford thank you,
I will check the wait_ticks().
Can not run it in listener cause I have more than one instanses of the step sequenser and they re run asyncronosly:(

So you wrote the wait_ticks is buggy..., what kind of "buggy"?


----------



## EvilDragon (Apr 14, 2016)

Don't depend on $DURATION_ constants if you want your instrument to support tempo changes at all.


----------



## anatholy (Apr 14, 2016)

@EvilDragon thanx!
Yeah, I already learned it
It was very hard lesson for me


----------



## willbedford (Apr 14, 2016)

anatholy said:


> So you wrote the wait_ticks is buggy..., what kind of "buggy"?


In some earlier versions of Kontakt, the wait_ticks() duration is completely unpredictable. If you play a script-generated pattern using wait_ticks against the metronome, the tempo will start to drift. This was fixed in 5.5 I believe.


----------



## anatholy (Apr 14, 2016)

@willbedford thak you!
Thanx a lot guys!!
So I will experimenting a bit with a wait_ticks


----------



## Lindon (Apr 15, 2016)

> Can not run it in listener cause I have more than one instanses of the step sequenser and they re run asyncronosly:(



Check here, there is another thread about how to run async sequencers in the listener, if this is your ONLY reason not to use on listener then you should use on listener with this technique...

work out the largest divisor of all sequencer steps and set the on listener beat to that value, then each sequencer is triggered on a multiple of your beats.. as I say there's at least one recent thread covering how to do this.


----------



## Lindon (Apr 15, 2016)

Just checked -- it wasn't here it was over in NI User Forums- Kontakt Scripting,

anyway this is what I said there:


So one approach is to set the listener to some small value (lets say 24/beat - you might need less...I usually find 12/beat is fine)
- so your listener fires 24 times per beat....in the listener you increment a counter(lets call it $my_counter).... 
and then hold an array of 6 values in your case(lets call it %my_array_tempos) that is defines when each sequencer gets actioned...

so in the on listener again: if mod($my_counter,%my_array_tempos[0]) = 1 then action the first sequencer...

so if for a given sequencer if its %my_array_tempos entry 
= 24 then it fires every beat (1/4 in a 4/4 structure)
= 12 then it fires 2x every beat (1/8)
= 8 it fires 3x per beat (1/12)
= 6 it 4 times a beat (1/16)
etc. etc.


----------



## anatholy (Apr 16, 2016)

@Lindon,
Thanx so much! 
Tests with wait_ticks failed:( just like you wrote.. It is out of sync in 5.5:(( , but this idea to use listener this way is briliant!!!! 
Thank you!


----------

