From bcb7ee0bdf4bf031846999ffc35b866ab4fb8990 Mon Sep 17 00:00:00 2001 From: Randy Jones Date: Wed, 10 Jul 2024 14:57:04 -0700 Subject: [PATCH] add clear() for some DSP objects --- source/DSP/MLDSPFilters.h | 34 +++++++++++++++++++++ source/DSP/MLDSPGens.h | 62 ++++++++++++++++++++++++++++++++------- 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/source/DSP/MLDSPFilters.h b/source/DSP/MLDSPFilters.h index a7d11b95..0f796fdc 100644 --- a/source/DSP/MLDSPFilters.h +++ b/source/DSP/MLDSPFilters.h @@ -481,6 +481,11 @@ struct OnePole { y1 = f; } + + void clear() + { + y1 = 0.f; + } }; // A one-pole, one-zero filter to attenuate DC. @@ -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. @@ -1386,6 +1400,16 @@ class Downsampler { return DSPVector(bufferPtr(_numBuffers - 1)); } + + void clear() + { + for(auto& f : _filters) + { + f.clear(); + } + _buffers.clear(); + _counter = 0; + } }; @@ -1450,6 +1474,16 @@ struct Upsampler load(result, bufferPtr(readIdx_++)); return result; } + + void clear() + { + for(auto& f : _filters) + { + f.clear(); + } + _buffers.clear(); + readIdx_ = 0; + } }; diff --git a/source/DSP/MLDSPGens.h b/source/DSP/MLDSPGens.h index f75cfc36..156518b3 100644 --- a/source/DSP/MLDSPGens.h +++ b/source/DSP/MLDSPGens.h @@ -398,16 +398,34 @@ 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(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}; @@ -415,7 +433,7 @@ class LinearGlide float mTargetValue{0}; float mDyPerVector{1.f / 32}; int mVectorsPerGlide{32}; - int mVectorsRemaining{0}; + int mVectorsRemaining{-1}; public: void setGlideTimeInSamples(float t) @@ -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); @@ -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--; } @@ -478,6 +500,14 @@ class LinearGlide return mCurrVec; } + + void clear() + { + mCurrVec = 0.f; + mStepVec = 0.f; + mTargetValue = 0.f; + mVectorsRemaining = -1; + } }; class SampleAccurateLinearGlide @@ -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) @@ -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); @@ -528,7 +562,6 @@ class SampleAccurateLinearGlide { // start glide: get change in output value per sample mStepValue = (mTargetValue - mCurrValue) * mDyPerSample; - mSamplesRemaining--; } else @@ -543,6 +576,13 @@ class SampleAccurateLinearGlide return mCurrValue; } + void clear() + { + mCurrValue = 0.f; + mStepValue = 0.f; + mTargetValue = 0.f; + mSamplesRemaining = -1; + } }; } // namespace ml