Audio clicks when un-freezing circular delay buffer?

Hello! I’m building a delay with freeze similar to Ableton’s Simple Delay.

Both the delay mode, and freeze mode work well independently, but whenever I un-freeze my buffer, there’s an audible click that occurs.

Currently I have a 4ms fade-in and fade-out at the extremes of the buffer whenever the “freeze” is on to prevent clicks from the loop’s discontinuity. I also have it set so that the fade turns off only when you’re clear from the extremes for that same reason.

Any and all suggestions greatly appreciated, thanks!

        // Handle Freeze
        float fadeFactor = 1.0;
        if (tempFreeze!=freeze && readIndex0==(circBufferLength-fadeLength)) {
            if (tempFreeze) {
                shouldFade=true;
            }
        }
        
        if (shouldFade) {
            fadeFactor = getFadeFactor(readIndex0);
            if (freeze==false) {
                if (readIndex0==fadeLength) {
                    shouldFade=false;
                }
            }
            if (fadeFactor==0.0) {
                if (tempFreeze!=freeze) {
                    freeze=tempFreeze;
                }
            }
        }