Tempo ramps - fix mucally-snapped dragging across the 'no time' gap (audio-locked...
[ardour.git] / libs / ardour / tempo.cc
index d869bed42c0b718dd954a6518b8a363aa7ac4b2d..a21ec43aeeb12f15c1be36d10a6f027a627dfd91 100644 (file)
@@ -84,6 +84,8 @@ TempoSection::TempoSection (const XMLNode& node)
        double pulse;
        uint32_t frame;
 
+       _legacy_bbt = BBT_Time (0, 0, 0);
+
        if ((prop = node.property ("start")) != 0) {
                if (sscanf (prop->value().c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
                            &bbt.bars,
@@ -92,20 +94,18 @@ TempoSection::TempoSection (const XMLNode& node)
                        /* legacy session - start used to be in bbt*/
                        _legacy_bbt = bbt;
                        pulse = -1.0;
-                       set_pulse (pulse);
+                       info << _("Legacy session detected. TempoSection XML node will be altered.") << endmsg;
                }
-       } else {
-               warning << _("TempoSection XML node has no \"start\" property") << endmsg;
        }
 
-
        if ((prop = node.property ("pulse")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &pulse) != 1 || pulse < 0.0) {
-                       error << _("TempoSection XML node has an illegal \"beat\" value") << endmsg;
-               } else {
-                       set_pulse (pulse);
+               if (sscanf (prop->value().c_str(), "%lf", &pulse) != 1) {
+                       error << _("TempoSection XML node has an illegal \"pulse\" value") << endmsg;
                }
        }
+
+       set_pulse (pulse);
+
        if ((prop = node.property ("frame")) != 0) {
                if (sscanf (prop->value().c_str(), "%" PRIu32, &frame) != 1) {
                        error << _("TempoSection XML node has an illegal \"frame\" value") << endmsg;
@@ -155,7 +155,11 @@ TempoSection::TempoSection (const XMLNode& node)
        }
 
        if ((prop = node.property ("lock-style")) == 0) {
-               set_position_lock_style (MusicTime);
+               if (movable()) {
+                       set_position_lock_style (MusicTime);
+               } else {
+                       set_position_lock_style (AudioTime);
+               }
        } else {
                set_position_lock_style (PositionLockStyle (string_2_enum (prop->value(), position_lock_style())));
        }
@@ -454,14 +458,13 @@ MeterSection::MeterSection (const XMLNode& node)
                        error << _("MeterSection XML node has an illegal \"start\" value") << endmsg;
                } else {
                        /* legacy session - start used to be in bbt*/
+                       info << _("Legacy session detected - MeterSection XML node will be altered.") << endmsg;
                        pulse = -1.0;
                }
-       } else {
-               error << _("MeterSection XML node has no \"start\" property") << endmsg;
        }
 
        if ((prop = node.property ("pulse")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &pulse) != 1 || pulse < 0.0) {
+               if (sscanf (prop->value().c_str(), "%lf", &pulse) != 1) {
                        error << _("MeterSection XML node has an illegal \"pulse\" value") << endmsg;
                }
        }
@@ -469,20 +472,20 @@ MeterSection::MeterSection (const XMLNode& node)
 
        if ((prop = node.property ("beat")) != 0) {
                if (sscanf (prop->value().c_str(), "%lf", &beat) != 1) {
-                       error << _("MeterSection XML node has an illegal \"beat\" vlue") << endmsg;
+                       error << _("MeterSection XML node has an illegal \"beat\" value") << endmsg;
                }
        }
 
        start.first = beat;
 
        if ((prop = node.property ("bbt")) == 0) {
-               error << _("MeterSection XML node has no \"bbt\" property") << endmsg;
+               warning << _("MeterSection XML node has no \"bbt\" property") << endmsg;
        } else if (sscanf (prop->value().c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
                    &bbt.bars,
                    &bbt.beats,
                    &bbt.ticks) < 3) {
                error << _("MeterSection XML node has an illegal \"bbt\" value") << endmsg;
-               //throw failed_constructor();
+               throw failed_constructor();
        }
 
        start.second = bbt;
@@ -518,19 +521,23 @@ MeterSection::MeterSection (const XMLNode& node)
                throw failed_constructor();
        }
 
