Fix tests
This commit is contained in:
parent
13752d8043
commit
e14c51e8da
3 changed files with 33 additions and 34 deletions
|
|
@ -295,10 +295,9 @@ const std::array<float, 1024> MipmapRange::FrequencyToIndex = []()
|
||||||
std::array<float, 1024> table;
|
std::array<float, 1024> table;
|
||||||
|
|
||||||
for (unsigned i = 0; i < table.size() - 1; ++i) {
|
for (unsigned i = 0; i < table.size() - 1; ++i) {
|
||||||
double r = i * (1.0 / (table.size() - 1));
|
float r = i * (1.0f / (table.size() - 1));
|
||||||
double f = F1 + r * (FN - F1);
|
float f = F1 + r * (FN - F1);
|
||||||
double t = std::log(K * f) / LogB;
|
table[i] = getExactIndexForFrequency(f);
|
||||||
table[i] = clamp<float>(t, 0, N - 1);
|
|
||||||
}
|
}
|
||||||
// ensure the last element to be exact
|
// ensure the last element to be exact
|
||||||
table[table.size() - 1] = N - 1;
|
table[table.size() - 1] = N - 1;
|
||||||
|
|
@ -321,6 +320,12 @@ float MipmapRange::getIndexForFrequency(float f)
|
||||||
frac * FrequencyToIndex[index2];
|
frac * FrequencyToIndex[index2];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float MipmapRange::getExactIndexForFrequency(float f)
|
||||||
|
{
|
||||||
|
float t = (f < F1) ? 0.0f : (std::log(K * f) / LogB);
|
||||||
|
return clamp<float>(t, 0, N - 1);
|
||||||
|
}
|
||||||
|
|
||||||
const std::array<float, MipmapRange::N + 1> MipmapRange::IndexToStartFrequency = []()
|
const std::array<float, MipmapRange::N + 1> MipmapRange::IndexToStartFrequency = []()
|
||||||
{
|
{
|
||||||
std::array<float, N + 1> table;
|
std::array<float, N + 1> table;
|
||||||
|
|
|
||||||
|
|
@ -142,6 +142,7 @@ public:
|
||||||
static constexpr float FN = 12000.0;
|
static constexpr float FN = 12000.0;
|
||||||
|
|
||||||
static float getIndexForFrequency(float f);
|
static float getIndexForFrequency(float f);
|
||||||
|
static float getExactIndexForFrequency(float f);
|
||||||
static MipmapRange getRangeForIndex(int o);
|
static MipmapRange getRangeForIndex(int o);
|
||||||
static MipmapRange getRangeForFrequency(float f);
|
static MipmapRange getRangeForFrequency(float f);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,45 +12,38 @@
|
||||||
|
|
||||||
TEST_CASE("[Wavetables] Frequency ranges")
|
TEST_CASE("[Wavetables] Frequency ranges")
|
||||||
{
|
{
|
||||||
int cur_oct = std::numeric_limits<int>::min();
|
int cur_index = std::numeric_limits<int>::min();
|
||||||
int min_oct = std::numeric_limits<int>::max();
|
int min_index = std::numeric_limits<int>::max();
|
||||||
int max_oct = std::numeric_limits<int>::min();
|
int max_index = std::numeric_limits<int>::min();
|
||||||
|
|
||||||
for (int note = 0; note < 128; ++note) {
|
for (int note = 0; note < 128; ++note) {
|
||||||
double f = midiNoteFrequency(note);
|
double f = midiNoteFrequency(note);
|
||||||
|
|
||||||
int oct = sfz::WavetableRange::getOctaveForFrequency(f);
|
float fractionalIndex = sfz::MipmapRange::getExactIndexForFrequency(f);
|
||||||
|
int index = static_cast<int>(fractionalIndex);
|
||||||
|
|
||||||
REQUIRE(oct >= 0);
|
REQUIRE(index >= 0);
|
||||||
REQUIRE(oct < sfz::WavetableRange::countOctaves);
|
REQUIRE(static_cast<unsigned>(index) < sfz::MipmapRange::N);
|
||||||
|
|
||||||
REQUIRE(oct >= cur_oct);
|
float lerpFractionalIndex = sfz::MipmapRange::getIndexForFrequency(f);
|
||||||
cur_oct = oct;
|
int lerpIndex = static_cast<int>(lerpFractionalIndex);
|
||||||
|
|
||||||
min_oct = std::min(min_oct, oct);
|
// approximation should be equal or off by 1 table in worst cases
|
||||||
max_oct = std::max(max_oct, oct);
|
bool lerpIndexValid = (lerpIndex - index) == 0 || (lerpIndex - index) == -1;
|
||||||
|
REQUIRE(lerpIndexValid);
|
||||||
|
|
||||||
sfz::WavetableRange range = sfz::WavetableRange::getRangeForOctave(oct);
|
REQUIRE(index >= cur_index);
|
||||||
REQUIRE((f >= range.minFrequency || oct == 0));
|
cur_index = index;
|
||||||
REQUIRE((f <= range.maxFrequency || oct == sfz::WavetableRange::countOctaves - 1));
|
|
||||||
|
min_index = std::min(min_index, index);
|
||||||
|
max_index = std::max(max_index, index);
|
||||||
|
|
||||||
|
sfz::MipmapRange range = sfz::MipmapRange::getRangeForIndex(index);
|
||||||
|
REQUIRE((f >= range.minFrequency || index == 0));
|
||||||
|
REQUIRE((f <= range.maxFrequency || index == sfz::MipmapRange::N - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
// check ranges to be decently adjusted to the MIDI frequency range
|
// check ranges to be decently adjusted to the MIDI frequency range
|
||||||
REQUIRE(min_oct == 0);
|
REQUIRE(min_index == 0);
|
||||||
REQUIRE(max_oct == sfz::WavetableRange::countOctaves - 1);
|
REQUIRE(max_index == sfz::MipmapRange::N - 1);
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("[Wavetables] Octave number lookup")
|
|
||||||
{
|
|
||||||
for (int note = 0; note < 128; ++note) {
|
|
||||||
double f = midiNoteFrequency(note);
|
|
||||||
|
|
||||||
float ref = std::log2(f * sfz::WavetableRange::frequencyScaleFactor);
|
|
||||||
float oct = sfz::WavetableRange::getFractionalOctaveForFrequency(f);
|
|
||||||
|
|
||||||
ref = clamp<float>(ref, 0, sfz::WavetableRange::countOctaves - 1);
|
|
||||||
oct = clamp<float>(oct, 0, sfz::WavetableRange::countOctaves - 1);
|
|
||||||
|
|
||||||
REQUIRE(oct == Approx(ref).margin(0.03f));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue