From 6c2f5b5ecb59dd5d6a48b9d4a89e5d87f20f6720 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Tue, 17 Dec 2019 21:12:11 +0100 Subject: [PATCH] Corrected the multiplicative envelope logic The previous version alternatively cleared or ignored messages every other block --- src/sfizz/EventEnvelopes.cpp | 21 ++++++++++++--------- tests/EventEnvelopesT.cpp | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/sfizz/EventEnvelopes.cpp b/src/sfizz/EventEnvelopes.cpp index 8887fd33..1128eba6 100644 --- a/src/sfizz/EventEnvelopes.cpp +++ b/src/sfizz/EventEnvelopes.cpp @@ -57,7 +57,9 @@ void EventEnvelope::setFunction(std::function function) template void EventEnvelope::registerEvent(int timestamp, Type inputValue) { - + if (resetEvents) + clear(); + if (static_cast(events.size()) < maxCapacity) events.emplace_back(timestamp, function(inputValue)); } @@ -65,14 +67,14 @@ void EventEnvelope::registerEvent(int timestamp, Type inputValue) template void EventEnvelope::prepareEvents() { - if (resetEvents) { + if (resetEvents) clear(); - } else { - absl::c_sort(events, [](const auto& lhs, const auto& rhs) { - return lhs.first < rhs.first; - }); - resetEvents = true; - } + + absl::c_sort(events, [](const auto& lhs, const auto& rhs) { + return lhs.first < rhs.first; + }); + + resetEvents = true; } template @@ -201,7 +203,8 @@ void MultiplicativeEnvelope::getBlock(absl::Span output) } const auto step = std::exp((std::log(event.second) - std::log(currentValue)) / length); - currentValue = multiplicativeRamp(output.subspan(index, length), currentValue, step); + multiplicativeRamp(output.subspan(index, length), currentValue, step); + currentValue = event.second; index += length; } diff --git a/tests/EventEnvelopesT.cpp b/tests/EventEnvelopesT.cpp index 4213f04a..10fc2c0d 100644 --- a/tests/EventEnvelopesT.cpp +++ b/tests/EventEnvelopesT.cpp @@ -22,6 +22,8 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "EventEnvelopes.h" +#include "SfzHelpers.h" +#include "Buffer.h" #include "catch2/catch.hpp" #include #include @@ -385,3 +387,20 @@ TEST_CASE("[MultiplicativeEnvelope] Going down quantized with 2 steps and starti envelope.getQuantizedBlock(absl::MakeSpan(output), 2.0f); REQUIRE(output == expected); } + +TEST_CASE("[MultiplicativeEnvelope] Pitch envelope basic function") +{ + sfz::Buffer output { 256 }; + sfz::MultiplicativeEnvelope envelope; + envelope.setFunction([](float pitchValue){ + const auto normalizedBend = sfz::normalizeBend(pitchValue); + const auto bendInCents = normalizedBend * 200; + return sfz::centsFactor(bendInCents); + }); + envelope.reset(0); + envelope.getBlock(absl::MakeSpan(output)); + REQUIRE(output[255] == 1.0_a); + envelope.registerEvent(252, -6020); + envelope.getBlock(absl::MakeSpan(output)); + REQUIRE(output[255] == Approx(0.9168).epsilon(0.01)); +}