diff --git a/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp b/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp index c47cc5e55c..7da13ab520 100644 --- a/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp +++ b/modules/juce_audio_basics/sources/juce_BufferingAudioSource.cpp @@ -26,7 +26,8 @@ BufferingAudioSource::BufferingAudioSource (PositionableAudioSource* s, TimeSliceThread& thread, const bool deleteSourceWhenDeleted, const int bufferSizeSamples, - const int numChannels) + const int numChannels, + bool prefillBufferOnPrepareToPlay) : source (s, deleteSourceWhenDeleted), backgroundThread (thread), numberOfSamplesToBuffer (jmax (1024, bufferSizeSamples)), @@ -36,7 +37,8 @@ BufferingAudioSource::BufferingAudioSource (PositionableAudioSource* s, nextPlayPos (0), sampleRate (0), wasSourceLooping (false), - isPrepared (false) + isPrepared (false), + prefillBuffer (prefillBufferOnPrepareToPlay) { jassert (source != nullptr); @@ -73,12 +75,13 @@ void BufferingAudioSource::prepareToPlay (int samplesPerBlockExpected, double ne backgroundThread.addTimeSliceClient (this); - while (bufferValidEnd - bufferValidStart < jmin (((int) newSampleRate) / 4, - buffer.getNumSamples() / 2)) + do { backgroundThread.moveToFrontOfQueue (this); Thread::sleep (5); } + while (prefillBuffer + && (bufferValidEnd - bufferValidStart < jmin (((int) newSampleRate) / 4, buffer.getNumSamples() / 2))); } } diff --git a/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h b/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h index 88116207c5..f725d6f441 100644 --- a/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h +++ b/modules/juce_audio_basics/sources/juce_BufferingAudioSource.h @@ -43,21 +43,24 @@ public: //============================================================================== /** Creates a BufferingAudioSource. - @param source the input source to read from - @param backgroundThread a background thread that will be used for the - background read-ahead. This object must not be deleted - until after any BufferingAudioSources that are using it - have been deleted! - @param deleteSourceWhenDeleted if true, then the input source object will - be deleted when this object is deleted - @param numberOfSamplesToBuffer the size of buffer to use for reading ahead - @param numberOfChannels the number of channels that will be played + @param source the input source to read from + @param backgroundThread a background thread that will be used for the + background read-ahead. This object must not be deleted + until after any BufferingAudioSources that are using it + have been deleted! + @param deleteSourceWhenDeleted if true, then the input source object will + be deleted when this object is deleted + @param numberOfSamplesToBuffer the size of buffer to use for reading ahead + @param numberOfChannels the number of channels that will be played + @param prefillBufferOnPrepareToPlay if true, then calling prepareToPlay on this object will + block until the buffer has been filled */ BufferingAudioSource (PositionableAudioSource* source, TimeSliceThread& backgroundThread, bool deleteSourceWhenDeleted, int numberOfSamplesToBuffer, - int numberOfChannels = 2); + int numberOfChannels = 2, + bool prefillBufferOnPrepareToPlay = true); /** Destructor. @@ -98,7 +101,7 @@ private: CriticalSection bufferStartPosLock; int64 volatile bufferValidStart, bufferValidEnd, nextPlayPos; double volatile sampleRate; - bool wasSourceLooping, isPrepared; + bool wasSourceLooping, isPrepared, prefillBuffer; bool readNextBufferChunk(); void readBufferSection (int64 start, int length, int bufferOffset);