Old 11-21-2018, 08:04 AM   #1
Trancit
Human being with feelings
 
Join Date: Aug 2009
Location: Gran Canaria
Posts: 488
Default How to add a volume slider to this code??

Hi there,

this is the code from loser´s stereo enhancer:

Code:
slider1:100<0,200,1>Width Low (%)
slider2:500<0,20000,1>Crossover (Hz)
slider3:100<0,200,1>Width High (%)

in_pin:left input
in_pin:right input
out_pin:left output
out_pin:right output

@init
cDenorm = 10^-30;

@slider
widthLP = slider1 / 200;
width = slider3 / 200;

freq = min(slider2,srate);
x = exp(-2.0*$pi*freq/srate);
a0 = 1.0-x;
b1 = -x;

@sample
s0 = spl0;
s1 = spl1;

spl0 = (tmpl = a0*spl0 - b1*tmpl + cDenorm);
spl1 = (tmpr = a0*spl1 - b1*tmpr + cDenorm);

s0 -= spl0;
s1 -= spl1;

mono = (s0 + s1)/2;
stereo = (s0 - s1);
s0 = (mono + stereo * width) / max(width,1);
s1 = (mono - stereo * width) / max(width,1);

monoLP = (spl0 + spl1)/2;
stereoLP = (spl0 - spl1);
spl0 = (monoLP + stereoLP * widthLP) / max(widthLP,1);
spl1 = (monoLP - stereoLP * widthLP) / max(widthLP,1);

spl0 += s0;
spl1 += s1;
How to add a volume slider with a range of -inf to +12db???
Trancit is offline   Reply With Quote
Old 11-21-2018, 09:55 AM   #2
geraintluff
Human being with feelings
 
geraintluff's Avatar
 
Join Date: Nov 2009
Location: mostly inside my own head
Posts: 346
Default

Here's how I would approach it:

Step 1: add a slider for it

We define a new slider, up at the top near the others.
Code:
slider4:gain_db=0<-60,12,0.1>Gain (dB)
-60 isn't the same as Infinity, but it's close enough. If it says -60, we're going to round it down to silence.

Step 2: calculate a gain factor from that slider

We add this code anywhere in the @slider block. It converts from decibels to a linear multiplication factor:
Code:
gain_factor = gain_db > -60 ? pow(10, gain_db/20) : 0;
As you can (hopefully) see, if the slider is at -60, then it rounds that down to silence.

Step 3: apply this gain to the input or output

We add this code to the @sample block:
Code:
spl0 *= gain_factor;
spl1 *= gain_factor;
This could go at the very beginning of @sample (i.e. applied to the input), or at the end (i.e. applied to the output).

At this point it should basically work, but it will produce subtle clicks when changing/automating the slider, because the gain is changing too fast, soooo...

Step 4: smooth out the gain changes

The simplest form of smoothing is exponential. First let's calculate an update-factor, based on a 10ms fade - this should go in @init:

Code:
fade_duration_samples = 0.01*srate;
fade_update_factor = 1/fade_duration_samples; // Accurate enough :)
Then, we add some code to @sample. This line constantly nudges gain_factor_smoothed towards gain_factor, at a rate determined by fade_update_factor.

Code:
gain_factor_smoothed += (gain_factor - gain_factor_smoothed)*fade_update_factor;
We then update the code we added in Step 3, so that it uses gain_factor_smoothed instead:

Code:
spl0 *= gain_factor_smoothed;
spl1 *= gain_factor_smoothed;
Step 5: set the initial gain correctly

At the moment, gain_factor_smoothed starts at 0, which means it will fade in at the beginning of playback.

This isn't ideal, so we add the following code to @init:
Code:
gain_factor_smoothed = gain_db > -60 ? pow(10, gain_db/20) : 0;
This should match the code we added to @slider in Step 2, but it's giving a value to gain_factor_smoothed instead, so that it starts at the same value as gain_factor will have.

I think that should work! I can't test it out right now, because I'm not near my setup, so I apologise for any typos.
__________________
JSFX set | Bandcamp/SoundCloud/Spotify

Last edited by geraintluff; 11-21-2018 at 10:06 AM.
geraintluff is offline   Reply With Quote
Old 11-21-2018, 05:34 PM   #3
Trancit
Human being with feelings
 
Join Date: Aug 2009
Location: Gran Canaria
Posts: 488
Default

This works perfectly... thx very much!!
Trancit is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.