Tempo ramps - correct previous commit.
[ardour.git] / libs / ardour / tempo.cc
index 4915c42e8a35c7970d43195361b2d08395629a6f..b8a1007e74adbca3c1a6850986c8095a552cc65f 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())));
        }
@@ -198,7 +202,7 @@ double
 TempoSection::tempo_at_frame (const framepos_t& f, const framecnt_t& frame_rate) const
 {
 
-       if (_type == Constant) {
+       if (_type == Constant || _c_func == 0.0) {
                return pulses_per_minute();
        }
 
@@ -213,7 +217,7 @@ TempoSection::tempo_at_frame (const framepos_t& f, const framecnt_t& frame_rate)
 framepos_t
 TempoSection::frame_at_tempo (const double& ppm, const double& b, const framecnt_t& frame_rate) const
 {
-       if (_type == Constant) {
+       if (_type == Constant || _c_func == 0.0) {
                return ((b - pulse())  * frames_per_pulse (frame_rate))  + frame();
        }
 
@@ -225,7 +229,7 @@ double
 TempoSection::tempo_at_pulse (const double& p) const
 {
 
-       if (_type == Constant) {
+       if (_type == Constant || _c_func == 0.0) {
                return pulses_per_minute();
        }
        double const ppm = pulse_tempo_at_pulse (p - pulse());
@@ -239,7 +243,7 @@ TempoSection::tempo_at_pulse (const double& p) const
 double
 TempoSection::pulse_at_tempo (const double& ppm, const framepos_t& f, const framecnt_t& frame_rate) const
 {
-       if (_type == Constant) {
+       if (_type == Constant || _c_func == 0.0) {
                double const pulses = ((f - frame()) / frames_per_pulse (frame_rate)) + pulse();
                return  pulses;
        }
@@ -253,7 +257,7 @@ TempoSection::pulse_at_tempo (const double& ppm, const framepos_t& f, const fram
 double
 TempoSection::pulse_at_frame (const framepos_t& f, const framecnt_t& frame_rate) const
 {
-       if (_type == Constant) {
+       if (_type == Constant || _c_func == 0.0) {
                return ((f - frame()) / frames_per_pulse (frame_rate)) + pulse();
        }
 
@@ -268,7 +272,7 @@ TempoSection::pulse_at_frame (const framepos_t& f, const framecnt_t& frame_rate)
 framepos_t
 TempoSection::frame_at_pulse (const double& p, const framecnt_t& frame_rate) const
 {
-       if (_type == Constant) {
+       if (_type == Constant || _c_func == 0.0) {
                return (framepos_t) floor ((p - pulse()) * frames_per_pulse (frame_rate)) + frame();
        }
 
@@ -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&
@@ -618,7 +625,7 @@ TempoMap::TempoMap (framecnt_t 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());
+       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);
@@ -885,51 +892,61 @@ TempoMap::replace_tempo (const TempoSection& ts, const Tempo& tempo, const frame
        PropertyChanged (PropertyChange ());
 }
 
-void
+TempoSection*
 TempoMap::add_tempo (const Tempo& tempo, const double& pulse, ARDOUR::TempoSection::Type type)
 {
+       TempoSection* ts = 0;
        {
                Glib::Threads::RWLock::WriterLock lm (lock);
-               add_tempo_locked (tempo, pulse, true, type);
+               ts = add_tempo_locked (tempo, pulse, true, type);
        }
 
        PropertyChanged (PropertyChange ());
+
+       return ts;
 }
 
-void
+TempoSection*
 TempoMap::add_tempo (const Tempo& tempo, const framepos_t& frame, ARDOUR::TempoSection::Type type)
 {
+       TempoSection* ts = 0;
        {
                Glib::Threads::RWLock::WriterLock lm (lock);
-               add_tempo_locked (tempo, frame, true, type);
+               ts = add_tempo_locked (tempo, frame, true, type);
        }
 
 
        PropertyChanged (PropertyChange ());
+
+       return ts;
 }
 
-void
+TempoSection*
 TempoMap::add_tempo_locked (const Tempo& tempo, double pulse, bool recompute, ARDOUR::TempoSection::Type type)
 {
-       TempoSection* ts = new TempoSection (pulse, tempo.beats_per_minute(), tempo.note_type(), type);
+       TempoSection* t = new TempoSection (pulse, tempo.beats_per_minute(), tempo.note_type(), type);
 
-       do_insert (ts);
+       do_insert (t);
 
        if (recompute) {
-               solve_map (_metrics, ts, ts->pulse());
+               solve_map (_metrics, t, t->pulse());
        }
+
+       return t;
 }
 
-void
+TempoSection*
 TempoMap::add_tempo_locked (const Tempo& tempo, framepos_t frame, bool recompute, ARDOUR::TempoSection::Type type)
 {
-       TempoSection* ts = new TempoSection (frame, tempo.beats_per_minute(), tempo.note_type(), type);
+       TempoSection* t = new TempoSection (frame, tempo.beats_per_minute(), tempo.note_type(), type);
 
-       do_insert (ts);
+       do_insert (t);
 
        if (recompute) {
-               solve_map (_metrics, ts, ts->frame());
+               solve_map (_metrics, t, t->frame());
        }
+
+       return t;
 }
 
 void
@@ -943,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);
        }
@@ -1003,7 +1019,7 @@ TempoMap::add_meter (const Meter& meter, const double& beat, const BBT_Time& whe
 #endif
 
        PropertyChanged (PropertyChange ());
-       dump (_metrics, std::cerr);
+
        return m;
 }
 
@@ -1058,7 +1074,6 @@ TempoMap::add_meter_locked (const Meter& meter, double beat, BBT_Time where, boo
 MeterSection*
 TempoMap::add_meter_locked (const Meter& meter, framepos_t frame, double beat, Timecode::BBT_Time where, bool recompute)
 {
-
        MeterSection* new_meter = new MeterSection (frame, beat, where, meter.divisions_per_bar(), meter.note_divisor());
 
        double pulse = pulse_at_frame_locked (_metrics, frame);
@@ -1236,6 +1251,11 @@ TempoMap::recompute_tempos (Metrics& metrics)
                        if (!t->active()) {
                                continue;
                        }
+                       if (!t->movable()) {
+                               t->set_pulse (0.0);
+                               prev_t = t;
+                               continue;
+                       }
                        if (prev_t) {
                                if (t->position_lock_style() == AudioTime) {
                                        prev_t->set_c_func (prev_t->compute_c_func_frame (t->pulses_per_minute(), t->frame(), _frame_rate));
@@ -1267,10 +1287,10 @@ TempoMap::recompute_meters (Metrics& metrics)
                                pair<double, BBT_Time> b_bbt;
                                if (meter->movable()) {
                                        const double beats = ((pulse_at_frame_locked (metrics, meter->frame()) - prev_m->pulse()) * prev_m->note_divisor());
-                                       const double ceil_beats = beats - fmod (beats, prev_m->divisions_per_bar());
-                                       b_bbt = make_pair (ceil_beats + prev_m->beat(), BBT_Time ((ceil_beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
-                                       const double true_pulse = prev_m->pulse() + (ceil_beats / prev_m->note_divisor());
-                                       const double pulse_off = true_pulse - (beats / 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;
                                } else {
                                        b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
@@ -1279,11 +1299,17 @@ 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->beat() - prev_m->beat()) / prev_m->note_divisor();
+                                       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());
                                }
+
+                               meter->set_beat (new_beat);
                                meter->set_frame (frame_at_pulse_locked (metrics, pulse));
                                meter->set_pulse (pulse);
                        }
@@ -1291,6 +1317,7 @@ TempoMap::recompute_meters (Metrics& metrics)
                        prev_m = meter;
                }
        }
+       //dump (_metrics, std::cerr;
 }
 
 void
@@ -1378,40 +1405,10 @@ TempoMap::metric_at (BBT_Time bbt) const
        return m;
 }
 
-double
-TempoMap::pulse_at_frame_locked (const Metrics& metrics, const framecnt_t& frame) const
-{
-       /* HOLD (at least) THE READER LOCK */
-       TempoSection* prev_t = 0;
-       double accumulated_pulses = 0.0;
-
-       for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
-               TempoSection* t;
-               if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
-                       if (!t->active()) {
-                               continue;
-                       }
-                       if (prev_t && t->frame() > frame) {
-                               /*the previous ts is the one containing the frame */
-                               const double ret = prev_t->pulse_at_frame (frame, _frame_rate);
-                               return ret;
-                       }
-                       accumulated_pulses = t->pulse();
-                       prev_t = t;
-               }
-       }
-
-       /* treated as constant for this ts */
-       const double pulses_in_section = (frame - prev_t->frame()) / prev_t->frames_per_pulse (_frame_rate);
-
-       return pulses_in_section + accumulated_pulses;
-}
-
 double
 TempoMap::pulse_at_beat_locked (const Metrics& metrics, const double& beat) const
 {
        MeterSection* prev_m = 0;
-       double accumulated_beats = 0.0;
 
        for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
                MeterSection* m;
@@ -1419,12 +1416,11 @@ TempoMap::pulse_at_beat_locked (const Metrics& metrics, const double& beat) cons
                        if (prev_m && m->beat() > beat) {
                                break;
                        }
-                       accumulated_beats = m->beat();
                        prev_m = m;
                }
 
        }
-       double const ret = prev_m->pulse() + ((beat - accumulated_beats) / prev_m->note_divisor());
+       double const ret = prev_m->pulse() + ((beat - prev_m->beat()) / prev_m->note_divisor());
        return ret;
 }
 
@@ -1439,7 +1435,6 @@ double
 TempoMap::beat_at_pulse_locked (const Metrics& metrics, const double& pulse) const
 {
        MeterSection* prev_m = 0;
-       double accumulated_beats = 0.0;
 
        for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
                MeterSection* m;
@@ -1447,14 +1442,13 @@ TempoMap::beat_at_pulse_locked (const Metrics& metrics, const double& pulse) con
                        if (prev_m && m->pulse() > pulse) {
                                break;
                        }
-                       accumulated_beats = m->beat();
                        prev_m = m;
                }
        }
 
        double const beats_in_section = (pulse - prev_m->pulse()) * prev_m->note_divisor();
 
-       return beats_in_section + accumulated_beats;
+       return beats_in_section + prev_m->beat();
 }
 
 double
@@ -1464,6 +1458,33 @@ TempoMap::beat_at_pulse (const double& pulse) const
        return beat_at_pulse_locked (_metrics, pulse);
 }
 
+double
+TempoMap::pulse_at_frame_locked (const Metrics& metrics, const framecnt_t& frame) const
+{
+       /* HOLD (at least) THE READER LOCK */
+       TempoSection* prev_t = 0;
+
+       for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
+               TempoSection* t;
+               if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
+                       if (!t->active()) {
+                               continue;
+                       }
+                       if (prev_t && t->frame() > frame) {
+                               /*the previous ts is the one containing the frame */
+                               const double ret = prev_t->pulse_at_frame (frame, _frame_rate);
+                               return ret;
+                       }
+                       prev_t = t;
+               }
+       }
+
+       /* treated as constant for this ts */
+       const double pulses_in_section = (frame - prev_t->frame()) / prev_t->frames_per_pulse (_frame_rate);
+
+       return pulses_in_section + prev_t->pulse();
+}
+
 framecnt_t
 TempoMap::frame_at_pulse_locked (const Metrics& metrics, const double& pulse) const
 {
@@ -1527,8 +1548,6 @@ TempoMap::bbt_to_beats_locked (const Metrics& metrics, const Timecode::BBT_Time&
 {
        /* CALLER HOLDS READ LOCK */
 
-       double accumulated_beats = 0.0;
-       double accumulated_bars = 0.0;
        MeterSection* prev_m = 0;
 
        /* because audio-locked meters have 'fake' integral beats,
@@ -1539,19 +1558,17 @@ TempoMap::bbt_to_beats_locked (const Metrics& metrics, const Timecode::BBT_Time&
                if ((m = dynamic_cast<MeterSection*> (*i)) != 0) {
                        if (prev_m) {
                                const double bars_to_m = (m->beat() - prev_m->beat()) / prev_m->divisions_per_bar();
-                               if ((bars_to_m + accumulated_bars) > (bbt.bars - 1)) {
+                               if ((bars_to_m + (prev_m->bbt().bars - 1)) > (bbt.bars - 1)) {
                                        break;
                                }
-                               accumulated_beats = m->beat();
-                               accumulated_bars += bars_to_m;
                        }
                        prev_m = m;
                }
        }
 
-       const double remaining_bars = (bbt.bars - 1) - accumulated_bars;
+       const double remaining_bars = bbt.bars - prev_m->bbt().bars;
        const double remaining_bars_in_beats = remaining_bars * prev_m->divisions_per_bar();
-       const double ret = remaining_bars_in_beats + accumulated_beats + (bbt.beats - 1) + (bbt.ticks / BBT_Time::ticks_per_beat);
+       const double ret = remaining_bars_in_beats + prev_m->beat() + (bbt.beats - 1) + (bbt.ticks / BBT_Time::ticks_per_beat);
 
        return ret;
 }
@@ -1569,8 +1586,6 @@ TempoMap::beats_to_bbt_locked (const Metrics& metrics, const double& b) const
        /* CALLER HOLDS READ LOCK */
        MeterSection* prev_m = 0;
        const double beats = (b < 0.0) ? 0.0 : b;
-       uint32_t accumulated_bars = 0;
-       double accumulated_beats = 0.0;
 
        for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
                MeterSection* m = 0;
@@ -1581,19 +1596,15 @@ TempoMap::beats_to_bbt_locked (const Metrics& metrics, const double& b) const
                                        /* this is the meter after the one our beat is on*/
                                        break;
                                }
-                               const double beats_to_m = m->beat() - prev_m->beat();
-                               /* we need a whole number of bars. */
-                               accumulated_bars += (beats_to_m + 1) / prev_m->divisions_per_bar();
-                               accumulated_beats += beats_to_m;
                        }
 
                        prev_m = m;
                }
        }
 
-       const double beats_in_ms = beats - accumulated_beats;
+       const double beats_in_ms = beats - prev_m->beat();
        const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
-       const uint32_t total_bars = bars_in_ms + accumulated_bars;
+       const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
        const double remaining_beats = beats_in_ms - (bars_in_ms * prev_m->divisions_per_bar());
        const double remaining_ticks = (remaining_beats - floor (remaining_beats)) * BBT_Time::ticks_per_beat;
 
@@ -1632,8 +1643,6 @@ TempoMap::pulse_to_bbt (const double& pulse)
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
        MeterSection* prev_m = 0;
-       uint32_t accumulated_bars = 0;
-       double accumulated_pulses = 0.0;
 
        for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
                MeterSection* m = 0;
@@ -1642,22 +1651,19 @@ TempoMap::pulse_to_bbt (const double& pulse)
 
                        if (prev_m) {
                                double const pulses_to_m = m->pulse() - prev_m->pulse();
-                               if (accumulated_pulses + pulses_to_m > pulse) {
+                               if (prev_m->pulse() + pulses_to_m > pulse) {
                                        /* this is the meter after the one our beat is on*/
                                        break;
                                }
-
-                               /* we need a whole number of bars. */
-                               accumulated_pulses += pulses_to_m;
-                               accumulated_bars += ((pulses_to_m * prev_m->note_divisor()) + 1) / prev_m->divisions_per_bar();
                        }
 
                        prev_m = m;
                }
        }
