Skip to content

Commit

Permalink
add clear() for some DSP objects
Browse files Browse the repository at this point in the history
  • Loading branch information
madronalabs committed Jul 10, 2024
1 parent b70ea9a commit bcb7ee0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
34 changes: 34 additions & 0 deletions source/DSP/MLDSPFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ struct OnePole
{
y1 = f;
}

void clear()
{
y1 = 0.f;
}
};

// A one-pole, one-zero filter to attenuate DC.
Expand Down Expand Up @@ -1296,6 +1301,15 @@ class HalfBandFilter
}
return vy;
}

void clear()
{
apa0.clear();
apa1.clear();
apb0.clear();
apb1.clear();
b1 = 0;
}

private:
// order=4, rejection=70dB, transition band=0.1.
Expand Down Expand Up @@ -1386,6 +1400,16 @@ class Downsampler
{
return DSPVector(bufferPtr(_numBuffers - 1));
}

void clear()
{
for(auto& f : _filters)
{
f.clear();
}
_buffers.clear();
_counter = 0;
}
};


Expand Down Expand Up @@ -1450,6 +1474,16 @@ struct Upsampler
load(result, bufferPtr(readIdx_++));
return result;
}

void clear()
{
for(auto& f : _filters)
{
f.clear();
}
_buffers.clear();
readIdx_ = 0;
}
};


Expand Down
62 changes: 51 additions & 11 deletions source/DSP/MLDSPGens.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,24 +398,42 @@ class SawGen
};

// ----------------------------------------------------------------
// LinearGlide
// Interpolator1

// convert a scalar float input into a DSPVector with linear slew.
// to allow optimization, glide time is quantized to DSPVectors.
// Note that a onepole or other IIR filter is not used because we must reach
// the actual value in a finite time.
// linear interpolate over signal length to next value.

constexpr float unityRampFn(int i) { return (i + 1) / static_cast<float>(kFloatsPerDSPVector); }
ConstDSPVector kUnityRampVec{unityRampFn};

struct Interpolator1
{
float currentValue{0};

DSPVector operator()(float f)
{
float dydt = f - currentValue;
DSPVector outputVec = DSPVector(currentValue) + kUnityRampVec*dydt;
currentValue = f;
return outputVec;
}
};

// TODO needs test

// ----------------------------------------------------------------
// LinearGlide

// convert a scalar float input into a DSPVector with linear slew.
// to allow optimization, glide time is quantized to DSPVectors.

class LinearGlide
{
DSPVector mCurrVec{0.f};
DSPVector mStepVec{0.f};
float mTargetValue{0};
float mDyPerVector{1.f / 32};
int mVectorsPerGlide{32};
int mVectorsRemaining{0};
int mVectorsRemaining{-1};

public:
void setGlideTimeInSamples(float t)
Expand Down Expand Up @@ -445,7 +463,11 @@ class LinearGlide
}

// process glide
if (mVectorsRemaining == 0)
if (mVectorsRemaining < 0)
{
// do nothing
}
else if (mVectorsRemaining == 0)
{
// end glide: write target value to output vector
mCurrVec = DSPVector(mTargetValue);
Expand All @@ -462,7 +484,7 @@ class LinearGlide
mStepVec = DSPVector(dydv);

// setup current vector with first interpolation ramp.
mCurrVec = DSPVector(currentValue) + kUnityRampVec * DSPVector(dydv);
mCurrVec = DSPVector(currentValue) + kUnityRampVec * mStepVec;

mVectorsRemaining--;
}
Expand All @@ -478,6 +500,14 @@ class LinearGlide

return mCurrVec;
}

void clear()
{
mCurrVec = 0.f;
mStepVec = 0.f;
mTargetValue = 0.f;
mVectorsRemaining = -1;
}
};

class SampleAccurateLinearGlide
Expand All @@ -487,7 +517,7 @@ class SampleAccurateLinearGlide
float mTargetValue{0.f};
int mSamplesPerGlide{32};
float mDyPerSample{1.f/32};
int mSamplesRemaining{0};
int mSamplesRemaining{-1};

public:
void setGlideTimeInSamples(float t)
Expand Down Expand Up @@ -517,7 +547,11 @@ class SampleAccurateLinearGlide
}

// process glide
if (mSamplesRemaining == 0)
if (mSamplesRemaining < 0)
{
// do nothing
}
else if (mSamplesRemaining == 0)
{
// end glide: write target value to output vector
mCurrValue = (mTargetValue);
Expand All @@ -528,7 +562,6 @@ class SampleAccurateLinearGlide
{
// start glide: get change in output value per sample
mStepValue = (mTargetValue - mCurrValue) * mDyPerSample;

mSamplesRemaining--;
}
else
Expand All @@ -543,6 +576,13 @@ class SampleAccurateLinearGlide

return mCurrValue;
}
void clear()
{
mCurrValue = 0.f;
mStepValue = 0.f;
mTargetValue = 0.f;
mSamplesRemaining = -1;
}
};

} // namespace ml

0 comments on commit bcb7ee0

Please sign in to comment.