sfizz/tests/EventEnvelopesT.cpp

277 lines
8.2 KiB
C++
Raw Normal View History

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/SfzHelpers.h"
#include "sfizz/Buffer.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 23:39:26 +02:00
#include <algorithm>
2019-08-25 14:01:03 +02:00
#include <array>
2019-08-22 23:39:26 +02:00
#include <iostream>
using namespace Catch::literals;
2019-08-25 14:01:03 +02:00
template <class Type>
inline bool approxEqual(absl::Span<const Type> lhs, absl::Span<const Type> rhs, Type eps = 1e-3)
2019-08-22 23:39:26 +02:00
{
if (lhs.size() != rhs.size())
return false;
for (size_t i = 0; i < rhs.size(); ++i)
2019-08-25 14:01:03 +02:00
if (rhs[i] != Approx(lhs[i]).epsilon(eps)) {
2019-08-22 23:39:26 +02:00
std::cerr << lhs[i] << " != " << rhs[i] << " at index " << i << '\n';
return false;
}
return true;
}
const auto idModifier = [] (float x) { return x; };
const auto twiceModifier = [] (float x) { return 2 * x; };
const auto expModifier = [] (float x) { return std::exp(x); };
TEST_CASE("[Envelopes] Empty")
2019-08-22 23:39:26 +02:00
{
sfz::EventVector events {
{0, 0.0f}
};
2019-08-22 23:39:26 +02:00
std::array<float, 5> output;
2020-01-05 00:19:08 +01:00
std::array<float, 5> expected { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
std::array<float, 5> expectedMul { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier);
REQUIRE(output == expected);
linearEnvelope(events, absl::MakeSpan(output), idModifier, 1.0f);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
multiplicativeEnvelope(events, absl::MakeSpan(output), expModifier);
REQUIRE(output == expectedMul);
multiplicativeEnvelope(events, absl::MakeSpan(output), expModifier, 2.0f);
REQUIRE(output == expectedMul);
2019-08-22 23:39:26 +02:00
}
TEST_CASE("[Envelopes] Linear basic")
2019-08-22 23:39:26 +02:00
{
sfz::EventVector events {
{0, 0.0f},
{4, 1.0f}
};
std::array<float, 9> output;
std::array<float, 9> expected { 0.0f, 0.25f, 0.5f, 0.75f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-22 23:39:26 +02:00
}
TEST_CASE("[LinearEnvelope] 2 events, close")
{
sfz::EventVector events {
{0, 0.0f},
{4, 1.0f},
{5, 2.0f}
};
std::array<float, 9> output;
std::array<float, 9> expected { 0.0f, 0.25f, 0.5f, 0.75f, 1.0f, 2.0f, 2.0f, 2.0f, 2.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-22 23:39:26 +02:00
}
TEST_CASE("[LinearEnvelope] 2 events, far")
2019-08-22 23:39:26 +02:00
{
sfz::EventVector events {
{0, 0.0f},
{2, 1.0f},
{6, 2.0f}
};
std::array<float, 9> output;
std::array<float, 9> expected { 0.0f, 0.5f, 1.0f, 1.25f, 1.5f, 1.75f, 2.0f, 2.0f, 2.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-22 23:39:26 +02:00
}
TEST_CASE("[LinearEnvelope] 3 events, out of block")
{
sfz::EventVector events {
{0, 0.0f},
{2, 1.0f},
{6, 2.0f},
{10, 3.0f}
};
std::array<float, 9> output;
std::array<float, 9> expected { 0.0f, 0.5f, 1.0f, 1.25f, 1.5f, 1.75f, 2.0f, 2.5f, 3.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-22 23:39:26 +02:00
}
TEST_CASE("[LinearEnvelope] 2 events, function")
{
sfz::EventVector events {
{0, 0.0f},
{2, 1.0f},
{6, 2.0f}
};
std::array<float, 9> output;
std::array<float, 9> expected { 0.0f, 1.0f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.0f, 4.0f };
linearEnvelope(events, absl::MakeSpan(output), twiceModifier);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-12-12 18:27:18 +01:00
}
TEST_CASE("[LinearEnvelope] Get quantized")
{
sfz::EventVector events {
{0, 0.0f},
{2, 1.0f},
{6, 2.0f}
};
2019-12-12 18:27:18 +01:00
std::array<float, 8> output;
std::array<float, 8> expected { 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier, 1.0f);
2019-12-12 18:27:18 +01:00
REQUIRE(output == expected);
}
TEST_CASE("[LinearEnvelope] Get quantized with unquantized targets")
{
sfz::EventVector events {
{0, 0.0f},
{2, 1.1f},
{6, 1.9f}
};
std::array<float, 8> output;
std::array<float, 8> expected { 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier, 1.0f);
REQUIRE(output == expected);
}
2019-12-12 18:27:18 +01:00
TEST_CASE("[LinearEnvelope] Get quantized with 2 steps")
{
sfz::EventVector events {
{0, 0.0f},
{2, 1.0f},
{6, 3.0f}
};
2019-12-12 18:27:18 +01:00
std::array<float, 8> output;
std::array<float, 8> expected { 0.0f, 0.0f, 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier, 1.0f);
2019-12-12 18:27:18 +01:00
REQUIRE(output == expected);
}
TEST_CASE("[LinearEnvelope] Get quantized with 2 steps and an unquantized out of block step")
{
sfz::EventVector events {
{0, 0.0f},
{2, 1.0f},
{6, 3.0f},
{10, 4.2f},
};
std::array<float, 8> output;
std::array<float, 8> expected { 0.0f, 0.0f, 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 4.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier, 1.0f);
REQUIRE(output == expected);
}
TEST_CASE("[LinearEnvelope] Going down quantized with 2 steps")
{
sfz::EventVector events {
{0, 3.0f},
{2, 2.0f},
{6, 0.0f}
};
2019-12-12 18:27:18 +01:00
std::array<float, 8> output;
std::array<float, 8> expected { 3.0f, 3.0f, 2.0f, 2.0f, 1.0f, 1.0f, 0.0f, 0.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier, 1.0f);
REQUIRE(output == expected);
}
TEST_CASE("[MultiplicativeEnvelope] Basic event")
{
sfz::EventVector events {
{0, 1.0f},
{4, 2.0f}
};
std::array<float, 8> output;
std::array<float, 8> expected { 1.0f, 1.1892f, 1.4142f, 1.68176f, 2.0f, 2.0f, 2.0f, 2.0f };
multiplicativeEnvelope(events, absl::MakeSpan(output), idModifier);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[MultiplicativeEnvelope] 2 events")
{
sfz::EventVector events {
{0, 1.0f},
{4, 2.0f},
{5, 4.0f}
};
std::array<float, 8> output;
std::array<float, 8> expected { 1.0f, 1.1892f, 1.4142f, 1.68176f, 2.0f, 4.0f, 4.0f, 4.0f};
multiplicativeEnvelope(events, absl::MakeSpan(output), idModifier);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[MultiplicativeEnvelope] 2 events, far")
{
sfz::EventVector events {
{0, 1.0f},
{2, 2.0f},
{6, 4.0f}
};
std::array<float, 8> output;
std::array<float, 8> expected { 1.0f, 1.4142f, 2.0f, 2.37841f, 2.82843f, 3.36358f, 4.0f, 4.0f};
multiplicativeEnvelope(events, absl::MakeSpan(output), idModifier);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[MultiplicativeEnvelope] Get quantized with 2 steps")
{
sfz::EventVector events {
{0, 1.0f},
{2, 2.0f},
{6, 4.0f}
};
std::array<float, 8> output;
std::array<float, 8> expected { 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, 2.0f, 4.0f, 4.0f};
multiplicativeEnvelope(events, absl::MakeSpan(output), idModifier, 2.0f);
REQUIRE(output == expected);
}
TEST_CASE("[MultiplicativeEnvelope] Get quantized with an unquantized out of range step")
{
sfz::EventVector events {
{0, 1.0f},
{2, 2.0f},
{6, 4.0f},
{10, 8.2f}
};
std::array<float, 8> output;
std::array<float, 8> expected { 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, 2.0f, 4.0f, 8.0f };
multiplicativeEnvelope(events, absl::MakeSpan(output), idModifier, 2.0f);
REQUIRE(output == expected);
}
TEST_CASE("[MultiplicativeEnvelope] Going down quantized with 2 steps")
{
sfz::EventVector events {
{0, 4.0f},
{2, 2.0f},
{6, 0.5f}
};
std::array<float, 8> output;
std::array<float, 8> expected { 4.0f, 4.0f, 2.0f, 2.0f, 1.0f, 1.0f, 0.5f, 0.5f };
multiplicativeEnvelope(events, absl::MakeSpan(output), idModifier, 2.0f);
REQUIRE(output == expected);
}
TEST_CASE("[MultiplicativeEnvelope] Get quantized with unclean events")
{
sfz::EventVector events {
{0, 1.0f},
{2, 1.2f},
{6, 2.5f}
};
std::array<float, 8> output;
std::array<float, 8> expected { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f };
multiplicativeEnvelope(events, absl::MakeSpan(output), idModifier, 2.0f);
REQUIRE(output == expected);
}