Skip to content

Commit

Permalink
Merge pull request #4064 from Holzhaus/engine-bpm
Browse files Browse the repository at this point in the history
Return Bpm from EngineBuffer and BpmControl
  • Loading branch information
daschuer authored Jul 7, 2021
2 parents 039583b + 9dc9d26 commit f3d9ad1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
26 changes: 13 additions & 13 deletions src/engine/controls/bpmcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ BpmControl::~BpmControl() {
delete m_pAdjustBeatsSlower;
}

double BpmControl::getBpm() const {
return m_pEngineBpm->get();
mixxx::Bpm BpmControl::getBpm() const {
return mixxx::Bpm(m_pEngineBpm->get());
}

void BpmControl::adjustBeatsBpm(double deltaBpm) {
Expand Down Expand Up @@ -311,14 +311,14 @@ bool BpmControl::syncTempo() {
return false;
}

double fThisBpm = m_pEngineBpm->get();
double fThisLocalBpm = m_pLocalBpm->get();
const auto thisBpm = getBpm();
const auto thisLocalBpm = getLocalBpm();

double fOtherBpm = pOtherEngineBuffer->getBpm();
double fOtherLocalBpm = pOtherEngineBuffer->getLocalBpm();
const auto otherBpm = pOtherEngineBuffer->getBpm();
const auto otherLocalBpm = pOtherEngineBuffer->getLocalBpm();

//qDebug() << "this" << "bpm" << fThisBpm << "filebpm" << fThisLocalBpm;
//qDebug() << "other" << "bpm" << fOtherBpm << "filebpm" << fOtherLocalBpm;
//qDebug() << "this" << "bpm" << thisBpm << "filebpm" << thisLocalBpm;
//qDebug() << "other" << "bpm" << otherBpm << "filebpm" << otherLocalBpm;

////////////////////////////////////////////////////////////////////////////
// Rough proof of how syncing works -- rryan 3/2011
Expand Down Expand Up @@ -346,21 +346,21 @@ bool BpmControl::syncTempo() {
//
// thisRateScale = ((otherFileBpm * (1.0 + otherRate)) / thisFileBpm - 1.0) / (thisRateDir * thisRateRange)

if (fOtherBpm > 0.0 && fThisBpm > 0.0) {
if (otherBpm.isValid() && thisBpm.isValid() && thisLocalBpm.isValid()) {
// The desired rate is the other decks effective rate divided by this
// deck's file BPM. This gives us the playback rate that will produce an
// effective BPM equivalent to the other decks.
double desiredRate = fOtherBpm / fThisLocalBpm;
double desiredRate = otherBpm / thisLocalBpm;

// Test if this buffer's bpm is the double of the other one, and adjust
// the rate scale. I believe this is intended to account for our BPM
// algorithm sometimes finding double or half BPMs. This avoids drastic
// scales.

double fFileBpmDelta = fabs(fThisLocalBpm - fOtherLocalBpm);
if (fabs(fThisLocalBpm * 2.0 - fOtherLocalBpm) < fFileBpmDelta) {
const double fileBpmDelta = fabs(thisLocalBpm - otherLocalBpm);
if (fabs(thisLocalBpm * 2.0 - otherLocalBpm) < fileBpmDelta) {
desiredRate /= 2.0;
} else if (fabs(fThisLocalBpm - 2.0 * fOtherLocalBpm) < fFileBpmDelta) {
} else if (fabs(thisLocalBpm - otherLocalBpm * 2.0) < fileBpmDelta) {
desiredRate *= 2.0;
}

Expand Down
7 changes: 5 additions & 2 deletions src/engine/controls/bpmcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ class BpmControl : public EngineControl {
BpmControl(const QString& group, UserSettingsPointer pConfig);
~BpmControl() override;

double getBpm() const;
double getLocalBpm() const { return m_pLocalBpm ? m_pLocalBpm->get() : 0.0; }
mixxx::Bpm getBpm() const;
mixxx::Bpm getLocalBpm() const {
return m_pLocalBpm ? mixxx::Bpm(m_pLocalBpm->get()) : mixxx::Bpm();
}

// When in master sync mode, ratecontrol calls calcSyncedRate to figure out
// how fast the track should play back. The returned rate is usually just
// the correct pitch to match bpms. The usertweak argument represents
Expand Down
4 changes: 2 additions & 2 deletions src/engine/enginebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,11 @@ void EngineBuffer::enableIndependentPitchTempoScaling(bool bEnable,
}
}

double EngineBuffer::getBpm() const {
mixxx::Bpm EngineBuffer::getBpm() const {
return m_pBpmControl->getBpm();
}

double EngineBuffer::getLocalBpm() const {
mixxx::Bpm EngineBuffer::getLocalBpm() const {
return m_pBpmControl->getLocalBpm();
}

Expand Down
9 changes: 5 additions & 4 deletions src/engine/enginebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "engine/engineobject.h"
#include "engine/sync/syncable.h"
#include "preferences/usersettings.h"
#include "track/bpm.h"
#include "track/track_decl.h"
#include "util/rotary.h"
#include "util/types.h"
Expand Down Expand Up @@ -97,10 +98,10 @@ class EngineBuffer : public EngineObject {
double getSpeed() const;
bool getScratching() const;
bool isReverse() const;
// Returns current bpm value (not thread-safe)
double getBpm() const;
// Returns the BPM of the loaded track around the current position (not thread-safe)
double getLocalBpm() const;
/// Returns current bpm value (not thread-safe)
mixxx::Bpm getBpm() const;
/// Returns the BPM of the loaded track around the current position (not thread-safe)
mixxx::Bpm getLocalBpm() const;
/// Sets a beatloop for the loaded track (not thread safe)
void setBeatLoop(double startPosition, bool enabled);
/// Sets a loop for the loaded track (not thread safe)
Expand Down
2 changes: 1 addition & 1 deletion src/engine/sync/enginesync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ Syncable* EngineSync::pickNonSyncSyncTarget(EngineChannel* pDontPick) const {
// mix, and are primary decks.
if (pChannel->isActive() && pChannel->isMasterEnabled() && pChannel->isPrimaryDeck()) {
EngineBuffer* pBuffer = pChannel->getEngineBuffer();
if (pBuffer && pBuffer->getBpm() > 0) {
if (pBuffer && pBuffer->getBpm().isValid()) {
if (pBuffer->getSpeed() != 0.0) {
if (pSyncable->getSyncMode() != SYNC_NONE) {
// Second choice: first playing sync deck
Expand Down
5 changes: 5 additions & 0 deletions src/track/bpm.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ inline Bpm operator/(Bpm bpm, double divisor) {
return Bpm(bpm.value() / divisor);
}

/// Bpm can be divided by another Bpm to get a ratio (represented as a double).
inline double operator/(Bpm bpm, Bpm otherBpm) {
return bpm.value() / otherBpm.value();
}

inline bool operator==(Bpm bpm1, Bpm bpm2) {
if (!bpm1.isValid() && !bpm2.isValid()) {
return true;
Expand Down

0 comments on commit f3d9ad1

Please sign in to comment.