+
        const double beats_in_ms = (pulse - prev_m->pulse()) * prev_m->note_divisor();
        const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
-       const uint32_t total_bars = bars_in_ms + accumulated_bars;
+       const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
        const double remaining_beats = beats_in_ms - (bars_in_ms * prev_m->divisions_per_bar());
        const double remaining_ticks = (remaining_beats - floor (remaining_beats)) * BBT_Time::ticks_per_beat;
 
@@ -1722,10 +1728,8 @@ TempoMap::frame_time (const BBT_Time& bbt)
                throw std::logic_error ("beats are counted from one");
        }
        Glib::Threads::RWLock::ReaderLock lm (lock);
-       const double beat = bbt_to_beats_locked (_metrics, bbt);
-       const framecnt_t frame = frame_at_beat_locked (_metrics, beat);
 
-       return frame;
+       return frame_time_locked (_metrics, bbt);
 }
 
 bool
@@ -1748,8 +1752,8 @@ TempoMap::check_solved (Metrics& metrics, bool by_frame)
                                        return false;
                                }
 
-                               /* precision check ensures pulses and frames align independent of lock style.*/
-                               if (by_frame && t->frame() != prev_t->frame_at_pulse (t->pulse(), _frame_rate)) {
+                               /* precision check ensures pulses and frames align.*/
+                               if (t->frame() != prev_t->frame_at_pulse (t->pulse(), _frame_rate)) {
                                        return false;
                                }
                        }
