diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index 90c23c70..823a8653 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -481,7 +481,7 @@ sfz::Voice* sfz::Synth::findFreeVoice() noexcept // Find voices that can be stolen absl::c_sort(voiceViewArray, [](Voice* lhs, Voice* rhs) { - return lhs->getMeanSquaredAverage() < rhs->getMeanSquaredAverage(); + return lhs->getAverageEnvelope() < rhs->getAverageEnvelope(); }); voiceViewArray.front()->reset(); diff --git a/src/sfizz/Voice.cpp b/src/sfizz/Voice.cpp index 992d97e7..fc7bd4f0 100644 --- a/src/sfizz/Voice.cpp +++ b/src/sfizz/Voice.cpp @@ -240,7 +240,13 @@ void sfz::Voice::renderBlock(AudioSpan buffer) noexcept updateChannelPowers(buffer); - this->triggerDelay = absl::nullopt; + age += buffer.getNumFrames(); + if (triggerDelay) { + // Should be OK but just in case; + age = min(age - *triggerDelay, 0); + triggerDelay = absl::nullopt; + } + #if 0 ASSERT(!hasNanInf(buffer.getConstSpan(0))); ASSERT(!hasNanInf(buffer.getConstSpan(1))); @@ -629,6 +635,7 @@ void sfz::Voice::reset() noexcept region = nullptr; currentPromise.reset(); sourcePosition = 0; + age = 0; floatPositionOffset = 0.0f; noteIsOff = false; @@ -642,7 +649,7 @@ void sfz::Voice::reset() noexcept equalizers.clear(); } -float sfz::Voice::getMeanSquaredAverage() const noexcept +float sfz::Voice::getAverageEnvelope() const noexcept { return max(channelPowers[0], channelPowers[1]); } diff --git a/src/sfizz/Voice.h b/src/sfizz/Voice.h index a8d30cbf..e3572c65 100644 --- a/src/sfizz/Voice.h +++ b/src/sfizz/Voice.h @@ -182,7 +182,7 @@ public: * * @return float */ - float getMeanSquaredAverage() const noexcept; + float getAverageEnvelope() const noexcept; /** * @brief Get the position of the voice in the source, in samples * @@ -215,6 +215,13 @@ public: */ void release(int delay, bool fastRelease = false) noexcept; + /** + * @brief gets the age of the Voice + * + * @return + */ + int getAge() const noexcept { return age; } + Duration getLastDataDuration() const noexcept { return dataDuration; } Duration getLastAmplitudeDuration() const noexcept { return amplitudeDuration; } Duration getLastFilterDuration() const noexcept { return filterDuration; } @@ -271,6 +278,7 @@ private: float floatPositionOffset { 0.0f }; int sourcePosition { 0 }; int initialDelay { 0 }; + int age { 0 }; FilePromisePtr currentPromise { nullptr };