1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Fixed a threading problem in BufferingAudioSource.

This commit is contained in:
Julian Storer 2011-09-11 13:58:11 +01:00
parent 74df644356
commit 54eb263055
6 changed files with 40 additions and 25 deletions

View file

@ -186,8 +186,8 @@ AudioDemoPlaybackPage::AudioDemoPlaybackPage (AudioDeviceManager& deviceManager_
AudioDemoPlaybackPage::~AudioDemoPlaybackPage()
{
//[Destructor_pre]. You can add your own custom destruction code here..
transportSource.setSource (0);
audioSourcePlayer.setSource (0);
transportSource.setSource (nullptr);
audioSourcePlayer.setSource (nullptr);
deviceManager.removeAudioCallback (&audioSourcePlayer);
fileTreeComp->removeListener (this);
@ -285,8 +285,8 @@ void AudioDemoPlaybackPage::loadFileIntoTransport (const File& audioFile)
{
// unload the previous file source and delete it..
transportSource.stop();
transportSource.setSource (0);
currentAudioFileSource = 0;
transportSource.setSource (nullptr);
currentAudioFileSource = nullptr;
// get a format manager and set it up with the basic types (wav and aiff).
AudioFormatManager formatManager;
@ -294,7 +294,7 @@ void AudioDemoPlaybackPage::loadFileIntoTransport (const File& audioFile)
AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
if (reader != 0)
if (reader != nullptr)
{
currentAudioFileSource = new AudioFormatReaderSource (reader, true);

View file

@ -153,6 +153,7 @@ void AudioSampleBuffer::setSize (const int newNumChannels,
const bool avoidReallocating) noexcept
{
jassert (newNumChannels > 0);
jassert (newNumSamples >= 0);
if (newNumSamples != size || newNumChannels != numChannels)
{

View file

@ -39,7 +39,8 @@ BufferingAudioSource::BufferingAudioSource (PositionableAudioSource* source_,
bufferValidStart (0),
bufferValidEnd (0),
nextPlayPos (0),
wasSourceLooping (false)
wasSourceLooping (false),
isPrepared (false)
{
jassert (source_ != nullptr);
@ -55,11 +56,20 @@ BufferingAudioSource::~BufferingAudioSource()
//==============================================================================
void BufferingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate_)
{
source->prepareToPlay (samplesPerBlockExpected, sampleRate_);
const int bufferSizeNeeded = jmax (samplesPerBlockExpected * 2, numberOfSamplesToBuffer);
if (sampleRate_ != sampleRate
|| bufferSizeNeeded != buffer.getNumSamples()
|| ! isPrepared)
{
backgroundThread.removeTimeSliceClient (this);
isPrepared = true;
sampleRate = sampleRate_;
buffer.setSize (numberOfChannels, jmax (samplesPerBlockExpected * 2, numberOfSamplesToBuffer));
source->prepareToPlay (samplesPerBlockExpected, sampleRate_);
buffer.setSize (numberOfChannels, bufferSizeNeeded);
buffer.clear();
bufferValidStart = 0;
@ -74,9 +84,11 @@ void BufferingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sa
Thread::sleep (5);
}
}
}
void BufferingAudioSource::releaseResources()
{
isPrepared = false;
backgroundThread.removeTimeSliceClient (this);
buffer.setSize (numberOfChannels, 0);
@ -108,6 +120,7 @@ void BufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info
{
for (int chan = jmin (numberOfChannels, info.buffer->getNumChannels()); --chan >= 0;)
{
jassert (buffer.getNumSamples() > 0);
const int startBufferIndex = (int) ((validStart + nextPlayPos) % buffer.getNumSamples());
const int endBufferIndex = (int) ((validEnd + nextPlayPos) % buffer.getNumSamples());
@ -144,6 +157,7 @@ void BufferingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info
int64 BufferingAudioSource::getNextReadPosition() const
{
jassert (source->getTotalLength() > 0);
return (source->isLooping() && nextPlayPos > 0)
? nextPlayPos % source->getTotalLength()
: nextPlayPos;
@ -204,6 +218,7 @@ bool BufferingAudioSource::readNextBufferChunk()
if (sectionToReadStart != sectionToReadEnd)
{
jassert (buffer.getNumSamples() > 0);
const int bufferIndexStart = (int) (sectionToReadStart % buffer.getNumSamples());
const int bufferIndexEnd = (int) (sectionToReadEnd % buffer.getNumSamples());

View file

@ -101,7 +101,7 @@ private:
CriticalSection bufferStartPosLock;
int64 volatile bufferValidStart, bufferValidEnd, nextPlayPos;
double volatile sampleRate;
bool wasSourceLooping;
bool wasSourceLooping, isPrepared;
friend class SharedBufferingAudioSourceThread;
bool readNextBufferChunk();

View file

@ -63,7 +63,7 @@ void AudioTransportSource::setSource (PositionableAudioSource* const newSource,
if (source == nullptr)
return;
setSource (0, 0, 0); // deselect and reselect to avoid releasing resources wrongly
setSource (nullptr, 0, nullptr); // deselect and reselect to avoid releasing resources wrongly
}
readAheadBufferSize = readAheadBufferSize_;

View file

@ -54,7 +54,6 @@ namespace
class CoreAudioReader : public AudioFormatReader
{
public:
//==============================================================================
CoreAudioReader (InputStream* const inp)
: AudioFormatReader (inp, TRANS (coreAudioFormatName)),
ok (false), lastReadPosition (0)