ThreadedWriter dropping samples / glitching

I’m using a ThreadedWriter to write out to an audio file. I changed from buffering the whole file to using ThreadedWriter. But now I get dropped samples in the output file.

I followed closely the example at https://github.com/julianstorer/JUCE/blob/0159102e100cd5c2f9ffb1027dedaee102c9d191/examples/Demo/Source/Demos/AudioRecordingDemo.cpp

Any ideas what might be happening? Realtime playback is fine. Got a bit more debugging to do just wondered if there was something obvious to check. Thanks.

My processBlock:

        const ScopedLock sl (writerLock);
        int numSamples = buffer.getNumSamples();

        if (activeWriter != nullptr)
        {
            activeWriter->write (buffer.getArrayOfReadPointers(), numSamples);
            filePosition += numSamples; 
        }

The dropouts are happening every 32768 samples, and the ThreadedWriter is setup with

threadedWriter = new AudioFormatWriter::ThreadedWriter (writer, backgroundThread, 32768);

So I think I worked out the issue, in the the offline rendering is done in a separate thread, and, my theory at least, is that the rendering thread carries on processing buffers each time the ThreadedWriter is writing to disk, and since the writer is writing on another thread I couldn’t see a way of blocking to allow it to write, which you wouldn’t want to be doing in realtime mode anyway.

So I wrote my own buffered write routine that blocks (only in nonRealtime mode) and it seems to be working well :slight_smile: