Fix undefined operations (according to gcc 4.5.2).
[ardour.git] / libs / ardour / tempo.cc
index 9536576713817e0e83f7315657805e468304858d..e1ef6f2685f2b1335c7e527eddab1b5a7218bac3 100644 (file)
@@ -38,14 +38,14 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
+using Timecode::BBT_Time;
+
 /* _default tempo is 4/4 qtr=120 */
 
 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 (nframes_t sr, const Meter& meter) const
+double Tempo::frames_per_beat (framecnt_t sr, const Meter& meter) const
 {
        return  ((60.0 * sr) / (_beats_per_minute * meter.note_divisor()/_note_type));
 }
@@ -53,7 +53,7 @@ double Tempo::frames_per_beat (nframes_t sr, const Meter& meter) const
 /***********************************************************************/
 
 double
-Meter::frames_per_bar (const Tempo& tempo, nframes_t sr) const
+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()));
 }
@@ -218,7 +218,7 @@ struct MetricSectionSorter {
     }
 };
 
-TempoMap::TempoMap (nframes64_t fr)
+TempoMap::TempoMap (framecnt_t fr)
 {
        metrics = new Metrics;
        _frame_rate = fr;
@@ -259,11 +259,11 @@ TempoMap::move_metric_section (MetricSection& section, const BBT_Time& when)
 
                /* position by audio frame, then recompute BBT timestamps from the audio ones */
 
-               nframes64_t frame = frame_time (when);
+               framepos_t frame = frame_time (when);
                // cerr << "nominal frame time = " << frame << endl;
 
-               nframes64_t prev_frame = round_to_type (frame, -1, Beat);
-               nframes64_t next_frame = round_to_type (frame, 1, Beat);
+               framepos_t prev_frame = round_to_type (frame, -1, Beat);
+               framepos_t next_frame = round_to_type (frame, 1, Beat);
 
                // cerr << "previous beat at " << prev_frame << " next at " << next_frame << endl;
 
@@ -299,7 +299,7 @@ void
 TempoMap::move_tempo (TempoSection& tempo, const BBT_Time& when)
 {
        if (move_metric_section (tempo, when) == 0) {
-               StateChanged (Change (0));
+               PropertyChanged (PropertyChange ());
        }
 }
 
@@ -307,7 +307,7 @@ void
 TempoMap::move_meter (MeterSection& meter, const BBT_Time& when)
 {
        if (move_metric_section (meter, when) == 0) {
-               StateChanged (Change (0));
+               PropertyChanged (PropertyChange ());
        }
 }
 
@@ -334,7 +334,7 @@ TempoMap::remove_tempo (const TempoSection& tempo)
        }
 
        if (removed) {
-               StateChanged (Change (0));
+               PropertyChanged (PropertyChange ());
        }
 }
 
@@ -361,7 +361,7 @@ TempoMap::remove_meter (const MeterSection& tempo)
        }
 
        if (removed) {
-               StateChanged (Change (0));
+               PropertyChanged (PropertyChange ());
        }
 }
 
