From 0c9c188e4b7d2ef3e9d30de9cd8327fb625955b9 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Thu, 7 May 2020 13:49:13 +0200 Subject: [PATCH] Refine the stealing algorithm --- src/sfizz/Synth.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index 1d8ccb49..d59fc85e 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -503,26 +503,33 @@ sfz::Voice* sfz::Synth::findFreeVoice() noexcept // Find voices that can be stolen absl::c_sort(voiceViewArray, [](Voice* lhs, Voice* rhs) { - return lhs->getAge() < rhs->getAge(); + return lhs->getAge() > rhs->getAge(); }); const auto sumEnvelope = absl::c_accumulate(voiceViewArray, 0.0f, [] (float sum, const Voice* v) { return sum + v->getAverageEnvelope(); }); - const auto threshold = sumEnvelope / static_cast(voiceViewArray.size()) / 2; + const auto envThreshold = sumEnvelope / static_cast(voiceViewArray.size()) * 0.25f; + const auto ageThreshold = voiceViewArray.front()->getAge() * 0.5f; Voice* returnedVoice = voiceViewArray.front(); for (auto & voice : voiceViewArray) { - if (voice->getAverageEnvelope() < threshold) { + if (voice->getAge() < ageThreshold) { + // std::cout << "Went too far, picking the oldest note..." << '\n'; + break; + } + if (voice->getAverageEnvelope() < envThreshold) { + // std::cout << "Found a better candidate!" << '\n'; returnedVoice = voice; break; } } + const auto killedVoices = killSisterVoices(returnedVoice); UNUSED(killedVoices); // only in debug assert(killedVoices > 0); assert(returnedVoice->isFree()); - std::cout << "Killed " << killedVoices << " voices"; + // std::cout << "Killed " << killedVoices << " voices" << '\n'; return returnedVoice; }