mod by zero in ResamplingAudioSource::getNextAudioBlock()

I’m using ResamplingAudioSource and getting a mod by zero on the initial call to getNextAudioBlock(). What is happening is bufferSize is initially zero and then we’re modding the bufferPos by that:

void ResamplingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
    //....
    int bufferSize = buffer.getNumSamples();

    if (bufferSize < sampsNeeded + 8)
    {
        bufferPos %= bufferSize; // mod by zero here
        bufferSize = sampsNeeded + 32;

Swapping the order of assignment of bufferPos and bufferSize seems to fix it:

        bufferSize = sampsNeeded + 32;
        bufferPos %= bufferSize;

I’m not sure how this works for anyone else - maybe I’m using it wrong?

Sounds like you’re calling getNextAudioBlock() before you’ve initialised it with prepareToPlay()…?

Sounds like you’re calling getNextAudioBlock() before you’ve initialised it with prepareToPlay()…?[/quote]

I wish that were the case but its not. Look at how bufferSize gets set:

int bufferSize = buffer.getNumSamples();

There are no samples in the buffer yet. “buffer” has physical storage available, because I call prepareToPlay with a good samplesPerBlockExpected, but there are no actual samples in it yet because audio is just starting.

I will try to cobble together a sample program.

The buffer is initialised in prepareToPlay():

buffer.setSize (numChannels, roundToInt (samplesPerBlockExpected * ratio) + 32);
If that method has been called, then

int bufferSize = buffer.getNumSamples();
can’t possibly be zero.

I agree, I was calling releaseResources() after prepareToPlay(). So there is no problem with ResamplingAudioSource.

However, I wish I had access to resetFilters() so I could clear the state when the input source material changes position.