# Analogue VU Meter



## Sasje (Feb 10, 2013)

Hi, 

I fiddled around with Kontakt and came up with this basic analogue VU meter.







It only responds to the Velocity value. 
What I really want is show the db level or at least the sample volume as it is played,
but I don't know how. Does anyone know to get the db value? I've searched 
through all my K4, K5 manuals, but haven't found a a solution yet.

Here is the current test script, it's very crude and basic.


```
on init
    make_perfview 
    set_ui_height(4) 
    declare $cn :=0
    declare ui_slider $knob (1,39)
    set_control_par_str(get_ui_id($knob),$CONTROL_PAR_TEXT,"")
    set_control_par_str(get_ui_id($knob),$CONTROL_PAR_PICTURE,"peak")
    set_control_par(get_ui_id($knob), $CONTROL_PAR_MOUSE_BEHAVIOUR, -500)
    set_control_par(get_ui_id($knob), $CONTROL_PAR_POS_X, 60)
    set_control_par(get_ui_id($knob), $CONTROL_PAR_POS_Y, 36)
end on

on note 
	while($cn < $EVENT_VELOCITY / 3) 
		$knob := $cn
		inc($cn)
	end while
end on

on release
	while($cn > 0)
		$knob := $cn
		wait(15000)
		dec($cn)
	end while
end on
```


----------



## mk282 (Feb 10, 2013)

That is not possible, sorry. KSP does not have any command to retrieve the dB level of instrument output

In Kontakt 5 you can use ui_level_meter, which is a new UI control, but you cannot skin it in any way (just change colors) - it's your regular vertical (well you can turn it to be horizontal) digital VU meter. More or less similar to the VU meter you can see above the volume slider in instrument header.


----------



## Sasje (Feb 10, 2013)

Aw, that's a pity... :( 

Any chance of getting the waveform data? That would be really awesome!  

I think the Velocity could suffice for now, if it's just an ornamental piece of the instrument. :D


----------



## Chriss Ons (Feb 10, 2013)

Sasje @ Sun 10 Feb said:


> I fiddled around with Kontakt and came up with this basic analogue VU meter.



I'm going to give this a go... looks spiffy! Is it free to use?



mk282 @ Sun 10 Feb said:


> In Kontakt 5 you can use ui_level_meter, which is a new UI control, but you cannot skin it in any way (just change colors) - it's your regular vertical (well you can turn it to be horizontal) digital VU meter. More or less similar to the VU meter you can see above the volume slider in instrument header.



...and on top of that you can only use it on the master outputs, I believe, not anywhere else in the signal path. On the plus side, the meter is (somewhat) more legible than the one in the upper right corner, as it's slightly larger. I tried implementing it in something I'm working on, came out looking like this:


----------



## Sasje (Feb 10, 2013)

Josquin @ Sun Feb 10 said:


> I'm going to give this a go... looks spiffy! Is it free to use?


Go ahead 



Josquin @ Sun Feb 10 said:


> ...and on top of that you can only use it on the master outputs, I believe, not anywhere else in the signal path. On the plus side, the meter is (somewhat) more legible than the one in the upper right corner, as it's slightly larger. I tried implementing it in something I'm working on, came out looking like this:


Looks very nice, lovely GUI!

.


----------



## Chriss Ons (Feb 10, 2013)

Thank you.
If I use it in something (non-commercial) I'll report back to you


----------



## Mike Greene (Feb 10, 2013)

Really nice looking, Josquin. 8)


----------



## Chriss Ons (Feb 10, 2013)

Hey thanks Mike. Knobs and switches are Knobman, so that wasn't too hard - except getting them to _work_, of course :mrgreen: 
(Trying to learn some KSP... that GUI is from a freebie thingie I'm working on, called _ORA _- I'll throw it on here... one of these days.)


----------



## Big Bob (Feb 10, 2013)

Hi Sasje,

Welcome to the forum.



> What I really want is show the db level or at least the sample volume as it is played,
> but I don't know how. Does anyone know to get the db value? I've searched
> through all my K4, K5 manuals, but haven't found a a solution yet.



