From 87c3cfacedd49676e94b549d0f05b30d74c04e24 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Tue, 18 Feb 2020 14:44:48 +0100 Subject: [PATCH] The voice state matches its envelope release state --- src/sfizz/ADSREnvelope.cpp | 13 +++++++++++-- src/sfizz/ADSREnvelope.h | 9 ++++++++- src/sfizz/Voice.cpp | 13 ++----------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/sfizz/ADSREnvelope.cpp b/src/sfizz/ADSREnvelope.cpp index d5aa2c87..c1fad38e 100644 --- a/src/sfizz/ADSREnvelope.cpp +++ b/src/sfizz/ADSREnvelope.cpp @@ -205,9 +205,15 @@ void ADSREnvelope::getBlock(absl::Span output) noexcept } } template -bool ADSREnvelope::isSmoothing() noexcept +bool ADSREnvelope::isSmoothing() const noexcept { - return currentState != State::Done; + return (currentState != State::Done); +} + +template +bool ADSREnvelope::isReleased() const noexcept +{ + return (currentState == State::Release); } template @@ -222,6 +228,9 @@ void ADSREnvelope::startRelease(int releaseDelay, bool fastRelease) noexce shouldRelease = true; this->releaseDelay = releaseDelay; + if (releaseDelay == 0) + currentState = State::Release; + if (fastRelease) this->release = 0; } diff --git a/src/sfizz/ADSREnvelope.h b/src/sfizz/ADSREnvelope.h index afa45f91..f2468a09 100644 --- a/src/sfizz/ADSREnvelope.h +++ b/src/sfizz/ADSREnvelope.h @@ -58,7 +58,14 @@ public: * @return true * @return false */ - bool isSmoothing() noexcept; + bool isSmoothing() const noexcept; + /** + * @brief Is the envelope released? + * + * @return true + * @return false + */ + bool isReleased() const noexcept; /** * @brief Get the remaining delay samples * diff --git a/src/sfizz/Voice.cpp b/src/sfizz/Voice.cpp index 95d854c5..0ec124ee 100644 --- a/src/sfizz/Voice.cpp +++ b/src/sfizz/Voice.cpp @@ -125,7 +125,6 @@ void sfz::Voice::release(int delay, bool fastRelease) noexcept if (egEnvelope.getRemainingDelay() > std::max(0, delay - initialDelay)) { reset(); } else { - state = State::release; egEnvelope.startRelease(delay, fastRelease); } } @@ -382,8 +381,6 @@ void sfz::Voice::fillWithData(AudioSpan buffer) noexcept sfzInterpolationCast(jumps, indices, leftCoeffs, rightCoeffs); add(sourcePosition, indices); - absl::optional releaseAt {}; - 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; @@ -400,7 +397,7 @@ void sfz::Voice::fillWithData(AudioSpan buffer) noexcept ) - 2; for (auto* index = indices.begin(); index < indices.end(); ++index) { if (*index >= sampleEnd) { - releaseAt = static_cast(std::distance(indices.begin(), index)); + release(static_cast(std::distance(indices.begin(), index))); const auto remainingElements = static_cast(std::distance(index, indices.end())); if (source.getNumFrames() - 1 < region->trueSampleEnd(currentPromise->oversamplingFactor)) { DBG("[sfizz] Underflow: source available samples " @@ -438,12 +435,6 @@ void sfz::Voice::fillWithData(AudioSpan buffer) noexcept sourcePosition = indices.back(); floatPositionOffset = rightCoeffs.back(); - - if (state != State::release && releaseAt) { - release(*releaseAt); - // TODO: not needed probably - buffer.subspan(*releaseAt).fill(0.0f); - } } void sfz::Voice::fillWithGenerator(AudioSpan buffer) noexcept @@ -535,7 +526,7 @@ float sfz::Voice::getMeanSquaredAverage() const noexcept bool sfz::Voice::canBeStolen() const noexcept { - return state == State::release; + return state == State::idle || egEnvelope.isReleased(); } uint32_t sfz::Voice::getSourcePosition() const noexcept