Syncronize playhead daw with my plugin

In My Plugin project I have a graph with other nodes inside:
My aim is get the plyhead of host and pass it to my graph and to all nodes inside graph.
to do this I wrote this code in processor “prepare to play”:

if (getPlayHead())
    {
        graph.setPlayHead(getPlayHead());
        for (AudioProcessorGraph::Node* node : graph.getNodes())
        {
            node->getProcessor()->setPlayHead(getPlayHead());
        }
    }

and to log some infos:

void timerCallback() override
{
    if (!audioProcessor.getPlayHead()) { return; }

    auto position = audioProcessor.getPlayHead()->getPosition();
    if (position.hasValue())
    {
        bool dawPlaying = position->getIsPlaying();
        String isPlayng = dawPlaying ? "is Playing" : "is Stopped";
        AudioPlayHead::TimeSignature timeSig = *(position->getTimeSignature());
        String timeSignature = "   |   TimeSignature: " + String(timeSig.numerator) + "/" + String(timeSig.denominator);
        String bpm = "   |   BPM: " + String(*(position->getBpm()));
        String timeInSeconds = "   |   Time in Seconds: " + String(*(position->getTimeInSeconds()));
        String quarterNotesPos =  "   |   Position QuarterNotes Based: " + String(*(position->getPpqPosition()));
        
        dawInfosLbl.setText(isPlayng + timeSignature + bpm + timeInSeconds + quarterNotesPos, dontSendNotification);
    }

}

But Nothing happens when I instantiate plugin what I’m missing?

The normal place to call getPlayhead() is from within the main processBlock callback. It’s possible that the playhead isn’t available yet in prepareToPlay

1 Like

yeah, prepareToPlay() happens before the DAW is processing audio.
Also, in a complex plugin it is likely only one or two node actually cares about the playhead. So it’s a waste of CPU repeatedly calling all nodes all the time. What is better is if nodes ‘register’ (“hey, add me to a list”) their interest in receiving the ‘setPlayHead’ callback. Then the plugin need call only the minimal number of nodes.

2 Likes

You should just setPlayhead() on the graph and it should pass that down through the context when performing all the processors in the graph.

Rail

1 Like

Ok really thank you to all, now I understand :pray: