# Need linear volume knob from 0 to 100 percent (done)



## Julian Ray (Oct 11, 2018)

Hello,

I am working on my first Kontakt Instrument and I'd like my custom volume knob to be linear and change volume from 0% to 100%. Is it possible to achieve and where should I start?

Thank you!


----------



## Tod (Oct 12, 2018)

Sure, what dB level do you want at 0% as well as dB level at 100%? Do you want fractions in 100ths like 72.34% or maybe just 10ths, 72.3%? 

I think For a good answer to your question, it would be important to know the answer to those questions.


----------



## JEPA (Oct 12, 2018)

Normally in digital 100% equals 1 and 0% = 0, where 1.1 is distortion, better 0.9


----------



## Tod (Oct 12, 2018)

Hi Julian, I did this with a script a while back so I put together some simple code that might get you started.

You don't need C1 or C2, I just thought it was easier to read by using them. This will use the full scope of the Amplifier Volume knob. About 63.0% will give you 0.0dB.

```
on init
  declare C1
  declare C2
  declare ui_knob $Knob (0,1000,1)
    C1 := $Knob / 10
    C2 := $Knob mod 10
    set_engine_par($ENGINE_PAR_VOLUME,$Knob*1000,0,-1,-1)
    set_knob_label ($Knob,""&C1&"."&C2&"%")
    move_control ($Knob,1,1)
end on

on ui_control ($Knob)
  C1 := $Knob / 10
  C2 := $Knob mod 10
  set_engine_par($ENGINE_PAR_VOLUME,$Knob*1000,0,-1,-1)
  set_knob_label ($Knob,""&C1&"."&C2&"%")
end on
```

Edit: I just got to thinking, I did this for Kontakt 5.5, some of the later versions might treat the "C1 := $Knob / 10" a little differently. I haven't used the later versions for scripting yet, but they might use fractions, I'm not sure.


----------



## Julian Ray (Oct 12, 2018)

Thanks for prompt replies, guys!
Tod, thank you for the script sample - appreciate it!
Now I see how I can solve the percentage. I'm not sure, however, about 'exp to linear' conversion, because when I rotate UI $Knob the volume knob of amplifier rotates absolutely same way.
Or, maybe I just didn't get it right :D


----------



## Tod (Oct 12, 2018)

I think the Amplifier volume knob is actually exponential, although I'm not 100% sure of that. However, I don't think it will make any difference, the script knob shouldn't change the way the amplifier knob works, your just basically changing the way it reads out on the script knob.

The Amplifier knob has a high of +12dB and with the script knob at 63.0% the Amplifier knob shows 0.0dB which is as it should be.


----------



## Julian Ray (Oct 12, 2018)

I see, thank you!
I have two sound sources, so, I think 0.0dB for each should be max.


----------



## Tod (Oct 12, 2018)

There are a couple of ways you can do that, this is a simple way.


```
on init
  declare C1
  declare C2
  declare ui_knob $Knob (0,1000,1)
    C1 := $Knob / 10
    C2 := $Knob mod 10
    set_engine_par($ENGINE_PAR_VOLUME,$Knob*1000,0,-1,-1)
    set_knob_label ($Knob,""&C1&"."&C2&"%")
    move_control ($Knob,1,1)
end on

on ui_control ($Knob)
  if ($Knob*1000 > 630000)
    $Knob:=630
  else
    C1 := $Knob / 10
    C2 := $Knob mod 10
    set_engine_par($ENGINE_PAR_VOLUME,$Knob*1000,0,-1,-1)
    set_knob_label ($Knob,""&C1&"."&C2&"%")
  end if
end on
```

Incidentally, I did a little testing and the Amplifier volume is not exponential. I'm not sure what it is, it's not exactly linear, but it's much closer to linear then it is exponential.


----------



## Julian Ray (Oct 12, 2018)

Hi Tod,
Thank you again !
Amplifier... Well, until now I was sure it is exponential :D


----------



## EvilDragon (Oct 13, 2018)

Tod, you're wrong  ENGINE_PAR_VOLUME is indeed scaled exponentially (because volume is logarithmic). Here's how engine par values to real world values curve looks:







