Having an hard time with logarythm and rms values

Hi everyone,
I’m doing a sampler here playing a fixed wave file according to track signal and now i’m getting to the point where i would like to link the volume of to the fixed sample to the track volume.

const float
g = apvts.getRawParameterValue("GAIN")->load(),
ratio = buffer.getRMSLevel(0, 0, buffer.getNumSamples());

sourceToBuffer(sample, info, mixBuffer, buffer,  (1.0*ratio *g));

something like that works for now but i’m struggling to find the way of doing it with logarythm… if i got it well rms scale goes form 0 to 0,7 its getting confusing for me making it work it out :wink:
would anybody get a clue? thanks a lot!

It’s not clear exactly what you’re trying to achieve. Are you trying to match the volume of a sample to the audio level currently playing through the plugin, or are you just trying to scale the gain parameter according to decibels so that the perceived volume changes at a constant rate?

If the latter, are you aware of the Decibels class?

If your gain value is in decibels, then you can convert it to a plain gain value by doing

const auto scaleFactor = juce::Decibels::decibelsToGain (gainParameterValue, minGainDb);
1 Like

Hi, thanks replying,

I’m trying to match the volume of a sample to the audio level currently playing through the plugin, it works fine now but when it gets to low volumes the sample is way earable but when high the sample gets drown.

I can’t figure out if its coz i’m not using the decibels scale or if i do something else wrong.
Thanks a lot for that i’m gonna have a try see if it works for me.

  1. get rms value from input signal
  2. convert to decibels using juce::Decibel class
  3. substract current db value from target db value
  4. apply gain result (now also in db) from step 3 to your audio signal

this will probably need some smoothing
you also need to think about what should happen when the input volume is very low/silent since there could be huge db differences

1 Like

It makes a lot of sense thanks a lot !