Sequencer sync issues in Logic


#1

I am working on a sequencer plugin that is synced to the host. Nothing fancy, the sequencer starts when the user hits play on the DAW. Everything is perfectly synced with one instance of the plugin, but the more I load (of the same plugin) or if I load a heavy plugins like the Logic built-in Drummer, I start getting sync issues.

Aparently this happens only in Logic. What I'm using to start the sequencer is this in processBlock:

    playHead = this->getPlayHead();
    playHead->getCurrentPosition (pos);

    if (arpPower)
    {
        if (pos.isPlaying || pos.isRecording)
        {
            if (shouldReset)
            {
                arpeggiator.resetPhase();
                shouldReset = false;
            }
            
            arpeggiator.setSync(pos.bpm,currentRate,getSampleRate());
            arpeggiator.handleMidi(midiMessages, buffer.getNumSamples());
        }
    }
    else
        // stuff...

The latency is always the same every time. 

I noticed something strange when I load multiple plugins or heavier ones (like Drummer), when I hit play the transport bar goes back a bit, kinda like a pre-roll, so it doesn't start exactly where it was when was stopped, and I suppose it's the cause of the sync issue. My code doesn't know that the transport bar went back and starts whenever isPlaying or isRecording is true.

I've noticed that if I manually hit the back or forward arrows to skip/jump a meausre, while logic is playing, the sequencer automatically syncs.  

I know that my code is in sync as long as the transport bar starts where it should...What can I do to detect the latency or to deal with the problem?


#2

I may have found a solution to detect the latency and re-sync my sequencer. Using the current ppqPosition and the last ppqPosition (stored previously) and it seems to work. Just wondering if there's a more elegant or proper way to do it.