@@ -1801,9 +1805,9 @@ TempoMap::solve_map (Metrics& imaginary, TempoSection* section, const framepos_t
        }
        if (section->movable() && frame <= first_m_frame) {
                return false;
-       } else {
-               section->set_active (true);
        }
+
+       section->set_active (true);
        section->set_frame (frame);
 
        for (Metrics::iterator i = imaginary.begin(); i != imaginary.end(); ++i) {
@@ -1859,11 +1863,12 @@ TempoMap::solve_map (Metrics& imaginary, TempoSection* section, const framepos_t
        } else {
                recompute_tempos (imaginary);
        }
+
        if (check_solved (imaginary, true)) {
                recompute_meters (imaginary);
                return true;
        }
-
+#if (0)
        MetricSectionSorter cmp;
        imaginary.sort (cmp);
        if (section->position_lock_style() == MusicTime) {
@@ -1874,10 +1879,12 @@ TempoMap::solve_map (Metrics& imaginary, TempoSection* section, const framepos_t
        } else {
                recompute_tempos (imaginary);
        }
+
        if (check_solved (imaginary, true)) {
                recompute_meters (imaginary);
                return true;
        }
+#endif
        //dump (imaginary, std::cerr);
 
        return false;
@@ -1897,6 +1904,11 @@ TempoMap::solve_map (Metrics& imaginary, TempoSection* section, const double& pu
                        if (!t->active()) {
                                continue;
                        }
+                       if (!t->movable()) {
+                               t->set_pulse (0.0);
+                               prev_t = t;
+                               continue;
+                       }
                        if (prev_t) {
                                if (t == section) {
                                        section_prev = prev_t;
@@ -1926,6 +1938,7 @@ TempoMap::solve_map (Metrics& imaginary, TempoSection* section, const double& pu
        } else {
                recompute_tempos (imaginary);
        }
+
        if (check_solved (imaginary, false)) {
                recompute_meters (imaginary);
                return true;
@@ -1946,7 +1959,7 @@ TempoMap::solve_map (Metrics& imaginary, TempoSection* section, const double& pu
                recompute_meters (imaginary);
                return true;
        }
-
+#if (0)
        MetricSectionFrameSorter fcmp;
        imaginary.sort (fcmp);
        if (section->position_lock_style() == AudioTime) {
@@ -1962,7 +1975,7 @@ TempoMap::solve_map (Metrics& imaginary, TempoSection* section, const double& pu
                recompute_meters (imaginary);
                return true;
        }
-
+#endif
        //dump (imaginary, std::cerr);
 
        return false;
@@ -2004,53 +2017,63 @@ TempoMap::solve_map (Metrics& imaginary, MeterSection* section, const framepos_t
                        if (m == section){
                                /*
                                  here we set the beat for this frame.
-                                 we set it 'incorrectly' to the next bar's beat
-                                 and use this difference to find the meter's pulse.
+                                 we set it 'incorrectly' to the next bar's first beat
+                                 and use the delat to find the meter's pulse.
                                */
-                               double pulse = 0.0;
+                               double new_pulse = 0.0;
                                pair<double, BBT_Time> b_bbt;
-                               if (m->movable()) {
+
+                               if (section->movable()) {
                                        const double beats = ((pulse_at_frame_locked (imaginary, frame) - prev_m->pulse()) * prev_m->note_divisor());
-                                       const double ceil_beats = beats - fmod (beats,  prev_m->divisions_per_bar());
-                                       b_bbt = make_pair (ceil_beats + prev_m->beat(), BBT_Time ((ceil_beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
-                                       const double true_pulse = prev_m->pulse() + (ceil_beats / prev_m->note_divisor());
-                                       const double pulse_off = true_pulse - (beats / prev_m->note_divisor());
-                                       pulse = true_pulse - pulse_off;
+                                       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;
                                } else {
                                        b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
                                }
-                               m->set_beat (b_bbt);
-                               m->set_pulse (pulse);
+
+                               section->set_beat (b_bbt);
+                               section->set_pulse (new_pulse);
                                prev_m = m;
+
                                continue;
                        }
                        if (prev_m) {
+                               double new_pulse = 0.0;
                                if (m->position_lock_style() == MusicTime) {
-                                       const double pulse = prev_m->pulse() + (m->beat() - prev_m->beat()) / prev_m->note_divisor();
-                                       m->set_frame (frame_at_pulse_locked (imaginary, pulse));
-                                       m->set_pulse (pulse);
+                                       new_pulse = prev_m->pulse() + ((m->bbt().bars - prev_m->bbt().bars) *  prev_m->divisions_per_bar() / prev_m->note_divisor());
+                                       m->set_frame (frame_at_pulse_locked (imaginary, new_pulse));
+                                       if (m->frame() > section->frame()) {
+                                               /* moving 'section' will affect later meters' beat (but not bbt).*/
+                                               pair<double, BBT_Time> new_beat (((new_pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat(), m->bbt());
+                                               m->set_beat (new_beat);
+                                       }
                                } else {
-                                       double pulse = 0.0;
                                        pair<double, BBT_Time> b_bbt;
                                        if (m->movable()) {
                                                const double beats = ((pulse_at_frame_locked (imaginary, m->frame()) - prev_m->pulse()) * prev_m->note_divisor());
-                                               const double ceil_beats = beats - fmod (beats , prev_m->divisions_per_bar());
-                                               b_bbt = make_pair (ceil_beats + prev_m->beat(), BBT_Time ((ceil_beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
-                                               const double true_pulse = prev_m->pulse() + (ceil_beats / prev_m->note_divisor());
-                                               const double pulse_off = true_pulse - (beats / prev_m->note_divisor());
-                                               pulse = true_pulse - pulse_off;
+                                               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;
                                        } else {
                                                b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
-                                               pulse = 0.0;
+                                               new_pulse = 0.0;
                                        }
                                        m->set_beat (b_bbt);
-                                       m->set_pulse (pulse);
                                }
+                               m->set_pulse (new_pulse);
                        }
                        prev_m = m;
                }
        }
 
+       MetricSectionFrameSorter fcmp;
+       imaginary.sort (fcmp);
        if (section->position_lock_style() == MusicTime) {
                /* we're setting the frame */
                section->set_position_lock_style (AudioTime);
@@ -2066,6 +2089,7 @@ void
 TempoMap::solve_map (Metrics& imaginary, MeterSection* section, const double& pulse)
 {
        MeterSection* prev_m = 0;
+       MeterSection* section_prev = 0;
 
        section->set_pulse (pulse);
 
@@ -2073,39 +2097,51 @@ TempoMap::solve_map (Metrics& imaginary, MeterSection* section, const double& pu
                MeterSection* m;
                if ((m = dynamic_cast<MeterSection*> (*i)) != 0) {
                        if (m == section){
-                               section->set_frame (frame_at_pulse_locked (imaginary, pulse));
-                               const double beats = ((pulse - prev_m->pulse()) * prev_m->note_divisor());
-                               const int32_t bars = (beats + 1) / prev_m->divisions_per_bar();
-                               pair<double, BBT_Time> b_bbt = make_pair (beats + prev_m->beat(), BBT_Time (bars + prev_m->bbt().bars, 1, 0));
-                               section->set_beat (b_bbt);
-                               prev_m = section;
+                               section_prev = prev_m;
                                continue;
                        }
                        if (prev_m) {
-                               double pulse = 0.0;
+                               double new_pulse = 0.0;
                                if (m->position_lock_style() == MusicTime) {
-                                       pulse = prev_m->pulse() + (m->beat() - prev_m->beat()) / prev_m->note_divisor();
-                                       m->set_frame (frame_at_pulse_locked (imaginary, pulse));
+                                       new_pulse = prev_m->pulse() + ((m->bbt().bars - prev_m->bbt().bars) *  prev_m->divisions_per_bar() / prev_m->note_divisor());
+                                       m->set_frame (frame_at_pulse_locked (imaginary, new_pulse));
+
+                                       if (new_pulse > section->pulse()) {
+                                               /* moving 'section' will affect later meters' beat (but not bbt).*/
+                                               pair<double, BBT_Time> new_beat (((new_pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat(), m->bbt());
+                                               m->set_beat (new_beat);
+                                       }
                                } else {
                                        pair<double, BBT_Time> b_bbt;
                                        if (m->movable()) {
                                                const double beats = ((pulse_at_frame_locked (imaginary, m->frame()) - prev_m->pulse()) * prev_m->note_divisor());
-                                               const double ceil_beats = beats - fmod (beats, prev_m->divisions_per_bar());
-                                               b_bbt = make_pair (ceil_beats + prev_m->beat(), BBT_Time ((ceil_beats / prev_m->divisions_per_bar()) + prev_m->bbt().bars, 1, 0));
-                                               const double true_pulse = prev_m->pulse() + (ceil_beats / prev_m->note_divisor());
-                                               const double pulse_off = true_pulse - (beats / prev_m->note_divisor());
-                                               pulse = true_pulse - pulse_off;
+                                               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;
                                        } else {
                                                b_bbt = make_pair (0.0, BBT_Time (1, 1, 0));
                                        }
                                        m->set_beat (b_bbt);
                                }
-                               m->set_pulse (pulse);
+                               m->set_pulse (new_pulse);
                        }
                        prev_m = m;
                }
        }
 
+       if (section_prev) {
+               const double beats = ((pulse - section_prev->pulse()) * section_prev->note_divisor());
+               const int32_t bars = (beats + 1) / section_prev->divisions_per_bar();
+               pair<double, BBT_Time> b_bbt = make_pair (beats + section_prev->beat(), BBT_Time (bars + section_prev->bbt().bars, 1, 0));
+               section->set_beat (b_bbt);
+               section->set_frame (frame_at_pulse_locked (imaginary, pulse));
+       }
+
+       MetricSectionSorter cmp;
+       imaginary.sort (cmp);
        if (section->position_lock_style() == AudioTime) {
                /* we're setting the pulse */
                section->set_position_lock_style (MusicTime);
@@ -2117,22 +2153,24 @@ TempoMap::solve_map (Metrics& imaginary, MeterSection* section, const double& pu
 }
 
 /** places a copy of _metrics into copy and returns a pointer
- *  to section's equivalent.
+ *  to section's equivalent in copy.
  */
 TempoSection*
 TempoMap::copy_metrics_and_point (Metrics& copy, TempoSection* section)
 {
        TempoSection* t;
-       TempoSection* ret = 0;
        MeterSection* m;
+       TempoSection* ret = 0;
 
        for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
                if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
                        if (t == section) {
                                if (t->position_lock_style() == MusicTime) {
                                        ret = new TempoSection (t->pulse(), t->beats_per_minute(), t->note_type(), t->type());
+                                       ret->set_frame (t->frame());
                                } else {
                                        ret = new TempoSection (t->frame(), t->beats_per_minute(), t->note_type(), t->type());
+                                       ret->set_pulse (t->pulse());
                                }
                                ret->set_active (t->active());
                                ret->set_movable (t->movable());
@@ -2142,8 +2180,10 @@ TempoMap::copy_metrics_and_point (Metrics& copy, TempoSection* section)
                        TempoSection* cp = 0;
                        if (t->position_lock_style() == MusicTime) {
                                cp = new TempoSection (t->pulse(), t->beats_per_minute(), t->note_type(), t->type());
+                               cp->set_frame (t->frame());
                        } else {
                                cp = new TempoSection (t->frame(), t->beats_per_minute(), t->note_type(), t->type());
+                               cp->set_pulse (t->pulse());
                        }
                        cp->set_active (t->active());
                        cp->set_movable (t->movable());
@@ -2162,7 +2202,7 @@ TempoMap::copy_metrics_and_point (Metrics& copy, TempoSection* section)
                        copy.push_back (cp);
                }
        }
-       //recompute_map (copy);
+
        return ret;
 }
 
@@ -2206,6 +2246,7 @@ TempoMap::predict_tempo_frame (TempoSection* section, const BBT_Time& bbt)
        TempoSection* new_section = copy_metrics_and_point (future_map, section);
 
        const double beat = bbt_to_beats_locked (future_map, bbt);
+
        if (solve_map (future_map, new_section, pulse_at_beat_locked (future_map, beat))) {
                ret = new_section->frame();
        } else {
@@ -2343,10 +2384,8 @@ TempoMap::bbt_duration_at (framepos_t pos, const BBT_Time& bbt, int dir)
        const double tick_at_time = beat_at_frame_locked (_metrics, pos) * BBT_Time::ticks_per_beat;
        const double bbt_ticks = bbt.ticks + (bbt.beats * BBT_Time::ticks_per_beat);
        const double total_beats = (tick_at_time + bbt_ticks) / BBT_Time::ticks_per_beat;
-       const framecnt_t time_at_bbt = frame_at_beat_locked (_metrics, total_beats);
-       const framecnt_t ret = time_at_bbt;
 
-       return ret;
+       return frame_at_beat_locked (_metrics, total_beats);
 }
 
 framepos_t
@@ -2522,22 +2561,22 @@ TempoMap::round_to_type (framepos_t frame, RoundMode dir, BBTPointType type)
                        /* find bar previous to 'frame' */
                        bbt.beats = 1;
                        bbt.ticks = 0;
-                       return frame_time (bbt);
+                       return frame_time_locked (_metrics, bbt);
 
                } else if (dir > 0) {
                        /* find bar following 'frame' */
                        ++bbt.bars;
                        bbt.beats = 1;
                        bbt.ticks = 0;
-                       return frame_time (bbt);
+                       return frame_time_locked (_metrics, bbt);
                } else {
                        /* true rounding: find nearest bar */
-                       framepos_t raw_ft = frame_time (bbt);
+                       framepos_t raw_ft = frame_time_locked (_metrics, bbt);
                        bbt.beats = 1;
                        bbt.ticks = 0;
-                       framepos_t prev_ft = frame_time (bbt);
+                       framepos_t prev_ft = frame_time_locked (_metrics, bbt);
                        ++bbt.bars;
-                       framepos_t next_ft = frame_time (bbt);
+                       framepos_t next_ft = frame_time_locked (_metrics, bbt);
 
                        if ((raw_ft - prev_ft) > (next_ft - prev_ft) / 2) { 
                                return next_ft;
@@ -2578,7 +2617,7 @@ TempoMap::get_grid (vector<TempoMap::BBTPoint>& points,
                const TempoSection tempo = tempo_section_at_locked (pos);
                const MeterSection meter = meter_section_at_locked (pos);
                const BBT_Time bbt = beats_to_bbt (cnt);
-               points.push_back (BBTPoint (meter, tempo_at_locked (pos), pos, bbt.bars, bbt.beats, tempo.get_c_func()));
+               points.push_back (BBTPoint (meter, tempo_at_locked (pos), pos, bbt.bars, bbt.beats, tempo.c_func()));
                ++cnt;
        }
 }
@@ -2675,8 +2714,8 @@ TempoMap::tempo_at_locked (const framepos_t& frame) const
                        }
                        if ((prev_t) && t->frame() > frame) {
                                /* t is the section past frame */
-                               const double ret = prev_t->tempo_at_frame (frame, _frame_rate) * prev_t->note_type();
-                               const Tempo ret_tempo (ret, prev_t->note_type());
+                               const double ret_bpm = prev_t->tempo_at_frame (frame, _frame_rate) * prev_t->note_type();
+                               const Tempo ret_tempo (ret_bpm, prev_t->note_type());
                                return ret_tempo;
                        }
                        prev_t = t;
@@ -2699,7 +2738,6 @@ TempoMap::meter_section_at (framepos_t frame) const
 const MeterSection&
 TempoMap::meter_section_at_locked (framepos_t frame) const
 {
-       //framepos_t const frame_off = frame + frame_offset_at (_metrics, frame);
        Metrics::const_iterator i;
        MeterSection* prev = 0;
 
@@ -2750,6 +2788,68 @@ TempoMap::meter_section_at (const double& beat) const
        return *prev_m;
 }
 
+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 ()
 {
@@ -2814,32 +2914,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 ((m = dynamic_cast<MeterSection*>(*i)) != 0) {
-                               if (prev_m && prev_m->pulse() < 0.0) {
-                                       /*XX we cannot possibly make this work??. */
-                                       pair<double, BBT_Time> start = make_pair (((prev_m->bbt().bars - 1) * prev_m->note_divisor()) + (prev_m->bbt().beats - 1) + (prev_m->bbt().ticks / BBT_Time::ticks_per_beat), prev_m->bbt());
-                                       prev_m->set_beat (start);
-                                       const double start_pulse = ((prev_m->bbt().bars - 1) * prev_m->note_divisor()) + (prev_m->bbt().beats - 1) + (prev_m->bbt().ticks / BBT_Time::ticks_per_beat);
-                                       prev_m->set_pulse (start_pulse);
-                               }
-                               prev_m = m;
-                       } else if ((t = dynamic_cast<TempoSection*>(*i)) != 0) {
-                               if (!t->active()) {
-                                       continue;
-                               }
-                               if (prev_t && prev_t->pulse() < 0.0) {
-                                       double const start = ((prev_t->legacy_bbt().bars - 1) * prev_m->note_divisor()) + (prev_t->legacy_bbt().beats - 1) + (prev_t->legacy_bbt().ticks / BBT_Time::ticks_per_beat);
-                                       prev_t->set_pulse (start);
+                       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.
                */