# Getting BPM and Beats

**URL:** <https://forum.juce.com/t/getting-bpm-and-beats/13151>\
**Category:** Audio Plugins\
**Created:** [July 9, 2014, 2:06pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151 "2014-07-09T14:06:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lapsang](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lapsang](https://forum.juce.com/u/lapsang)\
**Post date:** [July 9, 2014, 2:06pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/1 "2014-07-09T14:06:52Z")

</div>

I spent hours trying to figure out how to get BPM and Beats from the host. I looked at the JuceDemoPlugin but I really don't understand how to port that into my plugin.

I just need to get BPM and Beats in my plug-in processBlock, what can I do? I understand that I have to use&nbsp;AudioPlayHead::CurrentPositionInfo but how?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 9, 2014, 4:47pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/2 "2014-07-09T16:47:48Z")

</div>

The BPM is given in&nbsp;CurrentPositionInfo::bpm (?)

---

<div class="post-metadata">

**Author:** ![lapsang](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lapsang](https://forum.juce.com/u/lapsang)\
**Post date:** [July 9, 2014, 11:46pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/3 "2014-07-09T23:46:13Z")

</div>

I used this in PluginProcessor.h

```
AudioPlayHead::CurrentPositionInfo playHead;
```

and in PluginProcessor.cpp (inside processBlock)

```
DBG(String(playHead.bpm));
```

and I keep getting 0.

I'm using Logic as DAW.

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [July 10, 2014, 7:06am UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/4 "2014-07-10T07:06:16Z")

</div>

get a look at the AudioPlayHead documentation.

you got to do something like that &nbsp;:

```
AudioPlayHead playHead;
AudioPlayHead::CurrentPositionInfo currentPositionInfo;
playHead.getCurrentPosition (currentPositionInfo);
```

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![lapsang](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lapsang](https://forum.juce.com/u/lapsang)\
**Post date:** [July 10, 2014, 2:48pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/5 "2014-07-10T14:48:23Z")

</div>

I'm trying to understand but I'm still a newbie with C++.

I tried as you said but I got this error on the first line

Field type 'juce::AudioPlayHead' is an abstract class.

&nbsp;

Could you explain step by step what should I do? Thanks.

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [July 10, 2014, 7:57pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/6 "2014-07-10T19:57:00Z")

</div>

oh, yep.&nbsp;ok, so&nbsp;in your class that inherits from AudioProcessor :

- in the .h :

private:  
&nbsp; &nbsp; AudioPlayHead\* playHead;  
&nbsp; &nbsp; AudioPlayHead::CurrentPositionInfo currentPositionInfo;  
  
and in .c, in your processBlock method :

playHead = this-\>getPlayHead();  
playHead-\>getCurrentPosition (currentPositionInfo);  
&nbsp;&nbsp; &nbsp;  
just after you can look at&nbsp;currentPositionInfo.bpm  
&nbsp;&nbsp; &nbsp;  
&nbsp;

---

<div class="post-metadata">

**Author:** ![lapsang](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lapsang](https://forum.juce.com/u/lapsang)\
**Post date:** [July 10, 2014, 8:39pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/7 "2014-07-10T20:39:27Z")

</div>

Great! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![ldobson83](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@ldobson83](https://forum.juce.com/u/ldobson83)\
**Post date:** [October 22, 2023, 9:31pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/8 "2023-10-22T21:31:33Z")

</div>

I noticed that the documentation for AudioPlayHead specifically mentions not storing pointers to the object outside of the processBlock() function, so I would suggest avoiding the above solution and, instead, keeping your declarations inside of the processBlock.

Interestingly, I also note that getCurrentPosition() is deprecated and that getPosition() should instead be used. However, I encountered issues when using the latter, so stuck with the deprecated approach.

In case others find it useful, I was able to retrieve the BPM by placing this code inside of the processBlock() function:

```
juce::AudioPlayHead* playHead = getPlayHead();

if (playHead != nullptr) {
    // This approach didn't work for me, but leaving here for comparison:
    //juce::Optional<juce::AudioPlayHead::PositionInfo> positionInfo = playHead->getPosition();
    juce::AudioPlayHead::CurrentPositionInfo* info = new juce::AudioPlayHead::CurrentPositionInfo();
    auto isAvailable = playHead->getCurrentPosition(*info);
    
    if (!isAvailable)
        DBG("Not available!");

    // You would assign to a module level variable at this point, for tracking elsewhere
    auto bpm = info->bpm;
    DBG("Host BPM: " << bpm);
}

```

---

<div class="post-metadata">

**Author:** ![ldobson83](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@ldobson83](https://forum.juce.com/u/ldobson83)\
**Post date:** [October 22, 2023, 11:04pm UTC](https://forum.juce.com/t/getting-bpm-and-beats/13151/9 "2023-10-22T23:04:54Z")

</div>

In fact, the above approach is a bit long-winded nowadays. Instead, you can do the following:

```
    auto tmp_bpm = getPlayHead()->getPosition()->getBpm();
    
    // NOTE: When you're running standalone, you won't get a value here, as there is no host environment
    if (tmp_bpm.hasValue()) {
        // Set your module level variable to the retrieved value, for use elsewhere
        mHostBPM = (int)*tmp_bpm;
        DBG("Got BPM: " << mHostBPM);
    }
    else {
        // Not supported in host environment?
        DBG("BPM couldn't be retrieved");
    }

```

One caveat is that this approach yields no BPM when running in Standalone, while the previous approach seems to yield a BPM of 120. Which is interesting.
