+ assert (tempo);
+
+ DEBUG_TRACE (DEBUG::TempoMath,
+ string_compose ("frame %1 walk by %2 frames, start with tempo = %3 @ %4\n",
+ pos, distance, *((const Tempo*)tempo), tempo->frame()));
+
+ Evoral::MusicalTime beats = 0;
+
+ while (distance) {
+
+ /* End of this section */
+ framepos_t end;
+ /* Distance to `end' in frames */
+ framepos_t distance_to_end;
+
+ if (next_tempo == metrics.end ()) {
+ /* We can't do (end - pos) if end is max_framepos, as it will overflow if pos is -ve */
+ end = max_framepos;
+ distance_to_end = max_framepos;
+ } else {
+ end = (*next_tempo)->frame ();
+ distance_to_end = end - pos;
+ }
+
+ /* Amount to subtract this time */
+ double const sub = min (distance, distance_to_end);
+
+ DEBUG_TRACE (DEBUG::TempoMath, string_compose ("to reach end at %1 (end ? %2), distance= %3 sub=%4\n", end, (next_tempo == metrics.end()),
+ distance_to_end, sub));
+
+ /* Update */
+ pos += sub;
+ distance -= sub;
+ assert (tempo);
+ beats += sub / tempo->frames_per_beat (_frame_rate);
+
+ DEBUG_TRACE (DEBUG::TempoMath, string_compose ("now at %1, beats = %2 distance left %3\n",
+ pos, beats, distance));
+
+ /* Move on if there's anything to move to */
+
+ if (next_tempo != metrics.end()) {
+
+ tempo = dynamic_cast<const TempoSection*>(*next_tempo);
+
+ DEBUG_TRACE (DEBUG::TempoMath,
+ string_compose ("\tnew tempo = %1 @ %2 fpb = %3\n",
+ *((const Tempo*)tempo), tempo->frame(),
+ tempo->frames_per_beat (_frame_rate)));
+
+ while (next_tempo != metrics.end ()) {
+
+ ++next_tempo;
+
+ if (next_tempo != metrics.end() && dynamic_cast<const TempoSection*>(*next_tempo)) {
+ break;
+ }
+ }
+
+ if (next_tempo == metrics.end()) {
+ DEBUG_TRACE (DEBUG::TempoMath, "no more tempo sections\n");
+ } else {
+ DEBUG_TRACE (DEBUG::TempoMath, string_compose ("next tempo section is %1 @ %2\n",
+ **next_tempo, (*next_tempo)->frame()));
+ }
+
+ }
+ assert (tempo);