@@ -370,16 +370,46 @@ TempoMap::do_insert (MetricSection* section, bool with_bbt)
 {
        Metrics::iterator i;
 
+       /* Look for any existing MetricSection that is of the same type and
+          at the same time as the new one, and remove it before adding
+          the new one.
+       */
+
+       Metrics::iterator to_remove = metrics->end ();
+
        for (i = metrics->begin(); i != metrics->end(); ++i) {
 
-               if (with_bbt) {
-                       if ((*i)->start() < section->start()) {
-                               continue;
-                       }
-               } else {
-                       if ((*i)->frame() < section->frame()) {
-                               continue;
-                       }
+               int const c = (*i)->compare (section, with_bbt);
+
+               if (c < 0) {
+                       /* this section is before the one to be added; go back round */
+                       continue;
+               } else if (c > 0) {
+                       /* this section is after the one to be added; there can't be any at the same time */
+                       break;
+               }
+
+               /* hacky comparison of type */
+               bool const a = dynamic_cast<TempoSection*> (*i) != 0;
+               bool const b = dynamic_cast<TempoSection*> (section) != 0;
+
+               if (a == b) {
+                       to_remove = i;
+                       break;
+               }
+       }
+
+       if (to_remove != metrics->end()) {
+               /* remove the MetricSection at the same time as the one we are about to add */
+               metrics->erase (to_remove);
+       }
+
+       /* Add the given MetricSection */
+
+       for (i = metrics->begin(); i != metrics->end(); ++i) {
+
+               if ((*i)->compare (section, with_bbt) < 0) {
+                       continue;
                }
 
                metrics->insert (i, section);
@@ -400,24 +430,23 @@ TempoMap::add_tempo (const Tempo& tempo, BBT_Time where)
                Glib::RWLock::WriterLock lm (lock);
 
                /* new tempos always start on a beat */
-
                where.ticks = 0;
 
                do_insert (new TempoSection (where, tempo.beats_per_minute(), tempo.note_type()), true);
        }
 
-       StateChanged (Change (0));
+       PropertyChanged (PropertyChange ());
 }
 
 void
-TempoMap::add_tempo (const Tempo& tempo, nframes64_t where)
+TempoMap::add_tempo (const Tempo& tempo, framepos_t where)
 {
        {
                Glib::RWLock::WriterLock lm (lock);
                do_insert (new TempoSection (where, tempo.beats_per_minute(), tempo.note_type()), false);
        }
 
-       StateChanged (Change (0));
+       PropertyChanged (PropertyChange ());
 }
 
 void
@@ -445,7 +474,7 @@ TempoMap::replace_tempo (TempoSection& existing, const Tempo& replacement)
        }
 
        if (replaced) {
-               StateChanged (Change (0));
+               PropertyChanged (PropertyChange ());
        }
 }
 
@@ -468,24 +497,23 @@ 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);
        }
 
-       StateChanged (Change (0));
+       PropertyChanged (PropertyChange ());
 }
 
 void
-TempoMap::add_meter (const Meter& meter, nframes64_t where)
+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);
        }
 
-       StateChanged (Change (0));
+       PropertyChanged (PropertyChange ());
 }
 
 void
@@ -511,7 +539,7 @@ TempoMap::replace_meter (MeterSection& existing, const Meter& replacement)
        }
 
        if (replaced) {
-               StateChanged (Change (0));
+               PropertyChanged (PropertyChange ());
        }
 }
 
@@ -524,14 +552,14 @@ TempoMap::change_initial_tempo (double beats_per_minute, double note_type)
        for (Metrics::iterator i = metrics->begin(); i != metrics->end(); ++i) {
                if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
                        *((Tempo*) t) = newtempo;
-                       StateChanged (Change (0));
+                       PropertyChanged (PropertyChange ());
                        break;
                }
        }
 }
 
 void
-TempoMap::change_existing_tempo_at (nframes64_t where, double beats_per_minute, double note_type)
+TempoMap::change_existing_tempo_at (framepos_t where, double beats_per_minute, double note_type)
 {
        Tempo newtempo (beats_per_minute, note_type);
 
@@ -570,7 +598,7 @@ TempoMap::change_existing_tempo_at (nframes64_t where, double beats_per_minute,
        /* reset */
 
        *((Tempo*)prev) = newtempo;
-       StateChanged (Change (0));
+       PropertyChanged (PropertyChange ());
 }
 
 const MeterSection&
@@ -621,8 +649,8 @@ TempoMap::timestamp_metrics (bool use_bbt)
 
                // cerr << "\n\n\n ######################\nTIMESTAMP via BBT ##############\n" << endl;
 
-               nframes64_t current = 0;
-               nframes64_t section_frames;
+               framepos_t current = 0;
+               framepos_t section_frames;
                BBT_Time start;
                BBT_Time end;
 
@@ -677,7 +705,7 @@ TempoMap::timestamp_metrics (bool use_bbt)
                                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;
                                }