There is a known mathematical relationship between velocity and dB (provided you normalize it by specifying the dB value you want to correspond with Velo = 127). 

I haven't taken the time yet to study your script but does your needle animation respond linearly to the control value you are driving it with? If so, all you would need is a suitable format converter which I might be able to help you with.

Let me know if you'd like to give this a go and I'll rummage through my old notes to see if I can find what I wrote about the velocity transfer function in Kontakt (unfortunately it was several years ago so I'm not sure if I can find it). :roll: 

Rejoice,

Bob


----------



## mk282 (Feb 11, 2013)

Josquin @ 10.2.2013 said:


> ...and on top of that you can only use it on the master outputs, I believe, not anywhere else in the signal path.



You can attach it to outputs of instrument busses as well, not just master output.


----------



## Sasje (Feb 11, 2013)

Big Bob @ Sun Feb 10 said:


> Hi Sasje,
> 
> Welcome to the forum.
> 
> ...


Hi Bob, thanks,

I currently do this: Needle position = (Velocity / 3), which gives me a number to let the needle run through the animation, which has 31 possible positions. 
Would be a great idea to correlate dB value to the velocity, if possible at all? It doesn't have to be extremely exact, a close approximation is well enough. 
Most real world VU meters aren't that great either.  

.


----------



## Sasje (Feb 11, 2013)

I think I could get away with something like this:

$dB = 20log($EVENT_VELOCITY / 10) 

Problem is, there isn't a log() function in KSP :( 

Not sure how to do it otherwise than to create a table? because I can't even do basic exponentiation in KSP. Really weird that they didn't implement some basic Math functions in KSP... annoying to say the least.

Based on the meter I have, I'm think about doing it like so:

0 =-20
11 =-10
22 =-7
33 =-5
44 =-3
55 =-2
66 =-1
77 =0
99 +1
121 =+2
143 =+3


----------



## Big Bob (Feb 11, 2013)

Hi Sasje,



> Problem is, there isn't a log() function in KSP



You may be interested in downloading my KSP Math Library:

http://www.bigbobsmusicworld.com/kontak ... th-library

Although for the simple tablulation you have posted, a simple lookup table should be quite adequate. Nevertheless, there are situations for which one needs to do such things as logs and exponentials, etc so you still may want to have my library on hand.

Meanwhile, if I get some time later today I'll take a closer look at your at your script and the demo .nki and maybe I can cobble something together for you that will do the job.

But, I should point out that while the velocity of a single note can be rather easily related to dB, such is not the case for overall polyphonic volume output of an instrument. Are you just wanting to display an appropriate dB level for a 'one note at a time situation' or are you hoping to have the VU meter increase appropriately when additional notes are added? What about the other modulators of volume such as CC11 and CC7, etc. (not to mention the intensity slider settings used with velocity, CC7, and CC11 as modulators)

If you actually want the VU meter to show something similar to the overall instrument output volume level, the math will of course have to take all these things into account and will thus be much trickier to implement. Nevertheless, it should be doable, especially if your not expecting super accuracy.

To be continued ...

Bob


----------



## Sasje (Feb 11, 2013)

That's an interesting library, I'm going to toy with it :D 

About the VU Meter: currently it works per note, sometimes the needle drops when you quickly press another. I think it would be best if the needle responds to every note as it is pressed. I'm also thinking about way to emulate the "bounce" effect of the magnet in a VU meter. Faking a sustained note, maybe with random() function? Not sure yet.


----------



## Big Bob (Feb 11, 2013)

Hi Sasje,

 I'm also thinking about way to emulate the "bounce" effect of the magnet in a VU meter. Faking a sustained note, maybe with random() function? Not sure yet.

Sounds like a fun project, I'll be interested in seeing what you come up with when it's fully cooked. However, you may want to adjust the radius of curvature of your needle graphics to agree better with the curvature of the VU meter scale :lol: 

About the VU Meter: currently it works per note, sometimes the needle drops when you quickly press another. I think it would be best if the needle responds to every note as it is pressed.

Let me see what I can come up with. I'll try to post something for you later today, the Good Lord willing. 

But, in the meantime, since you are getting involved in Kontakt's volume control and display machinery, you may also find my *Volume Control Stud*y of some interest.

You can find it here:

http://www.bigbobsmusicworld.com/kontakt-documents

To be continued ...

Bob


----------



## Sasje (Feb 11, 2013)

Big Bob @ Mon Feb 11 said:


> Hi Sasje,
> However, you may want to adjust the radius of curvature of your needle graphics to agree better with the curvature of the VU meter scale :lol:



Indeed, it's a bit off. :D It simply rotates from -80° to 80°. The animation is a line that I've made in "Knobman". Maybe I can do something with the x,y position? Otherwise I'll have to animate the needle by hand, which I will do eventually in definitive version. 



Big Bob @ Mon Feb 11 said:


> Let me see what I can come up with. I'll try to post something for you later today, the Good Lord willing.
> 
> But, in the meantime, since you are getting involved in Kontakt's volume control and display machinery, you may also find my *Volume Control Stud*y of some interest.
> 
> ...



Nice I'll have a read. It's a fun project, and love to figure things out on my own, that's part of the joy :D

*Edit to add:* Very nice site, lots of interesting papers and scripts.  

.


----------



## Big Bob (Feb 11, 2013)

Hi Sasje,



> It's a fun project, and love to figure things out on my own, that's part of the joy



I agree and I assure you I'll leave you plenty of fun things to finish. :lol: I just intend to supply some of the math that relates Velocity (and other volume modulators) to the output volume in dB.

But, I've kind of run out of 'fun stuff' time for today so I guess I'll have to carry this over into tomorrow (and maybe beyond) :oops:

I dug up all the notes I needed and I have most of the math worked out now. But I still need to formulate the transfer function to drive the VU meter itself. And there I ran into a snag.

The VU meter scale image you are using and/or the needle graphics seem to be a bit off of what I would expect. Normally the -3dB position should be half-way between the -20dB and +3dB positions. But the meter graphic you used seems to have the mid-way point at about -2dB :? 

With 31 animation frames, numbered from N = 0 to N = 30, if we assign N = 0 to -inf and N = 30 to 'Pegged', we should be able to assign N = 1 to -20dB and N = 29 to +3dB. This more or less lines up correctly with the meter graphics. And, if the animation frames are uniformly spaced over the -20 dB to +3 dB arc, and, if the meter scale has the correct non-linearity, the following formula should relate VU to animation frame number between these values:

VU = 15.72*Log(N) - 20 dB

However, in actual fact (for the various frames between 1 and 29) the indicated VU is often quite far off. This may all be due to the VU scale image you used unless you also didn't make the angular spacing uniform (maybe in an effort to accommodate the meter scale?) But, since you made the needle graphics in Knobman, I would presume they are spaced uniformly. If so, the main problem must be the meter scale itself. Would you like to get a less distorted VU meter image first (one with -3dB at the 50% point) or would you like me to try to accommodate the one you have now with a couple of fudge factors?

I'll check the forum for any response from you before I continue with this tormorrow.

To be continued ...

Bob

BTW I forgot to mention that positioning your needle graphic at about 67,80 seems to align it better with the VU meter scale curvature. But of course the needle would then have to be lengthened to reach the scale. With the needle graphic positioned at 67,80, animation frames 5 and 24 just about line up with -20dB and +3dB respectively. Since a lot of frames will then be devoted to under/and overflow, it seems like maybe the graphic should be redone with the proper angle of arc and radius of curvature so that -20 and +3 again line up with N=1 and N=29. Try positioning $knob at X=67 and Y=80 and rotate it from frame 5 thru 24 and I think you'll see what needs to be done.


----------



## Big Bob (Feb 12, 2013)

Hi Sasje,

For phase one, let's do the math for the Meter driver. When we do the Velocity to dB stuff, we will be generating the equivalent Volume output in mdb so you will need a meter driver that can accept input volume in mdb and display the corresponding VU reading by selecting the appropriate animation frame, N.

The general relationship between VU in dB and N is given by the following formulae:


```
(1) VU = K*Lg[N - n20 + 1] - 20     where Lg is the binary log  and K = 23/Lg[n3 - n20 + 1]
                                                  n20 and n3 are the animation frames corresponding
                                                  with -20VU and +3VU respectively

solving (1) for N, we obtain:

(2) N = n20 - 1 + 2^[(VU + 20)/K]
```

I've uploaded a modified version of your VU.rar file which I named VU#2 and you can find it here:

https://dl.dropbox.com/u/80404485/Kontakt/VU/VU%232.rar

I've added a new demo instrument named Demo#1.nki. This instrument contains two scripts in the 1st and 2nd tabs and you can find the source code for these scripts in the KSE-Source folder. These scripts illustrate how you can implement the above equations using my Math Library. Of course, if you are only going to use 31 animation frames, you might find it easier to just pre-compute the N values for the various dB input ranges and use a simple array as a lookup table. But, personally, I would find that to be more work. :lol: However, if you decide to use more animation frames (which might be a good idea), eventually the lookup table idea will become less appealing so it may be good to know how to do it with library functions. 

For these demos, you can set a dB input value with the knob (upper right corner) and observe what the VU meter will display for that input volume.

The reason I did two versions of the Meter driver was to illustrate some things about the graphics misalignment. The 1st version uses your original positioning of the needle graphic at 60,36 while the 2nd positions the needle graphic at 66,80. This position brings the radius of curvature more in line with the meter graphics but of course the needle is too short to reach the scale. Secondly, there are more wasted animation frames because -20VU falls at N=5 and +3VU falls at N=24. So, if you used 160 degress of rotation to make the needle graphic, I would estimate that the arc between -20VU and +3VU to be about 103 degrees. But, to allow some overtravel on both ends (say one animation frame each), a rotation angle of about 115 degrees might be suitable.

You will notice that when the radii are nearly matched (as for the 2nd script), the tracking is quite a bit better. But, it's still not perfect so you may want to use a fudge factor with the above functions (or find another meter graphic). I also think it might be quite desireable to increase the number of animation frames.

After you have a chance to fool around with this a bit, I will begin to post the remaining math functions you will need to convert a played note at a given velocity to a corresponding volume in mdb.

Rejoice,

Bob


----------



## Sasje (Feb 12, 2013)

Oh my, that is some amazing scripting right there! ...I'm a bit more practical :D 

I went on a bit further, and it turns out, I was pretty accurate on doing a simple division of: $EVENT_VELOCITY / 3 :lol: 

I now use: $EVENT_VELOCITY / 4 which gives me 127 / 4 = 31.75. Near the exact amount of layers we got.

Because:

VELO 0 =-20 Needle position: 5
VELO 11 =-10 Needle position: 8
VELO 22 =-7 Needle position: 12
VELO 33 =-5 Needle position: 15
VELO 44 =-3 Needle position: 18
VELO 55 =-2 Needle position: 20
VELO 66 =-1 Needle position: 23
VELO 77 =0 Needle position: 25
VELO 99 =+1 Needle position: 28
VELO 121 =+2 Needle position: 31


The needle position is based upon the animation slides, which currently only have 31 possible states. Increasing the amount of animated slides will indeed really help the accuracy. I took the number 10 as an arbitrary number for setting the dB peaks, giving us 11 measurements for the averaging peaks on the meter. If a velocity falls between two peaks, it simply shifts the animation between 31 possible values, being at the mercy of Kontakt's velocity accuracy.

Here is my current script. As far as I can tell, it approximately nears the set values. Since I cannot get the actual dB anyway, I thought this would suffice. It also does some needle bouncing on a note held, and on a release. I must finetune these a little bit further, but it works basically as I intend. I also set the x,y position of the needle a bit better. I came up with 64, 55. And made the start position on a velocity value of 5. It still does run a tiny bit over the midsection, but I am thinking about simply shifting the Y position when it reaches between -3 and -1. Fairly straightforward approach I guess, albeit a bit hacked together. :wink: 


```
on init
	message("")
	make_perfview 
	set_ui_height(4) 
	declare $rd
	declare $velo
	declare $vol 
	declare $dur
	declare $poly
	declare $voices
	declare $ms0 := 10000
	declare $ms1 := 11000
	declare $ms2 := 12000
	declare $ms3 := 15000
	declare $cn  :=1
	declare ui_slider $knob (1,39)
	set_control_par_str(get_ui_id($knob),$CONTROL_PAR_TEXT,"")
	set_control_par_str(get_ui_id($knob),$CONTROL_PAR_PICTURE,"peak")
 	set_control_par(get_ui_id($knob), $CONTROL_PAR_POS_X, 64)
	set_control_par(get_ui_id($knob), $CONTROL_PAR_POS_Y, 55)
	SET_CONDITION(NO_SYS_SCRIPT_RLS_TRIG)
	$knob :=5
end on

{ Functions }

function __bounceDown

  $rd := random(1,4)
  while($knob >= $rd)
     $knob := $rd
     wait(random($ms2,$ms3))
    inc($rd)
  end while
    
end function
    
function __bounceUp

  if($velo > 5 and $velo < 31) 
     $knob := random($velo,$velo +1)
     else
     $knob := $velo
  end if
    
  wait(random($ms0,$ms2))
  if($velo >=10)
     $knob := random($velo,$velo -1)
  end if
    
end function
          
function __releaseMeter
  if($knob < 5 or $knob > 5)
    $knob := 5
  end if
end function
    
{ Note is played }

on note 
    
	$velo 	:= $EVENT_VELOCITY / 4
	$vol  	:= get_engine_par($ENGINE_PAR_VOLUME,-1,-1,-1) / 100000
	$dur  	:= %NOTE_DURATION[$EVENT_NOTE]
	$poly 	:= %POLY_AT[$EVENT_NOTE]
	$voices   := $PLAYED_VOICES_INST
    
    if($velo < 5)
       $velo :=5
    end if

	while($cn < $velo) 
       if($voices >1)
          else
          $knob := $cn
          inc($cn)
       end if
      wait(10)
	end while

    while ($NOTE_HELD = 1)
        call __bounceUp
    end while

end on

{ Our release functions }

on release

    $velo := $EVENT_VELOCITY / 4
      
	while($cn > 5)
		if($voices >1)
          else
          $knob := $cn
          dec($cn)
		end if
      wait(random($ms2,$ms3))
	end while
      
  $knob :=5

  call __bounceDown
  call __releaseMeter
    
end on
```


----------



## Big Bob (Feb 12, 2013)

Hey Sasje, if you're happy with keeping it simple that's fine with me.

I originally got the impression that you wanted to delve deeper into the gears and pulleys of how Kontakt relates things like Velocity (and other modulators) to actual output volume and wanted to know the underlying math. Not so much for the purpose of polishing your little VU meter demo but as background for something bigger later on. 

Maybe you just wanted to end up with a little fun demo, huh? Talk about using a steam roller to put out a match. :lol: I remember many years ago, when visiting the Public Library, I found a book titled 'More than anyone cares to know about Lobsters'. I kind of chuckled when I read that title. Evidentally, by the time the guy got done writing his masterpiece, reality must have struck him before he chose the title.

I presume I won't have to post the remaining math relationships required to calculate output volume then? Sorry I misunderstood what you wanted to accomplish. :roll: 

Rejoice,

Bob


----------



## Sasje (Feb 12, 2013)

I now use: ($EVENT_VELOCITY / 4) which gives me a Max: 127 / 4 = 31.75. 

Near the exact amount of layers we got: 31. Can't believe I didn't calculate this first. LOL! 

If I run the program, it comes very close to what it's supposed to do. When Kontakt's meters shoot in the red, mine does too. As the sound pressure relates to how hard a note is struck. (I hope I didn't ruin my MIDI keyboard by slamming to 127...) 

The reason why I think this would suffice, is because we want to stay under or near 0dB when playing a note distortion free. That's the whole point of a VU meter: Showing the approximate sound pressure/level and preventing from going into the reds by visual means. There is no point in getting the needle 100% exact with a lot of CPU cycles, because we won't see it anyway as it bounces all over the place. So the rough velocity number we get from Kontakt's engine relates to the behavior of a real world VU meter: it's an approximation.

Yes, I'm still fine-tuning it a little bit, but my time is limited also.  

I think this accomplishes it fairly well so far?


----------



## Sasje (Feb 13, 2013)

Big Bob @ Tue Feb 12 said:


> Hey Sasje, if you're happy with keeping it simple that's fine with me.
> 
> I originally got the impression that you wanted to delve deeper into the gears and pulleys of how Kontakt relates things like Velocity (and other modulators) to actual output volume and wanted to know the underlying math. Not so much for the purpose of polishing your little VU meter demo but as background for something bigger later on.
> 
> ...



I have read your paper about Volume Control Study, and it makes a lot of sense to me. However in a practical sense I go with the rough number Kontaks gives me for velocity and do not compensate any of it. Precision isn't an issue when dealing with a analogue VU meter, actually a VU meter is one of the least precise measuring instruments. :mrgreen: 

So I really don't worry about that, in fact, the approximation can contribute to the "realness" of the digital version, since one cannot keep up with the needle when one plays a bunch of notes after each other anyway. The basic factor is the relation between velocity and sound pressure, and then take the approximation and show it with the needle. I think my current script accomplishes that. (give or take a few tweaks) :wink:

I do appreciate all the Math you provided, some of it I figured out but was unable to do so without your Math library. But in any event, we simply cannot get the actual dB via Kontakt, rendering any attempt to get a high precision pretty much useless. At least that is where I stand right now.

Or do you think otherwise? Would love to hear it. o


----------



## Big Bob (Feb 13, 2013)

Hi Sasje,



> Or do you think otherwise? Would love to hear it.



No, I agree with everything that you say. But, again, I didn't realize that a practical implementation of a crude VU meter was your *only* objective. 

And I agree that for a simple application like this, analyzing it to death would be almost comical. But, that is not to say that there are no situations for which a more precise means of determining volume control issues in Kontakt isn't of value. There have been in the past many such situations involving not only velocity modulation but interactions with the associated intensity sliders. In fact, the whole business of modulators in Kontakt is frought with many unexpected complications. And they sometimes pop up in what at first appears to be the simplest of situations. 

As I said in my last post, I thought you perhaps had some such 'bigger and better' application in mind for which it might be helpful to know how all these control parameters relate in Kontakt. So, just chalk it up to the 'More than anyone wants to know about Kontakt control parameters'. Come to think of it, maybe I should write such a book :lol: 

Rejoice,

Bob


----------



## Sasje (Feb 13, 2013)

Big Bob @ Wed Feb 13 said:


> Hi Sasje,
> 
> 
> 
> ...



It wasn't at first, until I realized we're not getting the actual waveform dB from Kontakt anyway. So if we do it with complex Maths or just simple functional scripting then it doesn't really matter from that point on? Then it only becomes a discussion about the accuracy of Kontakt's engine and ways to compensate for it, on which I agree and it does some strange things. 

Maybe you should write a book, I would read it! :D


----------



## geronimo (Feb 27, 2013)

Josquin @ Mon 11 Feb said:


> ...and on top of that you can only use it on the master outputs, I believe, not anywhere else in the signal path.



Can also be used on the output bus of your choice _


----------



## Rick Bryan (Oct 6, 2016)

I am having a problem with getting my vu meter to work in kontakt can someone help me with this please
i am using this script

set_ui_height_px(4)
declare $i
declare ui_slider $meter(0, 127) 
set_control_par_str(get_ui_id($meter),$CONTROL_PAR_TEXT,"meter")
set_control_par_str(get_ui_id($meter),$CONTROL_PAR_PICTURE,"meter")
set_control_par(get_ui_id($meter), $CONTROL_PAR_MOUSE_BEHAVIOUR, -500)
set_control_par(get_ui_id($meter), $CONTROL_PAR_POS_X, 60)
set_control_par(get_ui_id($meter), $CONTROL_PAR_POS_Y, 36)
move_control_px($meter,390,90)
end on
on note
$meter := $EVENT_VELOCITY
end on
on release
if (search(%KEY_DOWN,1)=-1)
while ($meter>0)
$meter := $meter-1
inc($i)
wait(2500)
end while
end if
end on


----------



## geronimo (Dec 23, 2019)

It looks like a badly configured image file or its missing or correct .txt file _


----------