So yes, if you want to adjust volume as %, so that it's linear (at 50.0% you'd get -6 dB), you would have to linearize the curve. And this is how you do it:


```
on init
    declare ui_knob Vol (0, 1000, 10)
    set_knob_unit(Vol, KNOB_UNIT_PERCENT)
end on

function linearize_vol_ep(x) -> return
    return := real_to_int(pow(250000.0 * int_to_real(x), 1.0 / 3.0) * 1000.0)
end function

on ui_control (Vol)
    set_engine_par($ENGINE_PAR_VOLUME, linearize_vol_ep(Vol), 0, -1, -1)
end on
```


----------



## Tod (Oct 13, 2018)

Okay Mario, but I was going by the test I made. In the picture below, I went from -inf to -3dB using midi controllers. 

You can clearly see the difference, the Amplifier Volume is quite abrupt but still slightly rounded. The Instrument Volume has what I call an exponential curve.

I did the CC11 too, and in comparison it was totally abrupt, immediately on. I made some extensive tests about this back in 2005 to 2007, and they're all posted in this forum somewhere, I looked for them but couldn't find them.


----------



## EvilDragon (Oct 13, 2018)

Did you double-check that the range of MIDI CC #7 is set to maximum when you did the comparison?

In any case, the engine parameter is what matters here, and that curve is clearly exponential.


----------



## Julian Ray (Oct 13, 2018)

Thank you very much EvilDragon - it works perfectly!
On 50% of UI knob I get -6dB of Amplifier Volume.
Thank you all again


----------



## Tod (Oct 13, 2018)

Yeah, the Instrument Volume was at 0.0dB.

I tried your script in Kontakt 7.1 but I got an error when I hit Apply. The "declare ui_knob Vol (0, 1000, 10)" was high lighted and the Status line said: "ERROR (line 3) variable expected". Everything looks okay, but like I said above, I'm still working with Kontakt 5.5 and I haven't delved into the math in the later versions.

I wanted to make some more tests using the script I made and the one you made to see the differences.

Thanks Mario.


----------



## Julian Ray (Oct 13, 2018)

Tod, it worked fine with Sublime for me.


----------



## Tod (Oct 13, 2018)

Thanks Julian, and yes, I used Sublime for this too. What version of Kontakt do you have, I had the problem with 7.1, I haven't updated to latest version yet?


----------



## Julian Ray (Oct 13, 2018)

My Kontakt version is 5.8.1


----------



## EvilDragon (Oct 13, 2018)

Tod said:


> but like I said above, I'm still working with Kontakt 5.5 and I haven't delved into the math in the later versions.



The script is using new floating point math and math functions from K5.6 onward.


----------



## Tod (Oct 13, 2018)

Humm, what do you think the problem could be Mario? Sublime had no problems with it.


----------



## EvilDragon (Oct 13, 2018)

Because Sublime supports the new commands, but your K5.5 doesn't.


----------



## Tod (Oct 13, 2018)

Ha ha, but I did it with 7.1 not 5.5.


----------



## EvilDragon (Oct 13, 2018)

Then you did something wrong, it all works fine here in any version past 5.6 and including it


----------



## Tod (Oct 13, 2018)

Okay thanks much Mario, I'm not sure what I could have done wrong. I copied directly from your post into Sublime, compiled it (F5) and then loaded it into Kontakt 7.1.

I'll try it again tomorrow and see what happens, I'd really like to test things out.


----------



## Julian Ray (Oct 13, 2018)

As my Volume knob is working linear now (from 0 to 100%), I'm trying to understand how I could display it's value (in %) in the main_label field. I've tried to use function inside of "set_control_par_str", but it doesn't work.
So, is there any way to get value from UI controller and display it in the label field? Nothing about it in KSP reference manual though.
Thanks in advance!


----------



## EvilDragon (Oct 14, 2018)

Sure just use the name of the control. Since it goes from 0-1000, it'd be something like this:


```
set_knob_label(Vol, Vol / 10 & "." & Vol mod 10)
```


----------



## Tod (Oct 14, 2018)

Okay just got up and I'm going to give it another try Today.



EvilDragon said:


> ```
> set_knob_label(Vol, Vol / 10 & "." & Vol mod 10)
> ```


