Skip to content

Commit

Permalink
Add tests for EnvelopeADSR
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Feb 25, 2024
1 parent e727466 commit b693605
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ if(NOT CMAKE_CROSSCOMPILING)
"lib/grit/audio/dynamic/gain_computer_test.cpp"
"lib/grit/audio/dynamic/transient_shaper_test.cpp"

"lib/grit/audio/envelope/envelope_adsr_test.cpp"
"lib/grit/audio/envelope/envelope_follower_test.cpp"

"lib/grit/audio/filter/biquad_test.cpp"
Expand Down
45 changes: 45 additions & 0 deletions lib/grit/audio/envelope/envelope_adsr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "envelope_adsr.hpp"

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>

TEMPLATE_TEST_CASE("audio/envelope: EnvelopeADSR", "", float, double)
{
using Float = TestType;

// TODO: Check why the error with float32 is so high
static constexpr auto tolerance = sizeof(Float) == 4 ? 1e-2 : 1e-6;

auto const advance = [](auto& env, auto count) {
for (auto i{0}; i < count - 1; ++i) {
[[maybe_unused]] auto out = env();
}
return env();
};

auto const attack = GENERATE(2000, 44'100, 96'000);
auto const sustain = GENERATE(Float(0.75), Float(0.9));

auto const decay = attack / 4;
auto const release = attack / 2;

auto adsr = grit::EnvelopeADSR<Float>{};
adsr.setAttack(Float(attack));
adsr.setDecay(Float(decay));
adsr.setSustain(sustain);
adsr.setRelease(Float(release));

for (auto i{0}; i < 100; ++i) {
CAPTURE(i);
REQUIRE_THAT(adsr(), Catch::Matchers::WithinAbs(Float(0), tolerance));
}

adsr.gate(true);
REQUIRE_THAT(advance(adsr, attack), Catch::Matchers::WithinAbs(Float(1), tolerance)); // attack
REQUIRE_THAT(advance(adsr, decay), Catch::Matchers::WithinAbs(sustain, tolerance)); // decay -> sustain
REQUIRE_THAT(advance(adsr, decay * 2), Catch::Matchers::WithinAbs(sustain, tolerance)); // sustain

adsr.gate(false);
REQUIRE_THAT(advance(adsr, release), Catch::Matchers::WithinAbs(Float(0), tolerance)); // release
}

0 comments on commit b693605

Please sign in to comment.