From 4d20f7722a3f6d082120b12e14e6f1a9074faacc Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 29 Sep 2020 02:40:38 +0200 Subject: [PATCH 1/2] Add math helper: fast fmod --- src/sfizz/MathHelpers.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/sfizz/MathHelpers.h b/src/sfizz/MathHelpers.h index f0dbebc4..d5b0cd3a 100644 --- a/src/sfizz/MathHelpers.h +++ b/src/sfizz/MathHelpers.h @@ -139,6 +139,20 @@ constexpr T clamp(T v, T lo, T hi) return max(min(v, hi), lo); } +/** + * @brief Compute the floating-point remainder (fmod) + * + * @tparam T + * @param x + * @param m + * @return T + */ +template +inline constexpr T fastFmod(T x, T m) +{ + return x - m * static_cast(x / m); +} + template inline CXX14_CONSTEXPR void incrementAll(T& only) { From d6e8d547e4c1dda80b2d2f595581238341ea61bf Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 29 Sep 2020 03:20:02 +0200 Subject: [PATCH 2/2] Fix the case when multiple loops occur in one buffer --- src/sfizz/Voice.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/sfizz/Voice.cpp b/src/sfizz/Voice.cpp index a60b1af7..1c36df54 100644 --- a/src/sfizz/Voice.cpp +++ b/src/sfizz/Voice.cpp @@ -549,12 +549,12 @@ void sfz::Voice::fillWithData(AudioSpan buffer) noexcept if (region->shouldLoop() && region->loopEnd(currentPromise->oversamplingFactor) <= source.getNumFrames()) { const auto loopEnd = static_cast(region->loopEnd(currentPromise->oversamplingFactor)); - const auto offset = loopEnd - static_cast(region->loopStart(currentPromise->oversamplingFactor)) + 1; - for (auto* index = indices->begin(); index < indices->end(); ++index) { - if (*index > loopEnd) { - const auto remainingElements = static_cast(std::distance(index, indices->end())); - subtract1(offset, { index, remainingElements }); - } + const auto loopStart = static_cast(region->loopStart(currentPromise->oversamplingFactor)); + const auto loopSize = loopEnd + 1 - loopStart; + for (auto* it = indices->begin(), *end = indices->end(); it < end; ++it) { + auto index = *it; + *it = (index < loopEnd + 1) ? index : + (loopStart + (index - loopStart) % loopSize); } } else { const auto sampleEnd = min(