Maybe add a little more to that??

```
set_knob_label(Vol, Vol / 10 & "." & Vol mod 10 & "%")
```
[/QUOTE]


----------



## Julian Ray (Oct 14, 2018)

Thank you again 
Will it work with the custom slider as well?
I have a custom volume knob Vol_A and main_label field to display values of all knobs using "set_control_par_str". But as Vol_A value is changed by exp to linear function, you've suggested, I can't get value from the amplifier's volume knob anymore (as it is an exponential). So, I was thinking if I could get these values to display in main_label right from my custom knob Vol_A. Can I?
Thanks again!


----------



## EvilDragon (Oct 14, 2018)

Tod said:


> Maybe add a little more to that??



If it's a ui_knob as in my example code, no need, because you can use $KNOB_UNIT_PERCENT.



Julian Ray said:


> Will it work with the custom slider as well?



Yes, exact same code, but in this case do add " %" in the end, as Tod wrote above.



Julian Ray said:


> But as Vol_A value is changed by exp to linear function, you've suggested, I can't get value from the amplifier's volume knob anymore (as it is an exponential).



Sure you can, just like you would do ordinarily! get_engine_par_disp($ENGINE_PAR_VOLUME, <group>, -1, -1)


----------



## Tod (Oct 14, 2018)

Okay, I tried it again today, I recopied Mario's code above, compiled it in Sublime, and pasted it in to Kontakt, but no go, I still got the error. 

I also pasted the compiled code back into Sublime and it was exactly the same as the un-compiled code, should that be Mario?


----------



## EvilDragon (Oct 14, 2018)

I've no idea what's happening there. It works here perfectly fine.

Screenshot of the error, and please show code in Kontakt's script editor area?


----------



## Tod (Oct 14, 2018)

Here you go.


----------



## EvilDragon (Oct 14, 2018)

Yeah you didn't compile the script at all!  Vol should end up being $Vol, etc.


----------



## Tod (Oct 14, 2018)

Humm, I did compile it in Sublime, I should have looked closer.

Actually Sublime is not compiling the code at all. I added the dollar signs and pressed F5, but it didn't compile it. When I tried to paste into Kontakt there was nothing there, blank. When I pasted into Kontakt before, it must have been on the clipboard.

So I manually fixed the "$Vol" problem, copied and pasted into Kontakt, but still get an error, this time on the 3rd line. So I'm assuming it has to be compiled but my Sublime won't compile it, and I haven't a clue why? 

Could you compile and post it Mario, so I could at least see it. I just reinstalled Sublime a few months ago, and it's been compiling everything else.


----------



## EvilDragon (Oct 14, 2018)

You might have a broken installation of SublimeKSP then. It must work. Reinstall.

There are $ missing elsewhere, and Kontakt doesn't support functions with returns like there. 


```
on init
    declare ui_knob $Vol(0, 1000, 10)
    set_knob_unit($Vol, $KNOB_UNIT_PERCENT)
end on

on ui_control($Vol)
    set_engine_par($ENGINE_PAR_VOLUME, real_to_int(pow(250000.0 * int_to_real($Vol), 1.0 / 3.0) * 1000.0), 0, -1, -1)
end on
```


----------



## Tod (Oct 14, 2018)

Aah, okay I thought it might something like that, the script is working good now.

Back when Bob and I were working together he was using the "return" function in some of our later projects, but I never got a chance to ask him about it.

Thanks Mario. Do you know what's involved with reinstalling Sublime, do I have to uninstall first? I think I got the one I'm using from Nils web site.


----------



## EvilDragon (Oct 14, 2018)

https://github.com/nojanath/SublimeKSP

This is the latest sKSP, it's always here. You have instructions here as well.


----------



## Tod (Oct 14, 2018)

Thanks agian Mario, what would I do without you.


----------



## Julian Ray (Oct 14, 2018)

Thank you, EvilDragon and Tod!

set_knob_label(Vol, Vol / 10 & "." & Vol mod 10 & "%")

I've read everything I could find about set_knob_label, but, unfortunately, I can't understand how I should display value of my custom Volume knob in my main_label (where values of focused UI knobs are displayed).


----------



