Event triggered on DAW startup

I was wondering if anyone knows a way to have a block of code run when when a session gets opened in a daw.

I don’t think there is a generic solution, but for Reaper you can use the VST extensions and check for changes of the project path in your plugins setState/getState.

If a user is opening a session that had an instance of your plugin on a track, then this code would basically just be the plugin constructor, right?

The plugin constructer would run when the host opens a session with the plugin inserted, but unless I’m mistaken hosts will also destruct and re initialize plugins while the project is running. So the constructer would run when the project is opened but it also runs at other times

If you want to be sure it runs only on the first load, couldn’t you trigger your events in the setState method?

I’m new to this but I think that only runs once.

No, it can run any number of times. And it runs for each instance.

What you can do is setting a static flag in the constructor initialised with false and set it to true when your initialisation runs the first time.

static MyProcessor::initialised = false;

MyProcessor::MyProcessor()
{
    if (!initialised)
    {
        initialised = true;
        // do your work
    }
}

However, this might also give false positives for hosts like Logic, where the plugins run in a separate processes.

And it will only run once in the lifetime of the DAW process. I.e. if the user loads a new session, the plugins are usually not loaded fresh but remain in memory, so the initialisation won’t happen again.

The DAWs are not designed to tell the plugins their internals.

1 Like

Ah, good to know… outside of loading the plugin, what else will trigger setState?

Some might when doing an UNDO, some like ProTools have an A/B compare button which switches between the previous and current state.

1 Like

Ahh I had an idea. I’ll have the processor poll the local time on a timer (lets say every 5 seconds). If it polls the local time and there is more than a 5 second period since the last poll, it knows that time has passed without the timer running (i.e. the host has been closed).

Uhm, but if the host gets closed, then your plug-in also has been fully unloaded from memory, hence it’s not possible for it to keep the time of last poll in a variable in memory. Did you plan to write it to disk every time? that seems a little overkill

It sounds indeed like a fragile workaround.

If you could elaborate your use case it would be easier to give ideas on how to get the information you need.
But your original question can be answered as:
No, the standard APIs don’t give you the information when a session is opened or closed.
You can only deduce with a certain error rate depending on host, more guessing.

I’m testing it right now where it writes to a valuetree that i’m loading from AudioProcessor::setStateInformation.

I’m worried about its fragility as well-- but for right now it seems to working in the juce plugin host, logic, and reaper. I only set it up this morning so I still haven’t been able to test it thoroughly. I’ll report back after I test it more.