@@ -716,7 +744,7 @@ TempoMap::timestamp_metrics (bool use_bbt)
 }
 
 TempoMetric
-TempoMap::metric_at (nframes64_t frame) const
+TempoMap::metric_at (framepos_t frame) const
 {
        TempoMetric m (first_meter(), first_tempo());
        const Meter* meter;
@@ -784,7 +812,7 @@ TempoMap::metric_at (BBT_Time bbt) const
 }
 
 void
-TempoMap::bbt_time (nframes64_t frame, BBT_Time& bbt) const
+TempoMap::bbt_time (framepos_t frame, BBT_Time& bbt) const
 {
        {
                Glib::RWLock::ReaderLock lm (lock);
@@ -793,86 +821,87 @@ TempoMap::bbt_time (nframes64_t frame, BBT_Time& bbt) const
 }
 
 void
-TempoMap::bbt_time_unlocked (nframes64_t frame, BBT_Time& bbt) const
+TempoMap::bbt_time_unlocked (framepos_t frame, BBT_Time& bbt) const
 {
        bbt_time_with_metric (frame, bbt, metric_at (frame));
 }
 
 void
-TempoMap::bbt_time_with_metric (nframes64_t frame, BBT_Time& bbt, const TempoMetric& metric) const
+TempoMap::bbt_time_with_metric (framepos_t frame, BBT_Time& bbt, const TempoMetric& metric) const
 {
-       nframes64_t frame_diff;
+       framecnt_t frame_diff;
 
        // cerr << "---- BBT time for " << frame << " using metric @ " << metric.frame() << " BBT " << metric.start() << endl;
 
        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;
+       const double ticks_per_frame = metric.tempo().frames_per_beat (_frame_rate, metric.meter()) / BBT_Time::ticks_per_beat;
 
        /* 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 / 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(BBT_Time::ticks_per_beat * beat_fraction);
+
+       if (bbt.beats > (uint32_t)floor(beats_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;
 }
 
-nframes64_t
-TempoMap::count_frames_between ( const BBT_Time& start, const BBT_Time& end) const
+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
        */
 
-       nframes64_t frames = 0;
-       nframes64_t start_frame = 0;
-       nframes64_t end_frame = 0;
+       framecnt_t frames = 0;
+       framepos_t start_frame = 0;
+       framepos_t end_frame = 0;
 
        TempoMetric m = metric_at (start);
 
        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;
+               + start.ticks/BBT_Time::ticks_per_beat;
 
 
-       start_frame = m.frame() + (nframes64_t) rint( beat_offset * m.tempo().frames_per_beat(_frame_rate, m.meter()));
+       start_frame = m.frame() + (framepos_t) rint( beat_offset * m.tempo().frames_per_beat(_frame_rate, m.meter()));
 
        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;
+               + end.ticks/BBT_Time::ticks_per_beat;
 
-       end_frame = m.frame() + (nframes64_t) rint(beat_offset * m.tempo().frames_per_beat(_frame_rate, m.meter()));
+       end_frame = m.frame() + (framepos_t) rint(beat_offset * m.tempo().frames_per_beat(_frame_rate, m.meter()));
 
        frames = end_frame - start_frame;
 
@@ -880,12 +909,12 @@ TempoMap::count_frames_between ( const BBT_Time& start, const BBT_Time& end) con
 
 }
 
-nframes64_t
+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 */
 
-       nframes64_t frames = 0;
+       framecnt_t frames = 0;
        uint32_t bar = start.bars;
        double beat = (double) start.beats;
        double beats_counted = 0;
@@ -924,13 +953,13 @@ TempoMap::count_frames_between_metrics (const Meter& meter, const Tempo& tempo,
        // << " fpb was " << beat_frames
        // << endl;
 
-       frames = (nframes64_t) floor (beats_counted * beat_frames);
+       frames = (framecnt_t) llrint (floor (beats_counted * beat_frames));
 
        return frames;
 
 }
 
-nframes64_t
+framepos_t
 TempoMap::frame_time (const BBT_Time& bbt) const
 {
        BBT_Time start ; /* 1|1|0 */
@@ -938,10 +967,10 @@ TempoMap::frame_time (const BBT_Time& bbt) const
        return  count_frames_between ( start, bbt);
 }
 
-nframes64_t
-TempoMap::bbt_duration_at (nframes64_t pos, const BBT_Time& bbt, int dir) const
+framecnt_t
+TempoMap::bbt_duration_at (framepos_t pos, const BBT_Time& bbt, int dir) const
 {
-       nframes64_t frames = 0;
+       framecnt_t frames = 0;
 
        BBT_Time when;
        bbt_time(pos, when);
@@ -954,11 +983,10 @@ TempoMap::bbt_duration_at (nframes64_t pos, const BBT_Time& bbt, int dir) const
        return frames;
 }
 
-nframes64_t
+framecnt_t
 TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir) const
 {
-
-       nframes64_t frames = 0;
+       framecnt_t frames = 0;
 
        double beats_per_bar;
        BBT_Time result;
@@ -970,15 +998,12 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
        TempoMetric     metric = metric_at(result);
        beats_per_bar = metric.meter().beats_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;
@@ -990,17 +1015,20 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
                        beats_per_bar = metric.meter().beats_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 );
