From 4804e9afd27ea2872ba4a6d56c0f043b6483f46e Mon Sep 17 00:00:00 2001 From: attila Date: Thu, 27 Oct 2022 18:33:09 +0200 Subject: [PATCH] ARAPluginDemo: Fix incorrect sample reading in editor renderer --- examples/Plugins/ARAPluginDemo.h | 44 ++++++++++---------------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/examples/Plugins/ARAPluginDemo.h b/examples/Plugins/ARAPluginDemo.h index 0ef52e80c9..299c7689f2 100644 --- a/examples/Plugins/ARAPluginDemo.h +++ b/examples/Plugins/ARAPluginDemo.h @@ -110,7 +110,10 @@ public: void writeInto (AudioBuffer& buffer) { if (loopRange.getLength() == 0) + { buffer.clear(); + return; + } const auto numChannelsToCopy = std::min (inputBuffer->getNumChannels(), buffer.getNumChannels()); @@ -140,36 +143,15 @@ private: int64 pos; }; -class OptionalRange -{ -public: - using Type = Range; - - OptionalRange() : valid (false) {} - explicit OptionalRange (Type valueIn) : valid (true), value (std::move (valueIn)) {} - - explicit operator bool() const noexcept { return valid; } - - const auto& operator*() const - { - jassert (valid); - return value; - } - -private: - bool valid; - Type value; -}; - //============================================================================== // Returns the modified sample range in the output buffer. -inline OptionalRange readPlaybackRangeIntoBuffer (Range playbackRange, - const ARAPlaybackRegion* playbackRegion, - AudioBuffer& buffer, - const std::function& getReader) +inline std::optional> readPlaybackRangeIntoBuffer (Range playbackRange, + const ARAPlaybackRegion* playbackRegion, + AudioBuffer& buffer, + const std::function& getReader) { - const auto rangeInAudioModificationTime = playbackRange.movedToStartAt (playbackRange.getStart() - - playbackRegion->getStartInAudioModificationTime()); + const auto rangeInAudioModificationTime = playbackRange - playbackRegion->getStartInPlaybackTime() + + playbackRegion->getStartInAudioModificationTime(); const auto audioSource = playbackRegion->getAudioModification()->getAudioSource(); const auto audioModificationSampleRate = audioSource->getSampleRate(); @@ -181,7 +163,9 @@ inline OptionalRange readPlaybackRangeIntoBuffer (Range playbackRange, const auto inputOffset = jlimit ((int64_t) 0, audioSource->getSampleCount(), sampleRangeInAudioModification.getStart()); - const auto outputOffset = -std::min (sampleRangeInAudioModification.getStart(), (int64_t) 0); + // With the output offset it can always be said of the output buffer, that the zeroth element + // corresponds to beginning of the playbackRange. + const auto outputOffset = std::max (-sampleRangeInAudioModification.getStart(), (int64_t) 0); /* TODO: Handle different AudioSource and playback sample rates. @@ -203,12 +187,12 @@ inline OptionalRange readPlaybackRangeIntoBuffer (Range playbackRange, }(); if (readLength == 0) - return OptionalRange { {} }; + return Range(); auto* reader = getReader (audioSource); if (reader != nullptr && reader->read (&buffer, (int) outputOffset, (int) readLength, inputOffset, true, true)) - return OptionalRange { { outputOffset, readLength } }; + return Range::withStartAndLength (outputOffset, readLength); return {}; }