From e49af78b7147ded7651c50df741083dc3b85a142 Mon Sep 17 00:00:00 2001 From: Owen Williams Date: Tue, 2 Apr 2019 13:29:02 -0400 Subject: [PATCH] When loading a track that has a loop defined, prefill beatloop_size if one matches. --- src/engine/controls/loopingcontrol.cpp | 25 +++++++++++++++++++++++++ src/engine/controls/loopingcontrol.h | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/src/engine/controls/loopingcontrol.cpp b/src/engine/controls/loopingcontrol.cpp index 851bf86c42b5..f39249d4fe15 100644 --- a/src/engine/controls/loopingcontrol.cpp +++ b/src/engine/controls/loopingcontrol.cpp @@ -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(); @@ -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. diff --git a/src/engine/controls/loopingcontrol.h b/src/engine/controls/loopingcontrol.h index 697c21ab4da3..3623e53d990d 100644 --- a/src/engine/controls/loopingcontrol.h +++ b/src/engine/controls/loopingcontrol.h @@ -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.