Skip to content

Commit

Permalink
Merge pull request #3790 from daschuer/bpmRounding
Browse files Browse the repository at this point in the history
1/12 Bpm rounding
  • Loading branch information
Holzhaus authored May 12, 2021
2 parents 772b110 + 3dd39c8 commit 54652f2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
32 changes: 17 additions & 15 deletions src/engine/controls/bpmcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "engine/enginebuffer.h"
#include "engine/enginemaster.h"
#include "moc_bpmcontrol.cpp"
#include "track/beatutils.h"
#include "track/track.h"
#include "util/assert.h"
#include "util/duration.h"
Expand All @@ -28,12 +29,13 @@ constexpr double kBpmRangeSmallStep = 0.1;

constexpr double kBpmAdjustMin = kBpmRangeMin;
constexpr double kBpmAdjustStep = 0.01;
constexpr double kBpmTabRounding = 1 / 12.0;

// Maximum allowed interval between beats (calculated from kBpmTapMin).
constexpr double kBpmTapMin = 30.0;
const mixxx::Duration kBpmTapMaxInterval = mixxx::Duration::fromMillis(
static_cast<qint64>(1000.0 * (60.0 / kBpmTapMin)));
constexpr int kBpmTapFilterLength = 5;
constexpr int kBpmTapFilterLength = 80;

// The local_bpm is calculated forward and backward this number of beats, so
// the actual number of beats is this x2.
Expand Down Expand Up @@ -164,36 +166,33 @@ double BpmControl::getBpm() const {
return m_pEngineBpm->get();
}

void BpmControl::slotAdjustBeatsFaster(double v) {
if (v <= 0) {
return;
}
void BpmControl::adjustBeatsBpm(double deltaBpm) {
const TrackPointer pTrack = getEngineBuffer()->getLoadedTrack();
if (!pTrack) {
return;
}
const mixxx::BeatsPointer pBeats = pTrack->getBeats();
if (pBeats && (pBeats->getCapabilities() & mixxx::Beats::BEATSCAP_SETBPM)) {
double bpm = pBeats->getBpm();
double adjustedBpm = bpm + kBpmAdjustStep;
double centerBpm = math_max(kBpmAdjustMin, bpm + deltaBpm);
double adjustedBpm = BeatUtils::roundBpmWithinRange(
centerBpm - kBpmAdjustStep / 2, centerBpm, centerBpm + kBpmAdjustStep / 2);
pTrack->trySetBeats(pBeats->setBpm(adjustedBpm));
}
}

void BpmControl::slotAdjustBeatsSlower(double v) {
void BpmControl::slotAdjustBeatsFaster(double v) {
if (v <= 0) {
return;
}
const TrackPointer pTrack = getEngineBuffer()->getLoadedTrack();
if (!pTrack) {
adjustBeatsBpm(kBpmAdjustStep);
}

void BpmControl::slotAdjustBeatsSlower(double v) {
if (v <= 0) {
return;
}
const mixxx::BeatsPointer pBeats = pTrack->getBeats();
if (pBeats && (pBeats->getCapabilities() & mixxx::Beats::BEATSCAP_SETBPM)) {
double bpm = pBeats->getBpm();
double adjustedBpm = math_max(kBpmAdjustMin, bpm - kBpmAdjustStep);
pTrack->trySetBeats(pBeats->setBpm(adjustedBpm));
}
adjustBeatsBpm(-kBpmAdjustStep);
}

void BpmControl::slotTranslateBeatsEarlier(double v) {
Expand Down Expand Up @@ -259,6 +258,9 @@ void BpmControl::slotTapFilter(double averageLength, int numSamples) {
// (60 seconds per minute) * (1000 milliseconds per second) / (X millis per
// beat) = Y beats/minute
double averageBpm = 60.0 * 1000.0 / averageLength / rateRatio;
averageBpm = BeatUtils::roundBpmWithinRange(averageBpm - kBpmTabRounding,
averageBpm,
averageBpm + kBpmTabRounding);
pTrack->trySetBeats(pBeats->setBpm(averageBpm));
}

Expand Down
1 change: 1 addition & 0 deletions src/engine/controls/bpmcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class BpmControl : public EngineControl {
}
bool syncTempo();
double calcSyncAdjustment(bool userTweakingSync);
void adjustBeatsBpm(double deltaBpm);

friend class SyncControl;

Expand Down
11 changes: 8 additions & 3 deletions src/library/dlgtrackinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "preferences/colorpalettesettings.h"
#include "sources/soundsourceproxy.h"
#include "track/beatfactory.h"
#include "track/beatutils.h"
#include "track/keyfactory.h"
#include "track/keyutils.h"
#include "track/track.h"
Expand All @@ -24,14 +25,16 @@
#include "widget/wstarrating.h"

namespace {

constexpr double kBpmTabRounding = 1 / 12.0;
constexpr int kFilterLength = 80;
constexpr int kMinBpm = 30;
} // namespace

// Maximum allowed interval between beats (calculated from kMinBpm).
const mixxx::Duration kMaxInterval = mixxx::Duration::fromMillis(
static_cast<qint64>(1000.0 * (60.0 / kMinBpm)));

} // namespace

DlgTrackInfo::DlgTrackInfo(
const TrackModel* trackModel)
// No parent because otherwise it inherits the style parent's
Expand Down Expand Up @@ -545,7 +548,9 @@ void DlgTrackInfo::slotBpmTap(double averageLength, int numSamples) {
return;
}
double averageBpm = 60.0 * 1000.0 / averageLength;
// average bpm needs to be truncated for this comparison:
averageBpm = BeatUtils::roundBpmWithinRange(averageBpm - kBpmTabRounding,
averageBpm,
averageBpm + kBpmTabRounding);
if (averageBpm != m_dLastTapedBpm) {
m_dLastTapedBpm = averageBpm;
spinBpm->setValue(averageBpm);
Expand Down
11 changes: 9 additions & 2 deletions src/track/beatgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
#include <QMutexLocker>
#include <QtDebug>

#include "track/beatutils.h"
#include "track/track.h"
#include "util/math.h"

static const int kFrameSize = 2;
namespace {

struct BeatGridData {
double bpm;
double firstBeat;
};

constexpr int kFrameSize = 2;
constexpr double kBpmScaleRounding = 0.001;

} // namespace

namespace mixxx {

class BeatGridIterator : public BeatIterator {
Expand Down Expand Up @@ -326,8 +332,9 @@ BeatsPointer BeatGrid::scale(enum BPMScale scale) const {
if (bpm > getMaxBpm()) {
return BeatsPointer(new BeatGrid(*this));
}
grid.mutable_bpm()->set_bpm(bpm);

bpm = BeatUtils::roundBpmWithinRange(bpm - kBpmScaleRounding, bpm, bpm + kBpmScaleRounding);
grid.mutable_bpm()->set_bpm(bpm);
double beatLength = (60.0 * m_sampleRate / bpm) * kFrameSize;
return BeatsPointer(new BeatGrid(*this, grid, beatLength));
}
Expand Down
1 change: 0 additions & 1 deletion src/track/beatutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@ class BeatUtils {

static QVector<double> getBeats(const QVector<ConstRegion>& constantRegions);

private:
static double roundBpmWithinRange(double minBpm, double centerBpm, double maxBpm);
};

0 comments on commit 54652f2

Please sign in to comment.