Reading a file backwards with AudioFormatReaderSource audio dropouts


#1

In trying to read a file backwards using a class based on AudioFormatReaderSource but when I read some files I get drop outs. Here is the getNextAudioBlock() method I modified and the rest of the class:

class ReversibleAudioFormatReaderSource: public AudioFormatReaderSource
{
    
public:
    ReversibleAudioFormatReaderSource(AudioFormatReader* sourceReader, bool deleteReaderWhenThisIsDeleted)
    :AudioFormatReaderSource (sourceReader,  deleteReaderWhenThisIsDeleted)
    {
        reader.	setNonOwned(sourceReader);

    }
    
    void getNextAudioBlock (const AudioSourceChannelInfo& info) override
    {
        if (info.numSamples > 0)
        {
            blockSize= info.numSamples;
            if(reversed)
            {
                const int64 start = nextPlayPos;
                
                reader->read (info.buffer, info.startSample,
                              info.numSamples, start, true, true);
                info.buffer->reverse(info.startSample, info.numSamples);
                nextPlayPos-=info.numSamples;
            }
            else
            {
                const int64 start = nextPlayPos;
                
                reader->read (info.buffer, info.startSample,
                              info.numSamples, start, true, true);
                nextPlayPos += info.numSamples;

                
            }
        }
        
        
    }
    
    void reverse()
    {
        if(reversed)
            reversed=false;
        else
        {
            nextPlayPos-=blockSize;
            reversed=true;
        }
    }
    
    
private:
    //==============================================================================
    OptionalScopedPointer<AudioFormatReader> reader;
    
    int64 volatile nextPlayPos=0;
    bool volatile reversed=false;
    int64 blockSize;
    
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ReversibleAudioFormatReaderSource)
    
};

Here is my github repo, with some files to illustrate this issue.

Why are these drop outs happening, are there mutexes on the AudioFormatReader?. Is there any solution for this?

Thanks for your help.


#2

Do you still get the dropouts if you read the whole file into memory first?


#3

No if read the whole file in memory I don’t get dropouts. But I need to use big files so I don’t think this is a good approach. Thanks for the reply :slight_smile:


#4

Even when you’re reading forwards, it’s not safe to call audio format reader methods in an audio callback.

They’ll do disk access, which will definitely involve mutexes!