more fun and games with meter and the tempo map: rename Meter::beats_per_bar() to...
[ardour.git] / libs / ardour / tempo.cc
index adb207c752610f24fdc0d3b97954416d56c44715..1c36f129b4458995e08f4f3fece917277be33766 100644 (file)
@@ -24,9 +24,9 @@
 
 #include <cmath>
 
-
 #include <glibmm/thread.h>
 #include "pbd/xml++.h"
+#include "evoral/types.hpp"
 #include "ardour/debug.h"
 #include "ardour/tempo.h"
 #include "ardour/utils.h"
@@ -45,11 +45,10 @@ using Timecode::BBT_Time;
 Meter    TempoMap::_default_meter (4.0, 4.0);
 Tempo    TempoMap::_default_tempo (120.0);
 
-const double Meter::ticks_per_beat = 1920.0;
-
-double Tempo::frames_per_beat (framecnt_t sr, const Meter& meter) const
+double 
+Tempo::frames_per_beat (framecnt_t sr) const
 {
-       return  ((60.0 * sr) / (_beats_per_minute * meter.note_divisor()/_note_type));
+       return  (60.0 * sr) / _beats_per_minute;
 }
 
 /***********************************************************************/
@@ -57,7 +56,13 @@ double Tempo::frames_per_beat (framecnt_t sr, const Meter& meter) const
 double
 Meter::frames_per_bar (const Tempo& tempo, framecnt_t sr) const
 {
-       return ((60.0 * sr * _beats_per_bar) / (tempo.beats_per_minute() * _note_type/tempo.note_type()));
+       return (60.0 * sr * _divisions_per_bar) / tempo.beats_per_minute();
+}
+
+double 
+Meter::frames_per_division (const Tempo& tempo, framecnt_t sr) const
+{
+       return (60.0 * sr) / (tempo.beats_per_minute() * _note_type/tempo.note_type());
 }
 
 /***********************************************************************/
@@ -167,7 +172,7 @@ MeterSection::MeterSection (const XMLNode& node)
                throw failed_constructor();
        }
 
