Use rint, rounding modes, and corrected tests

This commit is contained in:
Paul Fd 2020-04-14 17:59:12 +02:00
parent aeba9dfaab
commit d120c2bc9a
3 changed files with 87 additions and 63 deletions

View file

@ -15,26 +15,27 @@
#include <algorithm>
#include <cmath>
#include <random>
#include <cfenv>
template<class T>
template <class T>
constexpr T max(T op1, T op2)
{
return op1 > op2 ? op1 : op2;
}
template<class T, class... Args>
template <class T, class... Args>
constexpr T max(T op1, Args... rest)
{
return max(op1, max(rest...));
}
template<class T>
template <class T>
constexpr T min(T op1, T op2)
{
return op1 > op2 ? op2 : op1;
}
template<class T, class... Args>
template <class T, class... Args>
constexpr T min(T op1, Args... rest)
{
return min(op1, min(rest...));
@ -46,7 +47,7 @@ constexpr T min(T op1, Args... rest)
* @param op
* @return T
*/
template<class T>
template <class T>
constexpr T power2(T in)
{
return in * in;
@ -111,8 +112,8 @@ constexpr Type mag2db(Type in)
*
*/
namespace Random {
static std::random_device randomDevice;
static std::minstd_rand randomGenerator { randomDevice() };
static std::random_device randomDevice;
static std::minstd_rand randomGenerator { randomDevice() };
} // namespace Random
/**
@ -135,42 +136,42 @@ inline float midiNoteFrequency(const int noteNumber)
* @param hi
* @return T
*/
template<class T>
constexpr T clamp( T v, T lo, T hi )
template <class T>
constexpr T clamp(T v, T lo, T hi)
{
return max(min(v, hi), lo);
}
template<int Increment = 1, class T>
template <int Increment = 1, class T>
inline CXX14_CONSTEXPR void incrementAll(T& only)
{
only += Increment;
}
template<int Increment = 1, class T, class... Args>
template <int Increment = 1, class T, class... Args>
inline CXX14_CONSTEXPR void incrementAll(T& first, Args&... rest)
{
first += Increment;
incrementAll<Increment>(rest...);
}
template<class ValueType>
template <class ValueType>
constexpr ValueType linearInterpolation(ValueType left, ValueType right, ValueType leftCoeff, ValueType rightCoeff)
{
return left * leftCoeff + right * rightCoeff;
}
template<class Type>
template <class Type>
constexpr Type pi() { return static_cast<Type>(3.141592653589793238462643383279502884); };
template<class Type>
template <class Type>
constexpr Type twoPi() { return pi<Type>() * 2; };
template<class Type>
template <class Type>
constexpr Type piTwo() { return pi<Type>() / 2; };
template<class Type>
template <class Type>
constexpr Type piFour() { return pi<Type>() / 4; };
template<class Type>
template <class Type>
constexpr Type sqrtTwo() { return static_cast<Type>(1.414213562373095048801688724209698078569671875376948073176); };
template<class Type>
template <class Type>
constexpr Type sqrtTwoInv() { return static_cast<Type>(0.707106781186547524400844362104849039284835937688474036588); };
/**
@ -205,23 +206,23 @@ inline Fraction<I>::operator float() const noexcept
template <class F>
struct FP_traits;
template <> struct FP_traits<double>
{
template <>
struct FP_traits<double> {
typedef double type;
typedef uint64_t same_size_int;
static_assert(sizeof(type) == sizeof(same_size_int),
"Unexpected size of floating point type");
"Unexpected size of floating point type");
static constexpr int e_bits = 11;
static constexpr int m_bits = 52;
static constexpr int e_offset = -1023;
};
template <> struct FP_traits<float>
{
template <>
struct FP_traits<float> {
typedef float type;
typedef uint32_t same_size_int;
static_assert(sizeof(type) == sizeof(same_size_int),
"Unexpected size of floating point type");
"Unexpected size of floating point type");
static constexpr int e_bits = 8;
static constexpr int m_bits = 23;
static constexpr int e_offset = -127;
@ -237,7 +238,10 @@ template <class F>
inline bool fp_sign(F x)
{
typedef FP_traits<F> T;
union { F real; typename T::same_size_int integer; } u;
union {
F real;
typename T::same_size_int integer;
} u;
u.real = x;
return ((u.integer >> (T::e_bits + T::m_bits)) & 1) != 0;
}
@ -254,7 +258,10 @@ template <class F>
inline int fp_exponent(F x)
{
typedef FP_traits<F> T;
union { F real; typename T::same_size_int integer; } u;
union {
F real;
typename T::same_size_int integer;
} u;
u.real = x;
int ex = (u.integer >> T::m_bits) & ((1u << T::e_bits) - 1);
return ex + T::e_offset;
@ -269,10 +276,13 @@ template <class F>
inline Fraction<uint64_t> fp_mantissa(F x)
{
typedef FP_traits<F> T;
union { F real; typename T::same_size_int integer; } u;
union {
F real;
typename T::same_size_int integer;
} u;
u.real = x;
Fraction<uint64_t> f;
f.den = uint64_t{1} << T::m_bits;
f.den = uint64_t { 1 } << T::m_bits;
f.num = u.integer & (f.den - 1);
return f;
}
@ -287,10 +297,11 @@ inline F fp_from_parts(bool sgn, int ex, uint64_t mant)
{
typedef FP_traits<F> T;
typedef typename T::same_size_int I;
union { F real; I integer; } u;
u.integer = mant |
(static_cast<I>(ex - T::e_offset) << T::m_bits) |
(static_cast<I>(sgn) << (T::e_bits + T::m_bits));
union {
F real;
I integer;
} u;
u.integer = mant | (static_cast<I>(ex - T::e_offset) << T::m_bits) | (static_cast<I>(sgn) << (T::e_bits + T::m_bits));
return u.real;
}
@ -328,3 +339,20 @@ bool isValidAudio(absl::Span<Type> span)
return true;
}
class ScopedRoundingMode {
public:
ScopedRoundingMode() = delete;
ScopedRoundingMode(int newRoundingMode)
: savedFloatMode(std::fegetround())
{
std::fesetround(newRoundingMode);
}
~ScopedRoundingMode()
{
std::fesetround(savedFloatMode);
}
private:
const int savedFloatMode;
};

View file

@ -5,6 +5,7 @@
#include "SfzHelpers.h"
#include "Resources.h"
#include "absl/types/span.h"
namespace sfz {
/**
@ -146,6 +147,8 @@ void multiplicativeEnvelope(const EventVector& events, absl::Span<float> envelop
ASSERT(events[0].delay == 0);
ASSERT(step != 0.0f);
ScopedRoundingMode roundingMode { Round ? FE_TONEAREST : FE_TOWARDZERO };
if (envelope.size() == 0)
return;
const auto maxDelay = static_cast<int>(envelope.size() - 1);
@ -156,14 +159,7 @@ void multiplicativeEnvelope(const EventVector& events, absl::Span<float> envelop
// log q log q
// and log(b)\log(q) is between 0 and 1.
auto quantize = [logStep](float value) -> float {
IF_CONSTEXPR(Round)
{
return std::exp(logStep * std::round(std::log(value) / logStep));
}
else
{
return std::exp(logStep * std::trunc(std::log(value) / logStep));
}
return std::exp(logStep * std::rint(std::log(value) / logStep));
};
auto lastValue = quantize(lambda(events[0].value));
@ -182,7 +178,7 @@ void multiplicativeEnvelope(const EventVector& events, absl::Span<float> envelop
const auto numSteps = std::round(std::log(difference) / logStep);
const auto stepLength = static_cast<int>(length / numSteps);
for (int i = 0; i < numSteps; ++i) {
for (int i = 0; i < static_cast<int>(numSteps); ++i) {
fill<float>(envelope.subspan(lastDelay, stepLength), lastValue);
lastValue = nextValue > lastValue ? lastValue * step : lastValue / step;
lastDelay += stepLength;

View file

@ -42,13 +42,13 @@ TEST_CASE("[Envelopes] Empty")
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);
REQUIRE(approxEqual<float>(output, expected));
linearEnvelope(events, absl::MakeSpan(output), idModifier, 1.0f);
REQUIRE(output == expected);
REQUIRE(approxEqual<float>(output, expected));
multiplicativeEnvelope(events, absl::MakeSpan(output), expModifier);
REQUIRE(output == expectedMul);
REQUIRE(approxEqual<float>(output, expectedMul));
multiplicativeEnvelope(events, absl::MakeSpan(output), expModifier, 2.0f);
REQUIRE(output == expectedMul);
REQUIRE(approxEqual<float>(output, expectedMul));
}
TEST_CASE("[Envelopes] Linear basic")
@ -60,7 +60,7 @@ TEST_CASE("[Envelopes] Linear basic")
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);
REQUIRE(output == expected);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[LinearEnvelope] 2 events, close")
@ -73,7 +73,7 @@ TEST_CASE("[LinearEnvelope] 2 events, close")
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);
REQUIRE(output == expected);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[LinearEnvelope] 2 events, far")
@ -86,7 +86,7 @@ TEST_CASE("[LinearEnvelope] 2 events, far")
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);
REQUIRE(output == expected);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[LinearEnvelope] 3 events, out of block")
@ -100,7 +100,7 @@ TEST_CASE("[LinearEnvelope] 3 events, out of block")
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);
REQUIRE(output == expected);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[LinearEnvelope] 2 events, function")
@ -113,7 +113,7 @@ TEST_CASE("[LinearEnvelope] 2 events, function")
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);
REQUIRE(output == expected);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[LinearEnvelope] Get quantized")
@ -126,7 +126,7 @@ TEST_CASE("[LinearEnvelope] Get quantized")
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);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[LinearEnvelope] Get quantized with unquantized targets")
@ -139,7 +139,7 @@ TEST_CASE("[LinearEnvelope] Get quantized with unquantized targets")
std::array<float, 8> output;
std::array<float, 8> expected { 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
linearEnvelope(events, absl::MakeSpan(output), idModifier, 1.0f);
REQUIRE(output == expected);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[LinearEnvelope] Get quantized with 2 steps")
@ -152,7 +152,7 @@ TEST_CASE("[LinearEnvelope] Get quantized with 2 steps")
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);
REQUIRE(output == expected);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[LinearEnvelope] Get quantized with 2 steps and an unquantized out of block step")
@ -166,7 +166,7 @@ TEST_CASE("[LinearEnvelope] Get quantized with 2 steps and an unquantized out of
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);
REQUIRE(approxEqual<float>(output, expected));
}
@ -180,7 +180,7 @@ TEST_CASE("[LinearEnvelope] Going down quantized with 2 steps")
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);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[MultiplicativeEnvelope] Basic event")
@ -231,7 +231,7 @@ TEST_CASE("[MultiplicativeEnvelope] Get quantized with 2 steps")
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);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[MultiplicativeEnvelope] Get quantized with an unquantized out of range step")
@ -245,7 +245,7 @@ TEST_CASE("[MultiplicativeEnvelope] Get quantized with an unquantized out of ran
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);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[MultiplicativeEnvelope] Going down quantized with 2 steps")
@ -258,7 +258,7 @@ TEST_CASE("[MultiplicativeEnvelope] Going down quantized with 2 steps")
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);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[MultiplicativeEnvelope] Get quantized with unclean events")
@ -271,7 +271,7 @@ TEST_CASE("[MultiplicativeEnvelope] Get quantized with unclean events")
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);
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[linearModifiers] Compare with envelopes")
@ -284,7 +284,7 @@ TEST_CASE("[linearModifiers] Compare with envelopes")
ccData.data.value = 100.0f;
resources.midiState.ccEvent(5, 20, 0.1);
resources.midiState.ccEvent(10, 20, 0.2);
resources.midiState.ccEvent(10, 20, 0.8);
std::array<float, 16> output;
std::array<float, 16> envelope;
@ -312,7 +312,7 @@ TEST_CASE("[linearModifiers] Compare with envelopes")
ccData.data.curve = 2;
ccData.data.value = 20.0f;
ccData.data.step = 2.0f;
ccData.data.step = 2.5f;
linearEnvelope(resources.midiState.getCCEvents(20), absl::MakeSpan(envelope), [&ccData](float x) {
return ccData.data.value * (1 - x);
}, ccData.data.step);
@ -330,7 +330,7 @@ TEST_CASE("[multiplicativeModifiers] Compare with envelopes")
ccData.data.value = 100.0f;
resources.midiState.ccEvent(5, 20, 0.1);
resources.midiState.ccEvent(10, 20, 0.8);
resources.midiState.ccEvent(15, 20, 0.8);
std::array<float, 16> output;
std::array<float, 16> envelope;
@ -364,7 +364,7 @@ TEST_CASE("[multiplicativeModifiers] Compare with envelopes")
ccData.data.curve = 2;
ccData.data.value = 20.0f;
ccData.data.step = 2.0f;
ccData.data.step = 2.5f;
multiplicativeEnvelope(resources.midiState.getCCEvents(20), absl::MakeSpan(envelope), [&ccData](float x) {
return db2mag(ccData.data.value * (1 - x));
}, db2mag(ccData.data.step) );