From 225e8dafd985ff1a0387731cac47f331d137f0bb Mon Sep 17 00:00:00 2001 From: hogliux Date: Sat, 1 Oct 2016 10:39:22 +0100 Subject: [PATCH] Added waitForNextAudioBlockReady method to BufferingAudioSource --- .../sources/juce_BufferingAudioSource.cpp | 37 +++++++++++++++++++ .../sources/juce_BufferingAudioSource.h | 7 ++++ 2 files changed, 44 insertions(+) diff --git a/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp b/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp index 7da13ab520..2898aed856 100644 --- a/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp +++ b/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp @@ -151,6 +151,41 @@ void BufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info } } +bool BufferingAudioSource::waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout) +{ + if (!source || source->getTotalLength() <= 0) + return false; + + if (nextPlayPos + info.numSamples < 0) + return true; + + if (! isLooping() && nextPlayPos > getTotalLength()) + return true; + + const uint32 endTime = Time::getMillisecondCounter () + timeout; + uint32 now = Time::getMillisecondCounter(); + + while (now < endTime) + { + { + const ScopedLock sl (bufferStartPosLock); + + const int validStart = static_cast (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos); + const int validEnd = static_cast (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos + info.numSamples) - nextPlayPos); + + if (validStart <= 0 && validStart < validEnd && validEnd >= info.numSamples) + return true; + } + + if (! bufferReadyEvent.wait (endTime - now)) + return false; + + now = Time::getMillisecondCounter(); + } + + return false; +} + int64 BufferingAudioSource::getNextReadPosition() const { jassert (source->getTotalLength() > 0); @@ -244,6 +279,8 @@ bool BufferingAudioSource::readNextBufferChunk() bufferValidEnd = newBVE; } + bufferReadyEvent.signal(); + return true; } diff --git a/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h b/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h index f725d6f441..717e35d5db 100644 --- a/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h +++ b/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h @@ -92,6 +92,12 @@ public: /** Implements the PositionableAudioSource method. */ bool isLooping() const override { return source->isLooping(); } + /** A useful function to block until the next the buffer info can be filled. + + This is useful for offline rendering. + */ + bool waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout); + private: //============================================================================== OptionalScopedPointer source; @@ -99,6 +105,7 @@ private: int numberOfSamplesToBuffer, numberOfChannels; AudioSampleBuffer buffer; CriticalSection bufferStartPosLock; + WaitableEvent bufferReadyEvent; int64 volatile bufferValidStart, bufferValidEnd, nextPlayPos; double volatile sampleRate; bool wasSourceLooping, isPrepared, prefillBuffer;