# Xfade: what am I doing incorrectly here?



## gregjazz (Dec 24, 2007)

Hey guys. So far you've all been great help to me, and here I am with another problem. For some reason the sin and cos aren't returning the expected values. I feel like I overlooked something obvious. This is supposed to be a simple operation (thanks to Bob's wonderful math library!!) for calculation equal-power crossfade volume levels:

*import* "KSPMath_V105.txt" 

*on init*
``*declare* xfv
``*declare* sin
``*declare* cos
``*declare* vol1
``*declare* vol2
*end on*

*on controller*
``xfv := %CC[1]
``xfv := Ang90*xfv/127
``
``SinCos(xfv, sin, cos)``````````````
``Get_db(cos, vol1)
``Get_db(sin, vol2)
``
``_set_engine_par($ENGINE_PAR_VOLUME, vol1,0,-1,-1)
``_set_engine_par($ENGINE_PAR_VOLUME, vol2,1,-1,-1)
*end on*


----------



## Big Bob (Dec 24, 2007)

Hi Greg?

Are you sure the sin/cos values are wrong? I haven't used the engine parameters too much but don't they require a value range of 0 to 1,000,000 (where I guess 1,000,000 is zero db)? Get_db returns an attenuation amount in milli-decibels which will be a number from about -100,000 to 0. (-100 db is considered silence, ie muted)

I don't know if the 0 to 1,000,000 range for the ep is linear or logarithmic but, if it's linear, then you may simply want to scale up the sin/cos by a factor of 100 and then use it directly (without using Get_db).


```
vol1 := 100*sin
vol2 := 100*cos
```
instead of the two Get_db lines.

However, if the ep control range of 0 to 1,000,000 is logarithmic, then things will be a little more complicated. I would then have to know what the attenuation formula for the ep is before I could tell you how to 'massage' it.

Hope this helps.

God Bless and Merry Christmas,

Bob

EDIT: Maybe you will need to interchange the sin and cos. If vol1 is for group 0 and vol2 is for group 1, then use the cos for the group you want to play when the Mod Wheel is at min and the sin for the group you want to play when the wheel is at max. The way I wrote it above, vol2 will be max at Mod Wheel = 0/


----------



## gregjazz (Dec 24, 2007)

Big Bob @ Mon Dec 24 said:


> Are you sure the sin/cos values are wrong? I haven't used the engine parameters too much but don't they require a value range of 0 to 1,000,000 (where I guess 1,000,000 is zero db)? Get_db returns an attenuation amount in milli-decibels which will be a number from about -100,000 to 0. (-100 db is considered silence, ie muted)



That sounds about right to me, actually. I was reading the manual and it said I could expect a number between 0 and 1, so that's what I initially expected the sin and cosin functions to return.

I have been able to "massage" the output to scale it everything from silence to 0.0 dB, and I can tell it's not a linear output, too. But I'm still getting a weaker signal in the middle of the crossfade. I'll play around with it more, look at Nils' crossfading script, and what not.

I really appreciate your help, and have a great Christmas/New Year!!


----------



## Nickie Fønshauge (Dec 25, 2007)

Big Bob @ 25th December 2007 said:


> I haven't used the engine parameters too much but don't they require a value range of 0 to 1,000,000 (where I guess 1,000,000 is zero db)?


Not quite so.
0 dB = 630000 &
12 dB = 1000000

Instead of using set_engine_par you would be better off using change_vol with Bob's magnificent Math library.


----------



## Big Bob (Dec 25, 2007)

Nickie Fønshauge @ Tue Dec 25 said:


> Big Bob @ 25th December 2007 said:
> 
> 
> > I haven't used the engine parameters too much but don't they require a value range of 0 to 1,000,000 (where I guess 1,000,000 is zero db)?
> ...



Nickie, perhaps 1000000 produces the max db that the instrument is set to? (which in your example appears to be +12db).

And Greg, regarding the sin and cos, while their max value mathematically is 1.0, my math library scales them by 10,000 (otherwise it could output only 0 or 1). For example, the sine or cosine of 500 dg (45 degrees) is 0.70711 but with only integers to work with, this would produce an output of 0. With my math library you should get an output of about 7071 at 45 degrees (500 decigrads). Since the scaled sin/cos value can thus range from 0 to 10000, multiplying the sin/cos output by another 100 will cause them to swing from 0 to 1000000 which is the desired range for the engine parameter control. However, if the ep control function is non-linear you won't get the expected results until you properly map the linear 100*sin and 100*cos values appropriately. As Nickie is suggesting, why not use the change_vol function which accepts a mdb control input? That way you can use the Get_db function to map from the linear sin/cos values.


----------



## Nickie Fønshauge (Dec 25, 2007)

Big Bob @ 25th December 2007 said:


> Nickie, perhaps 1000000 produces the max db that the instrument is set to? (which in your example appears to be +12db).



Bob,

_set_engine_par($ENGINE_PAR_VOLUME, 1000000,$g,-1,-1)

turns the Amplifier Volume knob of group $g to its maximum position, which is 12 dB.


----------



## gregjazz (Dec 25, 2007)

Big Bob @ Tue Dec 25 said:


> And Greg, regarding the sin and cos, while their max value mathematically is 1.0, my math library scales them by 10,000 (otherwise it could output only 0 or 1). For example, the sine or cosine of 500 dg (45 degrees) is 0.70711 but with only integers to work with, this would produce an output of 0. With my math library you should get an output of about 7071 at 45 degrees (500 decigrads). Since the scaled sin/cos value can thus range from 0 to 10000, multiplying the sin/cos output by another 100 will cause them to swing from 0 to 1000000 which is the desired range for the engine parameter control.



Ahhh, that explains it exactly. I was wondering why I wasn't getting an output range from 0 to 1, but yet it was non-linear in terms of its scale from 0 to 10000. Thanks!



Big Bob @ Tue Dec 25 said:


> However, if the ep control function is non-linear you won't get the expected results until you properly map the linear 100*sin and 100*cos values appropriately. As Nickie is suggesting, why not use the change_vol function which accepts a mdb control input? That way you can use the Get_db function to map from the linear sin/cos values.



What I'm doing is changing the volume of an entire group, using Kontakt's groups as a method to sort my crossfading samples... if I used the change_vol function, how would I detect which group the sample is sorted into? (that's not a rhetorical question)


----------



## Big Bob (Dec 26, 2007)

> What I'm doing is changing the volume of an entire group, using Kontakt's groups as a method to sort my crossfading samples... if I used the change_vol function, how would I detect which group the sample is sorted into? (that's not a rhetorical question) What I'm doing is changing the volume of an entire group, using Kontakt's groups as a method to sort my crossfading samples... if I used the change_vol function, how would I detect which group the sample is sorted into? (that's not a rhetorical question)



The answer to that depends on what method you are using to 'select' the active group in the first place. For example, if you are using a MIDI CC for group start control, then the script can read (in the NCB) that same CC value to determine which group is 'sounding' the note.

I guess what I'm trying to say is that when a note comes into K2, it will be 'sorted' into all groups unless you steer it by setting some sort of group start control such as keyswitching, MIDI CC, etc. Whatever that mechanism is (that you are currently using) is the same mechanism your script can use to determine which group is sounding when an 'on note' callback occurs.

Clear as mud? :lol: 

Bob


----------



## Big Bob (Dec 26, 2007)

Hi Nickie,

Have you by any chance noted a few more data points other than these?



> 0 dB = 630000 &
> 12 dB = 1000000



If the relationship is at least a psuedo-db scaling, then the function might well be something like:

X = 30.8*Gain +630000 where Gain is the desired gain in mdb
and X is the engine parameter value required to achieve that Gain.

The above equation would be approximately correct for the two data points you've given so it would be interesting to see how well it tracks some additional data points.

Bob


----------



## Thonex (Dec 26, 2007)

Interesting Bob and Nickie.

I have to say it boggles my mind why NI doesn't release some sort of spec sheet to describe the relationships and formulas used to derive the curves they have.

I mean... isn't it totally silly that we need to be sleuthing around like this just to figure out their implementation of NI value --> db value??

They have everything to gain and nothing to lose by releasing these formulas. It would make supporting K2 and K3 so much easier for developers and hence make it a more popular platform. I just don't "get" their lack of transparency on these issues.

T


----------



## Big Bob (Dec 26, 2007)

Thonex @ Wed Dec 26 said:


> Interesting Bob and Nickie.
> 
> I have to say it boggles my mind why NI doesn't release some sort of spec sheet to describe the relationships and formulas used to derive the curves they have.
> 
> ...



Yes indeed Andrew, I've often said that NI in many ways is a 'strange' company :lol: 

One of the things that always 'worries' me a bit is that when they don't disclose such details, they may think they are at liberty to change it anytime they please without any warning. So, just as soon as we get it all worked out, they will up and change how they are doing it and we'll have to start all over trying to re-discover what they did :( 

Rejoice,

Bob


----------



## Tod (Dec 26, 2007)

Hi Bob, 

Actually I posted a couple of scripts the other day that show the various volume numbers for -12, -6, 0, +6, and +12db. They also have the different diviser numbers used with each setting.

http://vi-control.net/forum/viewtopic.php?t=8336

Basically if you know the actual K2 number for any given db you can simply divid that by 128 and it will work out almost exactly the same as what NI is doing when going from 0db (infinity) to what ever db level your going for. By that I mean the analogous taper NI uses is still there and will only be off by .2db at most at any given point in time. Also I should mention that the Amplifier Volume works the same as the Main Volume in this respect.

What threw me in my way of thinking (this old brain of mine) is that by useing a simple diviser like this would give it a linear taper but that's not the case, NI has it's own way of dealing with this. 

Based on this I'm left wondering how the volume taper can be improved upon other than the smoothing. Unless you know exactly how NI is dealing with this taper and have the means to over-ride it, how can you confidently come up with something that won't be distorted as far as the taper is concerned?

Hehe, I think I've figured out what they're doing for the smoothing too. I started to put a script together for it and got it to working somewhat but then I got side tracked and didn't finish it. It's pretty simple. :roll: 

If I'm totally off base from what you guys and ladies are discussing here please forgive me. :oops: 

Tod


----------



## gmet (Dec 27, 2007)

pocoapoco,

try this:


> *on init*
> ``*declare* ui_knob $Vol (0,1000000,1)
> ``set_text($Vol, "Volume")
> ``set_knob_unit($Vol, $KNOB_UNIT_DB)
> ...



As you move the knob (db) you will get the corresponding data displayed as a message (bottom left of Kontakt)

Justin

p.s. the data values for volume are not the same as data values for gain (when using inserts or sends) i.e. 0db volume = 630000 and 0db gain = 500000


----------



## Nickie Fønshauge (Dec 27, 2007)

Big Bob @ 26th December 2007 said:


> Hi Nickie,
> 
> Have you by any chance noted a few more data points other than these?
> 
> ...


Hi Bob,

Only 

-6 db = 500400 & 
6 dB = 795000

but you can use Justin's script to get the rest. Instrument Volume and Group Volume follow the same curve. Bear in mind, that due to roundoff all dB values correspond to a range of values, not a single value. The numbers I have relayed here are just the simplest/nicest looking.


----------



## Nickie Fønshauge (Dec 27, 2007)

Big Bob @ 27th December 2007 said:


> Right now I'm in the thick of chasing down a strange K2 bug related to polyphonic variables losing their content in a certain case. I've been trying to nail this problem all afternoon but, it's still fighting me.



K2 does have some "nice" little mind-twisting surprises for you, doesn't it!? :twisted: Ever tried to use 2 instances of the same modulator, like a Multi LFO, to modulate pitch *and* volume? It works like a charm in the stand alone, which you would normally use for development, and you would think all is well. But then try the same instrument in the VST or DXi plugin >8o . It took me a couple of days to figure that one out. :evil:


----------



## Big Bob (Dec 27, 2007)

Nickie Fønshauge @ Thu Dec 27 said:


> Big Bob @ 27th December 2007 said:
> 
> 
> > Right now I'm in the thick of chasing down a strange K2 bug related to polyphonic variables losing their content in a certain case. I've been trying to nail this problem all afternoon but, it's still fighting me.
> ...



Ouch! I can feel the pain that must have been :( 



> but you can use Justin's script to get the rest. Instrument Volume and Group Volume follow the same curve. Bear in mind, that due to roundoff all dB values correspond to a range of values, not a single value. The numbers I have relayed here are just the simplest/nicest looking.



Sure, but, I was just being lazy and hoping someone had already taken the necessary data and distilled it to its essence :wink: For example, it looks like Tod put a lot of time into it, but I'm not sure I can extract from it the simple kind of data I wanted without expending even more effort. For now, I think your two additional data pairs pretty much tell me what I wanted to know at this point. Later on I'll tackle Tod's stuff (after I finish punching my way out of my current polyvar mess :lol: )

God Bless,

Bob


----------



## Big Bob (Dec 27, 2007)

> I've found that you can make velocity modulated volume match controller modulated volume by applying this function.
> 
> change_vol($EVENT_ID,84000-(logb2($EVENT_VELOCITY)*12000),0)
> 
> where logb2 is log base 2. I found there is no log function in the script So I had to enter all the log base 2 from 0 to 127 into an array manually.



You may want to check out my KSP Math Library, it has a log2 function.

http://vi-control.net/forum/viewtopic.p ... pitch+bend

God Bless,

Bob

BTW You can also download the Technical Guide here:

http://www.andrewkmusic.com/filearea/SIPS/MathLibraryTechGuide.zip (http://www.andrewkmusic.com/filearea/SI ... hGuide.zip)


----------



## Big Bob (Dec 27, 2007)

Hi Mr little by little :lol: 



> So I guess there is no way to access the internal scaling tables' data. That's too bad because it would make the initial problem posted in this thread very simple to solve by simply applying my conversion.



I'm not aware of any way to 'read' the scaling data, but, that doesn't mean too much because I haven't had much usage of the engine parameter stuff so there might be something in there that I'm not familiar with :? 



> I'd still have to set up an array and enter values manually for most of my needs as I very often max out the processing capability with my orchestra library. Run-time efficiency is pretty important.



You still may want to try the log2 function because it executes very fast (I think I recall something on the order of 1 or 2 micro-seconds). I used DeVos approximation because it was simple and fast yet accurate enough for most purposes.

God Bless,

Bob


----------



## Nickie Fønshauge (Dec 28, 2007)

Tod @ 28th December 2007 said:


> Actually this presents a question I've had for some time. I've got some EW-Gold programs that use both cc1 and cc11 as modulators on the Amplifier. Of course cc1 is for Xfadeing the layers and cc11 is straight volume or expression. These seem to work okay. However, I've tried useing them together on my own Xfade programs and they seemed to conflict with each other.


I have used CC#11 for x-fading and for straight volume modulation at the same time with no problems and I have used CC#11 for x-fading and CC#67 for straight volume modulation, still with no problems. What is your problem?


----------



## Tod (Dec 28, 2007)

> I have used CC#11 for x-fading and for straight volume modulation at the same time with no problems and I have used CC#11 for x-fading and CC#67 for straight volume modulation, still with no problems. What is your problem?



Hi Nickie,

It basically sounds like two separate controllers trying to control the same parameter, giving a kind of sputtering intermitent sound. Since this seems logical when two controllers are trying to have control of the same controller I just assumed that this was the case. I should mention that this can be quite subtle, causeing just slight sputtering that you have to listen close to hear. 

However, since it seems to work with my EW samples/programs and you have also had success with it I'm left wondering. Are you by any chance totally reverseing the scaleing and intensity? I've also tried that but it didn't seem to make any difference.

It shouldn't make any difference what controllers are used as long as they're not conflicting somewhere else.

Of course it stands to reason why you can't put two controllers on the main volume or the Ampifier volume but when it comes to the modulators I don't know. It'd be great if you can use 2 or 3 modulators on the volume at the same time but if that's the case then I guess I'll have to revisit the problems I had in trying to do that. :( :?  o/~ 

Thankyou,

Tod


----------



## Thonex (Dec 28, 2007)

Tod @ Fri Dec 28 said:


> > I have used CC#11 for x-fading and for straight volume modulation at the same time with no problems and I have used CC#11 for x-fading and CC#67 for straight volume modulation, still with no problems. What is your problem?
> 
> 
> 
> ...




OK... I'm glad we're off-topic on this particular topic... 

Tod, I think (though I'm not sure) there may be an issue with what order (slots) you put the CC 11 and CC 1 modulators affecting the amplitude. Make sure (for example) that all the CC 1 (layer x-fade) modulators are in the same slot and all the CC11 are in another slot. In fact.. now that I think about it I'm really not sure. Maybe it's related to a scaling thing... or if you have the scaling set to a negative number for 1 slot and a positive for another. Maybe I'll start a new thread for this topic... as well as this next question:

What is the parameter in the CC modulators affecting the AMP to make it so that when the patch first loads that the patch isn't silent. This drives me nuts... Do I have to set the second field to -1? 

Thanks,

T


----------



## Nickie Fønshauge (Dec 29, 2007)

Hey Tod, 

What do you have as smoothing (the last parameter)? I use 100 or 200 (=ms). 0 might give you sputtering.

The intensity slider shouldn't matter. Neither should the initial value (set this to anything > 0%, if you want the instrument to load non-silent (if intenssity slider = -100% it should be "anything < 100%", I think); "-1" means: don't initialize this controller).


----------



## Nickie Fønshauge (Dec 29, 2007)

Thonex said:


> Tod, I think (though I'm not sure) there may be an issue with what order (slots) you put the CC 11 and CC 1 modulators affecting the amplitude. Make sure (for example) that all the CC 1 (layer x-fade) modulators are in the same slot and all the CC11 are in another slot.


Good thinking, Andrew. This is exactly the kind of weird behaviour, that Kontakt could exhibit, although I don't think it is in this particular case. In recent instruments I maintain the same order for reasons having to do with scripting: a script can't (as far as I know) determine the particulars of a CC modulator, only that it *is* a CC modulator, but in earlier instruments the order was more haphazard (made haphazard by K2, not by me), and they still worked without sputtering.

However, sometimes the modulation of an instrument gets screwed up for reasons beyond me, and all you can do is wipe out all modulators and start over from scratch. I have had this happen a few times, but it is rare, so it may not be the problem here. Just something to keep in mind, though.


----------



## Thonex (Dec 29, 2007)

Nickie Fønshauge @ Sat Dec 29 said:


> However, sometimes the modulation of an instrument gets screwed up for reasons beyond me, and all you can do is wipe out all modulators and start over from scratch. I have had this happen a few times, but it is rare, so it may not be the problem here. Just something to keep in mind, though.



Yeah... I've had that happen before. It's like an invisible K2 crash :D :roll: . I'll be editing an instrument and then all of a sudden I'll notice either really weird modulator behavior or sputtering (like Tod mentioned)... but it is rather rare.

Nickie... I'm sure you know this... (and I thought I knew this)... what is the trick to getting a patch with a lot of CC1 and CC11 modulators to load so that it is NOT silent the first time you play it?? I have to sweep my mod-wheel and expression slider full range to get the patch to "play" and sound properly.

I thought putting the value of 100 (meaning 100%?) in the second input field would do the trick. But it doesn't seem to be working as expected. Sorry for the newbie question.

T


----------



## Nickie Fønshauge (Dec 29, 2007)

Thonex @ 29th December 2007 said:


> I thought putting the value of 100 (meaning 100%?) in the second input field would do the trick. But it doesn't seem to be working as expected.


Well, a value well above 0% does the trick when I do it. Are you still lingering with 2.1.1, Andrew? I am using 2.2.3.


----------



## Thonex (Dec 29, 2007)

Nickie Fønshauge @ Sat Dec 29 said:


> Thonex @ 29th December 2007 said:
> 
> 
> > I thought putting the value of 100 (meaning 100%?) in the second input field would do the trick. But it doesn't seem to be working as expected.
> ...



Thanks Nickie,

I'm using K2.2.3 on my computers which use my custom programming and K2.1 on all my "older stuff" computers.

I've been working on patches that have over 100 groups and it can get a little unwieldy.. I'll have to go back and make sure I really did put those 2nd fields to 100%. :lol: 

T


----------



## Tod (Dec 29, 2007)

I decided to run a test on this this morning useing sine wav test tones. Any sputtering or other problems would definitely show up this way. 

I started of with just two tones (440hz & 880hz) setting up their xfade scaleing useing cc1. I then added cc11 to both of them. I tried all kinds of possible combinations useing cc1 and cc11 but found no sputtering what so ever or any other abnormalities. :?  

I then added 110hz and 220hz to the equation and set all four groups up with their respective xfade scaleing for cc1. Here again, absolutely no abnormalities or sputtering.  

I didn't try mixing things up (changeing some of the modulators around) and if I get the chance I might try that later today.

For me this is good news because I feel now I can add modulators with the confidence that it's supposed to work. o=< 

Tod


----------



## Thonex (Dec 29, 2007)

Tod @ Sat Dec 29 said:


> I decided to run a test on this this morning useing sine wav test tones. Any sputtering or other problems would definitely show up this way.
> 
> I started of with just two tones (440hz & 880hz) setting up their xfade scaleing useing cc1. I then added cc11 to both of them. I tried all kinds of possible combinations useing cc1 and cc11 but found no sputtering what so ever or any other abnormalities. :?
> 
> ...



Tod,

When you get a moment, try to save the patch an reload it and see if you can get it to load so that you can play it "right off the bat" without sweeping the cc1 and cc11 controllers.

Maybe try putting the number 100 in the second data field of the External CC modulators box.

T


----------



## Tod (Dec 29, 2007)

Hi Thonex, check the other ò"   jéž"   jéŸ"   jé "   jé¡"   jé¢


----------

