2020-01-25 10:04:31 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
2020-01-25 13:13:07 +01:00
|
|
|
// This code is part of the sfizz library and is licensed under a BSD 2-clause
|
|
|
|
|
// license. You should have receive a LICENSE.md file along with the code.
|
|
|
|
|
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
|
2019-08-30 00:49:58 +02:00
|
|
|
|
2020-01-18 17:35:27 +01:00
|
|
|
#include "sfizz/ADSREnvelope.h"
|
2020-09-16 09:03:39 +02:00
|
|
|
#include "TestHelpers.h"
|
2019-08-25 14:01:03 +02:00
|
|
|
#include "catch2/catch.hpp"
|
|
|
|
|
#include <absl/algorithm/container.h>
|
|
|
|
|
#include <absl/types/span.h>
|
2019-08-22 17:18:09 +02:00
|
|
|
#include <algorithm>
|
2019-08-25 14:01:03 +02:00
|
|
|
#include <array>
|
2019-08-22 17:18:09 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
using namespace Catch::literals;
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Basic state")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
|
|
|
|
std::array<float, 5> output;
|
|
|
|
|
std::array<float, 5> expected { 0.0, 0.0, 0.0, 0.0, 0.0 };
|
2020-04-01 00:18:03 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
|
2020-01-05 00:22:26 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Attack")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.02f;
|
|
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
std::array<float, 5> output;
|
2020-04-01 00:18:03 +02:00
|
|
|
std::array<float, 5> expected { 0.5f, 1.0f, 1.0f, 1.0f, 1.0f };
|
|
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2020-01-05 00:22:26 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Attack again")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.03f;
|
|
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
std::array<float, 5> output;
|
2020-04-01 00:18:03 +02:00
|
|
|
std::array<float, 5> expected { 0.33333f, 0.66667f, 1.0f, 1.0f, 1.0f };
|
|
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2020-01-05 00:19:08 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Release")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.02f;
|
|
|
|
|
region.amplitudeEG.release = 0.04f;
|
|
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.startRelease(2);
|
2020-04-01 00:18:03 +02:00
|
|
|
std::array<float, 8> output;
|
|
|
|
|
std::array<float, 8> expected { 0.5f, 1.0f, 0.13534f, 0.018f, 0.0024f, 0.0f, 0.0f, 0.0f };
|
|
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
|
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.startRelease(2);
|
2020-01-05 00:19:08 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Delay")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.02f;
|
|
|
|
|
region.amplitudeEG.release = 0.04f;
|
|
|
|
|
region.amplitudeEG.delay = 0.02f;
|
2020-03-20 13:03:26 +01:00
|
|
|
std::array<float, 10> output;
|
|
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.startRelease(4);
|
2020-04-01 00:18:03 +02:00
|
|
|
std::array<float, 10> expected { 0.0f, 0.0f, 0.5f, 1.0f, 0.13534f, 0.018f, 0.0024f, 0.0f, 0.0f, 0.0f };
|
|
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
|
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.startRelease(4);
|
2020-01-05 00:19:08 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Lower sustain")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.02f;
|
|
|
|
|
region.amplitudeEG.release = 0.04f;
|
|
|
|
|
region.amplitudeEG.delay = 0.02f;
|
|
|
|
|
region.amplitudeEG.sustain = 50.0f;
|
2020-03-20 13:03:26 +01:00
|
|
|
std::array<float, 10> output;
|
|
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2020-04-01 00:18:03 +02:00
|
|
|
std::array<float, 10> expected { 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f };
|
|
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2020-01-05 00:19:08 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Decay")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.02f;
|
|
|
|
|
region.amplitudeEG.release = 0.04f;
|
|
|
|
|
region.amplitudeEG.delay = 0.02f;
|
|
|
|
|
region.amplitudeEG.sustain = 50.0f;
|
|
|
|
|
region.amplitudeEG.decay = 0.02f;
|
2020-03-20 13:03:26 +01:00
|
|
|
std::array<float, 10> output;
|
|
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2020-04-01 00:18:03 +02:00
|
|
|
std::array<float, 10> expected { 0.0f, 0.0f, 0.5f, 1.0f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5 };
|
|
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2020-01-05 00:19:08 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Hold")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.02f;
|
|
|
|
|
region.amplitudeEG.release = 0.04f;
|
|
|
|
|
region.amplitudeEG.delay = 0.02f;
|
|
|
|
|
region.amplitudeEG.sustain = 50.0f;
|
|
|
|
|
region.amplitudeEG.decay = 0.02f;
|
|
|
|
|
region.amplitudeEG.hold = 0.02f;
|
2020-03-20 13:03:26 +01:00
|
|
|
std::array<float, 12> output;
|
|
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
|
|
|
|
std::array<float, 12> expected { 0.0f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f, 0.707107f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f };
|
2020-04-01 00:18:03 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2020-01-05 00:19:08 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Hold with release")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.02f;
|
|
|
|
|
region.amplitudeEG.release = 0.04f;
|
|
|
|
|
region.amplitudeEG.delay = 0.02f;
|
|
|
|
|
region.amplitudeEG.sustain = 50.0f;
|
|
|
|
|
region.amplitudeEG.decay = 0.02f;
|
|
|
|
|
region.amplitudeEG.hold = 0.02f;
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.startRelease(8);
|
2020-03-31 00:03:21 +02:00
|
|
|
std::array<float, 15> output;
|
|
|
|
|
std::array<float, 15> expected { 0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f, 0.707107f, 0.5f, 0.05f, 0.005f, 0.0005f, 0.00005f, 0.0f, 0.0f };
|
2020-04-01 00:18:03 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
|
|
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.startRelease(8);
|
2020-01-05 00:19:08 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2019-08-22 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[ADSREnvelope] Hold with release 2")
|
|
|
|
|
{
|
|
|
|
|
sfz::ADSREnvelope<float> envelope;
|
2020-02-17 20:51:41 +01:00
|
|
|
sfz::MidiState state;
|
|
|
|
|
sfz::Region region { state };
|
|
|
|
|
region.amplitudeEG.attack = 0.02f;
|
|
|
|
|
region.amplitudeEG.release = 0.04f;
|
|
|
|
|
region.amplitudeEG.delay = 0.02f;
|
|
|
|
|
region.amplitudeEG.sustain = 50.0f;
|
|
|
|
|
region.amplitudeEG.decay = 0.02f;
|
|
|
|
|
region.amplitudeEG.hold = 0.02f;
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.startRelease(4);
|
2020-03-31 00:03:21 +02:00
|
|
|
std::array<float, 15> output;
|
2020-04-01 00:18:03 +02:00
|
|
|
std::array<float, 15> expected { 0.0f, 0.0f, 0.5f, 1.0f, 0.08409f, 0.00707f, 0.000594604f, 0.00005f, 0.0f, 0.0f, 0.0f, 0.0 };
|
|
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2020-03-20 13:03:26 +01:00
|
|
|
envelope.reset(region.amplitudeEG, region, state, 0, 0.0f, 100.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.startRelease(4);
|
2020-01-05 00:19:08 +01:00
|
|
|
absl::c_fill(output, -1.0f);
|
2019-08-22 17:18:09 +02:00
|
|
|
envelope.getBlock(absl::MakeSpan(output));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual<float>(output, expected));
|
2020-01-05 00:19:08 +01:00
|
|
|
}
|