Skip to content

Commit

Permalink
BeatMap: Reduce back-and-forth conversions between samples/frames
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Jul 1, 2021
1 parent af40d4f commit cd8b88c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/track/beatmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,28 +539,30 @@ double BeatMap::getBpmAroundPosition(double curSample, int n) const {
// lower bound, then iterate forward from there to the upper bound.
// a value of -1 indicates we went off the map -- count from the beginning.
double lowerSample = findNthBeat(curSample, -n);
mixxx::audio::FramePos lowerFrame;
if (lowerSample == -1) {
lowerSample = mixxx::audio::FramePos(m_beats.first().frame_position()).toEngineSamplePos();
lowerFrame = mixxx::audio::FramePos(m_beats.first().frame_position());
} else {
lowerFrame = mixxx::audio::FramePos::fromEngineSamplePos(lowerSample);
}

// If we hit the end of the beat map, recalculate the lower bound.
double upperSample = findNthBeat(lowerSample, n * 2);
const double upperSample = findNthBeat(lowerSample, n * 2);
mixxx::audio::FramePos upperFrame;
if (upperSample == -1) {
upperSample = mixxx::audio::FramePos(m_beats.last().frame_position()).toEngineSamplePos();
lowerSample = findNthBeat(upperSample, n * -2);
upperFrame = mixxx::audio::FramePos(m_beats.last().frame_position());
lowerSample = findNthBeat(upperFrame.toEngineSamplePos(), n * -2);
// Super edge-case -- the track doesn't have n beats! Do the best
// we can.
if (lowerSample == -1) {
lowerSample =
mixxx::audio::FramePos(m_beats.first().frame_position())
.toEngineSamplePos();
lowerFrame = mixxx::audio::FramePos(m_beats.first().frame_position());
} else {
lowerFrame = mixxx::audio::FramePos::fromEngineSamplePos(lowerSample);
}
} else {
upperFrame = mixxx::audio::FramePos::fromEngineSamplePos(upperSample);
}

// TODO: Reduce back and forth frame<->sample conversions
mixxx::audio::FramePos lowerFrame = mixxx::audio::FramePos::fromEngineSamplePos(lowerSample);
mixxx::audio::FramePos upperFrame = mixxx::audio::FramePos::fromEngineSamplePos(upperSample);

VERIFY_OR_DEBUG_ASSERT(lowerFrame < upperFrame) {
return -1;
}
Expand Down

0 comments on commit cd8b88c

Please sign in to comment.