# Crossfade dynamic layers with new syntax



## derstefmitf (Dec 21, 2016)

Hi, 

I am currently working on a bigger project where I need to implement a dynamic crossfade function. I am not sure if this is the standard term, so a short explanation: I have multiple dynamic layers and want to crossfade between them via the modwheel. 
So after some research here in the forum, I come to the conclusion that either I could go with something sophisticated like Nils' Crossfade Script / Big Bobs Math Library or the easier way with separate groups for each layer and classic modulator curves.

I really like Nils' script, but I would need to implement and modify it into my script because the final NKI will have a lot of different articulations with different amounts of dynamic layers and thus on global setting will not work.

Now my actual question: has someone used the new arithmetic commands to build a dynamic crossfade (with equal power) system? I simply wonder if it would be much easier now with the new syntax? I am not very keen on mathematical things like that, so excuse me if my question is stupid :D

Thanks for your help and wish you all Merry Christmas


----------



## d.healey (Dec 21, 2016)

Use modulators it's so much simpler and has no cons


----------



## derstefmitf (Dec 21, 2016)

d.healey said:


> Use modulators it's so much simpler and has no cons


Yeah, it seems so, I just need to overcome my deep urge to get my NKIs as tidy as possible.


----------



## d.healey (Dec 21, 2016)

derstefmitf said:


> Yeah, it seems so, I just need to overcome my deep urge to get my NKIs as tidy as possible.


No reason why modulators can't be tidy


----------



## EvilDragon (Dec 21, 2016)

Modulators are more CPU efficient too.


----------



## Stiltzkin (Dec 21, 2016)

There are ways to do it about as efficient that allow for greater customization than the typical modulator, but I agree with others in this thread, if you don't NEED to do it by script, don't.

Only reason to keep it with the script is if you're planning on making instruments at very fast rates, want to keep the group count down, want to stop users changing things under the hood, or have a creative reason for changing or adapting the curves on the fly (ie light and sound cheer strings "split slider"). In most cases you won't need this, so you can save yourself the work!

Also, there's no need to use the math functions if you don't want to. Even in pre 5.6, it was still possible to do, and even without big Bob's math library. You'd only need two master arrays that you reference, much better than mathing it out every time the mod wheel is moved!


----------



## robgb (Dec 21, 2016)

Okay, complete idiot here. How would you do this by modulators? The only way I know of creating a mod wheel crossfade is through AET.


----------



## EvilDragon (Dec 21, 2016)

Add a modwheel modulator to two groups, one of the groups gets the modulator inverted. Then use the modulation shaper to adjust the response with a curve.


----------



## robgb (Dec 21, 2016)

EvilDragon said:


> Add a modwheel modulator to two groups, one of the groups gets the modulator inverted. Then use the modulation shaper to adjust the response with a curve.


Excellent. Thanks!


----------



## derstefmitf (Dec 22, 2016)

Stiltzkin said:


> Only reason to keep it with the script is if you're planning on making instruments at very fast rates, want to keep the group count down, want to stop users changing things under the hood, or have a creative reason for changing or adapting the curves on the fly


Well, keeping the group count down is a little concern for me.



Stiltzkin said:


> Also, there's no need to use the math functions if you don't want to. Even in pre 5.6, it was still possible to do, and even without big Bob's math library. You'd only need two master arrays that you reference, much better than mathing it out every time the mod wheel is moved!


Excuse me to be so direct, but do you have the right reference arrays as .nka files or in a Excel sheet? I would like to try that. Thanks.


----------



## Stiltzkin (Dec 22, 2016)

I do but I'm not home this Christmas, I can grab them after Christmas if you like, but it's a fairly simple array to create within a for loop, using either big Bob's or the new 5.6 math!

Once you have it created in the for loop, just use save array and you can open that file with a notepad document - then just put it in your deceleration!


----------



## polypx (Dec 22, 2016)

or put a load to it in the 'persistance_changed' , to reduce your init size


----------



## EvilDragon (Dec 23, 2016)

Or use load_array() mode 2 to load the NKA file from resource container, to reduce init declaration even further


----------



## derstefmitf (Dec 23, 2016)

Stiltzkin said:


> I do but I'm not home this Christmas, I can grab them after Christmas if you like, but it's a fairly simple array to create within a for loop, using either big Bob's or the new 5.6 math


That would be great, thanks. 

But just to learn something new (again, sorry for my poor math skills). How would I have to calculate something like that with the new 5.6 math? 

I understand that I need to use sin und cos to calculate the curve right? Let's say I want to fill a table with 100 columns with a sin curve which starts with 0 at table[0] and ends with 100 at table[100].


----------



## EvilDragon (Dec 23, 2016)

That's not a full sine then. 

Here's a quick example. Can probably be done in a more elegant way.


```
on init
   set_ui_height(4)

   declare const $NUM_STEPS := 128
   declare const ~2PI := 2.0 * ~NI_MATH_PI
   declare ~STEP
   ~STEP := ~2PI / int_to_real($NUM_STEPS)

   declare ui_table %T[$NUM_STEPS] (6,6,-100)
   declare ui_button $Sine
   declare $i
   declare ~val

   message("")
end on

on ui_control ($Sine)
   $i := 0
   while ($i < $NUM_STEPS)
     ~val := sin(~STEP * int_to_real($i)) * 100.0
     %T[$i] := real_to_int(~val)
     inc($i)
   end while
   $Sine := 0
end on
```


----------



## polypx (Dec 23, 2016)

If he's using it for crossfading he only needs the first 90 degrees ... right? ~PI/2 ? That takes him from 0 to maximum.


----------



## EvilDragon (Dec 23, 2016)

Right, that is true. The example I gave generates a full sine cycle.


----------



## Stiltzkin (Dec 23, 2016)

Definitely load array for something like this, helps to have a lot of different curves saved in array files so you can test them during development. And I'd recommend larger, more accurate arrays so you will never have replicated whole numbers when using curves between few midi values


----------



## polypx (Dec 23, 2016)

...and then of course there's the matter of mapping this sine curve onto Kontakt's engine parameters, like Volume, in a meaningful way. For me this requires using another array, so in a simple case it's easier to just have an array that combines the sine curve with the engine parameter mapping of that curve, as a single array. This can only be done if there's a one-to-one relationship between the curve and the map of course, which is not always the case.


----------



## Stiltzkin (Dec 23, 2016)

Which thankfully big Bob's math library takes care of. And that would be within the original for loops for the arrays, no point having the arrays as anything else in most cases


----------



## EvilDragon (Dec 23, 2016)

It's definitely possible to track down engine par scaling - BB already did it, he just converted it to cordic system, which we should re-convert back to floating point using native math functions now, then scale to 0-1000000.


----------

