Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions src/engine/controls/loopingcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,15 @@ void LoopingControl::trackLoaded(TrackPointer pNewTrack) {
m_pBeats = m_pTrack->getBeats();
connect(m_pTrack.get(), &Track::beatsUpdated,
this, &LoopingControl::slotUpdatedTrackBeats);

LoopSamples loopSamples = m_loopSamples.getValue();
if (loopSamples.start != kNoTrigger && loopSamples.end != kNoTrigger) {
double loaded_loop_size = findBeatloopSizeForLoop(
loopSamples.start, loopSamples.end);
if (loaded_loop_size != -1) {
m_pCOBeatLoopSize->setAndConfirm(loaded_loop_size);
}
}
} else {
m_pTrack.reset();
m_pBeats.clear();
Expand Down Expand Up @@ -910,6 +919,22 @@ bool LoopingControl::currentLoopMatchesBeatloopSize() {
loopSamples.end < beatLoopOutPoint + 2;
}

double LoopingControl::findBeatloopSizeForLoop(double start, double end) const {
BeatsPointer pBeats = m_pBeats;
if (!pBeats) {
return -1;
}

for (unsigned int i = 0; i < (sizeof(s_dBeatSizes) / sizeof(s_dBeatSizes[0])); ++i) {
double beatLoopOutPoint =
pBeats->findNBeatsFromSample(start, s_dBeatSizes[i]);
if (end > beatLoopOutPoint - 2 && end < beatLoopOutPoint + 2) {
return s_dBeatSizes[i];
}
}
return -1;
}

void LoopingControl::updateBeatLoopingControls() {
// O(n) search, but there are only ~10-ish beatloop controls so this is
// fine.
Expand Down
4 changes: 4 additions & 0 deletions src/engine/controls/loopingcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class LoopingControl : public EngineControl {
void clearActiveBeatLoop();
void updateBeatLoopingControls();
bool currentLoopMatchesBeatloopSize();

// Given loop in and out points, determine if this is a beatloop of a particular
// size.
double findBeatloopSizeForLoop(double start, double end) const;
// When a loop changes size such that the playposition is outside of the loop,
// we can figure out the best place in the new loop to seek to maintain
// the beat. It will even keep multi-bar phrasing correct with 4/4 tracks.
Expand Down