Adding a Knob to Your UI
What is a Knob?
The knob control is often a bread and butter control, something that you will find yourself using in many scenarios. While, funny enough, when you start creating your own UI knobs, you won’t actually be using this knob control, it is still very handy if you want a basic control and has many functions that are great to learn for other controls.
Declaring a Knob
A knob is declared in the on init callback (see this article). It creates a UI knob variable (see this article) and that is then attached to a function later on. In the example below, we can see a knob being declared and then positioned on the UI. The title for the knob is set with the set text command and the make persistent command is preserving its position even when the library reinitialises (such as reopening a DAW session with this library in it).
When declaring the knob, the declare command is used and then the ui knob type is selected. The variable is then given a name, in this case $Vol. The UI Knob then has 3 parameters that need to be set in the brackets (<min>, <max>, <display-ratio>). The minimum and maximum values can be between 0 and 1,000,000. If you want to control only a portion of the parameter it is attached to later, you can set these values to be target that set range. For example, below the max value is 629,000. This is so the volume only controls up to 0.0dB, not the full +12dB it can go to. The display ratio is a divider for the parameter read out (the value of the variable) and is often best set to 1 to show the exact value the variable knob is currently set to.
on init make_perfview {Creating the performance view.} set_ui_width_px(580) {Set UI Width} set_ui_height_px(300) {Set UI Height} set_ui_color(9767676h) {Set UI Colour} {knob} declare ui_knob $Vol (0, 629000, 1) {Declare the Knob} make_persistent($Vol) {Make Persistent} move_control_px($Vol, 100, 140) {Position on the UI} set_text($Vol, "Volume") {Set the Title of the Knob to Volume} end on
Adding Functionality to the Knob
In order to then make the knob control something, and on ui control callback is used. The callback runs the code anytime that knob is moved, so that the parameter is moved whenever the on screen control is. In the example below, the set engine par function (see this article) has been used to tie the knob to the first group’s volume control.
At this point, you can also update the knob label, so it reads out the current value of the parameter, by using the get engine par disp function (see this article) inside of the set knob label command. This means you can then see the value of the parameter you are adjusting (for example, decibels on the volume control) rather than just the integer values of the knob between the minimum and maximum numbers (eg. 203476). In the example below, you can see this also being used.
on ui_control ($Vol) set_engine_par ($ENGINE_PAR_VOLUME, $Vol, 0, -1, -1) {Controlling the Volume} set_knob_label ($Vol, get_engine_par_disp ($ENGINE_PAR_VOLUME, 0, -1, -1)) {Setting the knob label} end on