+                                       (1 - (ceil(beats_per_bar) - beats_per_bar))* BBT_Time::ticks_per_beat
+                                          : BBT_Time::ticks_per_beat );
 
                while (result.ticks >= ticks_at_beat) {
                        result.beats++;
@@ -1012,8 +1040,8 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
                                beats_per_bar = metric.meter().beats_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);
+                                      (1 - (ceil(beats_per_bar) - beats_per_bar) ) * BBT_Time::ticks_per_beat
+                                      : BBT_Time::ticks_per_beat);
 
                }
 
@@ -1021,10 +1049,10 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
        } else {
                uint32_t b = bbt.beats;
 
-                /* count beats */
+               /* count beats */
                while( b > when.beats ) {
-
-                       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();
                        if (b >= ceil(beats_per_bar)) {
@@ -1036,26 +1064,27 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
                }
                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) ;
+                                       ticks_at_beat = (uint32_t) ((1 - (ceil(beats_per_bar) - beats_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) {
@@ -1081,29 +1110,28 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
 
 
 
-nframes64_t
-TempoMap::round_to_bar (nframes64_t fr, int dir)
+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);
        }
 }
 
 
-nframes64_t
-TempoMap::round_to_beat (nframes64_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);
        }
 }
 
-nframes64_t
-TempoMap::round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir)
+framepos_t
+TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, int dir)
 {
-
        BBT_Time the_beat;
        uint32_t ticks_one_half_subdivisions_worth;
        uint32_t ticks_one_subdivisions_worth;
@@ -1111,7 +1139,7 @@ TempoMap::round_to_beat_subdivision (nframes64_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) {
@@ -1130,13 +1158,7 @@ TempoMap::round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir)
                        difference = ticks_one_subdivisions_worth - mod;
                }
 
-               if (the_beat.ticks + difference >= (uint32_t)Meter::ticks_per_beat) {
-                       the_beat.beats++;
-                       the_beat.ticks += difference;
-                       the_beat.ticks -= (uint32_t)Meter::ticks_per_beat;
-               } else {
-                       the_beat.ticks += difference;
-               }
+               the_beat = bbt_add (the_beat, BBT_Time (0, 0, difference));
 
        } else if (dir < 0) {
 
@@ -1147,27 +1169,19 @@ TempoMap::round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir)
                if (mod == 0) {
                        /* right on the subdivision, so the difference is just the subdivision ticks */
                        difference = ticks_one_subdivisions_worth;
-                       cerr << "On the sub, move by 1 sub = " << difference << endl;
                } else {
                        /* not on subdivision, compute distance to previous subdivision, which
                           is just the modulus.
                        */
 
                        difference = mod;
-                       cerr << "off the sub, move by 1 sub = " << difference << endl;
                }
 
