# A balance slider.



## moonunit (May 8, 2012)

Hi,
does Kontakt have a balance slider for controlling the volume of two groups?
I have two groups and want to have a single slider that mixes the levels of each group so that if I move the slider fully to the right group 2 is the only group you can hear-If I move the slider to the left then group 2 becomes silent and group 1 can be heard.
If the slider is in the middle both groups can be heard.
I may not be explaining it too well-Imagine an old osc balance dial that is used to control the levels of 2 osc's.
Is it possible to do something like this with just one slider?
Thanks.


----------



## moonunit (May 8, 2012)

Hi,
Think I have got it working.
Here it is for those who may have a similar request.
on init 
declare ui_slider $vol(-100,100)

end on 

on ui_control($vol) 

_set_engine_par($ENGINE_PAR_VOLUME,($vol + 100)*5000,1,-1,-1) 
_set_engine_par($ENGINE_PAR_VOLUME,(1-$vol + 100)*5000,0,-1,-1) 

end on 
There are probably better ways to do it,but I think it works.
Thanks.


----------



## moonunit (May 8, 2012)

Damn!!!I spoke to soon.
It works...sort of.
The problem is if you move the slider to the right it increases the volume of group 1 as expected,but if you move the slider to the centre it reduces the volume of both groups.
So as it stands it creates like a smile curve in terms of volume, the centred slider being the middle of the curve.
I just wanted to have a sort of blend dial that did not increase the volume to the extreme my script does.


----------



## moonunit (May 8, 2012)

Ok here is the latest bit incase it helps anyone else,I have tried the following.
on init 
declare ui_slider $vol(-100,100)

end on 

on ui_control($vol) 
if ($vol <0 )

_set_engine_par($ENGINE_PAR_VOLUME,($vol + 100)*5000,1,-1,-1) 
end if
if ($vol > 0)
_set_engine_par($ENGINE_PAR_VOLUME,(1-$vol + 100)*5000,0,-1,-1) 
end if


end on

Think this is working.
Has anyone got a better/easier way of doing this?


----------



## moonunit (May 16, 2012)

Hello all,
I have been using this balance slider and it was working ok, but I found that if I move the slider quite fast from left to right then it has unpredictable results in terms of volumes that are applied to groups and sometimes the volume goes so low the sound dissapears.

Is this just something that happens with Kontakt Sliders if they are moved too fast or is there a way to stop this from happening?
Can someone load the script into their Kontakt just to confirm that it is happening on other systems and not just with my Kontakt?
You need to have two groups and the balance dial will blend the two(if you move the dial slowly).


Thanks guys.


----------



## Big Bob (May 16, 2012)

Hi Moonunit,

Try changing it to this:

*on init* 
*declare* ui_slider $vol(0,100) 
*end on* 

*on ui_control*($vol) 
_set_engine_par($ENGINE_PAR_VOLUME,$vol*5000,1,-1,-1) 
_set_engine_par($ENGINE_PAR_VOLUME,(100-$vol)*5000,0,-1,-1) 
*end on* 

You are essentially trying to do a crossfade of the two groups as you run the slider from full left (zero in my version above), to full right. So you want Group 1 to start at max (100-vol)*5000 = 1000000 and finish at zero and you want Group 2 to start at zero and finish at 1000000.

However, keep in mind that if you just use a linear crossfade of the eps you will experience a dip in volume in the center. For equal-power crossfading you have to shape the crossfade properly. Do you have my Math Library?

Rejoice,

Bob

BTW the reason you are losing proper control when you move the slider rapidly is that the way you had it coded, only one of the groups gets corrected per pass of the callback. You want to be sure that both groups always get updated for each pass through the callback.


----------



## moonunit (May 16, 2012)

Hi Bob,
thanks for this I will try it out when I'm at my music computer.

I always had in the back of my mind a feeling that your maths library may be needed and it filled me with fear 
My maths is terrible lol.
I think I should jump in and give your maths system a go though as I have noticed lots of people in these threads use it.

Thanks very much for your help.


----------



## Big Bob (May 16, 2012)

Hey moon,

Just to encourage you a bit, I'm attaching a script that you can load into your two-group instrument to try. The source code looks like this:

*import* "Libs\KSPMathV406.txt"

*on init* 
``message('')
``SetMathMode(0)
``*declare* ui_slider bal(0,10000)
``*declare* ep1
``*declare* ep2
*end on* 

*on ui_control*(bal)
``VR_to_ep((10000-bal),ep1)
``VR_to_ep(bal,ep2)
``set_engine_par(ENGINE_PAR_VOLUME,ep1,0,-1,-1) 
``set_engine_par(ENGINE_PAR_VOLUME,ep2,1,-1,-1) 
*end on* 

