Skip to content

Commit

Permalink
fixes #256 - checking input to env with filters (#263)
Browse files Browse the repository at this point in the history
* check if env follow input is finite, if not replace with last valid input

* clang-formatted

* initialised the way @weefuzzy likes it
  • Loading branch information
tremblap committed Feb 11, 2024
1 parent 398601a commit 3335b40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
16 changes: 9 additions & 7 deletions include/algorithms/public/Envelope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,24 @@ class Envelope
mInitialized = true;
}

double processSample(const double in,
double floor, index fastRampUpTime, index slowRampUpTime,
index fastRampDownTime, index slowRampDownTime,
double hiPassFreq)
double processSample(const double in, double floor, index fastRampUpTime,
index slowRampUpTime, index fastRampDownTime,
index slowRampDownTime, double hiPassFreq)
{
using namespace std;
assert(mInitialized);
mFastSlide.updateCoeffs(fastRampUpTime, fastRampDownTime);
mSlowSlide.updateCoeffs(slowRampUpTime, slowRampDownTime);
double filtered = in;
if (std::isfinite(in)) mPrevValid = in;
double filtered = mPrevValid;
if (hiPassFreq != mHiPassFreq)
{
initFilters(hiPassFreq);
mHiPassFreq = hiPassFreq;
}
if (mHiPassFreq > 0){
filtered = mHiPass2.processSample(mHiPass1.processSample(in));
if (mHiPassFreq > 0)
{
filtered = mHiPass2.processSample(mHiPass1.processSample(filtered));
}
double rectified = abs(filtered);
double dB = 20 * log10(rectified);
Expand All @@ -70,6 +71,7 @@ class Envelope

double mHiPassFreq{0};
bool mInitialized{false};
double mPrevValid{0};

ButterworthHPFilter mHiPass1;
ButterworthHPFilter mHiPass2;
Expand Down
33 changes: 14 additions & 19 deletions include/algorithms/public/EnvelopeGate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ class EnvelopeGate

public:
EnvelopeGate(index maxSize, Allocator& alloc = FluidDefaultAllocator())
: mInputBuffer(maxSize, alloc),
mOutputBuffer(maxSize, alloc)
: mInputBuffer(maxSize, alloc), mOutputBuffer(maxSize, alloc)
{}

void init(double onThreshold, double offThreshold, double hiPassFreq,
index minTimeAboveThreshold, index upwardLookupTime,
index minTimeBelowThreshold, index downwardLookupTime)
index minTimeAboveThreshold, index upwardLookupTime,
index minTimeBelowThreshold, index downwardLookupTime)
{
using namespace std;

Expand All @@ -44,8 +43,8 @@ class EnvelopeGate
mMinTimeBelowThreshold = minTimeBelowThreshold,
mDownwardLookupTime = downwardLookupTime;
mDownwardLatency = max<index>(minTimeBelowThreshold, mDownwardLookupTime);
mLatency = max<index>(
mMinTimeAboveThreshold + mUpwardLookupTime, mDownwardLatency);
mLatency = max<index>(mMinTimeAboveThreshold + mUpwardLookupTime,
mDownwardLatency);
if (mLatency < 0) mLatency = 1;
assert(mLatency <= mInputBuffer.size());
mHiPassFreq = hiPassFreq;
Expand All @@ -63,22 +62,23 @@ class EnvelopeGate
}

double processSample(const double in, double onThreshold, double offThreshold,
index rampUpTime, index rampDownTime, double hiPassFreq,
index minEventDuration, index minSilenceDuration)
index rampUpTime, index rampDownTime, double hiPassFreq,
index minEventDuration, index minSilenceDuration)
{
using namespace std;
assert(mInitialized);

mSlide.updateCoeffs(rampUpTime, rampDownTime);

double filtered = in;
if (std::isfinite(in)) mPrevValid = in;
double filtered = mPrevValid;
if (hiPassFreq != mHiPassFreq)
{
initFilters(hiPassFreq);
mHiPassFreq = hiPassFreq;
}
if (mHiPassFreq > 0)
filtered = mHiPass2.processSample(mHiPass1.processSample(in));
filtered = mHiPass2.processSample(mHiPass1.processSample(filtered));

double rectified = abs(filtered);
double dB = 20 * log10(rectified);
Expand Down Expand Up @@ -122,7 +122,7 @@ class EnvelopeGate
{
index onsetIndex =
refineStart(mWriteHead - mMinTimeAboveThreshold - mUpwardLookupTime,
mUpwardLookupTime);
mUpwardLookupTime);

index blockSize = mWriteHead > onsetIndex
? mWriteHead - onsetIndex
Expand Down Expand Up @@ -248,19 +248,14 @@ class EnvelopeGate
mOnStateCount = 0;
mOffStateCount = 1;
}
else if (mInputState && nextState)
{
mOnStateCount++;
}
else if (!mInputState && !nextState)
{
mOffStateCount++;
}
else if (mInputState && nextState) { mOnStateCount++; }
else if (!mInputState && !nextState) { mOffStateCount++; }
}

index mLatency;
index mFillCount;
double mHiPassFreq{0};
double mPrevValid{0};

index mMinTimeAboveThreshold{440};
index mDownwardLookupTime{10};
Expand Down

0 comments on commit 3335b40

Please sign in to comment.