Small issue in LoopingAudioSource::getNextAudioBlock


#1

Hello,

It seems the LoopingAudioSource cannot really cope with buffer requests bigger than what was set in the prepareToPlay call.

However, when connecting the LoopingAudioSource to a ResamplingAudioSource, the buffer size requested at each getNextAudioBlock can vary wildly :slight_smile:

I have made a small crude patch for this problem. Please let me know if this makes sense.

I have pasted below the original version, followed by the version with my small fix.

Thanks for your comments

Benjamin Golinvaux

void LoopingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
if (info.numSamples > 0)
{
if (isLoopingBetweenTimes)
{
const ScopedLock sl (loopPosLock);

        const int64 newStart = getNextReadPosition();
        int64 newEnd = loopStartSample + ((newStart + info.numSamples) % loopEndSample);
        
        if (newStart > loopEndSample)
            newEnd = newStart + info.numSamples;
        
        if (newEnd > newStart)
        {
            input->getNextAudioBlock (info);
        }
        else
        {
            const int64 numEndSamps = loopEndSample - newStart;
            const int64 numStartSamps = newEnd - loopStartSample;
            
            tempInfo.startSample = 0;
            tempInfo.numSamples = (int) numEndSamps;
            input->getNextAudioBlock (tempInfo);
            
            tempInfo.startSample = (int) numEndSamps;
            tempInfo.numSamples = (int) numStartSamps;
            input->setNextReadPosition (loopStartSample);
            input->getNextAudioBlock (tempInfo);

            for (int i = 0; i < info.buffer->getNumChannels(); ++i)
                info.buffer->copyFrom (i, info.startSample, 
                                       tempBuffer,
                                       i, 0, info.numSamples);
        }
    }
    else
    {
        input->getNextAudioBlock (info);
    }
}    

}

void LoopingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
if (info.numSamples > 0)
{
if (isLoopingBetweenTimes)
{
const ScopedLock sl (loopPosLock);

        const int64 newStart = getNextReadPosition();
        int64 newEnd = loopStartSample + ((newStart + info.numSamples) % loopEndSample);
        
        if (newStart > loopEndSample)
            newEnd = newStart + info.numSamples;
        
        if (newEnd > newStart)
        {
            input->getNextAudioBlock (info);
        }
        else
        {
            const int64 numEndSamps = loopEndSample - newStart;
            const int64 numStartSamps = newEnd - loopStartSample;
            
            tempInfo.startSample = 0;
            tempInfo.numSamples = (int) numEndSamps;

            if (tempBuffer.getNumSamples() < (tempInfo.startSample + tempInfo.numSamples))
                tempBuffer.setSize (2, tempInfo.startSample + tempInfo.numSamples);

            input->getNextAudioBlock (tempInfo);
            
            tempInfo.startSample = (int) numEndSamps;
            tempInfo.numSamples = (int) numStartSamps;

            if (tempBuffer.getNumSamples() < (tempInfo.startSample + tempInfo.numSamples))
                tempBuffer.setSize (2, tempInfo.startSample + tempInfo.numSamples);

            input->setNextReadPosition (loopStartSample);
            input->getNextAudioBlock (tempInfo);

            for (int i = 0; i < info.buffer->getNumChannels(); ++i)
                info.buffer->copyFrom (i, info.startSample, 
                                       tempBuffer,
                                       i, 0, info.numSamples);
        }
    }
    else
    {
        input->getNextAudioBlock (info);
    }
}    

}


#2

Huh…? LoopingAudioSource? I don’t have a class with that name…


#3

Ooops. Sorry I forgot to mention : it’s in the dRow modules :slight_smile:


#4

Phew… Thought I was losing the plot for a moment there!