How to output multiple buffers?

The goal is a multitrack loopstation (in the likes of boss rc-505)
Have written some code that finally seems to work
One issue though
How to add multiple buffers to audio output?

The goal is to have 5 buffers and the live line input going to the line output

What was tried so far:
adding the buffer to the output inside the NextSamples() call => adds a high pitched noise
making NextSamples() return the buffer and adding it inside the AudioCallback() => also adds a high pitched noise

dont have much knowledge on this and no idea what could be causing the issue

can still work on the function of the looper in the meantime, but without fixing this, it will never manage to be a multitrack looper

figured it out

instead of sending the output (float) into the NextSamples() function
the NextSamples() function returns the current buffer position (float)
each track gets stored in its own variable
all are added together at once, going right into the output

static void AudioCallback(AudioHandle::InterleavingInputBuffer  in,
                          AudioHandle::InterleavingOutputBuffer out,
                          size_t                                size)
{
    float output = 0;

    for(size_t i = 0; i < size; i += 2)
    {
        float x1 = track1.NextSamples(in, i);
        float x2 = track2.NextSamples(in, i);
        float x3 = track3.NextSamples(in, i);
        float x4 = track4.NextSamples(in, i);
        float x5 = track5.NextSamples(in, i);

        // left and right outs
        out[i] = out[i + 1] = output + in[i] + in[i + 1] + x1 + x2 + x3 + x4 + x5;
    }
}

float NextSamples(AudioHandle::InterleavingInputBuffer in, size_t i)
{
    if(recording)
    {
        WriteBuffer(in, i);
    }

    if(playing)
    {
        position++;
        position %= mod;
    }
        
    return buffer[position];
}

Note: when summing samples, you will probably need to attenuate them to avoid clipping.

when testing there was no issue
gonna implement volume per track anyway, so guess it shouldnt be an issue