diff --git a/src/track/beats.cpp b/src/track/beats.cpp index 41f470b0833b..6d4343586713 100644 --- a/src/track/beats.cpp +++ b/src/track/beats.cpp @@ -787,6 +787,29 @@ int Beats::numBeatsInRange(audio::FramePos startPosition, audio::FramePos endPos return i - 2; }; +double Beats::numFractionalBeatsInRange(audio::FramePos startPos, audio::FramePos endPos) const { + double pBeatPercentage; + // get the ratio of first beat / position: + // 1 - ((startPos - beat before range) / first beat length) + if (!getContext(startPos, nullptr, nullptr, nullptr, &pBeatPercentage)) { + return -1; + } + double numBeats = 1 - pBeatPercentage; + + // get the last beat ratio: + // (endPos - last beat in range) / last beat length + if (!getContext(endPos, nullptr, nullptr, nullptr, &pBeatPercentage)) { + return -1; + } + numBeats += pBeatPercentage; + + // get the beats inside the range + // subtract 1 because we already counted the first beat + numBeats += numBeatsInRange(findNextBeat(startPos), endPos) - 1; + + return numBeats; +} + audio::FramePos Beats::findNextBeat(audio::FramePos position) const { return findNthBeat(position, 1); } diff --git a/src/track/beats.h b/src/track/beats.h index 4d8b9401c85e..a2e78f731d36 100644 --- a/src/track/beats.h +++ b/src/track/beats.h @@ -350,6 +350,8 @@ class Beats : private std::enable_shared_from_this { audio::FramePos snapPosToNearBeat(audio::FramePos position) const; int numBeatsInRange(audio::FramePos startPosition, audio::FramePos endPosition) const; + double numFractionalBeatsInRange(audio::FramePos startPosition, + audio::FramePos endPosition) const; /// Find the frame position N beats away from `position`. The number of beats may be /// negative and does not need to be an integer. In this case the returned position will