JUCE  v6.1.6 (6.0.8-1114)
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::BufferingAudioSource Class Reference

An AudioSource which takes another source as input, and buffers it using a thread. More...

#include <juce_BufferingAudioSource.h>

Inheritance diagram for juce::BufferingAudioSource:
Collaboration diagram for juce::BufferingAudioSource:

Public Member Functions

 BufferingAudioSource (PositionableAudioSource *source, TimeSliceThread &backgroundThread, bool deleteSourceWhenDeleted, int numberOfSamplesToBuffer, int numberOfChannels=2, bool prefillBufferOnPrepareToPlay=true)
 Creates a BufferingAudioSource. More...
 
 ~BufferingAudioSource () override
 Destructor. More...
 
void getNextAudioBlock (const AudioSourceChannelInfo &) override
 Implementation of the AudioSource method. More...
 
int64 getNextReadPosition () const override
 Implements the PositionableAudioSource method. More...
 
int64 getTotalLength () const override
 Implements the PositionableAudioSource method. More...
 
bool isLooping () const override
 Implements the PositionableAudioSource method. More...
 
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override
 Implementation of the AudioSource method. More...
 
void releaseResources () override
 Implementation of the AudioSource method. More...
 
virtual void setLooping (bool shouldLoop)
 Tells the source whether you'd like it to play in a loop. More...
 
void setNextReadPosition (int64 newPosition) override
 Implements the PositionableAudioSource method. More...
 
bool waitForNextAudioBlockReady (const AudioSourceChannelInfo &info, const uint32 timeout)
 A useful function to block until the next the buffer info can be filled. More...
 

Private Member Functions

Range< intgetValidBufferRange (int numSamples) const
 
void readBufferSection (int64 start, int length, int bufferOffset)
 
bool readNextBufferChunk ()
 
int useTimeSlice () override
 Called back by a TimeSliceThread. More...
 

Private Attributes

TimeSliceThreadbackgroundThread
 
AudioBuffer< floatbuffer
 
CriticalSection bufferRangeLock
 
WaitableEvent bufferReadyEvent
 
int64 bufferValidEnd = 0
 
int64 bufferValidStart = 0
 
CriticalSection callbackLock
 
bool isPrepared = false
 
Time nextCallTime
 
std::atomic< int64nextPlayPos { 0 }
 
int numberOfChannels
 
int numberOfSamplesToBuffer
 
const bool prefillBuffer
 
double sampleRate = 0
 
OptionalScopedPointer< PositionableAudioSourcesource
 
bool wasSourceLooping = false
 

Detailed Description

An AudioSource which takes another source as input, and buffers it using a thread.

Create this as a wrapper around another thread, and it will read-ahead with a background thread to smooth out playback. You can either create one of these directly, or use it indirectly using an AudioTransportSource.

See also
PositionableAudioSource, AudioTransportSource

@tags{Audio}

Constructor & Destructor Documentation

◆ BufferingAudioSource()

juce::BufferingAudioSource::BufferingAudioSource ( PositionableAudioSource source,
TimeSliceThread backgroundThread,
bool  deleteSourceWhenDeleted,
int  numberOfSamplesToBuffer,
int  numberOfChannels = 2,
bool  prefillBufferOnPrepareToPlay = true 
)

Creates a BufferingAudioSource.

Parameters
sourcethe input source to read from
backgroundThreada 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!
deleteSourceWhenDeletedif true, then the input source object will be deleted when this object is deleted
numberOfSamplesToBufferthe size of buffer to use for reading ahead
numberOfChannelsthe number of channels that will be played
prefillBufferOnPrepareToPlayif true, then calling prepareToPlay on this object will block until the buffer has been filled

◆ ~BufferingAudioSource()

juce::BufferingAudioSource::~BufferingAudioSource ( )
override

Destructor.

The input source may be deleted depending on whether the deleteSourceWhenDeleted flag was set in the constructor.

Member Function Documentation

◆ getNextAudioBlock()

void juce::BufferingAudioSource::getNextAudioBlock ( const AudioSourceChannelInfo )
overridevirtual

Implementation of the AudioSource method.

Implements juce::AudioSource.

◆ getNextReadPosition()

int64 juce::BufferingAudioSource::getNextReadPosition ( ) const
overridevirtual

Implements the PositionableAudioSource method.

Implements juce::PositionableAudioSource.

◆ getTotalLength()

