Skip to content

Commit

Permalink
Make TPTSVFilter cloneable
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-13 committed Jan 28, 2024
1 parent d8b2655 commit e9df863
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
19 changes: 18 additions & 1 deletion WECore/WEFilters/TPTSVFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ namespace WECore::TPTSVF {
_calculateCoefficients();
}

TPTSVFilter(const TPTSVFilter& other) = default;
virtual ~TPTSVFilter() = default;

/**
Expand Down Expand Up @@ -95,6 +94,10 @@ namespace WECore::TPTSVF {

/** @} */

TPTSVFilter clone() const {
return TPTSVFilter(*this);
}

private:
double _sampleRate,
_cutoffHz,
Expand All @@ -109,6 +112,20 @@ namespace WECore::TPTSVF {
int _mode;

void _calculateCoefficients();

TPTSVFilter(const TPTSVFilter& other) {
_sampleRate = other._sampleRate;
_cutoffHz = other._cutoffHz;
_q = other._q;
_gain = other._gain;

_s1 = other._s1;
_s2 = other._s2;
_g = other._g;
_h = other._h;

_mode = other._mode;
}
};

template <typename T>
Expand Down
48 changes: 48 additions & 0 deletions WECore/WEFilters/Tests/TPTSVFilterTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,51 @@ SCENARIO("TPTSVFilter: Silence in = silence out") {
}
}
}

SCENARIO("TPTSVFilter: Clone works correctly") {
GIVEN("A TPTSVFilter and a buffer of samples") {
auto generateBuffer = []() {
std::vector<double> buffer(1024);
std::fill(buffer.begin(), buffer.end(), 0);
std::fill(buffer.begin() + 300, buffer.end(), 0.5);
std::fill(buffer.begin() + 600, buffer.end(), 0.7);
return buffer;
};

std::vector<double> initialBuffer = generateBuffer();
WECore::TPTSVF::TPTSVFilter<double> filter;

// Set some unique values so we can test for them later
filter.setMode(WECore::TPTSVF::Parameters::FILTER_MODE.HIGHPASS);
filter.setCutoff(1001);
filter.setQ(1.1);
filter.setGain(0.8);
filter.setSampleRate(48000);

// Set up some internal state
filter.processBlock(&initialBuffer[0], initialBuffer.size());

WHEN("It is cloned") {
WECore::TPTSVF::TPTSVFilter<double> clonedFilter = filter.clone();

THEN("The cloned filter is equal to the original") {
CHECK(clonedFilter.getMode() == filter.getMode());
CHECK(clonedFilter.getCutoff() == Approx(filter.getCutoff()));
CHECK(clonedFilter.getQ() == Approx(filter.getQ()));
CHECK(clonedFilter.getGain() == Approx(filter.getGain()));
// CHECK(clonedFilter.getSampleRate() == Approx(filter.getSampleRate()));

// Check internal state
std::vector<double> buffer1 = generateBuffer();
filter.processBlock(&buffer1[0], buffer1.size());

std::vector<double> buffer2 = generateBuffer();
clonedFilter.processBlock(&buffer2[0], buffer2.size());

for (int index {0}; index < buffer.size(); index++) {
CHECK(buffer1[index] == Approx(buffer2[index]));
}
}
}
}
}

0 comments on commit e9df863

Please sign in to comment.