From 7a59b35b803d1973ca587f8d65229356bcd0cd60 Mon Sep 17 00:00:00 2001 From: Paul Fd Date: Tue, 7 Apr 2020 17:55:22 +0200 Subject: [PATCH] Used clamp int64 and get the modified offsets in getOffset() --- src/sfizz/Defaults.h | 8 ++++---- src/sfizz/Region.cpp | 9 ++++++--- src/sfizz/Region.h | 8 ++++---- src/sfizz/Synth.cpp | 5 +---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/sfizz/Defaults.h b/src/sfizz/Defaults.h index 34a43ba5..4fe7e973 100644 --- a/src/sfizz/Defaults.h +++ b/src/sfizz/Defaults.h @@ -46,10 +46,10 @@ namespace Default constexpr float delay { 0.0 }; constexpr float delayRandom { 0.0 }; constexpr Range delayRange { 0.0, 100.0 }; - constexpr uint32_t offset { 0 }; - constexpr uint32_t offsetRandom { 0 }; - constexpr Range offsetRange { 0, std::numeric_limits::max() }; - constexpr Range offsetCCRange = offsetRange; + constexpr int64_t offset { 0 }; + constexpr int64_t offsetRandom { 0 }; + constexpr Range offsetRange { 0, std::numeric_limits::max() }; + constexpr Range offsetCCRange = offsetRange; constexpr Range sampleEndRange { 0, std::numeric_limits::max() }; constexpr Range sampleCountRange { 0, std::numeric_limits::max() }; constexpr SfzLoopMode loopMode { SfzLoopMode::no_loop }; diff --git a/src/sfizz/Region.cpp b/src/sfizz/Region.cpp index a8ffa43f..8fbed19c 100644 --- a/src/sfizz/Region.cpp +++ b/src/sfizz/Region.cpp @@ -1140,10 +1140,13 @@ float sfz::Region::getPhase() const noexcept return phase; } -uint32_t sfz::Region::getOffset(Oversampling factor) const noexcept +uint64_t sfz::Region::getOffset(Oversampling factor) const noexcept { - std::uniform_int_distribution offsetDistribution { 0, offsetRandom }; - return (offset + offsetDistribution(Random::randomGenerator)) * static_cast(factor); + std::uniform_int_distribution offsetDistribution { 0, offsetRandom }; + uint64_t finalOffset = offset + offsetDistribution(Random::randomGenerator); + for (const auto& mod: offsetCC) + finalOffset += static_cast(mod.value * midiState.getCCValue(mod.cc)); + return Default::offsetCCRange.clamp(offset + offsetDistribution(Random::randomGenerator)) * static_cast(factor); } float sfz::Region::getDelay() const noexcept diff --git a/src/sfizz/Region.h b/src/sfizz/Region.h index 6e93b96f..9540b1cc 100644 --- a/src/sfizz/Region.h +++ b/src/sfizz/Region.h @@ -189,7 +189,7 @@ struct Region { * * @return uint32_t */ - uint32_t getOffset(Oversampling factor = Oversampling::x1) const noexcept; + uint64_t getOffset(Oversampling factor = Oversampling::x1) const noexcept; /** * @brief Get the region delay in seconds * @@ -230,9 +230,9 @@ struct Region { std::string sample {}; // Sample float delay { Default::delay }; // delay float delayRandom { Default::delayRandom }; // delay_random - uint32_t offset { Default::offset }; // offset - uint32_t offsetRandom { Default::offsetRandom }; // offset_random - CCMap offsetCC { Default::offset }; + int64_t offset { Default::offset }; // offset + int64_t offsetRandom { Default::offsetRandom }; // offset_random + CCMap offsetCC { Default::offset }; uint32_t sampleEnd { Default::sampleEndRange.getEnd() }; // end absl::optional sampleCount {}; // count absl::optional loopMode {}; // loopmode diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index 46c0109d..0913f9a6 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -378,10 +378,7 @@ bool sfz::Synth::loadSfzFile(const fs::path& file) uint64_t sumOffsetCC = region->offset + region->offsetRandom; for (const auto& offsets: region->offsetCC) sumOffsetCC += offsets.value; - if (static_cast(Default::offsetCCRange.getEnd()) < sumOffsetCC) - return Default::offsetCCRange.getEnd(); - - return static_cast(sumOffsetCC); + return Default::offsetCCRange.clamp(sumOffsetCC); }(); if (!resources.filePool.preloadFile(region->sample, maxOffset))