int64 juce::BufferingAudioSource::getTotalLength ( ) const
inlineoverridevirtual

Implements the PositionableAudioSource method.

Implements juce::PositionableAudioSource.

References juce::gl::source.

◆ getValidBufferRange()

Range<int> juce::BufferingAudioSource::getValidBufferRange ( int  numSamples) const
private

◆ isLooping()

bool juce::BufferingAudioSource::isLooping ( ) const
inlineoverridevirtual

Implements the PositionableAudioSource method.

Implements juce::PositionableAudioSource.

References juce::gl::source.

◆ prepareToPlay()

void juce::BufferingAudioSource::prepareToPlay ( int  samplesPerBlockExpected,
double  sampleRate 
)
overridevirtual

Implementation of the AudioSource method.

Implements juce::AudioSource.

◆ readBufferSection()

void juce::BufferingAudioSource::readBufferSection ( int64  start,
int  length,
int  bufferOffset 
)
private

◆ readNextBufferChunk()

bool juce::BufferingAudioSource::readNextBufferChunk ( )
private

◆ releaseResources()

void juce::BufferingAudioSource::releaseResources ( )
overridevirtual

Implementation of the AudioSource method.

Implements juce::AudioSource.

◆ setLooping()

virtual void juce::PositionableAudioSource::setLooping ( bool  shouldLoop)
inlinevirtualinherited

Tells the source whether you'd like it to play in a loop.

Reimplemented in juce::MemoryAudioSource, and juce::AudioFormatReaderSource.

References juce::ignoreUnused().

◆ setNextReadPosition()

void juce::BufferingAudioSource::setNextReadPosition ( int64  newPosition)
overridevirtual

Implements the PositionableAudioSource method.

Implements juce::PositionableAudioSource.

◆ useTimeSlice()

int juce::BufferingAudioSource::useTimeSlice ( )
overrideprivatevirtual

Called back by a TimeSliceThread.

When you register this class with it, a TimeSliceThread will repeatedly call this method.

The implementation of this method should use its time-slice to do something that's quick - never block for longer than absolutely necessary.

Returns
Your method should return the number of milliseconds which it would like to wait before being called again. Returning 0 will make the thread call again as soon as possible (after possibly servicing other busy clients). If you return a value below zero, your client will be removed from the list of clients, and won't be called again. The value you specify isn't a guarantee, and is only used as a hint by the thread - the actual time before the next callback may be more or less than specified. You can force the TimeSliceThread to wake up and poll again immediately by calling its notify() method.

Implements juce::TimeSliceClient.

◆ waitForNextAudioBlockReady()

bool juce::BufferingAudioSource::waitForNextAudioBlockReady ( const AudioSourceChannelInfo info,
const uint32  timeout 
)

A useful function to block until the next the buffer info can be filled.

This is useful for offline rendering.

Member Data Documentation

◆ backgroundThread

TimeSliceThread& juce::BufferingAudioSource::backgroundThread
private

◆ buffer

AudioBuffer<float> juce::BufferingAudioSource::buffer
private

◆ bufferRangeLock

CriticalSection juce::BufferingAudioSource::bufferRangeLock
private

◆ bufferReadyEvent

WaitableEvent juce::BufferingAudioSource::bufferReadyEvent
private

◆ bufferValidEnd

int64 juce::BufferingAudioSource::bufferValidEnd = 0
private

◆ bufferValidStart

int64 juce::BufferingAudioSource::bufferValidStart = 0
private

◆ callbackLock

CriticalSection juce::BufferingAudioSource::callbackLock
private

◆ isPrepared

bool juce::BufferingAudioSource::isPrepared = false
private

◆ nextCallTime

Time juce::TimeSliceClient::nextCallTime
privateinherited

◆ nextPlayPos

std::atomic<int64> juce::BufferingAudioSource::nextPlayPos { 0 }
private

◆ numberOfChannels

int juce::BufferingAudioSource::numberOfChannels
private

◆ numberOfSamplesToBuffer

int juce::BufferingAudioSource::numberOfSamplesToBuffer
private

◆ prefillBuffer

const bool juce::BufferingAudioSource::prefillBuffer
private

◆ sampleRate

double juce::BufferingAudioSource::sampleRate = 0
private

◆ source

OptionalScopedPointer<PositionableAudioSource> juce::BufferingAudioSource::source
private

◆ wasSourceLooping

bool juce::BufferingAudioSource::wasSourceLooping = false
private

The documentation for this class was generated from the following file: