Playback Issues using AudioIODeviceCallback

Hello everyone,

i am new to JUCE and am currently writing my first application.
My goal is to implement some kind of looping device.
I am recording Audio to a TrackComponent containing a AudioBuffer[16] called StepBuffer.
The Audio is stereo and when i record it to a wav file on my PC (Windows) it sounds good.
But when i output the Audio via my AudioIODeviceCallback i get output glitches. So i am pretty confident the output part of my AudioIODeviceCallback or maybe something threading related might be the issue. I tested it with my laptops Soundcard and with my Steinberg Interface.
This is my Callback:

void AudioIOHandler::audioDeviceIOCallbackWithContext(const float* const* inputChannelData, int numInputChannels, float* const* outputChannelData, int numOutputChannels, int numSamples, const juce::AudioIODeviceCallbackContext& context)
{
//loop through the tracks
for (auto* trackComponent : trackComponents)
{
//check if track is playing
if (trackComponent->isPlaying())
{
int currentStep = trackComponent->getCurrentStep();
float* outputLeft = outputChannelData[0];
float* outputRight = outputChannelData[1];

        juce::AudioBuffer<float>& trackBuffer = trackComponent->stepBuffer[currentStep];
        int bufferLength = trackBuffer.getNumSamples();
        
        int remainingSamples = bufferLength - trackComponent->playbackPosition;
        
        if (bufferLength > 0)
        {

            const float* bufferLeft = trackBuffer.getReadPointer(0);
            const float* bufferRight = trackBuffer.getReadPointer(1);
            //int length = std::min(numSamples, remainingSamples);

            // Write to output
            for (int sample = 0; sample < numSamples; ++sample)
            {
                outputLeft[sample] = bufferLeft[trackComponent->playbackPosition];
                outputRight[sample] = bufferRight[trackComponent->playbackPosition];
                trackComponent->playbackPosition++;
            }

            if (trackComponent->playbackPosition >= bufferLength)
            {
                trackComponent->playbackPosition = 0;
            }
        }
        // Check transport control
        trackComponent->updateTransport();
    }
    //send audio to track if track is not playing
    else if(!trackComponent->isPlaying())
    {
        int channel = 0;
        channel = trackComponent->getChannel();
        const float* audioSend[2];
        audioSend[0] = inputChannelData[channel];
        audioSend[1] = inputChannelData[channel + 1];
        trackComponent->processAudio(audioSend, numSamples);
    }
}

}

Any help would be greatly appreciated.

Greetings

edit: i am sorry my code is only partly formatted as Code. I have no Idea how to do that and it happend automatically :confused:

edit2: I figured it out and after posting it became pretty obvious: The problem is my currentStep, which is set by a midiClock in my MainComponent. I am switching my Buffers during playback.