How to get the BPM since getCurrentPosition is deprecated?

I’m trying to write a delay plugin so I need to get the bpm.

However I have no idea how to use juce::AudioPlayHead::PositionInfo::getBpm()

I get this error

../../Source/PluginProcessor.cpp:199:23: error: ‘positionInfo’ was not declared in this scope
  199 |     auto currentBpm = positionInfo->getBpm();
         | 

with the declaration in the .h

  juce::AudioPlayHead::PositionInfo* positionInfo;

and in the .cpp

    auto currentBpm = positionInfo->getBpm();

I’ve tried other configurations but nothing works. Just wondering how to get the bpm so I can set timed delays

1 Like

in the processBlock()

void MyPluginAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
    double bpm { 120 }; // default fallback when host does not provide info
    if (auto bpmFromHost = *getPlayHead()->getPosition()->getBpm())
        bpm = bpmFromHost;
}

EDIT:
yes, it was actually way simpler than i thought it would be.

Thanks @hugoderwolf for pointing it out.

2 Likes

You can omit inheriting AudioPlayHead, it does nothing in the code example.

Also, since the PositionInfo is an Optional, the code in processBlock() should check if there is a value, and act accordingly.

For example, I think in a Standalone build, you don’t get a positionInfo. In AudioPluginHost on the other hand, you get a positionInfo, but the BPM is 0, which is also a failure mode you might want to address. In these cases, going with a default like 120 BPM or switching off any tempo syncing would make sense.