Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a rounding issue is BpmControl::getBeatContext #11263

Merged
merged 4 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/engine/controls/bpmcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ double BpmControl::getBeatDistance(mixxx::audio::FramePos thisPosition) const {
}

// static
bool BpmControl::getBeatContext(const mixxx::BeatsPointer& pBeats,
bool BpmControl::getBeatContext(
const mixxx::BeatsPointer& pBeats,
mixxx::audio::FramePos position,
mixxx::audio::FramePos* pPrevBeatPosition,
mixxx::audio::FramePos* pNextBeatPosition,
Expand Down Expand Up @@ -795,19 +796,30 @@ mixxx::audio::FramePos BpmControl::getBeatMatchPosition(
return thisPosition;
}

const auto otherPosition = pOtherEngineBuffer->getExactPlayPos();
const mixxx::audio::FramePos otherPosition = pOtherEngineBuffer->getExactPlayPos();
const mixxx::audio::SampleRate thisSampleRate = m_pBeats->getSampleRate();

// Seek our next beat to the other next beat near our beat.
// This is the only thing we can do if the track has different BPM,
// playing the next beat together.

// First calculate the position in the other track where this next beat will be.
const double thisSecs2ToNextBeat = (thisNextBeatPosition - thisPosition) /
thisSampleRate / thisRateRatio;
const mixxx::audio::FramePos otherPositionOfThisNextBeat = otherPosition +
thisSecs2ToNextBeat * otherBeats->getSampleRate() *
pOtherEngineBuffer->getRateRatio();
// This original version has double precision issues (we kept it for documentation):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of leaving the old code as comment, src/test/bpmcontrol_test.cpp should be extended by a testcase that detects the precision issue.

// const mixxx::audio::FrameDiff_t thisSecs2ToNextBeat =
// (thisNextBeatPosition - thisPosition) / thisSampleRate / thisRateRatio;
// const mixxx::audio::FramePos otherPositionOfThisNextBeat = otherPosition +
// thisSecs2ToNextBeat * otherBeats->getSampleRate() *
// pOtherEngineBuffer->getRateRatio();

// calculate framesTransposeFactor first to avoid rounding issues, because it is often 1
double framesTransposeFactor = otherBeats->getSampleRate() /
thisSampleRate * pOtherEngineBuffer->getRateRatio() / thisRateRatio;
// subtract first to avoid a rounding issue, because lower double values
// have a smaller minimum step width
const mixxx::audio::FrameDiff_t otherToThisOffset =
otherPosition - thisPosition * framesTransposeFactor;
const mixxx::audio::FramePos otherPositionOfThisNextBeat =
thisNextBeatPosition * framesTransposeFactor + otherToThisOffset;

mixxx::audio::FramePos otherPrevBeatPosition;
mixxx::audio::FramePos otherNextBeatPosition;
Expand Down
14 changes: 9 additions & 5 deletions src/track/beats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,20 +416,24 @@ bool Beats::findPrevNextBeats(audio::FramePos position,
return true;
}

// Find the next beat at or after the position
Beats::ConstIterator Beats::iteratorFrom(audio::FramePos position) const {
DEBUG_ASSERT(isValid());
auto it = cfirstmarker();
if (position > m_lastMarkerPosition) {

audio::FrameDiff_t diff = position - m_lastMarkerPosition;
if (diff > 0) {
DEBUG_ASSERT(*clastmarker() == m_lastMarkerPosition);
// Lookup position is after the last marker position
const double n = std::ceil((position - m_lastMarkerPosition) / lastBeatLengthFrames());
const double n = std::ceil(diff / lastBeatLengthFrames());
if (n >= static_cast<double>(std::numeric_limits<int>::max())) {
return cend();
}
it = clastmarker() + static_cast<int>(n);

// In some rare cases there may be extremely tiny floating point errors
// that make `std::ceil` round up and makes us end up one beat too
// In positive direction the minimum step width of a double increases.
// This may lead to tiny floating point errors that make `std::ceil`
// round up and makes us end up one beat too
// late. This works around that issue by going back to the previous
// beat if necessary.
auto previousBeatIt = it - 1;
Expand All @@ -448,7 +452,7 @@ Beats::ConstIterator Beats::iteratorFrom(audio::FramePos position) const {
}
DEBUG_ASSERT(it == cbegin() || it == cend() || *it >= position);
DEBUG_ASSERT(it == cbegin() || it == cend() ||
*it - position < std::prev(it).beatLengthFrames());
(*it >= position && *std::prev(it) < position));
return it;
}

Expand Down