Skip to content

Commit

Permalink
Tweak the amplitude stage a bit (#132)
Browse files Browse the repository at this point in the history
1. Consolidate some of the code into the param
2. If the param is at 0db don't touch the signal
   at all
3. Fix a -0.00db display which arose
  • Loading branch information
baconpaul authored Jun 11, 2024
1 parent c7dd166 commit 349fd89
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
26 changes: 14 additions & 12 deletions src-juce/AWConsolidatedProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,15 @@ template <typename T> void AWConsolidatedAudioProcessor::processBlockT(juce::Aud
awProcessor->setParameter(i, fxParams[i]->get());
}

T inScale = inLev->get();
inScale = inScale * inScale * inScale * CubicDBParam::maxLev;

T outScale = outLev->get();
outScale = outScale * outScale * outScale * CubicDBParam::maxLev;
auto inScale = inLev->getAmplitude<T>();
auto inScaleActive = inLev->isAmplifiyingOrAttenuating();
auto outScale = outLev->getAmplitude<T>();
auto outScaleActrive = outLev->isAmplifiyingOrAttenuating();

if constexpr (std::is_same_v<T, float>)
{
// FIXME - deal with very large block case by not skipping level
if (buffer.getNumSamples() < maxInBlock)
if (inScaleActive && buffer.getNumSamples() < maxInBlock)
{
for (int i = 0; i < buffer.getNumSamples(); ++i)
{
Expand All @@ -250,7 +249,7 @@ template <typename T> void AWConsolidatedAudioProcessor::processBlockT(juce::Aud
}
else
{
if (buffer.getNumSamples() < maxInBlock)
if (inScaleActive && buffer.getNumSamples() < maxInBlock)
{
for (int i = 0; i < buffer.getNumSamples(); ++i)
{
Expand All @@ -265,10 +264,13 @@ template <typename T> void AWConsolidatedAudioProcessor::processBlockT(juce::Aud
buffer.getNumSamples());
}

for (int i = 0; i < buffer.getNumSamples(); ++i)
if (outScaleActrive)
{
outputs[0][i] *= outScale;
outputs[1][i] *= outScale;
for (int i = 0; i < buffer.getNumSamples(); ++i)
{
outputs[0][i] *= outScale;
outputs[1][i] *= outScale;
}
}
}

Expand Down Expand Up @@ -410,9 +412,9 @@ void AWConsolidatedAudioProcessor::setStateInformation(const void *data, int siz
fxParams[i]->setValueNotifyingHost(f);
}

auto il = xmlState->getDoubleAttribute("inlev", std::cbrt(1.f/CubicDBParam::maxLev));
auto il = xmlState->getDoubleAttribute("inlev", CubicDBParam::defaultVal);
inLev->setValueNotifyingHost(il);
auto ol = xmlState->getDoubleAttribute("outlev", std::cbrt(1.f/CubicDBParam::maxLev));
auto ol = xmlState->getDoubleAttribute("outlev", CubicDBParam::defaultVal);
outLev->setValueNotifyingHost(ol);
}

Expand Down
31 changes: 25 additions & 6 deletions src-juce/AWConsolidatedProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,28 @@ class AWConsolidatedAudioProcessor : public juce::AudioProcessor,
struct CubicDBParam : public APFPublicDefault
{
static constexpr float maxDb{18.0};
static constexpr float maxLev{7.943282347242815}; // pow(10, maxDb/20)
static constexpr double maxLev{7.943282347242815}; // pow(10, maxDb/20)
static constexpr double defaultVal{0.5011872336272724}; // 1.0 / cbrt(maxLeb)
CubicDBParam(const juce::ParameterID &id, const juce::String &parameterName)
: APFPublicDefault(id, parameterName, juce::NormalisableRange<float>(0.0, 1.0),
std::cbrt(1.f / maxLev))
defaultVal)
{
}

juce::String getText(float f, int i) const override
{
auto val = f;
auto val = (double)f;
if (val <= 0)
{
return "-inf";
}

auto v3 = val * val * val * maxLev;
auto db = 20 * std::log10(v3);

// This 1e-6 pushes the display off -0.00db to 0.00db
// which happens due to float truncation since the
// param is float valued
auto db = 20 * std::log10(v3 + 1e-6);
char res[64];
snprintf(res, 63, "%.2f dB", db);
return res;
Expand All @@ -239,13 +244,27 @@ class AWConsolidatedAudioProcessor : public juce::AudioProcessor,
auto lv = std::cbrt(db / maxLev);
if (lv < 0 || lv > 1)
{
return std::cbrt(1.f / maxLev);
return defaultVal;
}

return (float)lv;
}

float getDefaultValue() const override { return std::cbrt(1.0 / maxLev); }
float getDefaultValue() const override { return defaultVal; }

template<typename T>
T getAmplitude() const {
T lev = (T)get();
lev = lev * lev * lev * maxLev;
return lev;
}

bool isAmplifiyingOrAttenuating() const
{
return std::fabs(get() - defaultVal) > 5e-6;
}


};

//==============================================================================
Expand Down

0 comments on commit 349fd89

Please sign in to comment.