-
-               cerr << "ticks = " << the_beat.ticks << endl;
-
-               if (the_beat.ticks < difference) {
-                       cerr << "backup beats, set ticks to "
-                            << (uint32_t)Meter::ticks_per_beat - difference << endl;
-                       the_beat.beats--;
-                       the_beat.ticks = (uint32_t)Meter::ticks_per_beat - difference;
-               } else {
-                       cerr << " reduce ticks\n";
-                       the_beat.ticks -= difference;
+               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 {
@@ -1175,13 +1189,7 @@ TempoMap::round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir)
 
                if (the_beat.ticks % ticks_one_subdivisions_worth > ticks_one_half_subdivisions_worth) {
                        difference = ticks_one_subdivisions_worth - (the_beat.ticks % ticks_one_subdivisions_worth);
-                       if (the_beat.ticks + difference >= (uint32_t)Meter::ticks_per_beat) {
-                               the_beat.beats++;
-                               the_beat.ticks += difference;
-                               the_beat.ticks -= (uint32_t)Meter::ticks_per_beat;
-                       } else {
-                               the_beat.ticks += difference;
-                       }
+                       the_beat = bbt_add (the_beat, BBT_Time (0, 0, difference));
                } else {
                        // difference = ticks_one_subdivisions_worth - (the_beat.ticks % ticks_one_subdivisions_worth);
                        the_beat.ticks -= the_beat.ticks % ticks_one_subdivisions_worth;
@@ -1191,8 +1199,8 @@ TempoMap::round_to_beat_subdivision (nframes64_t fr, int sub_num, int dir)
        return frame_time (the_beat);
 }
 
-nframes64_t
-TempoMap::round_to_type (nframes64_t frame, int dir, BBTPointType type)
+framepos_t
+TempoMap::round_to_type (framepos_t frame, int dir, BBTPointType type)
 {
        TempoMetric metric = metric_at (frame);
        BBT_Time bbt;
@@ -1204,7 +1212,7 @@ TempoMap::round_to_type (nframes64_t frame, int dir, BBTPointType type)
 
        switch (type) {
        case Bar:
-               DEBUG_TRACE(DEBUG::SnapBBT, string_compose ("round from %1 (%3) to bars in direction %2\n", frame, (dir < 0 ? "back" : "forward"), bbt));
+               DEBUG_TRACE(DEBUG::SnapBBT, string_compose ("round from %1 (%3) to bars in direction %2\n", frame, dir, bbt));
 
                if (dir < 0) {
 
@@ -1237,8 +1245,8 @@ TempoMap::round_to_type (nframes64_t frame, int dir, BBTPointType type)
                        float midbar_beats;
                        float midbar_ticks;
 
-                       midbar_beats = metric.meter().beats_per_bar() / 2;
-                       midbar_ticks = Meter::ticks_per_beat * fmod (midbar_beats, 1.0f);
+                       midbar_beats = metric.meter().beats_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));
@@ -1291,7 +1299,7 @@ TempoMap::round_to_type (nframes64_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);
@@ -1312,7 +1320,7 @@ TempoMap::round_to_type (nframes64_t frame, int dir, BBTPointType type)
 }
 
 TempoMap::BBTPointList *
-TempoMap::get_points (nframes64_t lower, nframes64_t upper) const
+TempoMap::get_points (framepos_t lower, framepos_t upper) const
 {
 
        Metrics::const_iterator i;
@@ -1331,7 +1339,7 @@ TempoMap::get_points (nframes64_t lower, nframes64_t upper) const
        double delta_bars;
        double delta_beats;
        double dummy;
-       nframes64_t limit;
+       framepos_t limit;
 
        meter = &first_meter ();
        tempo = &first_tempo ();
@@ -1408,7 +1416,7 @@ TempoMap::get_points (nframes64_t lower, nframes64_t upper) const
                        if (beat == 1) {
                                if (current >= lower) {
                                        // cerr << "Add Bar at " << bar << "|1" << " @ " << current << endl;
-                                       points->push_back (BBTPoint (*meter, *tempo,(nframes64_t)rint(current), Bar, bar, 1));
+                                       points->push_back (BBTPoint (*meter, *tempo,(framepos_t)rint(current), Bar, bar, 1));
 
                                }
                        }
@@ -1420,7 +1428,7 @@ TempoMap::get_points (nframes64_t lower, nframes64_t upper) const
                        while (beat <= ceil( beats_per_bar) && beat_frame < limit) {
                                if (beat_frame >= lower) {
                                        // cerr << "Add Beat at " << bar << '|' << beat << " @ " << beat_frame << endl;
-                                       points->push_back (BBTPoint (*meter, *tempo, (nframes64_t) rint(beat_frame), Beat, bar, beat));
+                                       points->push_back (BBTPoint (*meter, *tempo, (framepos_t) rint(beat_frame), Beat, bar, beat));
                                }
                                beat_frame += beat_frames;
                                current+= beat_frames;
@@ -1501,10 +1509,10 @@ TempoMap::get_points (nframes64_t lower, nframes64_t upper) const
 }
 
 const TempoSection&
-TempoMap::tempo_section_at (nframes64_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) {
@@ -1528,7 +1536,7 @@ TempoMap::tempo_section_at (nframes64_t frame)
 }
 
 const Tempo&
-TempoMap::tempo_at (nframes64_t frame) const
+TempoMap::tempo_at (framepos_t frame) const
 {
        TempoMetric m (metric_at (frame));
        return m.tempo();
@@ -1536,7 +1544,7 @@ TempoMap::tempo_at (nframes64_t frame) const
 
 
 const Meter&
-TempoMap::meter_at (nframes64_t frame) const
+TempoMap::meter_at (framepos_t frame) const
 {
        TempoMetric m (metric_at (frame));
        return m.meter();
@@ -1549,9 +1557,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());
                }
        }
 
@@ -1609,7 +1617,7 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                }
        }
 