To compile the above you will need Nils' Editor and V406 of the math library. 

However, the attachment also contains a precompiled .nkp for this script so, presuming you know how to load an .nkp script file you can try it out without needing to get the KSE or the Math Library. Then, if you like how it works, it may motivate you to get both the KSE and Math Library so you can diddle with the source code.

Please let me know if you need help with loading the .nkp.

Rejoice,

Bob


----------



## moonunit (May 17, 2012)

Big Thanks Bob,
I have tried the firts script you sent and can see what you mean about the dip in volume when the slider is moved to the centre.

I have downloaded Nil's editor(wow it's brilliant),just need to download your maths system if I may.
Thanks again.


----------



## Big Bob (May 17, 2012)

But did you try loading the precompiled .nkp? It should *not* exhibit any volume dip.

Rejoice,

Bob

BTW I think I already included the source code for V406 of the Math Library in the above attachment (it's in the Libs folder) but, if you want the documentation and such also, send me a PM with your email address and I'll send it to you.


----------



## moonunit (May 17, 2012)

Thanks for the reply Bob,
I loaded your script and it works perfectly-Awesome.
I have had a look at the script inside Kontakt and it looks like it has been encrypted or something-Does the Nils Editor do this?
I'm keen to have a play around with your maths library to try and understnd how the script actually works, at the moment I have no clue as to how it is compensating for the volume dip.Very clever stuff 
Thanks.


----------



## Big Bob (May 17, 2012)

Sorry about that, I had name compaction turned on when I compiled the .nkp for you. That's an option with the KSE.

I've recompiled the .nkp with name compaction off and I'll attach it to this post.

Rejoice,

Bob


----------



## Big Bob (May 17, 2012)

Hi again moonunit.

The reason that linear crossfading the eps doesn't work properly is that in Kontakt, the ep has a cubic relationship to the desired Volume ratio, VR. Therefore, you need to process the control values with a cube-root math function.

That's the purpose of the VR_to_ep function in my Math Library.

I'm going to attach a copy of a Kontakt Volume Control Study I did a long time ago for your info. Most of the content of this study still applies to K4/5.

Rejoice,

Bob


----------



## moonunit (May 18, 2012)

Thanks Bob.
_'The reason that linear crossfading the eps doesn't work properly is that in Kontakt, the ep has a cubic relationship to the desired Volume ratio, VR'_
Where do you guys learn and figure all this stuff out?  The script works perfectly,I'm well impressed.
I would never have worked out the maths behind it in a million years lol
I'm just glancing over the doc you sent to me, thanks for that.

Time to dive in and get the old maths gears turning again-It's been along time.
Thanks Bob.
o-[][]-o


----------



## Big Bob (May 18, 2012)

> Where do you guys learn and figure all this stuff out?



The hard way by 'reverse engineering'. :( 

It is most unfortunate that NI chooses to not disclose the math functions they use internally to relate displayed/actual parameters to the engine parameters that we have to control them with. There are basically two difficulties with using eps to control things.

(1) We aren't given the functional relationship that is needed to convert from one to the other and vice versa.

(2) The KSP has only integer arithmetic available.

I wrote the Math library to mitigate (2). But, whenever we need a format conversion routine, we are first required to obtain (1) by reverse engineering. This of course is a royal pain and at best we often end up with only an approximation of what they are probably really using internally.

But, on the other hand, the KSP has come a long way since its introduction and sometimes it's almost fun trying to figure out how to work around some of its limitations don't you think? :lol: 

Rejoice,

Bob


----------



## moonunit (May 19, 2012)

I cannot even begin to imagine how one would create a Maths Library.
Very clever stuff Bob.

I have looked at the PDF you sent  Alot of it is way over my head, but I will plod on with it.
Cheers,
Fabe


----------



## Big Bob (May 19, 2012)

Hi Fabe,



> I cannot even begin to imagine how one would create a Maths Library.



For anyone that is interested in how the Math Library works, there is a Technical Guide and TG Addendum that details the algorithms used. But, most scripters are content to just use the routines and could care less about how they are put together.

When I first released the Math Library, I did so as a complete package including a User Guide and Technical Guide along with some demo software illustrating possible uses for the Library. However, the pdfs are too big to attach to forum posts so as the library has been updated and expanded, I usually just post the revised source code and maybe a User Guide addendum occasionally. 

I don't have a WEB site and I hate to keep bothering others with the task of hosting the library and all its updates. However, anyone interested in more documenatation for the library can just PM me with their email address and I'll send them what they request (if I can find it on my hard drive :lol: ).

Rejoice,

Bob


----------

