Get plugin to get DAW playback status

I am just starting out with plugin programming and I would like to be able to get the playback states of the DAW (record, play, stop) in order to mute the channel during recording and have it open during playback. How can I read the playback state?

Thanks in advance

see getAudioPlayhead() in PluginProcessor and https://docs.juce.com/master/classAudioPlayHead.html

Thank you! However I am still just starting out and cannot quite seem to figure it out. I guess I have to use AudioPlayHead::getCurrentPosition(). It then tells me that in there I can find an isRecording Attribute. How can I assign that to my variable?
My code is:

void PluginTest1AudioProcessor::processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages)
{
 ScopedNoDenormals noDenormals;
 auto totalNumInputChannels  = getTotalNumInputChannels();
 auto totalNumOutputChannels = getTotalNumOutputChannels();


if (recStatus == true)
{
    amplitude = 0;
}
else
{
    amplitude = 1;
}

for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
    buffer.clear (i, 0, buffer.getNumSamples());

for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
    auto* channelData = buffer.getWritePointer (channel);

    for(int sample = 0; sample < buffer.getNumSamples(); ++sample)
    {
        channelData[sample] = buffer.getSample(channel, sample) * amplitude;
    }

}
}

In your processBlock method :

amplitude = 1.0f; // default to full gain here because of all the things that can go wrong in the following code
	auto playhead = getPlayHead(); 
	if (playhead!=nullptr) // playhead may not always exist
	{
		AudioPlayHead::CurrentPositionInfo info;
		if (playhead->getCurrentPosition(info)) // even if playhead exists, it may not return valid position info
		{
			if (info.isRecording) // note that even this probably can't be trusted to work consistently across plugin formats and hosts
			{
				amplitude = 0.0;
			}
		}
	}

Note that you can only use the plugin’s AudioPlayHead and attempt to get the position info in the processBlock method. If you need to pass the information outside the method for example to show it in the GUI, you need to store the information in a member variable of your AudioProcessor subclass.

2 Likes

Thank you!

I do have a follow up question though. How do I make it work in the opposite direction (make the DAW record or play through the plugin)? My guess was to use
playhead->transportPlay( true );

but that doesn’t seem to do anything at all.

You cannot control the DAW from your plugin

1 Like

You can do it using ReWire, but I strongly advise against it unless you absolutely must, especially if you’re only getting started with plugin programming.

You may be interested in this approach: I have 2 open source plugins that can “override” the host transport, and run by themselves. In override mode you can set e.g. meter and BPM in the plugin itself. Source is here: https://github.com/tomto66/Topiary-Beatz - there is a manual that describes how this works from a user point of view here: https://topiaryplugins.wordpress.com/topiary-beatz-manual-2/ - do a search for “override” and you’ll find the relevant section.

Hello,

I am working on a simple correlation meter and would like to get the DAW playback status in order to display or stop displaying the minimum correlation value since the user last hit the play button.

I have tried the method suggested above:

        AudioPlayHead::CurrentPositionInfo info;
		if ( playhead->getCurrentPosition( info ) ) // even if playhead exists, it may not return valid position info
		{
			if ( info.isPlaying ) // note that even this probably can't be trusted to work consistently across plugin formats and hosts
			{
				DBG( "PLAYING" );
			}
		}

as well as a newer approach that avoids the deprecated method getCurrentPosition (though I think conceptually it is the same):

/*
helper that unwraps Optional values;
from: https://forum.juce.com/t/juce-optional-get/51982/2
*/
template <typename Value>
Value get( juce::Optional<Value> opt, Value fallback = {} )
{
    if ( opt.hasValue() )
        return *opt;

    jassertfalse;
    return fallback;
}
[ ... ]
[ in processBlock() ]
    auto playhead = getPlayHead();
	if ( playhead != nullptr ) // playhead may not always exist
	{
        if ( playhead->getPosition() ) {
            auto info = get( playhead->getPosition() ); // unwrap Optional value
            if ( info.getIsPlaying() ) {
                DBG( "PLAYING" );
            } else {
                DBG( "PAUSED" );
            }
        }
	}

It seems like in both cases isPlaying/getIsPlaying() is always false. I am using AudioPluginHost to test the resulting VST plugin – is it because of the host that isPlaying is never true?

My current workaround is to stop displaying the current minimum correlation value after 1 second of consecutive completely silent buffers, but this option seems less than ideal. I would really like to display the current minimum correlation value more sensibly e.g. since the last time the user hit play, like the built-in correlation meter in Logic does.

Almost 100% yes.
iirc AudioPluginHost doesn’t provide any playhead info. Try it in a real DAW, I personally like using Reaper when developing and needing to debug in a host as it’s nice and lightweight, so doesn’t take forever to load like some DAWs and it’s not doing any funny out-of-process plugin hosting, so DBG shows in the IDE console and breakpoints all work as expected.

1 Like

Thanks for the clarification – I just tried it in Logic and info.getIsPlaying() works as expected (it resets the value every time I press Play in the DAW).

I guess I’ll continue developing with the expectation that hosts that will use my plug-in provide playhead info, but I’m curious if there is a way to find out from within the plug-in that playhead info is not available for the current host (so I can, for example, fall back to the more blunt method of reseting the display after 1 second of silence).

In my experience I haven’t come across a proper DAW that doesn’t report playhead info (sometimes buggy positions though, especially in Logic when there’s significant latency in the graph). The thing about AudioPluginHost is that it doesn’t have a timeline, ergo doesn’t have an actual playhead, so what would you expect it to report?

1 Like

I haven’t come across a proper DAW that doesn’t report playhead info

That’s good enough for me, I will work off of that assumption.

For the fallback when the host does not provide proper playhead info, I guess I would expect some kind of size variable to be equal to zero or some other variable that would be equal to nullptr. I’m only wondering if there is an easy way to find that out within the plug-in code.