-       if ((prop = node.property ("lock-style")) == 0) {
-               warning << _("MeterSection XML node has no \"lock-style\" property") << endmsg;
-               set_position_lock_style (MusicTime);
-       } else {
-               set_position_lock_style (PositionLockStyle (string_2_enum (prop->value(), position_lock_style())));
-       }
-
        if ((prop = node.property ("movable")) == 0) {
                error << _("MeterSection XML node has no \"movable\" property") << endmsg;
                throw failed_constructor();
        }
 
        set_movable (string_is_affirmative (prop->value()));
+
+       if ((prop = node.property ("lock-style")) == 0) {
+               warning << _("MeterSection XML node has no \"lock-style\" property") << endmsg;
+               if (movable()) {
+                       set_position_lock_style (MusicTime);
+               } else {
+                       set_position_lock_style (AudioTime);
+               }
+       } else {
+               set_position_lock_style (PositionLockStyle (string_2_enum (prop->value(), position_lock_style())));
+       }
 }
 
 XMLNode&
@@ -617,8 +624,8 @@ TempoMap::TempoMap (framecnt_t fr)
        _frame_rate = fr;
        BBT_Time start (1, 1, 0);
 
-       TempoSection *t = new TempoSection (0.0, _default_tempo.beats_per_minute(), _default_tempo.note_type(), TempoSection::Constant);
-       MeterSection *m = new MeterSection (0.0, 0.0, start, _default_meter.divisions_per_bar(), _default_meter.note_divisor());
+       TempoSection *t = new TempoSection ((framepos_t) 0, _default_tempo.beats_per_minute(), _default_tempo.note_type(), TempoSection::Constant);
+       MeterSection *m = new MeterSection ((framepos_t) 0, 0.0, start, _default_meter.divisions_per_bar(), _default_meter.note_divisor());
 
        t->set_movable (false);
        m->set_movable (false);
@@ -789,7 +796,7 @@ TempoMap::do_insert (MetricSection* section)
                                         */
 
                                        *(dynamic_cast<Meter*>(*i)) = *(dynamic_cast<Meter*>(insert_meter));
-                                       (*i)->set_position_lock_style (insert_meter->position_lock_style());
+                                       (*i)->set_position_lock_style (AudioTime);
                                        need_add = false;
                                } else {
                                        _metrics.erase (i);
@@ -953,10 +960,9 @@ TempoMap::replace_meter (const MeterSection& ms, const Meter& meter, const BBT_T
                        add_meter_locked (meter, bbt_to_beats_locked (_metrics, where), where, true);
                } else {
                        MeterSection& first (first_meter());
-                       const PositionLockStyle pl = ms.position_lock_style();
                        /* cannot move the first meter section */
                        *static_cast<Meter*>(&first) = meter;
-                       first.set_position_lock_style (pl);
+                       first.set_position_lock_style (AudioTime);
                }
                recompute_map (_metrics);
        }