## EvilDragon (Oct 14, 2018)

If it's just a regular ui_label, then you'd just use set_text() instead.


----------



## polypx (Oct 15, 2018)

Are K6 commands in there yet Mario?


----------



## EvilDragon (Oct 15, 2018)

Not yet, but check Kontakt Centercode in Discussion section, DIS-00060


----------



## Julian Ray (Oct 15, 2018)

How do you guys put code into post?


----------



## EvilDragon (Oct 15, 2018)

Use CODE tags.


----------



## Julian Ray (Oct 15, 2018)

```
Thank you :D
```


----------



## EvilDragon (Oct 15, 2018)

polypx said:


> Are K6 commands in there yet Mario?



Nabeel has updated the main sKSP repo, it should be available via Package Control soonish.


----------



## Tod (Oct 15, 2018)

I took some time today to make the tests with the 2 scripts, along with the Instrument volume and Amplifier volume. This might be a little OT, but I thought it would be interesting to see what the results for these 2 scripts might be and I wanted to share them. Also there was some concern about exponential, which is not definitive in itself so I'm not sure what the connotations were. 

The top picture is the main Instrument volume (CC7), the 2nd is the results of Mario's script, the 3rd is the script I made and the 4th is the actual Amplifier volume. This test was made with a BPM of 120 and a midi envelope 1/16th long, as shown at the bottom. The envelope was adjusted for each controller so that the maximum resultant volume was -1.0dB.

I found the results to be not only interesting, but rather unexpected, at least for me. It appears that Mario's code results in a linear appearance, while mine basically mirrored the Amplifier volume. 

I also tested CC11, but it turned out to be linear just like Mario's image. I wish I had a little more time, I'd make some more tests with scripted code.

I might add though, I don't really think about any of this when I'm programming midi, I adjust all my controllers according to what I hear.


----------



## Julian Ray (Oct 15, 2018)

So, the final working piece looks like this (essential part):
(it converts amplifier's Volume values to percents for UI custom knob "Vol_A" and displays % values in the main_label field)

```
function exp_to_linear(x) -> return
    return := real_to_int(pow(250000.0 * int_to_real(x), 1.0 / 3.0) * 1000.0)
end function

on init
    declare Vol_A_val
    declare ui_label main_label(1, 1)
    declare_knob(Vol_A,... this is a macro)
end on

on ui_control(Vol_A)
    set_engine_par($ENGINE_PAR_VOLUME), exp_to_linear(Vol_A), 0, -1, -1)
    Vol_A_val := (get_control_par (Vol_A, $CONTROL_PAR_VALUE))/10
    set_control_par_str(get_ui_id(main_label)$CONTROL_PAR_TEXT, Vol_A_val & "%")
end on
```

EvilDragon & Tod - Thanks for the help!


----------



## EvilDragon (Oct 15, 2018)

Tod said:


> It appears that Mario's code results in a linear appearance



Of course, because it is setting volume as a percentage, _linearly_, not exponentially, as it is by default. This is to be expected. You wouldn't use this for doing dynamics adjustments, but you would totally use this to, for example, mix two oscillators together, because % makes more sense than dB in this case.


----------



## Julian Ray (Oct 15, 2018)

EvilDragon said:


> ... but you would totally use this to, for example, mix two oscillators together, because % makes more sense than dB in this case.


Exactly what I was needed it for - I have two sound sources (groups) and wanted their volumes to be in %.


----------



## Tod (Oct 15, 2018)

EvilDragon said:


> Of course, because it is setting volume as a percentage, _linearly_, not exponentially, as it is by default. This is to be expected. You wouldn't use this for doing dynamics adjustments, but you would totally use this to, for example, mix two oscillators together, because % makes more sense than dB in this case.



Okay Mario, I wasn't clear on that, there was quite a bit of discussion about being exponential so I thought that's what you were trying to do. That and the fact I don't know anything about the new K5.6 math functions. 

@Julian, that's looking pretty good. Are the bigger circles your volume controls?


----------



## Julian Ray (Oct 15, 2018)

@Tod Thanks Tod, glad you like it  Yes, red and blue entities are volume controls and now the info label at the center are showing 67% for the red one. So, it works perfectly!


----------