-       if (sscanf (prop->value().c_str(), "%lf", &_beats_per_bar) != 1 || _beats_per_bar < 0.0) {
+       if (sscanf (prop->value().c_str(), "%lf", &_divisions_per_bar) != 1 || _divisions_per_bar < 0.0) {
                error << _("MeterSection XML node has an illegal \"beats-per-bar\" value") << endmsg;
                throw failed_constructor();
        }
@@ -204,7 +209,7 @@ MeterSection::get_state() const
        root->add_property ("start", buf);
        snprintf (buf, sizeof (buf), "%f", _note_type);
        root->add_property ("note-type", buf);
-       snprintf (buf, sizeof (buf), "%f", _beats_per_bar);
+       snprintf (buf, sizeof (buf), "%f", _divisions_per_bar);
        root->add_property ("beats-per-bar", buf);
        snprintf (buf, sizeof (buf), "%s", movable()?"yes":"no");
        root->add_property ("movable", buf);
@@ -232,7 +237,7 @@ TempoMap::TempoMap (framecnt_t fr)
        start.ticks = 0;
 
        TempoSection *t = new TempoSection (start, _default_tempo.beats_per_minute(), _default_tempo.note_type());
-       MeterSection *m = new MeterSection (start, _default_meter.beats_per_bar(), _default_meter.note_divisor());
+       MeterSection *m = new MeterSection (start, _default_meter.divisions_per_bar(), _default_meter.note_divisor());
 
        t->set_movable (false);
        m->set_movable (false);
@@ -370,6 +375,25 @@ TempoMap::remove_meter (const MeterSection& tempo)
 void
 TempoMap::do_insert (MetricSection* section, bool with_bbt)
 {
+       assert (section->start().ticks == 0);
+
+       /* we only allow new meters to be inserted on beat 1 of an existing
+        * measure. 
+        */
+
+       if (dynamic_cast<MeterSection*>(section) && 
+           (section->start().beats != 1 || section->start().ticks != 0)) {
+
+               BBT_Time corrected = section->start();
+               corrected.beats = 1;
+               corrected.ticks = 0;
+               
+               warning << string_compose (_("Meter changes can only be positioned on the first beat of a bar. Moving from %1 to %2"),
+                                          section->start(), corrected) << endmsg;
+
+               section->set_start (corrected);
+       }
+
        Metrics::iterator i;
 
        /* Look for any existing MetricSection that is of the same type and
@@ -501,7 +525,7 @@ TempoMap::add_meter (const Meter& meter, BBT_Time where)
                /* new meters *always* start on a beat. */
                where.ticks = 0;
 
-               do_insert (new MeterSection (where, meter.beats_per_bar(), meter.note_divisor()), true);
+               do_insert (new MeterSection (where, meter.divisions_per_bar(), meter.note_divisor()), true);
        }
 
        PropertyChanged (PropertyChange ());
@@ -512,7 +536,7 @@ TempoMap::add_meter (const Meter& meter, framepos_t where)
 {
        {
                Glib::RWLock::WriterLock lm (lock);
-               do_insert (new MeterSection (where, meter.beats_per_bar(), meter.note_divisor()), false);
+               do_insert (new MeterSection (where, meter.divisions_per_bar(), meter.note_divisor()), false);
        }
 
        PropertyChanged (PropertyChange ());
@@ -648,7 +672,7 @@ TempoMap::timestamp_metrics (bool use_bbt)
        tempo = &first_tempo ();
 
        if (use_bbt) {
-
+               
                // cerr << "\n\n\n ######################\nTIMESTAMP via BBT ##############\n" << endl;
 
                framepos_t current = 0;
@@ -699,15 +723,14 @@ TempoMap::timestamp_metrics (bool use_bbt)
                        }
 
                        bbt_time_with_metric ((*i)->frame(), bbt, metric);
-
+                       
                        // cerr << "timestamp @ " << (*i)->frame() << " with " << bbt.bars << "|" << bbt.beats << "|" << bbt.ticks << " => ";
 
-
                        if (first) {
                                first = false;
                        } else {
 
-                               if (bbt.ticks > Meter::ticks_per_beat/2) {
+                               if (bbt.ticks > BBT_Time::ticks_per_beat/2) {
                                        /* round up to next beat */
                                        bbt.beats += 1;
                                }
@@ -721,10 +744,10 @@ TempoMap::timestamp_metrics (bool use_bbt)
                                }
                        }
 
-                       //s cerr << bbt.bars << "|" << bbt.beats << "|" << bbt.ticks << endl;
+                       // cerr << bbt << endl;
 
                        (*i)->set_start (bbt);
-
+                       
                        if ((t = dynamic_cast<TempoSection*>(*i)) != 0) {
                                tempo = t;
                                // cerr << "NEW TEMPO, frame = " << (*i)->frame() << " start = " << (*i)->start() <<endl;
@@ -761,6 +784,8 @@ TempoMap::metric_at (framepos_t frame) const
 
        for (Metrics::const_iterator i = metrics->begin(); i != metrics->end(); ++i) {
 
+               // cerr << "Looking at a metric section " << **i << endl;
+
                if ((*i)->frame() > frame) {
                        break;
                }
@@ -775,6 +800,7 @@ TempoMap::metric_at (framepos_t frame) const
                m.set_start ((*i)->start ());
        }
 
+       // cerr << "for framepos " << frame << " returning " << m.meter() << " @ " << m.tempo() << endl;
        return m;
 }
 
@@ -833,43 +859,48 @@ TempoMap::bbt_time_with_metric (framepos_t frame, BBT_Time& bbt, const TempoMetr
 {
        framecnt_t frame_diff;
 
-       // cerr << "---- BBT time for " << frame << " using metric @ " << metric.frame() << " BBT " << metric.start() << endl;
+       const double divisions_per_bar = metric.meter().divisions_per_bar();
+       const double ticks_per_frame = metric.meter().frames_per_division (metric.tempo(),_frame_rate) / BBT_Time::ticks_per_beat;
 
-       const double beats_per_bar = metric.meter().beats_per_bar();
-       const double ticks_per_frame = metric.tempo().frames_per_beat (_frame_rate, metric.meter()) / Meter::ticks_per_beat;
+       // cerr << "*** Compute BBT time for " << frame 
+       // <<  " from metric at " << metric.frame() << " tempo = " << metric.tempo().beats_per_minute () << " meter " 
+       // << metric.meter().divisions_per_bar() << '/' << metric.meter().note_divisor() 
+       // << endl; 
 
        /* now compute how far beyond that point we actually are. */
 
        frame_diff = frame - metric.frame();
 
-        bbt.ticks = metric.start().ticks + (uint32_t)round((double)frame_diff / ticks_per_frame);
-        uint32_t xtra_beats = bbt.ticks / (uint32_t)Meter::ticks_per_beat;
-        bbt.ticks %= (uint32_t)Meter::ticks_per_beat;
-
-        bbt.beats = metric.start().beats + xtra_beats - 1; // correction for 1-based counting, see below for matching operation.
-        bbt.bars = metric.start().bars + (uint32_t)floor((double)bbt.beats / beats_per_bar);
-        bbt.beats = (uint32_t)fmod((double)bbt.beats, beats_per_bar);
-
-        /* if we have a fractional number of beats per bar, we see if
-           we're in the last beat (the fractional one).  if so, we
-           round ticks appropriately and bump to the next bar. */
-        double beat_fraction = beats_per_bar - floor(beats_per_bar);
-        /* XXX one problem here is that I'm not sure how to handle
-           fractional beats that don't evenly divide ticks_per_beat.
-           If they aren't handled consistently, I would guess we'll
-           continue to have strange discrepancies occuring.  Perhaps
-           this will also behave badly in the case of meters like
-           0.1/4, but I can't be bothered to test that.
-        */
-        uint32_t ticks_on_last_beat = (uint32_t)floor(Meter::ticks_per_beat * beat_fraction);
-
-        if (bbt.beats > (uint32_t)floor(beats_per_bar) && bbt.ticks >= ticks_on_last_beat) {
+       bbt.ticks = metric.start().ticks + (uint32_t)round((double)frame_diff / ticks_per_frame);
+       uint32_t xtra_beats = bbt.ticks / (uint32_t)BBT_Time::ticks_per_beat;
+       bbt.ticks %= (uint32_t)BBT_Time::ticks_per_beat;
+
+       bbt.beats = metric.start().beats + xtra_beats - 1; // correction for 1-based counting, see below for matching operation.
+       bbt.bars = metric.start().bars + (uint32_t)floor((double)bbt.beats / divisions_per_bar);
+       bbt.beats = (uint32_t)fmod((double)bbt.beats, divisions_per_bar);
+
+       /* if we have a fractional number of beats per bar, we see if
+          we're in the last beat (the fractional one).  if so, we
+          round ticks appropriately and bump to the next bar. 
+       */
+       double beat_fraction = divisions_per_bar - floor(divisions_per_bar);
+
+       /* XXX one problem here is that I'm not sure how to handle
+          fractional beats that don't evenly divide ticks_per_beat.
+          If they aren't handled consistently, I would guess we'll
+          continue to have strange discrepancies occuring.  Perhaps
+          this will also behave badly in the case of meters like
+          0.1/4, but I can't be bothered to test that.
+       */
+       uint32_t ticks_on_last_beat = (uint32_t)floor(BBT_Time::ticks_per_beat * beat_fraction);
+
+       if (bbt.beats > (uint32_t)floor(divisions_per_bar) && bbt.ticks >= ticks_on_last_beat) {
                bbt.ticks -= ticks_on_last_beat;
                bbt.beats = 0;
                bbt.bars++;
-        }
+       }
 
-        bbt.beats++; // correction for 1-based counting, see above for matching operation.
+       bbt.beats++; // correction for 1-based counting, see above for matching operation.
 
        // cerr << "-----\t RETURN " << bbt << endl;
 }
@@ -877,8 +908,9 @@ TempoMap::bbt_time_with_metric (framepos_t frame, BBT_Time& bbt, const TempoMetr
 framecnt_t
 TempoMap::count_frames_between (const BBT_Time& start, const BBT_Time& end) const
 {
-        /* for this to work with fractional measure types, start and end have to be "legal" BBT types,
-          that means that the beats and ticks should be inside a bar
+       /* for this to work with fractional measure types, start and end have to be
+          "legal" BBT types, that means that the beats and ticks should be inside
+          a bar
        */
 
        framecnt_t frames = 0;
@@ -889,20 +921,29 @@ TempoMap::count_frames_between (const BBT_Time& start, const BBT_Time& end) cons
 
        uint32_t bar_offset = start.bars - m.start().bars;
 
-       double  beat_offset = bar_offset*m.meter().beats_per_bar() - (m.start().beats-1) + (start.beats -1)
-               + start.ticks/Meter::ticks_per_beat;
+       double  beat_offset = bar_offset*m.meter().divisions_per_bar() - (m.start().beats-1) + (start.beats -1)
+               + start.ticks/BBT_Time::ticks_per_beat;
 
+       start_frame = m.frame() + (framepos_t) rint(beat_offset * m.meter().frames_per_division(m.tempo(),_frame_rate));
 
-       start_frame = m.frame() + (framepos_t) rint( beat_offset * m.tempo().frames_per_beat(_frame_rate, m.meter()));
+       // cerr << "from start " << start << " compute frame = " << start_frame 
+       // <<  " from metric at " << m.frame() << " tempo = " << m.tempo().beats_per_minute () << " meter " 
+       // << m.meter().divisions_per_bar() << '/' << m.meter().note_divisor() 
+       // << endl;
 
        m =  metric_at(end);
 
        bar_offset = end.bars - m.start().bars;
 
-       beat_offset = bar_offset * m.meter().beats_per_bar() - (m.start().beats -1) + (end.beats - 1)
-               + end.ticks/Meter::ticks_per_beat;
+       beat_offset = bar_offset * m.meter().divisions_per_bar() - (m.start().beats -1) + (end.beats - 1)
+               + end.ticks/BBT_Time::ticks_per_beat;
 
-       end_frame = m.frame() + (framepos_t) rint(beat_offset * m.tempo().frames_per_beat(_frame_rate, m.meter()));
+       end_frame = m.frame() + (framepos_t) rint(beat_offset * m.meter().frames_per_division(m.tempo(),_frame_rate));
+
+       // cerr << "from end " << end << " compute frame = " << end_frame 
+       // <<  " from metric at " << m.frame() << " tempo = " << m.tempo().beats_per_minute () << " meter " 
+       // << m.meter().divisions_per_bar() << '/' << m.meter().note_divisor() 
+       // << endl;
 
        frames = end_frame - start_frame;
 
@@ -913,48 +954,48 @@ TempoMap::count_frames_between (const BBT_Time& start, const BBT_Time& end) cons
 framecnt_t
 TempoMap::count_frames_between_metrics (const Meter& meter, const Tempo& tempo, const BBT_Time& start, const BBT_Time& end) const
 {
-        /* this is used in timestamping the metrics by actually counting the beats */
+       /* this is used in timestamping the metrics by actually counting the beats */
 
        framecnt_t frames = 0;
        uint32_t bar = start.bars;
        double beat = (double) start.beats;
-       double beats_counted = 0;
-       double beats_per_bar = 0;
-       double beat_frames = 0;
+       double divisions_counted = 0;
+       double divisions_per_bar = 0;
+       double division_frames = 0;
 
-       beats_per_bar = meter.beats_per_bar();
-       beat_frames = tempo.frames_per_beat (_frame_rate,meter);
+       divisions_per_bar = meter.divisions_per_bar();
+       division_frames = meter.frames_per_division (tempo, _frame_rate);
 
        frames = 0;
 
        while (bar < end.bars || (bar == end.bars && beat < end.beats)) {
 
-               if (beat >= beats_per_bar) {
+               if (beat >= divisions_per_bar) {
                        beat = 1;
                        ++bar;
-                       ++beats_counted;
+                       ++divisions_counted;
 
-                       if (beat > beats_per_bar) {
+                       if (beat > divisions_per_bar) {
 
                                /* this is a fractional beat at the end of a fractional bar
                                   so it should only count for the fraction
                                */
 
-                               beats_counted -= (ceil(beats_per_bar) - beats_per_bar);
+                               divisions_counted -= (ceil(divisions_per_bar) - divisions_per_bar);
                        }
 
                } else {
                        ++beat;
-                       ++beats_counted;
+                       ++divisions_counted;
                }
        }
 
        // cerr << "Counted " << beats_counted << " from " << start << " to " << end
-       // << " bpb were " << beats_per_bar
+       // << " bpb were " << divisions_per_bar
        // << " fpb was " << beat_frames
        // << endl;
 
-       frames = (framecnt_t) llrint (floor (beats_counted * beat_frames));
+       frames = (framecnt_t) llrint (floor (divisions_counted * division_frames));
 
        return frames;
 
@@ -965,7 +1006,7 @@ TempoMap::frame_time (const BBT_Time& bbt) const
 {
        BBT_Time start ; /* 1|1|0 */
 
-       return  count_frames_between ( start, bbt);
+       return count_frames_between (start, bbt);
 }
 
 framecnt_t
@@ -989,7 +1030,7 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
 {
        framecnt_t frames = 0;
 
-       double beats_per_bar;
+       double divisions_per_bar;
        BBT_Time result;
 
        result.bars = max(1U, when.bars + dir * bbt.bars) ;
@@ -997,94 +1038,93 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
        result.ticks = 0;
 
        TempoMetric     metric = metric_at(result);
-       beats_per_bar = metric.meter().beats_per_bar();
-
+       divisions_per_bar = metric.meter().divisions_per_bar();
 
-
-        /*reduce things to legal bbt  values
-         we have to handle possible fractional=shorter beats at the end of measures
-          and things like 0|11|9000  as a duration in a 4.5/4 measure
-         the musical decision is that the fractional beat is also a beat , although a shorter one
+       /* Reduce things to legal bbt values we have to handle possible
+         fractional=shorter beats at the end of measures and things like 0|11|9000
+         as a duration in a 4.5/4 measure the musical decision is that the
+         fractional beat is also a beat , although a shorter one
        */
 
-
        if (dir >= 0) {
                result.beats = when.beats +  bbt.beats;
                result.ticks = when.ticks +  bbt.ticks;
 
-               while (result.beats >= (beats_per_bar + 1)) {
+               while (result.beats >= (divisions_per_bar + 1)) {
                        result.bars++;
-                       result.beats -=  (uint32_t) ceil(beats_per_bar);
+                       result.beats -=  (uint32_t) ceil(divisions_per_bar);
                        metric = metric_at(result); // maybe there is a meter change
-                       beats_per_bar = metric.meter().beats_per_bar();
+                       divisions_per_bar = metric.meter().divisions_per_bar();
 
                }
-               /*we now counted the beats and landed in the target measure, now deal with ticks
-                 this seems complicated, but we want to deal with the corner case of a sequence of time signatures like 0.2/4-0.7/4
-                 and with request like bbt = 3|2|9000 ,so we repeat the same loop but add ticks
+
+               /* We now counted the beats and landed in the target measure, now deal
+                 with ticks this seems complicated, but we want to deal with the
+                 corner case of a sequence of time signatures like 0.2/4-0.7/4 and
+                 with request like bbt = 3|2|9000 ,so we repeat the same loop but add
+                 ticks
                */
 
                /* of course gtk_ardour only allows bar with at least 1.0 beats .....
                 */
 
-               uint32_t ticks_at_beat = (uint32_t) ( result.beats == ceil(beats_per_bar) ?
-                                       (1 - (ceil(beats_per_bar) - beats_per_bar))* Meter::ticks_per_beat
-                                          : Meter::ticks_per_beat );
+               uint32_t ticks_at_beat = (uint32_t) (result.beats == ceil(divisions_per_bar) ?
+                                       (1 - (ceil(divisions_per_bar) - divisions_per_bar))* BBT_Time::ticks_per_beat
+                                          : BBT_Time::ticks_per_beat );
 
                while (result.ticks >= ticks_at_beat) {
                        result.beats++;
                        result.ticks -= ticks_at_beat;
-                       if  (result.beats >= (beats_per_bar + 1)) {
+                       if  (result.beats >= (divisions_per_bar + 1)) {
                                result.bars++;
                                result.beats = 1;
                                metric = metric_at(result); // maybe there is a meter change
-                               beats_per_bar = metric.meter().beats_per_bar();
+                               divisions_per_bar = metric.meter().divisions_per_bar();
                        }
-                       ticks_at_beat= (uint32_t) ( result.beats == ceil(beats_per_bar) ?
-                                      (1 - (ceil(beats_per_bar) - beats_per_bar) ) * Meter::ticks_per_beat
-                                      : Meter::ticks_per_beat);
-
+                       ticks_at_beat= (uint32_t) (result.beats == ceil(divisions_per_bar) ?
+                                      (1 - (ceil(divisions_per_bar) - divisions_per_bar) ) * BBT_Time::ticks_per_beat
+                                      : BBT_Time::ticks_per_beat);
                }
 
 
        } else {
                uint32_t b = bbt.beats;
 
-                /* count beats */
-               while( b > when.beats ) {
-
-                       result.bars = max(1U,result.bars-- ) ;
+               /* count beats */
+               while (b > when.beats) {
+                       --result.bars;
+                       result.bars = max(1U, result.bars);
                        metric = metric_at(result); // maybe there is a meter change
-                       beats_per_bar = metric.meter().beats_per_bar();
-                       if (b >= ceil(beats_per_bar)) {
-
-                               b -= (uint32_t) ceil(beats_per_bar);
+                       divisions_per_bar = metric.meter().divisions_per_bar();
+                       if (b >= ceil(divisions_per_bar)) {
+                               b -= (uint32_t) ceil(divisions_per_bar);
                        } else {
-                               b = (uint32_t) ceil(beats_per_bar) - b + when.beats ;
+                               b = (uint32_t) ceil(divisions_per_bar) - b + when.beats ;
                        }
                }
                result.beats = when.beats - b;
 
-                /*count ticks */
+               /* count ticks */
 
                if (bbt.ticks <= when.ticks) {
                        result.ticks = when.ticks - bbt.ticks;
                } else {
 
-                       uint32_t ticks_at_beat= (uint32_t) Meter::ticks_per_beat;
+                       uint32_t ticks_at_beat= (uint32_t) BBT_Time::ticks_per_beat;
                        uint32_t t = bbt.ticks - when.ticks;
 
                        do {
 
                                if (result.beats == 1) {
-                                       result.bars = max(1U, result.bars-- ) ;
+                                       --result.bars;
+                                       result.bars = max(1U, result.bars) ;
                                        metric = metric_at(result); // maybe there is a meter change
-                                       beats_per_bar = metric.meter().beats_per_bar();
-                                       result.beats = (uint32_t) ceil(beats_per_bar);
-                                       ticks_at_beat = (uint32_t) ((1 - (ceil(beats_per_bar) - beats_per_bar)) * Meter::ticks_per_beat) ;
+                                       divisions_per_bar = metric.meter().divisions_per_bar();
+                                       result.beats = (uint32_t) ceil(divisions_per_bar);
+                                       ticks_at_beat = (uint32_t) ((1 - (ceil(divisions_per_bar) - divisions_per_bar)) * BBT_Time::ticks_per_beat) ;
                                } else {
-                                       result.beats --;
-                                       ticks_at_beat = (uint32_t) Meter::ticks_per_beat;
+                                       --result.beats;
+                                       ticks_at_beat = (uint32_t) BBT_Time::ticks_per_beat;
                                }
 
                                if (t <= ticks_at_beat) {
@@ -1099,8 +1139,8 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
 
        }
 
-       if (dir < 0 ) {
-               frames = count_frames_between( result,when);
+       if (dir < 0) {
+               frames = count_frames_between(result, when);
        } else {
                frames = count_frames_between(when,result);
        }
@@ -1113,8 +1153,8 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
 framepos_t
 TempoMap::round_to_bar (framepos_t fr, int dir)
 {
-        {
-               Glib::RWLock::ReaderLock lm (lock);
+       {
+               Glib::RWLock::ReaderLock lm (lock);
                return round_to_type (fr, dir, Bar);
        }
 }
@@ -1123,8 +1163,8 @@ TempoMap::round_to_bar (framepos_t fr, int dir)
 framepos_t
 TempoMap::round_to_beat (framepos_t fr, int dir)
 {
-        {
-               Glib::RWLock::ReaderLock lm (lock);
+       {
+               Glib::RWLock::ReaderLock lm (lock);
                return round_to_type (fr, dir, Beat);
        }
 }
@@ -1139,7 +1179,7 @@ TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, int dir)
 
        bbt_time(fr, the_beat);
 
-       ticks_one_subdivisions_worth = (uint32_t)Meter::ticks_per_beat / sub_num;
+       ticks_one_subdivisions_worth = (uint32_t)BBT_Time::ticks_per_beat / sub_num;
        ticks_one_half_subdivisions_worth = ticks_one_subdivisions_worth / 2;
 
        if (dir > 0) {
@@ -1177,13 +1217,12 @@ TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, int dir)
                        difference = mod;
                }
 
-                try { 
-                        the_beat = bbt_subtract (the_beat, BBT_Time (0, 0, difference));
-                } catch (...) {
-                        /* can't go backwards from wherever pos is, so just return it */
-                        return fr;
-                }
-                        
+               try {
+                       the_beat = bbt_subtract (the_beat, BBT_Time (0, 0, difference));
+               } catch (...) {
+                       /* can't go backwards from wherever pos is, so just return it */
+                       return fr;
+               }
 
        } else {
                /* round to nearest */
@@ -1246,10 +1285,10 @@ TempoMap::round_to_type (framepos_t frame, int dir, BBTPointType type)
                        float midbar_beats;
                        float midbar_ticks;
 
-                       midbar_beats = metric.meter().beats_per_bar() / 2 + 1;
-                       midbar_ticks = Meter::ticks_per_beat * fmod (midbar_beats, 1.0f);
+                       midbar_beats = metric.meter().divisions_per_bar() / 2 + 1;
+                       midbar_ticks = BBT_Time::ticks_per_beat * fmod (midbar_beats, 1.0f);
                        midbar_beats = floor (midbar_beats);
-                       
+
                        BBT_Time midbar (bbt.bars, lrintf (midbar_beats), lrintf (midbar_ticks));
 
                        if (bbt < midbar) {
@@ -1276,7 +1315,7 @@ TempoMap::round_to_type (framepos_t frame, int dir, BBTPointType type)
                        /* find beat position preceding frame */
 
                        try {
-                               bbt = bbt_subtract (bbt, one_beat); 
+                               bbt = bbt_subtract (bbt, one_beat);
                        }
 
                        catch (...) {
@@ -1300,7 +1339,7 @@ TempoMap::round_to_type (framepos_t frame, int dir, BBTPointType type)
                        /* "true" rounding */
 
                        /* round to nearest beat */
-                       if (bbt.ticks >= (Meter::ticks_per_beat/2)) {
+                       if (bbt.ticks >= (BBT_Time::ticks_per_beat/2)) {
 
                                try {
                                        bbt = bbt_add (bbt, one_beat, metric);
@@ -1333,7 +1372,7 @@ TempoMap::get_points (framepos_t lower, framepos_t upper) const
        const TempoSection* t;
        uint32_t bar;
        uint32_t beat;
-       double beats_per_bar;
+       double divisions_per_bar;
        double beat_frame;
        double beat_frames;
        double frames_per_bar;
@@ -1369,9 +1408,9 @@ TempoMap::get_points (framepos_t lower, framepos_t upper) const
           Now start generating points.
        */
 
-       beats_per_bar = meter->beats_per_bar ();
+       divisions_per_bar = meter->divisions_per_bar ();
        frames_per_bar = meter->frames_per_bar (*tempo, _frame_rate);
-       beat_frames = tempo->frames_per_beat (_frame_rate, *meter);
+       beat_frames = meter->frames_per_division (*tempo,_frame_rate);
 
        if (meter->frame() > tempo->frame()) {
                bar = meter->start().bars;
@@ -1389,7 +1428,7 @@ TempoMap::get_points (framepos_t lower, framepos_t upper) const
        */
 
        delta_bars = (lower-current) / frames_per_bar;
-       delta_beats = modf(delta_bars, &dummy) * beats_per_bar;
+       delta_beats = modf(delta_bars, &dummy) * divisions_per_bar;
        current += (floor(delta_bars) * frames_per_bar) +  (floor(delta_beats) * beat_frames);
 
        // adjust bars and beats too
@@ -1426,7 +1465,7 @@ TempoMap::get_points (framepos_t lower, framepos_t upper) const
 
                        beat_frame = current;
 
-                       while (beat <= ceil( beats_per_bar) && beat_frame < limit) {
+                       while (beat <= ceil(divisions_per_bar) && beat_frame < limit) {
                                if (beat_frame >= lower) {
                                        // cerr << "Add Beat at " << bar << '|' << beat << " @ " << beat_frame << endl;
                                        points->push_back (BBTPoint (*meter, *tempo, (framepos_t) rint(beat_frame), Beat, bar, beat));
@@ -1437,11 +1476,12 @@ TempoMap::get_points (framepos_t lower, framepos_t upper) const
                                beat++;
                        }
 
-                       //  cerr << "out of beats, @ end ? " << (i == metrics->end()) << " out of bpb ? "
-                       // << (beat > ceil(beats_per_bar))
+                       // cerr << "out of beats, @ end ? " << (i == metrics->end()) << " out of bpb ? "
+                       // << (beat > ceil(divisions_per_bar))
+                       // << " beat frame @ " << beat_frame << " vs. " << limit
                        // << endl;
 
-                       if (beat > ceil(beats_per_bar) || i != metrics->end()) {
+                       if (beat > ceil(divisions_per_bar) || i != metrics->end()) {
 
                                /* we walked an entire bar. its
                                   important to move `current' forward
@@ -1459,9 +1499,9 @@ TempoMap::get_points (framepos_t lower, framepos_t upper) const
                                   so we subtract the possible extra fraction from the current
                                */
 
-                               if (beat > ceil (beats_per_bar)) {
+                               if (beat > ceil (divisions_per_bar)) {
                                        /* next bar goes where the numbers suggest */
-                                       current -=  beat_frames * (ceil(beats_per_bar)-beats_per_bar);
+                                       current -=  beat_frames * (ceil(divisions_per_bar)-divisions_per_bar);
                                        // cerr << "++ next bar from numbers\n";
                                } else {
                                        /* next bar goes where the next metric is */
@@ -1493,13 +1533,13 @@ TempoMap::get_points (framepos_t lower, framepos_t upper) const
                                /* new MeterSection, beat always returns to 1 */
                                beat = 1;
                        }
-
+                       
                        current = (*i)->frame ();
                        // cerr << "loop around with current @ " << current << endl;
 
-                       beats_per_bar = meter->beats_per_bar ();
+                       divisions_per_bar = meter->divisions_per_bar ();
                        frames_per_bar = meter->frames_per_bar (*tempo, _frame_rate);
-                       beat_frames = tempo->frames_per_beat (_frame_rate, *meter);
+                       beat_frames = meter->frames_per_division (*tempo, _frame_rate);
 
                        ++i;
                }
@@ -1510,10 +1550,10 @@ TempoMap::get_points (framepos_t lower, framepos_t upper) const
 }
 
 const TempoSection&
-TempoMap::tempo_section_at (framepos_t frame)
+TempoMap::tempo_section_at (framepos_t frame) const
 {
        Glib::RWLock::ReaderLock lm (lock);
-       Metrics::iterator i;
+       Metrics::const_iterator i;
        TempoSection* prev = 0;
 
        for (i = metrics->begin(); i != metrics->end(); ++i) {
@@ -1558,9 +1598,9 @@ TempoMap::get_state ()
        XMLNode *root = new XMLNode ("TempoMap");
 
        {
-                Glib::RWLock::ReaderLock lm (lock);
+               Glib::RWLock::ReaderLock lm (lock);
                for (i = metrics->begin(); i != metrics->end(); ++i) {
-                       root->add_child_nocopy ((*i)->get_state());
+                       root->add_child_nocopy ((*i)->get_state());
                }
        }
 
@@ -1635,7 +1675,7 @@ TempoMap::dump (std::ostream& o) const
                        o << "Tempo @ " << *i << ' ' << t->beats_per_minute() << " BPM (denom = " << t->note_type() << ") at " << t->start() << " frame= " << t->frame() << " (move? "
                          << t->movable() << ')' << endl;
                } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
-                       o << "Meter @ " << *i << ' ' << m->beats_per_bar() << '/' << m->note_divisor() << " at " << m->start() << " frame= " << m->frame()
+                       o << "Meter @ " << *i << ' ' << m->divisions_per_bar() << '/' << m->note_divisor() << " at " << m->start() << " frame= " << m->frame()
                          << " (move? " << m->movable() << ')' << endl;
                }
        }
@@ -1675,7 +1715,7 @@ void
 TempoMap::insert_time (framepos_t where, framecnt_t amount)
 {
        for (Metrics::iterator i = metrics->begin(); i != metrics->end(); ++i) {
-               if ((*i)->frame() >= where) {
+               if ((*i)->frame() >= where && (*i)->movable ()) {
                        (*i)->set_frame ((*i)->frame() + amount);
                }
        }
@@ -1702,9 +1742,9 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& increment, const Tempo
        BBT_Time op = increment; /* argument is const, but we need to modify it */
        uint32_t ticks = result.ticks + op.ticks;
 
-       if (ticks >= Meter::ticks_per_beat) {
+       if (ticks >= BBT_Time::ticks_per_beat) {
                op.beats++;
-               result.ticks = ticks % (uint32_t) Meter::ticks_per_beat;
+               result.ticks = ticks % (uint32_t) BBT_Time::ticks_per_beat;
        } else {
                result.ticks += op.ticks;
        }
@@ -1712,30 +1752,30 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& increment, const Tempo
        /* now comes the complicated part. we have to add one beat a time,
           checking for a new metric on every beat.
        */
-       
+
        /* grab all meter sections */
-       
+
        list<const MeterSection*> meter_sections;
-       
+
        for (Metrics::const_iterator x = metrics->begin(); x != metrics->end(); ++x) {
                const MeterSection* ms;
                if ((ms = dynamic_cast<const MeterSection*>(*x)) != 0) {
                        meter_sections.push_back (ms);
                }
        }
-       
+
        assert (!meter_sections.empty());
-       
+
        list<const MeterSection*>::const_iterator next_meter;
        const Meter* meter = 0;
-       
+
        /* go forwards through the meter sections till we get to the one
-          covering the current value of result. this positions i to point to 
+          covering the current value of result. this positions i to point to
           the next meter section too, or the end.
        */
-       
+
        for (next_meter = meter_sections.begin(); next_meter != meter_sections.end(); ++next_meter) {
-               
+
                if (result < (*next_meter)->start()) {
                        /* this metric is past the result time. stop looking, we have what we need */
                        break;
@@ -1749,34 +1789,34 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& increment, const Tempo
                        ++next_meter;
                        break;
                }
-               
+
                meter = *next_meter;
        }
-       
+
        assert (meter != 0);
-               
-       /* OK, now have the meter for the bar start we are on, and i is an iterator 
-          that points to the metric after the one we are currently dealing with 
-          (or to metrics->end(), of course) 
+
+       /* OK, now have the meter for the bar start we are on, and i is an iterator
+          that points to the metric after the one we are currently dealing with
+          (or to metrics->end(), of course)
        */
-       
+
        while (op.beats) {
-               
+
                /* given the current meter, have we gone past the end of the bar ? */
-               
-               if (result.beats >= meter->beats_per_bar()) {
+
+               if (result.beats >= meter->divisions_per_bar()) {
                        /* move to next bar, first beat */
                        result.bars++;
                        result.beats = 1;
                } else {
                        result.beats++;
                }
-               
+
                /* one down ... */
-               
+
                op.beats--;
-               
-               /* check if we need to use a new meter section: has adding beats to result taken us 
+
+               /* check if we need to use a new meter section: has adding beats to result taken us
                   to or after the start of the next meter section? in which case, use it.
                */
 
@@ -1805,7 +1845,7 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& decrement) const
        if (op.ticks > result.ticks) {
                /* subtract an extra beat later; meanwhile set ticks to the right "carry" value */
                op.beats++;
-               result.ticks = Meter::ticks_per_beat - (op.ticks - result.ticks);
+               result.ticks = BBT_Time::ticks_per_beat - (op.ticks - result.ticks);
        } else {
                result.ticks -= op.ticks;
        }
@@ -1813,35 +1853,35 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& decrement) const
        /* now comes the complicated part. we have to subtract one beat a time,
           checking for a new metric on every beat.
        */
-       
+
        /* grab all meter sections */
-       
+
        list<const MeterSection*> meter_sections;
-       
+
        for (Metrics::const_iterator x = metrics->begin(); x != metrics->end(); ++x) {
                const MeterSection* ms;
                if ((ms = dynamic_cast<const MeterSection*>(*x)) != 0) {
                        meter_sections.push_back (ms);
                }
                }
-       
+
        assert (!meter_sections.empty());
-       
+
        /* go backwards through the meter sections till we get to the one
-          covering the current value of result. this positions i to point to 
+          covering the current value of result. this positions i to point to
           the next (previous) meter section too, or the end.
        */
-       
+
        const MeterSection* meter = 0;
-       list<const MeterSection*>::reverse_iterator next_meter; // older versions of GCC don't 
+       list<const MeterSection*>::reverse_iterator next_meter; // older versions of GCC don't
                                                                // support const_reverse_iterator::operator!=()
-       
+
        for (next_meter = meter_sections.rbegin(); next_meter != meter_sections.rend(); ++next_meter) {
-               
+
                /* when we find the first meter section that is before or at result, use it,
-                  and set next_meter to the previous one 
+                  and set next_meter to the previous one
                */
-               
+
                if ((*next_meter)->start() < result || (*next_meter)->start() == result) {
                        meter = *next_meter;
                        ++next_meter;
@@ -1850,40 +1890,40 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& decrement) const
        }
 
        assert (meter != 0);
-       
-       /* OK, now have the meter for the bar start we are on, and i is an iterator 
-          that points to the metric after the one we are currently dealing with 
-          (or to metrics->end(), of course) 
+
+       /* OK, now have the meter for the bar start we are on, and i is an iterator
+          that points to the metric after the one we are currently dealing with
+          (or to metrics->end(), of course)
        */
-       
+
        while (op.beats) {
 
                /* have we reached the start of the bar? if so, move to the last beat of the previous
                   bar. opwise, just step back 1 beat.
                */
-               
+
                if (result.beats == 1) {
-                       
+
                        /* move to previous bar, last beat */
-                       
+
                        if (result.bars <= 1) {
                                /* i'm sorry dave, i can't do that */
                                throw std::out_of_range ("illegal BBT subtraction");
                        }
-                       
+
                        result.bars--;
-                       result.beats = meter->beats_per_bar();
+                       result.beats = meter->divisions_per_bar();
                } else {
 
                        /* back one beat */
 
                        result.beats--;
                }
-               
+
                /* one down ... */
                op.beats--;
-               
-               /* check if we need to use a new meter section: has subtracting beats to result taken us 
+
+               /* check if we need to use a new meter section: has subtracting beats to result taken us
                   to before the start of the current meter section? in which case, use the prior one.
                */
 
@@ -1904,8 +1944,411 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& decrement) const
        return result;
 }
 
+/** Add some (fractional) beats to a session frame position, and return the result in frames.
+ *  pos can be -ve, if required.
+ */
+framepos_t
+TempoMap::framepos_plus_beats (framepos_t pos, Evoral::MusicalTime beats) const
+{
+       Metrics::const_iterator i;
+       const TempoSection* tempo;
+       const MeterSection* meter;
+
+       /* Find the starting metrics for tempo & meter */
+
+       for (i = metrics->begin(); i != metrics->end(); ++i) {
+
+               /* 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 = (*i)->frame ();
+               if (pos < 0 && f == 0) {
+                       f = pos;
+               }
+
+               if (f > pos) {
+                       break;
+               }
+
+               const TempoSection* t;
+               const MeterSection* m;
+
+               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()
+       */
+
+       while (beats) {
+
+               /* Distance to the end of this section in frames */
+               framecnt_t distance_frames = i == metrics->end() ? max_framepos : ((*i)->frame() - pos);
+
+               /* Distance to the end in beats */
+               Evoral::MusicalTime distance_beats = distance_frames / tempo->frames_per_beat (_frame_rate);
+
+               /* Amount to subtract this time */
+               double const sub = min (distance_beats, beats);
+
+               /* Update */
+               beats -= sub;
+               pos += sub * tempo->frames_per_beat (_frame_rate);
+
+               /* Move on if there's anything to move to */
+               if (i != metrics->end ()) {
+                       const TempoSection* t;
+                       const MeterSection* m;
+                       
+                       if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
+                               tempo = t;
+                       } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
+                               meter = m;
+                       }
+
+                       ++i;
+               }
+       }
+
+       return pos;
+}
+
+/** Subtract some (fractional) beats to a frame position, and return the result in frames */
+framepos_t
+TempoMap::framepos_minus_beats (framepos_t pos, Evoral::MusicalTime beats) const
+{
+       Metrics::const_iterator i;
+       const TempoSection* tempo = 0;
+       const MeterSection* meter = 0;
+       
+       /* Find the starting metrics for tempo & meter */
+
+       for (i = metrics->begin(); i != metrics->end(); ++i) {
+
+               /* 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 = (*i)->frame ();
+               if (pos < 0 && f == 0) {
+                       f = pos;
+               }
+
+               if ((*i)->frame() > pos) {
+                       break;
+               }
+
+               const TempoSection* t;
+               const MeterSection* m;
+
+               if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
+                       tempo = t;
+               } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
+                       meter = m;
+               }
+       }
+
+       bool no_more_metrics = false;
+
+       /* Move i back to the metric before "pos" */
+       if (i != metrics->begin ()) {
+               --i;
+       } else {
+               no_more_metrics = true;
+       }
+
+       /* We now have:
+
+          meter -> the Meter for "pos"
+          tempo -> the Tempo for "pos"
+          i     -> the first metric before "pos", possibly metrics->end()
+       */
+
+       while (beats) {
+
+               /* Distance to the end of this section in frames */
+               framecnt_t distance_frames = no_more_metrics ? max_framepos : (pos - (*i)->frame());
+
+               /* Distance to the end in beats */
+               Evoral::MusicalTime distance_beats = distance_frames / tempo->frames_per_beat (_frame_rate);
+
+               /* Amount to subtract this time */
+               double const sub = min (distance_beats, beats);
+
+               /* Update */
+               beats -= sub;
+               pos -= sub * tempo->frames_per_beat (_frame_rate);
+
+               /* Move i, tempo and meter back, if there's anything to move to.
+                  This is more complicated than the forward case, as we have to
+                  a) move back to the previous change in tempo or metric
+                  then b) scan back further to the last change in the opposite thing
+                  so that tempo/meter are both set up correctly.
+
+                  e.g. if we have (where M is a meter change and T a tempo change):
+                  M1  T1  T2  T3  M2
+
+                  and we move i back to M2, we must also move tempo back to T3 so
+                  that tempo/meter continue to reflect the current state.
+
+                  Moving further back we'd move i to T3, and meter to M1, then
+                  i to T2 and meter (still) to M1, etc.
+
+                  XXX: this is slightly farcical.
+               */
+
+               if (i != metrics->begin ()) {
+
+                       --i;
+
+                       bool found_tempo = false;
+                       bool found_meter = false;
+
+                       const TempoSection* t;
+                       const MeterSection* m;
+
+                       if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
+                               tempo = t;
+                               found_tempo = true;
+                       } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
+                               meter = m;
+                               found_meter = true;
+                       }
+
+                       Metrics::const_iterator j = i;
+                       while (j != metrics->begin ()) {
+                               --j;
+                               if (found_tempo && ((m = dynamic_cast<const MeterSection*> (*j)) != 0)) {
+                                       meter = m;
+                                       break;
+                               } else if (found_meter && ((t = dynamic_cast<const TempoSection*> (*j)) != 0)) {
+                                       tempo = t;
+                                       break;
+                               }
+                       }
+               } else {
+                       no_more_metrics = true;
+               }
+       }
+
+       return pos;
+}
+
+/** Add the BBT interval op to pos and return the result */
+framepos_t
+TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
+{
+       Metrics::const_iterator i;
+       const MeterSection* meter;
+       const MeterSection* m;
+       const TempoSection* tempo;
+       const TempoSection* t;
+       double frames_per_beat;
+
+       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);
+
+       uint64_t bars = 0;
+
+       while (op.bars) {
+
+               bars++;
+               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) {
+
+                               /* about to change tempo or meter, so add the
+                                * number of frames for the bars we've just
+                                * traversed before we change the
+                                * frames_per_beat value.
+                                */
+                               
+                               pos += llrint (frames_per_beat * (bars * meter->divisions_per_bar()));
+                               bars = 0;
+
+                               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);
+
+                       }
+               }
+
+       }
+
+       pos += llrint (frames_per_beat * (bars * meter->divisions_per_bar()));
+
+       uint64_t beats = 0;
+
+       while (op.beats) {
+
+               /* given the current meter, have we gone past the end of the bar ? */
+
+               beats++;
+               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) {
+
+                               /* about to change tempo or meter, so add the
+                                * number of frames for the beats we've just
+                                * traversed before we change the
+                                * frames_per_beat value.
+                                */
+
+                               pos += llrint (beats * frames_per_beat);
+                               beats = 0;
+
+                               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);
+                       }
+               }
+       }
+
+       pos += llrint (beats * frames_per_beat);
+
+       if (op.ticks) {
+               if (op.ticks >= BBT_Time::ticks_per_beat) {
+                       pos += llrint (frames_per_beat + /* extra beat */
+                                      (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 going forward,
+    starting at pos.
+*/
+Evoral::MusicalTime
+TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
+{
+       Metrics::const_iterator i;
+       const TempoSection* tempo;
+       const MeterSection* meter;
+       
+       /* Find the starting metrics for tempo & meter */
+
+       for (i = metrics->begin(); i != metrics->end(); ++i) {
+
+               if ((*i)->frame() > pos) {
+                       break;
+               }
+
+               const TempoSection* t;
+               const MeterSection* m;
+
+               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     -> the first metric after "pos", possibly metrics->end()
+       */
+
+       Evoral::MusicalTime beats = 0;
+
+       while (distance) {
+
+               /* End of this section */
+               framepos_t const end = i == metrics->end() ? max_framepos : (*i)->frame ();
+
+               /* Distance to the end in frames */
+               framecnt_t const distance_to_end = end - pos;
+
+               /* Amount to subtract this time */
+               double const sub = min (distance, distance_to_end);
+
+               /* Update */
+               pos += sub;
+               distance -= sub;
+               beats += sub / tempo->frames_per_beat (_frame_rate);
+
+               /* Move on if there's anything to move to */
+               if (i != metrics->end ()) {
+                       const TempoSection* t;
+                       const MeterSection* m;
+                       
+                       if ((t = dynamic_cast<const TempoSection*>(*i)) != 0) {
+                               tempo = t;
+                       } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
+                               meter = m;
+                       }
+
+                       ++i;
+               }
+       }
+
+       return beats;
+}
+
 /** Compare the time of this with that of another MetricSection.
- *  @param with_bbt True to compare using ::start(), false to use ::frame().
+ *  @param with_bbt True to compare using start(), false to use frame().
  *  @return -1 for less than, 0 for equal, 1 for greater than.
  */
 
@@ -1933,3 +2376,28 @@ MetricSection::compare (MetricSection* other, bool with_bbt) const
        /* NOTREACHED */
        return 0;
 }
+
+std::ostream& 
+operator<< (std::ostream& o, const Meter& m) {
+       return o << m.divisions_per_bar() << '/' << m.note_divisor();
+}
+std::ostream& 
+operator<< (std::ostream& o, const Tempo& t) {
+       return o << t.beats_per_minute() << " (1/" << t.note_type() << " per minute)" << endl;
+}
+std::ostream& 
+operator<< (std::ostream& o, const MetricSection& section) {
+
+       o << "MetricSection @ " << section.frame() << " aka " << section.start() << ' ';
+
+       const TempoSection* ts;
+       const MeterSection* ms;
+
+       if ((ts = dynamic_cast<const TempoSection*> (&section)) != 0) {
+               o << *((Tempo*) ts);
+       } else if ((ms = dynamic_cast<const MeterSection*> (&section)) != 0) {
+               o << *((Meter*) ms);
+       }
+
+       return o;
+}