@@ -1282,10 +1288,16 @@ TempoMap::recompute_meters (Metrics& metrics)
                                if (meter->movable()) {
                                        const double beats = ((pulse_at_frame_locked (metrics, meter->frame()) - prev_m->pulse()) * prev_m->note_divisor());
                                        const double floor_beats = beats - fmod (beats, prev_m->divisions_per_bar());
-                                       b_bbt = make_pair (floor_beats + prev_m->beat(), BBT_Time ((floor_beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
-                                       const double true_pulse = prev_m->pulse() + (floor_beats / prev_m->note_divisor());
-                                       const double pulse_off = true_pulse - (beats / prev_m->note_divisor()) - prev_m->pulse();
-                                       pulse = true_pulse - pulse_off;
+                                       if (floor_beats + prev_m->beat() < meter->beat()) {
+                                               /* tempo change caused a change in beat (bar). */
+                                               b_bbt = make_pair (floor_beats + prev_m->beat(), BBT_Time ((floor_beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
+                                               const double true_pulse = prev_m->pulse() + (floor_beats / prev_m->note_divisor());
+                                               const double pulse_off = true_pulse - (beats / prev_m->note_divisor()) - prev_m->pulse();
+                                               pulse = true_pulse - pulse_off;
+                                       } else {
+                                               b_bbt = make_pair (meter->beat(), meter->bbt());
+                                               pulse = pulse_at_frame_locked (metrics, meter->frame());
+                                       }
                                } else {
                                        b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
                                }
@@ -1293,13 +1305,16 @@ TempoMap::recompute_meters (Metrics& metrics)
                                meter->set_pulse (pulse);
                        } else {
                                double pulse = 0.0;
+                               pair<double, BBT_Time> new_beat;
                                if (prev_m) {
                                        pulse = prev_m->pulse() + ((meter->bbt().bars - prev_m->bbt().bars) *  prev_m->divisions_per_bar() / prev_m->note_divisor());
+                                       new_beat = make_pair (((pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat(), meter->bbt());
                                } else {
                                        /* shouldn't happen - the first is audio-locked */
                                        pulse = pulse_at_beat_locked (metrics, meter->beat());
+                                       new_beat = make_pair (pulse, meter->bbt());
                                }
-                               pair<double, BBT_Time> new_beat (((pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat(), meter->bbt());
+
                                meter->set_beat (new_beat);
                                meter->set_frame (frame_at_pulse_locked (metrics, pulse));
                                meter->set_pulse (pulse);
@@ -1431,7 +1446,9 @@ TempoMap::beat_at_pulse_locked (const Metrics& metrics, const double& pulse) con
                MeterSection* m;
                if ((m = dynamic_cast<MeterSection*> (*i)) != 0) {
                        if (prev_m && m->pulse() > pulse) {
-                               break;
+                               if ((pulse - prev_m->pulse()) * prev_m->note_divisor() < m->beat()) {
+                                       break;
+                               }
                        }
                        prev_m = m;
                }
@@ -1476,6 +1493,13 @@ TempoMap::pulse_at_frame_locked (const Metrics& metrics, const framecnt_t& frame
        return pulses_in_section + prev_t->pulse();
 }
 
+double
+TempoMap::pulse_at_frame (const framecnt_t& frame) const
+{
+       Glib::Threads::RWLock::ReaderLock lm (lock);
+       return pulse_at_frame_locked (_metrics, frame);
+}
+
 framecnt_t
 TempoMap::frame_at_pulse_locked (const Metrics& metrics, const double& pulse) const
 {
@@ -1506,11 +1530,40 @@ TempoMap::frame_at_pulse_locked (const Metrics& metrics, const double& pulse) co
        return ret;
 }
 
+framecnt_t
+TempoMap::frame_at_pulse (const double& pulse) const
+{
+       Glib::Threads::RWLock::ReaderLock lm (lock);
+       return frame_at_pulse_locked (_metrics, pulse);
+}
+
 double
 TempoMap::beat_at_frame_locked (const Metrics& metrics, const framecnt_t& frame) const
 {
-       const double pulse = pulse_at_frame_locked (metrics, frame);
-       return beat_at_pulse_locked (metrics, pulse);
+       const TempoSection& ts = tempo_section_at_locked (metrics, frame);
+       MeterSection* prev_m = 0;
+       MeterSection* next_m = 0;
+
+       for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
+               MeterSection* m;
+               if ((m = dynamic_cast<MeterSection*> (*i)) != 0) {
+                       if (prev_m && m->frame() > frame) {
+                               next_m = m;
+                               break;
+                       }
+                       prev_m = m;
+               }
+       }
+       if (frame < prev_m->frame()) {
+               return 0.0;
+       }
+       const double beat = prev_m->beat() + (ts.pulse_at_frame (frame, _frame_rate) - prev_m->pulse()) * prev_m->note_divisor();
+
+       if (next_m && next_m->beat() < beat) {
+               return next_m->beat();
+       }
+
+       return beat;
 }
 
 double
@@ -1523,8 +1576,20 @@ TempoMap::beat_at_frame (const framecnt_t& frame) const
 framecnt_t
 TempoMap::frame_at_beat_locked (const Metrics& metrics, const double& beat) const
 {
-       const framecnt_t frame = frame_at_pulse_locked (metrics, pulse_at_beat_locked (metrics, beat));
-       return frame;
+       const TempoSection& prev_t = tempo_section_at_beat_locked (metrics, beat);
+       MeterSection* prev_m = 0;
+
+       for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
+               MeterSection* m;
+               if ((m = dynamic_cast<MeterSection*> (*i)) != 0) {
+                       if (prev_m && m->beat() > beat) {
+                               break;
+                       }
+                       prev_m = m;
+               }
+       }
+
+       return prev_t.frame_at_pulse (((beat - prev_m->beat()) / prev_m->note_divisor()) + prev_m->pulse(), _frame_rate);
 }
 
 framecnt_t
@@ -1724,7 +1789,7 @@ TempoMap::frame_time (const BBT_Time& bbt)
 }
 
 bool
-TempoMap::check_solved (Metrics& metrics, bool by_frame)
+TempoMap::check_solved (const Metrics& metrics, bool by_frame) const
 {
        TempoSection* prev_t = 0;
 
@@ -1975,6 +2040,11 @@ TempoMap::solve_map (Metrics& imaginary, TempoSection* section, const double& pu
 void
 TempoMap::solve_map (Metrics& imaginary, MeterSection* section, const framepos_t& frame)
 {
+       /* disallow moving first meter past any subsequent one, and any movable meter before the first one */
+       const MeterSection* other =  &meter_section_at_locked (imaginary, frame);
+       if ((!section->movable() && other->movable()) || (!other->movable() && section->movable() && other->frame() >= frame)) {
+               return;
+       }
        MeterSection* prev_m = 0;
 
        if (!section->movable()) {
@@ -2000,8 +2070,6 @@ TempoMap::solve_map (Metrics& imaginary, MeterSection* section, const framepos_t
                }
        }
 
-       section->set_frame (frame);
-
        for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
                MeterSection* m;
                if ((m = dynamic_cast<MeterSection*> (*i)) != 0) {
@@ -2009,7 +2077,7 @@ TempoMap::solve_map (Metrics& imaginary, MeterSection* section, const framepos_t
                                /*
                                  here we set the beat for this frame.
                                  we set it 'incorrectly' to the next bar's first beat
-                                 and use the delat to find the meter's pulse.
+                                 and use the delta to find the meter's pulse.
                                */
                                double new_pulse = 0.0;
                                pair<double, BBT_Time> b_bbt;
@@ -2017,14 +2085,20 @@ TempoMap::solve_map (Metrics& imaginary, MeterSection* section, const framepos_t
                                if (section->movable()) {
                                        const double beats = ((pulse_at_frame_locked (imaginary, frame) - prev_m->pulse()) * prev_m->note_divisor());
                                        const double floor_beats = beats - fmod (beats,  prev_m->divisions_per_bar());
-                                       b_bbt = make_pair (floor_beats + prev_m->beat(), BBT_Time ((floor_beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
-                                       const double true_pulse = prev_m->pulse() + (floor_beats / prev_m->note_divisor());
-                                       const double pulse_off = true_pulse - (beats / prev_m->note_divisor()) - prev_m->pulse();
-                                       new_pulse = true_pulse - pulse_off;
+                                       if (floor_beats + prev_m->beat() < section->beat()) {
+                                               /* disallow position change if it will alter out beat
+                                                  we allow tempo changes to do this in recompute_meters().
+                                                  blocking this is an option, but i'm not convinced that
+                                                  this is what the user would actually want.
+                                               */
+                                               return;
+                                       }
+                                       b_bbt = make_pair (section->beat(), section->bbt());
+                                       new_pulse = pulse_at_frame_locked (imaginary, frame);
                                } else {
                                        b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
                                }
-
+                               section->set_frame (frame);
                                section->set_beat (b_bbt);
                                section->set_pulse (new_pulse);
                                prev_m = m;
@@ -2491,7 +2565,7 @@ void
 TempoMap::round_bbt (BBT_Time& when, const int32_t& sub_num)
 {
        if (sub_num == -1) {
-               const double bpb = meter_section_at (bbt_to_beats_locked (_metrics, when)).divisions_per_bar();
+               const double bpb = meter_section_at_beat (bbt_to_beats_locked (_metrics, when)).divisions_per_bar();
                if ((double) when.beats > bpb / 2.0) {
                        ++when.bars;
                }
@@ -2499,7 +2573,7 @@ TempoMap::round_bbt (BBT_Time& when, const int32_t& sub_num)
                when.ticks = 0;
                return;
        } else if (sub_num == 0) {
-               const double bpb = meter_section_at (bbt_to_beats_locked (_metrics, when)).divisions_per_bar();
+               const double bpb = meter_section_at_beat (bbt_to_beats_locked (_metrics, when)).divisions_per_bar();
                if ((double) when.ticks > BBT_Time::ticks_per_beat / 2.0) {
                        ++when.beats;
                        while ((double) when.beats > bpb) {
@@ -2597,18 +2671,19 @@ TempoMap::get_grid (vector<TempoMap::BBTPoint>& points,
                    framepos_t lower, framepos_t upper)
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
-       const int32_t upper_beat = (int32_t) ceil (beat_at_frame_locked (_metrics, upper));
-       int32_t cnt = floor (beat_at_frame_locked (_metrics, lower));
+       const int32_t upper_beat = (int32_t) floor (beat_at_frame_locked (_metrics, upper));
+       int32_t cnt = ceil (beat_at_frame_locked (_metrics, lower));
+       framecnt_t pos = 0;
        /* although the map handles negative beats, bbt doesn't. */
        if (cnt < 0.0) {
                cnt = 0.0;
        }
-       while (cnt <= upper_beat) {
-               framecnt_t pos = frame_at_beat_locked (_metrics, cnt);
-               const TempoSection tempo = tempo_section_at_locked (pos);
-               const MeterSection meter = meter_section_at_locked (pos);
+       while (pos < upper) {
+               pos = frame_at_beat_locked (_metrics, cnt);
+               const TempoSection tempo = tempo_section_at_locked (_metrics, pos);
+               const MeterSection meter = meter_section_at_locked (_metrics, pos);
                const BBT_Time bbt = beats_to_bbt (cnt);
-               points.push_back (BBTPoint (meter, tempo_at_locked (pos), pos, bbt.bars, bbt.beats, tempo.c_func()));
+               points.push_back (BBTPoint (meter, tempo_at_locked (_metrics, pos), pos, bbt.bars, bbt.beats, tempo.c_func()));
                ++cnt;
        }
 }
@@ -2617,23 +2692,23 @@ const TempoSection&
 TempoMap::tempo_section_at (framepos_t frame) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
-       return tempo_section_at_locked (frame);
+       return tempo_section_at_locked (_metrics, frame);
 }
 
 const TempoSection&
-TempoMap::tempo_section_at_locked (framepos_t frame) const
+TempoMap::tempo_section_at_locked (const Metrics& metrics, framepos_t frame) const
 {
        Metrics::const_iterator i;
        TempoSection* prev = 0;
 
-       for (i = _metrics.begin(); i != _metrics.end(); ++i) {
+       for (i = metrics.begin(); i != metrics.end(); ++i) {
                TempoSection* t;
 
                if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
                        if (!t->active()) {
                                continue;
                        }
-                       if (t->frame() > frame) {
+                       if (prev && t->frame() > frame) {
                                break;
                        }
 
@@ -2649,6 +2724,24 @@ TempoMap::tempo_section_at_locked (framepos_t frame) const
        return *prev;
 }
 
+const TempoSection&
+TempoMap::tempo_section_at_beat_locked (const Metrics& metrics, const double& beat) const
+{
+       TempoSection* prev_t = 0;
+       const MeterSection* prev_m = &meter_section_at_beat_locked (metrics, beat);
+
+       for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
+               TempoSection* t;
+               if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
+                       if (prev_t && ((t->pulse() - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat() > beat) {
+                               break;
+                       }
+                       prev_t = t;
+               }
+
+       }
+       return *prev_t;
+}
 
 /* don't use this to calculate length (the tempo is only correct for this frame).
    do that stuff based on the beat_at_frame and frame_at_beat api
@@ -2658,7 +2751,7 @@ TempoMap::frames_per_beat_at (const framepos_t& frame, const framecnt_t& sr) con
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
-       const TempoSection* ts_at = &tempo_section_at_locked (frame);
+       const TempoSection* ts_at = &tempo_section_at_locked (_metrics, frame);
        const TempoSection* ts_after = 0;
        Metrics::const_iterator i;
 
@@ -2684,14 +2777,7 @@ TempoMap::frames_per_beat_at (const framepos_t& frame, const framecnt_t& sr) con
 }
 
 const Tempo
-TempoMap::tempo_at (const framepos_t& frame) const
-{
-       Glib::Threads::RWLock::ReaderLock lm (lock);
-       return tempo_at_locked (frame);
-}
-
-const Tempo
-TempoMap::tempo_at_locked (const framepos_t& frame) const
+TempoMap::tempo_at_locked (const Metrics& metrics, const framepos_t& frame) const
 {
        TempoSection* prev_t = 0;
 
@@ -2719,20 +2805,20 @@ TempoMap::tempo_at_locked (const framepos_t& frame) const
        return ret_tempo;
 }
 
-const MeterSection&
-TempoMap::meter_section_at (framepos_t frame) const
+const Tempo
+TempoMap::tempo_at (const framepos_t& frame) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
-       return meter_section_at_locked (frame);
+       return tempo_at_locked (_metrics, frame);
 }
 
 const MeterSection&
-TempoMap::meter_section_at_locked (framepos_t frame) const
+TempoMap::meter_section_at_locked (const Metrics& metrics, framepos_t frame) const
 {
        Metrics::const_iterator i;
        MeterSection* prev = 0;
 
-       for (i = _metrics.begin(); i != _metrics.end(); ++i) {
+       for (i = metrics.begin(); i != metrics.end(); ++i) {
                MeterSection* m;
 
                if ((m = dynamic_cast<MeterSection*> (*i)) != 0) {
@@ -2753,20 +2839,20 @@ TempoMap::meter_section_at_locked (framepos_t frame) const
        return *prev;
 }
 
-const Meter&
-TempoMap::meter_at (framepos_t frame) const
+
+const MeterSection&
+TempoMap::meter_section_at (framepos_t frame) const
 {
-       TempoMetric m (metric_at (frame));
-       return m.meter();
+       Glib::Threads::RWLock::ReaderLock lm (lock);
+       return meter_section_at_locked (_metrics, frame);
 }
 
 const MeterSection&
-TempoMap::meter_section_at (const double& beat) const
+TempoMap::meter_section_at_beat_locked (const Metrics& metrics, const double& beat) const
 {
        MeterSection* prev_m = 0;
-       Glib::Threads::RWLock::ReaderLock lm (lock);
 
-       for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
+       for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
                MeterSection* m;
                if ((m = dynamic_cast<MeterSection*> (*i)) != 0) {
                        if (prev_m && m->beat() > beat) {
@@ -2779,6 +2865,82 @@ TempoMap::meter_section_at (const double& beat) const
        return *prev_m;
 }
 
+const MeterSection&
+TempoMap::meter_section_at_beat (double beat) const
+{
+       Glib::Threads::RWLock::ReaderLock lm (lock);
+       return meter_section_at_beat_locked (_metrics, beat);
+}
+
+const Meter&
+TempoMap::meter_at (framepos_t frame) const
+{
+       TempoMetric m (metric_at (frame));
+       return m.meter();
+}
+
+void
+TempoMap::fix_legacy_session ()
+{
+       MeterSection* prev_m = 0;
+       TempoSection* prev_t = 0;
+
+       for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
+               MeterSection* m;
+               TempoSection* t;
+
+               if ((m = dynamic_cast<MeterSection*>(*i)) != 0) {
+                       if (!m->movable()) {
+                               pair<double, BBT_Time> bbt = make_pair (0.0, BBT_Time (1, 1, 0));
+                               m->set_beat (bbt);
+                               m->set_pulse (0.0);
+                               m->set_frame (0);
+                               m->set_position_lock_style (AudioTime);
+                               prev_m = m;
+                               continue;
+                       }
+                       if (prev_m) {
+                               pair<double, BBT_Time> start = make_pair (((m->bbt().bars - 1) * prev_m->note_divisor())
+                                                                         + (m->bbt().beats - 1)
+                                                                         + (m->bbt().ticks / BBT_Time::ticks_per_beat)
+                                                                         , m->bbt());
+                               m->set_beat (start);
+                               const double start_beat = ((m->bbt().bars - 1) * prev_m->note_divisor())
+                                       + (m->bbt().beats - 1)
+                                       + (m->bbt().ticks / BBT_Time::ticks_per_beat);
+                               m->set_pulse (start_beat / prev_m->note_divisor());
+                       }
+                       prev_m = m;
+               } else if ((t = dynamic_cast<TempoSection*>(*i)) != 0) {
+
+                       if (!t->active()) {
+                               continue;
+                       }
+
+                       if (!t->movable()) {
+                               t->set_pulse (0.0);
+                               t->set_frame (0);
+                               t->set_position_lock_style (AudioTime);
+                               prev_t = t;
+                               continue;
+                       }
+
+                       if (prev_t) {
+                               const double beat = ((t->legacy_bbt().bars - 1) * ((prev_m) ? prev_m->note_divisor() : 4.0))
+                                       + (t->legacy_bbt().beats - 1)
+                                       + (t->legacy_bbt().ticks / BBT_Time::ticks_per_beat);
+                               if (prev_m) {
+                                       t->set_pulse (beat / prev_m->note_divisor());
+                               } else {
+                                       /* really shouldn't happen but.. */
+                                       t->set_pulse (beat / 4.0);
+                               }
+                       }
+                       prev_t = t;
+               }
+       }
+}
+
 XMLNode&
 TempoMap::get_state ()
 {
@@ -2843,59 +3005,19 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                        MetricSectionSorter cmp;
                        _metrics.sort (cmp);
                }
+
                /* check for legacy sessions where bbt was the base musical unit for tempo */
-               for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
-                       MeterSection* m;
+               for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
                        TempoSection* t;
-                       MeterSection* prev_m = 0;
-                       TempoSection* prev_t = 0;
-                       /* if one is < 0.0, they all are. this is a legacy session */
-                       if ((m = dynamic_cast<MeterSection*>(*i)) != 0 && m->pulse() < 0.0) {
-                               if (!m->movable()) {
-                                       pair<double, BBT_Time> bbt = make_pair (0.0, BBT_Time (1, 1, 0));
-                                       m->set_beat (bbt);
-                                       m->set_pulse (0.0);
-                                       m->set_frame (0);
-                                       m->set_position_lock_style (AudioTime);
-                                       prev_m = m;
-                                       continue;
-                               }
-                               if (prev_m && prev_m->pulse() < 0.0) {
-                                       /*XX we cannot possibly make this work??. */
-                                       pair<double, BBT_Time> start = make_pair (((m->bbt().bars - 1) * prev_m->note_divisor())
-                                                                                 + (m->bbt().beats - 1)
-                                                                                 + (m->bbt().ticks / BBT_Time::ticks_per_beat)
-                                                                                 , m->bbt());
-                                       m->set_beat (start);
-                                       const double start_beat = ((m->bbt().bars - 1) * prev_m->note_divisor())
-                                               + (m->bbt().beats - 1)
-                                               + (m->bbt().ticks / BBT_Time::ticks_per_beat);
-                                       m->set_pulse (start_beat / prev_m->note_divisor());
-                               }
-                               prev_m = m;
-                       } else if ((t = dynamic_cast<TempoSection*>(*i)) != 0 && t->pulse() < 0.0) {
-                               if (!t->active()) {
-                                       continue;
-                               }
-                               if (!t->movable()) {
-                                       t->set_pulse (0.0);
-                                       t->set_frame (0);
-                                       t->set_position_lock_style (AudioTime);
-                                       prev_t = t;
-                                       continue;
-                               }
-                               if (prev_t && prev_t->pulse() < 0.0) {
-                                       double const beat = ((t->legacy_bbt().bars - 1) * prev_m->note_divisor()) + (t->legacy_bbt().beats - 1) + (t->legacy_bbt().ticks / BBT_Time::ticks_per_beat);
-                                       if (prev_m) {
-                                               t->set_pulse (beat / prev_m->note_divisor());
-                                       } else {
-                                               /* really shouldn't happen but.. */
-                                               t->set_pulse (beat / 4.0);
-                                       }
+                       if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
+                               if (t->legacy_bbt().bars != 0) {
+                                       fix_legacy_session();
+                                       break;
                                }
-                               prev_t = t;
+                               break;
                        }
                }
+
                /* check for multiple tempo/meters at the same location, which
                   ardour2 somehow allowed.
                */
@@ -3206,10 +3328,10 @@ TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
        }
        pos_bbt.beats += op.beats;
        /* the meter in effect will start on the bar */
-       double divisions_per_bar = meter_section_at (bbt_to_beats_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar();
+       double divisions_per_bar = meter_section_at_beat (bbt_to_beats_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar();
        while (pos_bbt.beats >= divisions_per_bar + 1) {
                ++pos_bbt.bars;
-               divisions_per_bar = meter_section_at (bbt_to_beats_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar();
+               divisions_per_bar = meter_section_at_beat (bbt_to_beats_locked (_metrics, BBT_Time (pos_bbt.bars + op.bars, 1, 0))).divisions_per_bar();
                pos_bbt.beats -= divisions_per_bar;
        }
        pos_bbt.bars += op.bars;