Skip to content

Commit

Permalink
Bpm: Add assertions when triggering invalid behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Jul 3, 2021
1 parent 08d1f73 commit 2217470
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/engine/enginebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,10 @@ void EngineBuffer::postProcess(const int iBufferSize) {
}
const mixxx::Bpm localBpm = m_pBpmControl->updateLocalBpm();
double beatDistance = m_pBpmControl->updateBeatDistance();
m_pSyncControl->setLocalBpm(localBpm.value());
// FIXME: Verify what should happend if the bpm is invalid
m_pSyncControl->setLocalBpm(localBpm.isValid()
? localBpm.value()
: mixxx::Bpm::kValueUndefined);
m_pSyncControl->updateAudible();
SyncMode mode = m_pSyncControl->getSyncMode();
if (isMaster(mode)) {
Expand Down
69 changes: 62 additions & 7 deletions src/track/bpm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QtDebug>

#include "util/fpclassify.h"
#include "util/math.h"

namespace mixxx {
Expand Down Expand Up @@ -37,13 +38,16 @@ class Bpm final {
}

static bool isValidValue(double value) {
return kValueMin < value;
return util_isfinite(value) && kValueMin < value;
}

bool isValid() const {
return isValidValue(m_value);
}
double value() const {
VERIFY_OR_DEBUG_ASSERT(isValid()) {
return mixxx::Bpm::kValueUndefined;
}
return m_value;
}
void setValue(double value) {
Expand Down Expand Up @@ -84,22 +88,34 @@ class Bpm final {
}

Bpm& operator+=(double increment) {
m_value += increment;
DEBUG_ASSERT(isValid());
if (isValid()) {
m_value += increment;
}
return *this;
}

Bpm& operator-=(double decrement) {
m_value -= decrement;
DEBUG_ASSERT(isValid());
if (isValid()) {
m_value -= decrement;
}
return *this;
}

Bpm& operator*=(double multiple) {
m_value *= multiple;
DEBUG_ASSERT(isValid());
if (isValid()) {
m_value *= multiple;
}
return *this;
}

Bpm& operator/=(double divisor) {
m_value /= divisor;
DEBUG_ASSERT(isValid());
if (isValid()) {
m_value /= divisor;
}
return *this;
}

Expand All @@ -109,56 +125,95 @@ class Bpm final {

/// Bpm can be added to a double
inline Bpm operator+(Bpm bpm, double bpmDiff) {
VERIFY_OR_DEBUG_ASSERT(bpm.isValid()) {
return Bpm();
}

return Bpm(bpm.value() + bpmDiff);
}

/// Bpm can be subtracted from a double
inline Bpm operator-(Bpm bpm, double bpmDiff) {
VERIFY_OR_DEBUG_ASSERT(bpm.isValid()) {
return Bpm();
}

return Bpm(bpm.value() - bpmDiff);
}

/// Two Bpm values can be subtracted to get a double
inline double operator-(Bpm bpm1, Bpm bpm2) {
VERIFY_OR_DEBUG_ASSERT(bpm1.isValid() && bpm2.isValid()) {
return mixxx::Bpm::kValueUndefined;
}

return bpm1.value() - bpm2.value();
}

// Adding two Bpm is not allowed, because it makes no sense semantically.

/// Bpm can be multiplied or divided by a double
inline Bpm operator*(Bpm bpm, double multiple) {
VERIFY_OR_DEBUG_ASSERT(bpm.isValid()) {
return Bpm();
}

return Bpm(bpm.value() * multiple);
}

inline Bpm operator/(Bpm bpm, double divisor) {
VERIFY_OR_DEBUG_ASSERT(bpm.isValid()) {
return Bpm();
}

return Bpm(bpm.value() / divisor);
}

inline bool operator<(Bpm bpm1, Bpm bpm2) {
VERIFY_OR_DEBUG_ASSERT(bpm1.isValid() && bpm2.isValid()) {
return false;
}
return bpm1.value() < bpm2.value();
}

inline bool operator<=(Bpm bpm1, Bpm bpm2) {
VERIFY_OR_DEBUG_ASSERT(bpm1.isValid() && bpm2.isValid()) {
return !bpm1.isValid() && !bpm2.isValid();
}
return bpm1.value() <= bpm2.value();
}

inline bool operator>(Bpm bpm1, Bpm bpm2) {
VERIFY_OR_DEBUG_ASSERT(bpm1.isValid() && bpm2.isValid()) {
return false;
}
return bpm1.value() > bpm2.value();
}

inline bool operator>=(Bpm bpm1, Bpm bpm2) {
VERIFY_OR_DEBUG_ASSERT(bpm1.isValid() && bpm2.isValid()) {
return !bpm1.isValid() && !bpm2.isValid();
}
return bpm1.value() >= bpm2.value();
}

inline bool operator==(Bpm bpm1, Bpm bpm2) {
if (!bpm1.isValid() || !bpm2.isValid()) {
return !bpm1.isValid() && !bpm2.isValid();
}
return bpm1.value() == bpm2.value();
}

inline bool operator!=(Bpm bpm1, Bpm bpm2) {
return !(bpm1.value() == bpm2.value());
return !(bpm1 == bpm2);
}

inline QDebug operator<<(QDebug dbg, Bpm arg) {
dbg << arg.value();
if (arg.isValid()) {
dbg << arg.value();
} else {
dbg.nospace() << "<Invalid FramePos>";
}
return dbg;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/track/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,11 @@ bool Track::trySetBpmWhileLocked(double bpmValue) {

double Track::getBpm() const {
const QMutexLocker lock(&m_qMutex);
return getBpmWhileLocked().value();
const auto bpm = getBpmWhileLocked();
if (!bpm.isValid()) {
return mixxx::Bpm::kValueUndefined;
}
return bpm.value();
}

bool Track::trySetBpm(double bpmValue) {
Expand Down

0 comments on commit 2217470

Please sign in to comment.