+
+ /* We now have:
+
+ meter -> the Meter for "pos"
+ tempo -> the Tempo for "pos"
+ i -> for first new metric after "pos", possibly metrics->end()
+ */
+
+ /* now comes the complicated part. we have to add one beat a time,
+ checking for a new metric on every beat.
+ */
+
+ frames_per_beat = tempo->frames_per_beat (_frame_rate, *meter);
+
+ while (op.bars) {
+
+ pos += llrint (frames_per_beat * meter->beats_per_bar());
+ op.bars--;
+
+ /* check if we need to use a new metric section: has adding frames moved us
+ to or after the start of the next metric section? in which case, use it.
+ */
+
+ if (i != metrics->end()) {
+ if ((*i)->frame() <= pos) {
+
+ if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
+ tempo = t;
+ } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
+ meter = m;
+ }
+ ++i;
+ frames_per_beat = tempo->frames_per_beat (_frame_rate, *meter);
+
+ }
+ }
+
+ }
+
+ while (op.beats) {
+
+ /* given the current meter, have we gone past the end of the bar ? */
+
+ pos += frames_per_beat;
+ op.beats--;
+
+ /* check if we need to use a new metric section: has adding frames moved us
+ to or after the start of the next metric section? in which case, use it.
+ */
+
+ if (i != metrics->end()) {
+ if ((*i)->frame() <= pos) {
+
+ if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
+ tempo = t;
+ } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
+ meter = m;
+ }
+ ++i;
+ frames_per_beat = tempo->frames_per_beat (_frame_rate, *meter);
+ }
+ }
+ }
+
+ if (op.ticks) {
+ if (op.ticks >= BBT_Time::ticks_per_beat) {
+ pos += frames_per_beat;
+ pos += llrint (frames_per_beat * ((op.ticks % (uint32_t) BBT_Time::ticks_per_beat) / (double) BBT_Time::ticks_per_beat));
+ } else {
+ pos += llrint (frames_per_beat * (op.ticks / (double) BBT_Time::ticks_per_beat));
+ }
+ }
+
+ return pos;
+}
+
+/** Count the number of beats that are equivalent to distance when starting at pos */
+double
+TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
+{
+ Metrics::const_iterator i;
+ double beats = 0;
+ const MeterSection* meter;
+ const MeterSection* m;
+ const TempoSection* tempo;
+ const TempoSection* t;
+ double frames_per_beat;
+
+ double ddist = distance;
+ double dpos = pos;
+
+ meter = &first_meter ();
+ tempo = &first_tempo ();
+
+ assert (meter);
+ assert (tempo);
+
+ /* find the starting metrics for tempo & meter */
+
+ for (i = metrics->begin(); i != metrics->end(); ++i) {
+
+ if ((*i)->frame() > pos) {
+ break;
+ }
+
+ if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
+ tempo = t;
+ } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
+ meter = m;
+ }
+ }
+
+ /* We now have:
+
+ meter -> the Meter for "pos"
+ tempo -> the Tempo for "pos"
+ i -> for first new metric after "pos", possibly metrics->end()
+ */
+
+ /* now comes the complicated part. we have to add one beat a time,
+ checking for a new metric on every beat.
+ */
+
+ frames_per_beat = tempo->frames_per_beat (_frame_rate, *meter);
+
+ while (ddist > 0) {
+
+ /* if we're nearly at the end, but have a fractional beat left,
+ compute the fraction and then its all over
+ */
+
+ if (ddist < frames_per_beat) {
+ beats += ddist / frames_per_beat;
+ break;
+ }
+
+ /* walk one beat */
+
+ ddist -= frames_per_beat;
+ dpos += frames_per_beat;
+ beats += 1.0;
+
+ /* check if we need to use a new metric section: has adding frames moved us
+ to or after the start of the next metric section? in which case, use it.
+ */
+
+ if (i != metrics->end()) {
+ if ((*i)->frame() <= (framepos_t) dpos) {
+
+ if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
+ tempo = t;
+ } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
+ meter = m;
+ }
+ ++i;
+ frames_per_beat = tempo->frames_per_beat (_frame_rate, *meter);
+ }
+ }
+
+ }
+
+ return beats;
+}
+
+
+/** Compare the time of this with that of another MetricSection.
+ * @param with_bbt True to compare using ::start(), false to use ::frame().
+ * @return -1 for less than, 0 for equal, 1 for greater than.
+ */
+
+int
+MetricSection::compare (MetricSection* other, bool with_bbt) const
+{
+ if (with_bbt) {
+ if (start() == other->start()) {
+ return 0;
+ } else if (start() < other->start()) {
+ return -1;
+ } else {
+ return 1;
+ }
+ } else {
+ if (frame() == other->frame()) {
+ return 0;
+ } else if (frame() < other->frame()) {
+ return -1;
+ } else {
+ return 1;
+ }
+ }
+
+ /* NOTREACHED */
+ return 0;