-       StateChanged (Change (0));
+       PropertyChanged (PropertyChange ());
 
        return 0;
 }
@@ -1663,7 +1671,7 @@ TempoMap::n_meters() const
 }
 
 void
-TempoMap::insert_time (nframes64_t where, nframes64_t amount)
+TempoMap::insert_time (framepos_t where, framecnt_t amount)
 {
        for (Metrics::iterator i = metrics->begin(); i != metrics->end(); ++i) {
                if ((*i)->frame() >= where) {
@@ -1673,7 +1681,7 @@ TempoMap::insert_time (nframes64_t where, nframes64_t amount)
 
        timestamp_metrics (false);
 
-       StateChanged (Change (0));
+       PropertyChanged (PropertyChange ());
 }
 
 BBT_Time
@@ -1687,16 +1695,18 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& other) const
  * add the BBT interval @param increment to  @param start and return the result
  */
 BBT_Time
-TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& increment, const TempoMetric& metric) const
+TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& increment, const TempoMetric& /*metric*/) const
 {
        BBT_Time result = start;
        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;
+       }
 
        /* now comes the complicated part. we have to add one beat a time,
           checking for a new metric on every beat.
@@ -1794,7 +1804,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;
        }
@@ -1892,3 +1902,235 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& decrement) const
        result.bars -= op.bars;
        return result;
 }
+
+/** Add the BBT interval op to pos and return the result */
+framepos_t
+TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
+{
+       /* XXX: this is a little inaccurate as small errors are introduced
+          every time a probably-fractional product of something and
+          frames_per_beat is rounded.  Other errors can be introduced
+          by op.ticks' integer nature.
+       */
+       
+       Metrics::const_iterator i;
+       const MeterSection* meter;
+       const MeterSection* m;
+       const TempoSection* tempo;
+       const TempoSection* t;
+       framecnt_t 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, *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;
+}