# Help how to invert knob value



## Claud9 (Sep 18, 2018)

Hello,
I have scripted a knob that controls the sample start. Knob set to zero the sample start playing from the beginning, Knob at its max value the sample starts at 5 seconds. How can I flip it? The knob set at zero sample starts at 5 sec, Knob at its max value sample start at the beginning? I tried to invert the Knob value :
declare ui_knob $sampleStart ( 0,5000000,1) with ( 5000000,0,1) but is not working...
Thanks for any help!


----------



## aaronventure (Sep 18, 2018)

Many ways to do it... I think this is the simplest one.

You can declare a variable called $sampleStartMax and set it to your desired max value for the knob (so that's the only place you change it if it needs tweaking later on), then declare the knob


```
declare ui_knob $sampleStart ( 0,$sampleStartMax,1)
```

Then use


```
abs ($sampleStart - $sampleStartMax)
```

instead of

```
$sampleStart
```

in your on ui_control callback or wherever.

Or declare another variable ($sampleStartInv ?)that is set to that inverted value on callback, and use just that variable everywhere else.


----------



## Claud9 (Sep 18, 2018)

aaronventure said:


> Many ways to do it... I think this is the simplest one.
> 
> You can declare a variable called $sampleStartMax and set it to your desired max value for the knob (so that's the only place you change it if it needs tweaking later on), then declare the knob
> 
> ...


Thanks for your help.
But if I try to follow your instructions and change from 

```
on ui_control $sampleStart
```
to

```
on ui_control abs ($sampleStart - $sampleStartMax)
```

I'm getting a 'Parse Error" in the code...


----------



## aaronventure (Sep 18, 2018)

That's because what you wrote isn't a control.

on ui_control $sampleStart is still correct, since this is the code that will trigger when the $sampleStart control is moved.

Now, inside that callback, wherever you use $sampleStart as a value, use
abs ($sampleStart - $sampleStartMax) instead. Or in the first line assign that to another variable, for example

```
on init

     declare $sampleStartMax := 5000000
     declare $sampleStartInv

     declare ui_knob $sampleStart (0, $sampleStartMax, 1)

end on



on ui_control $sampleStart

    $sampleStartInv := abs($sampleStart - $sampleStartMax)

    {now goes the rest of your code, and you just use $sampleStartInv instead of $sampleStart}

...................
end on
```


----------



## EvilDragon (Sep 18, 2018)

```
on ui_control ($sampleStart)
```

Parentheses are important here.


----------



## aaronventure (Sep 18, 2018)

EvilDragon said:


> ```
> on ui_control ($sampleStart)
> ```
> 
> Parentheses are important here.



Indeed. Spoiled by NL's editor. Sorry @Claud9


----------



## Claud9 (Sep 18, 2018)

aaronventure said:


> That's because what you wrote isn't a control.
> 
> on ui_control $sampleStart is still correct, since this is the code that will trigger when the $sampleStart control is moved.
> 
> ...


Thanks a lot, it works!
I'm using the Groups of my instrument in DFD mode and in order to make the "sample start" knob works I have to put the "S.Mod" value for each zone to his max. The point is that I have plenty of groups and zones, is there a way to change that value for all groups/zone altogether or the only way is that I have to manually change it "zone by zone"... ?


----------



## aaronventure (Sep 18, 2018)

After you select all the zones, click the gear icon


----------



## EvilDragon (Sep 18, 2018)

Claud9 said:


> in order to make the "sample start" knob works I have to put the "S.Mod" value for each zone to his max.



Why to the max? At that point just change the groups from DFD mode to Sampler mode, as the effect is going to be exactly the same.


----------



## Claud9 (Sep 18, 2018)

EvilDragon said:


> Why to the max? At that point just change the groups from DFD mode to Sampler mode, as the effect is going to be exactly the same.


Yes you are right I have to put the amount of time I have in my script. In my case 5 seconds. 
Is this correct to do the conversion from seconds to samples in order to put the correct value in Kontakt? (my samples are recorded @ 44100):
44100X5=220500
I use DFD because in sampler mode Kontakt load all samples in RAM instead in DFD it load only a portion and stream the rest from disk (correct?)


----------



## EvilDragon (Sep 18, 2018)

Claud9 said:


> I use DFD because in sampler mode Kontakt load all samples in RAM instead in DFD it load only a portion and stream the rest from disk (correct?)



Yes and no.

You said you would need to put S.Mod to max, which will basically loads the whole sample into RAM, which is the same as if you were using sampler mode instead of DFD. S.Mod is there to load a larger part of the sample into the RAM. So if your samples are exactly 5 seconds long, and you want to change sample start offset throughout them whole, using sampler mode will be identical to setting S.Mod to max when in DFD mode. So use sampler mode then and you don't need to adjust S.Mod at all.


----------



## Claud9 (Sep 19, 2018)

Thanks you all for the help, I have integrated this script in my two layers instrument but now I have the problem that my "Mute, Solo" script is not working anymore... Any suggestion why? 

```
on note

{SAMPLE START}

ignore_event ($EVENT_ID)
disallow_group($ALL_GROUPS)

$i := $LAYER1_FIRST
while ($i<=$LAYER1_LAST)
allow_group($i)
inc($i)
end while
play_note($EVENT_NOTE,$EVENT_VELOCITY,$sampleStart_1,-1)

$i := $LAYER2_FIRST
while ($i<=$LAYER2_LAST)
allow_group($i)
inc($i)
end while
play_note($EVENT_NOTE,$EVENT_VELOCITY,$sampleStart_2,-1)

 
{LAYER SOLO MUTE}
 
if ($layer1_solo = 1)
disallow_group($ALL_GROUPS)
$i := $LAYER1_FIRST
while ($i<=$LAYER1_LAST) 
allow_group($i)
inc($i)
end while
 
end if
 

if ($layer1_mute = 1)
$i := $LAYER1_FIRST
while ($i<=$LAYER1_LAST) 
disallow_group($i)
inc($i)
end while
end if

if ($layer2_solo = 1)
disallow_group($ALL_GROUPS)
$i := $LAYER2_FIRST
while ($i<=$LAYER2_LAST) 
allow_group($i)
inc($i)
end while
 
end if
 

if ($layer2_mute = 0)
$i := $LAYER2_FIRST
while ($i<=$LAYER2_LAST) 
disallow_group($i)
inc($i)
end while
end if
```


----------



## andreasOL (Sep 19, 2018)

Things you do below "{LAYER SOLO MUTE}" have no effect. The groups allow/disallow state is taken at the point in the script where you issue a play_note(...). So you must set up the groups to be used before each play_note.


----------



## Claud9 (Sep 19, 2018)

andreasOL said:


> Things you do below "{LAYER SOLO MUTE}" have no effect. The groups allow/disallow state is taken at the point in the script where you issue a play_note(...). So you must set up the groups to be used before each play_note.


Thanks for the help. But if I try to put all together before the play_note is working fine only when I "solo" the layer otherwise none of the layers are working...

```
on note

 

ignore_event ($EVENT_ID)
if ($layer1_solo = 1)
disallow_group($ALL_GROUPS)

$i := $LAYER1_FIRST
while ($i<=$LAYER1_LAST)
allow_group($i)
inc($i)
end while


play_note($EVENT_NOTE,$EVENT_VELOCITY,$sampleStart_1,-1)
end if
```


----------



## andreasOL (Sep 19, 2018)

Well, this is a bit like a 101 of programming. You must get a clear picture about which are the conditions for what. In your example above, if $layer1_solo is 0, then the if-clause is not executed, which you have reported correctly. The condition you have programmed is "play groups LAYER1_FIRST to LAYER1_LAST if button solo1 is pressed". But that's not what you really intend. You want to "play groups LAYER1_FIRST to LAYER1_LAST (if button solo1 is pressed or if no solo button at all is pressed) and if button mute1 is not pressed". So the if could be written as


```
if ((($layer1_solo = 1) or ($layer1_solo = 0 and $layer2_solo = 0)) and ($layer1_mute = 0))
```

I personally would do it differently (count solo buttons and play if none is pressed or the correct one is pressed and mute is not pressed) but this is another step.


----------



## Claud9 (Sep 19, 2018)

andreasOL said:


> Well, this is a bit like a 101 of programming. You must get a clear picture about which are the conditions for what. In your example above, if $layer1_solo is 0, then the if-clause is not executed, which you have reported correctly. The condition you have programmed is "play groups LAYER1_FIRST to LAYER1_LAST if button solo1 is pressed". But that's not what you really intend. You want to "play groups LAYER1_FIRST to LAYER1_LAST (if button solo1 is pressed or if no solo button at all is pressed) and if button mute1 is not pressed". So the if could be written as
> 
> 
> ```
> ...


Thanks It works!


----------

