Browse Source

Added waitForNextAudioBlockReady method to BufferingAudioSource

tags/2021-05-28
hogliux 9 years ago
parent
commit
225e8dafd9
2 changed files with 44 additions and 0 deletions
  1. +37
    -0
      modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp
  2. +7
    -0
      modules/juce_audio_basics/sources/juce_BufferingAudioSource.h

+ 37
- 0
modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp View File

@@ -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<int> (jlimit (bufferValidStart, bufferValidEnd, nextPlayPos) - nextPlayPos);
const int validEnd = static_cast<int> (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;
}


+ 7
- 0
modules/juce_audio_basics/sources/juce_BufferingAudioSource.h View File

@@ -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<PositionableAudioSource> 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;


Loading…
Cancel
Save