+ /* Find the starting tempo metric */
+
+ for (next_tempo = metrics.begin(); next_tempo != metrics.end(); ++next_tempo) {
+
+ const TempoSection* t;
+
+ if ((t = dynamic_cast<const TempoSection*>(*next_tempo)) != 0) {
+
+ /* This is a bit of a hack, but pos could be -ve, and if it is,
+ we consider the initial metric changes (at time 0) to actually
+ be in effect at pos.
+ */
+
+ framepos_t f = (*next_tempo)->frame ();
+
+ if (pos < 0 && f == 0) {
+ f = pos;
+ }
+
+ if (f > pos) {
+ break;
+ }
+
+ tempo = t;
+ }
+ }
+
+ /* We now have:
+
+ tempo -> the Tempo for "pos"
+ next_tempo -> first tempo after "pos", possibly metrics.end()
+ */
+ assert(tempo);
+
+ DEBUG_TRACE (DEBUG::TempoMath,
+ string_compose ("frame %1 plus %2 beats, start with tempo = %3 @ %4\n",
+ pos, beats, *((const Tempo*)tempo), tempo->frame()));
+
+ while (!!beats) {
+
+ /* Distance to the end of this section in frames */
+ framecnt_t distance_frames = (next_tempo == metrics.end() ? max_framepos : ((*next_tempo)->frame() - pos));
+
+ /* Distance to the end in beats */
+ Evoral::Beats distance_beats = Evoral::Beats::ticks_at_rate(
+ distance_frames, tempo->frames_per_beat (_frame_rate));
+
+ /* Amount to subtract this time */
+ Evoral::Beats const delta = min (distance_beats, beats);
+
+ DEBUG_TRACE (DEBUG::TempoMath, string_compose ("\tdistance to %1 = %2 (%3 beats)\n",
+ (next_tempo == metrics.end() ? max_framepos : (*next_tempo)->frame()),
+ distance_frames, distance_beats));
+
+ /* Update */
+ beats -= delta;
+ pos += delta.to_ticks(tempo->frames_per_beat (_frame_rate));
+
+ DEBUG_TRACE (DEBUG::TempoMath, string_compose ("\tnow at %1, %2 beats left\n", pos, beats));
+
+ /* step forwards to next tempo section */
+
+ 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;
+ }
+ }
+ }
+ }
+
+ return pos;