fix issue with initialization of a BBT_Time variable.
[ardour.git] / libs / ardour / tempo.cc
index c04f6803fd345fa45f8ddd844647b23b2d77831f..5d3be149644012c923ff209a69b1eaa85ef7ea2d 100644 (file)
@@ -55,7 +55,7 @@ MetricSection::frame_at_minute (const double& time) const
 }
 
 double
-MetricSection::minute_at_frame (const framepos_t& frame) const
+MetricSection::minute_at_frame (const framepos_t frame) const
 {
        return (frame / (double) _sample_rate) / 60.0;
 }
@@ -159,10 +159,6 @@ TempoSection::TempoSection (const XMLNode& node, framecnt_t sample_rate)
        , _locked_to_meter (false)
        , _clamped (false)
 {
-       LocaleGuard lg;
-
-       _legacy_bbt = BBT_Time (0, 0, 0);
-
        BBT_Time bbt;
        std::string start_bbt;
        if (node.get_property ("start", start_bbt)) {
@@ -206,7 +202,7 @@ TempoSection::TempoSection (const XMLNode& node, framecnt_t sample_rate)
        }
 
        TempoSection::Type old_type;
-       if (!node.get_property ("tempo-type", old_type)) {
+       if (node.get_property ("tempo-type", old_type)) {
                /* sessions with a tempo-type node contain no end-beats-per-minute.
                   if the legacy node indicates a constant tempo, simply fill this in with the
                   start tempo. otherwise we need the next neighbour to know what it will be.
@@ -242,7 +238,6 @@ XMLNode&
 TempoSection::get_state() const
 {
        XMLNode *root = new XMLNode (xml_state_node_name);
-       LocaleGuard lg;
 
        MetricSection::add_state_to_node (*root);
 
@@ -363,7 +358,7 @@ TempoSection::minute_at_pulse (const double& p) const
  *  for use with musical units whose granularity is coarser than frames (e.g. ticks)
 */
 double
-TempoSection::pulse_at_frame (const framepos_t& f) const
+TempoSection::pulse_at_frame (const framepos_t f) const
 {
        const bool constant = type() == Constant || _c == 0.0 || (initial() && f < frame());
        if (constant) {
@@ -553,7 +548,6 @@ const string MeterSection::xml_state_node_name = "Meter";
 MeterSection::MeterSection (const XMLNode& node, const framecnt_t sample_rate)
        : MetricSection (0.0, 0, MusicTime, false, sample_rate), Meter (TempoMap::default_meter())
 {
-       LocaleGuard lg;
        pair<double, BBT_Time> start;
        start.first = 0.0;
 
@@ -612,7 +606,6 @@ XMLNode&
 MeterSection::get_state() const
 {
        XMLNode *root = new XMLNode (xml_state_node_name);
-       LocaleGuard lg;
 
        MetricSection::add_state_to_node (*root);
 
@@ -1025,7 +1018,7 @@ TempoMap::do_insert (MetricSection* section)
 }
 /* user supplies the exact pulse if pls == MusicTime */
 TempoSection*
-TempoMap::add_tempo (const Tempo& tempo, const double& pulse, const framepos_t& frame, PositionLockStyle pls)
+TempoMap::add_tempo (const Tempo& tempo, const double& pulse, const framepos_t frame, PositionLockStyle pls)
 {
        if (tempo.note_types_per_minute() <= 0.0) {
                warning << "Cannot add tempo. note types per minute must be greater than zero." << endmsg;
@@ -1033,27 +1026,11 @@ TempoMap::add_tempo (const Tempo& tempo, const double& pulse, const framepos_t&
        }
 
        TempoSection* ts = 0;
-       TempoSection* prev_tempo = 0;
        {
                Glib::Threads::RWLock::WriterLock lm (lock);
-               ts = add_tempo_locked (tempo, pulse, minute_at_frame (frame), pls, true);
-               for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
-
-                       if ((*i)->is_tempo()) {
-                               TempoSection* const this_t = static_cast<TempoSection*> (*i);
+               /* here we default to not clamped for a new tempo section. preference? */
+               ts = add_tempo_locked (tempo, pulse, minute_at_frame (frame), pls, true, false, false);
 
-                               bool const ipm = ts->position_lock_style() == MusicTime;
-                               bool const lm = ts->locked_to_meter();
-                               if ((ipm && this_t->pulse() == ts->pulse()) || (!ipm && this_t->frame() == ts->frame())
-                                   || (lm && this_t->pulse() == ts->pulse())) {
-                                       if (prev_tempo && prev_tempo->type() == TempoSection::Ramp) {
-                                               prev_tempo->set_end_note_types_per_minute (ts->note_types_per_minute());
-                                       }
-                                       break;
-                               }
-                               prev_tempo = this_t;
-                       }
-               }
                recompute_map (_metrics);
        }
 
@@ -1063,7 +1040,7 @@ TempoMap::add_tempo (const Tempo& tempo, const double& pulse, const framepos_t&
 }
 
 void
-TempoMap::replace_tempo (TempoSection& ts, const Tempo& tempo, const double& pulse, const framepos_t& frame, PositionLockStyle pls)
+TempoMap::replace_tempo (TempoSection& ts, const Tempo& tempo, const double& pulse, const framepos_t frame, PositionLockStyle pls)
 {
        if (tempo.note_types_per_minute() <= 0.0) {
                warning << "Cannot replace tempo. note types per minute must be greater than zero." << endmsg;
@@ -1086,27 +1063,11 @@ TempoMap::replace_tempo (TempoSection& ts, const Tempo& tempo, const double& pul
                                }
                        } else {
                                remove_tempo_locked (ts);
-                               new_ts = add_tempo_locked (tempo, pulse, minute_at_frame (frame), pls, true, locked_to_meter);
-                               new_ts->set_clamped (ts_clamped);
-
-                               if (new_ts && new_ts->type() == TempoSection::Constant) {
-                                       new_ts->set_end_note_types_per_minute (new_ts->note_types_per_minute());
-                               } else {
-                                       for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
-
-                                               if ((*i)->is_tempo()) {
-                                                       TempoSection* const this_t = static_cast<TempoSection*> (*i);
-
-                                                       bool const ipm = new_ts->position_lock_style() == MusicTime;
-                                                       bool const lm = new_ts->locked_to_meter();
-                                                       if ((ipm && this_t->pulse() > new_ts->pulse()) || (!ipm && this_t->frame() > new_ts->frame())
-                                                           || (lm && this_t->pulse() > new_ts->pulse())) {
-                                                               new_ts->set_end_note_types_per_minute (tempo.end_note_types_per_minute());
-
-                                                               break;
-                                                       }
-                                               }
-                                       }
+                               new_ts = add_tempo_locked (tempo, pulse, minute_at_frame (frame), pls, true, locked_to_meter, ts_clamped);
+                               /* enforce clampedness of next tempo section */
+                               TempoSection* next_t = next_tempo_section_locked (_metrics, new_ts);
+                               if (next_t && next_t->clamped()) {
+                                       next_t->set_note_types_per_minute (new_ts->end_note_types_per_minute());
                                }
                        }
 
@@ -1129,13 +1090,28 @@ TempoMap::replace_tempo (TempoSection& ts, const Tempo& tempo, const double& pul
 
 TempoSection*
 TempoMap::add_tempo_locked (const Tempo& tempo, double pulse, double minute
-                           , PositionLockStyle pls, bool recompute, bool locked_to_meter)
+                           , PositionLockStyle pls, bool recompute, bool locked_to_meter, bool clamped)
 {
        TempoSection* t = new TempoSection (pulse, minute, tempo, pls, _frame_rate);
        t->set_locked_to_meter (locked_to_meter);
+       t->set_clamped (clamped);
 
        do_insert (t);
 
+       TempoSection* prev_tempo = 0;
+       for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
+               TempoSection* const this_t = dynamic_cast<TempoSection*>(*i);
+               if (this_t) {
+                       if (this_t == t) {
+                               if (prev_tempo && prev_tempo->type() == TempoSection::Ramp) {
+                                       prev_tempo->set_end_note_types_per_minute (t->note_types_per_minute());
+                               }
+                               break;
+                       }
+                       prev_tempo = this_t;
+               }
+       }
+
        if (recompute) {
                if (pls == AudioTime) {
                        solve_map_minute (_metrics, t, t->minute());
@@ -1149,12 +1125,12 @@ TempoMap::add_tempo_locked (const Tempo& tempo, double pulse, double minute
 }
 
 MeterSection*
-TempoMap::add_meter (const Meter& meter, const double& beat, const Timecode::BBT_Time& where, framepos_t frame, PositionLockStyle pls)
+TempoMap::add_meter (const Meter& meter, const Timecode::BBT_Time& where, framepos_t frame, PositionLockStyle pls)
 {
        MeterSection* m = 0;
        {
                Glib::Threads::RWLock::WriterLock lm (lock);
-               m = add_meter_locked (meter, beat, where, frame, pls, true);
+               m = add_meter_locked (meter, where, frame, pls, true);
        }
 
 
@@ -1173,11 +1149,10 @@ TempoMap::replace_meter (const MeterSection& ms, const Meter& meter, const BBT_T
 {
        {
                Glib::Threads::RWLock::WriterLock lm (lock);
-               const double beat = beat_at_bbt_locked (_metrics, where);
 
                if (!ms.initial()) {
                        remove_meter_locked (ms);
-                       add_meter_locked (meter, beat, where, frame, pls, true);
+                       add_meter_locked (meter, where, frame, pls, true);
                } else {
                        MeterSection& first (first_meter());
                        TempoSection& first_t (first_tempo());
@@ -1200,24 +1175,25 @@ TempoMap::replace_meter (const MeterSection& ms, const Meter& meter, const BBT_T
 }
 
 MeterSection*
-TempoMap::add_meter_locked (const Meter& meter, double beat, const BBT_Time& where, framepos_t frame, PositionLockStyle pls, bool recompute)
+TempoMap::add_meter_locked (const Meter& meter, const BBT_Time& bbt, framepos_t frame, PositionLockStyle pls, bool recompute)
 {
-       const MeterSection& prev_m = meter_section_at_minute_locked  (_metrics, minute_at_beat_locked (_metrics, beat) - minute_at_frame (1));
-       const double pulse = ((where.bars - prev_m.bbt().bars) * (prev_m.divisions_per_bar() / prev_m.note_divisor())) + prev_m.pulse();
-       const double time_minutes = minute_at_pulse_locked (_metrics, pulse);
-       TempoSection* mlt = 0;
+       double const minute_at_bbt = minute_at_bbt_locked (_metrics, bbt);
+       const MeterSection& prev_m = meter_section_at_minute_locked  (_metrics, minute_at_bbt - minute_at_frame (1));
+       double const pulse = ((bbt.bars - prev_m.bbt().bars) * (prev_m.divisions_per_bar() / prev_m.note_divisor())) + prev_m.pulse();
+       /* the natural time of the BBT position */
+       double const time_minutes = minute_at_pulse_locked (_metrics, pulse);
 
        if (pls == AudioTime) {
-               /* add meter-locked tempo */
-               mlt = add_tempo_locked (tempo_at_minute_locked (_metrics, time_minutes), pulse, minute_at_frame (frame), AudioTime, true, true);
+               /* add meter-locked tempo at the natural time in the current map (frame may differ). */
+               Tempo const tempo_at_time = tempo_at_minute_locked (_metrics, time_minutes);
+               TempoSection* mlt = add_tempo_locked (tempo_at_time, pulse, time_minutes, AudioTime, true, true, false);
 
                if (!mlt) {
                        return 0;
                }
-
        }
-
-       MeterSection* new_meter = new MeterSection (pulse, minute_at_frame (frame), beat, where, meter.divisions_per_bar(), meter.note_divisor(), pls, _frame_rate);
+       /* still using natural time for the position, ignoring lock style. */
+       MeterSection* new_meter = new MeterSection (pulse, time_minutes, beat_at_bbt_locked (_metrics, bbt), bbt, meter.divisions_per_bar(), meter.note_divisor(), pls, _frame_rate);
 
        bool solved = false;
 
@@ -1226,6 +1202,7 @@ TempoMap::add_meter_locked (const Meter& meter, double beat, const BBT_Time& whe
        if (recompute) {
 
                if (pls == AudioTime) {
+                       /* now set the audio locked meter's position to frame */
                        solved = solve_map_minute (_metrics, new_meter, minute_at_frame (frame));
                        /* we failed, most likely due to some impossible frame requirement wrt audio-locked tempi.
                           fudge frame so that the meter ends up at its BBT position instead.
@@ -1234,7 +1211,7 @@ TempoMap::add_meter_locked (const Meter& meter, double beat, const BBT_Time& whe
                                solved = solve_map_minute (_metrics, new_meter, minute_at_frame (prev_m.frame() + 1));
                        }
                } else {
-                       solved = solve_map_bbt (_metrics, new_meter, where);
+                       solved = solve_map_bbt (_metrics, new_meter, bbt);
                        /* required due to resetting the pulse of meter-locked tempi above.
                           Arguably  solve_map_bbt() should use solve_map_pulse (_metrics, TempoSection) instead,
                           but afaict this cannot cause the map to be left unsolved (these tempi are all audio locked).
@@ -1425,6 +1402,7 @@ TempoMap::recompute_tempi (Metrics& metrics)
                                        continue;
                                }
                        }
+
                        if (prev_t) {
                                if (t->position_lock_style() == AudioTime) {
                                        prev_t->set_c (prev_t->compute_c_minute (prev_t->end_note_types_per_minute(), t->minute()));
@@ -1618,7 +1596,7 @@ TempoMap::metric_at (BBT_Time bbt) const
  * This function uses both tempo and meter.
  */
 double
-TempoMap::beat_at_frame (const framecnt_t& frame) const
+TempoMap::beat_at_frame (const framecnt_t frame) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -1715,7 +1693,7 @@ TempoMap::minute_at_beat_locked (const Metrics& metrics, const double& beat) con
  *
  */
 Tempo
-TempoMap::tempo_at_frame (const framepos_t& frame) const
+TempoMap::tempo_at_frame (const framepos_t frame) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -1882,7 +1860,7 @@ TempoMap::quarter_note_at_tempo (const Tempo& tempo) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
-       return pulse_at_tempo_locked (_metrics, tempo) * 4.0;;
+       return pulse_at_tempo_locked (_metrics, tempo) * 4.0;
 }
 
 /** Returns the whole-note pulse corresponding to the supplied  BBT (meter-based) beat.
@@ -2609,7 +2587,7 @@ TempoMap::check_solved (const Metrics& metrics) const
 }
 
 bool
-TempoMap::set_active_tempi (const Metrics& metrics, const framepos_t& frame)
+TempoMap::set_active_tempi (const Metrics& metrics, const framepos_t frame)
 {
        for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
                TempoSection* t;
@@ -2703,16 +2681,6 @@ TempoMap::solve_map_minute (Metrics& imaginary, TempoSection* section, const dou
                }
        }
 
-#if (0)
-       recompute_tempi (imaginary);
-
-       if (check_solved (imaginary)) {
-               return true;
-       } else {
-               dunp (imaginary, std::cout);
-       }
-#endif
-
        MetricSectionFrameSorter fcmp;
        imaginary.sort (fcmp);
 
@@ -2770,16 +2738,6 @@ TempoMap::solve_map_pulse (Metrics& imaginary, TempoSection* section, const doub
                section->set_minute (section_prev->minute_at_ntpm (section_prev->end_note_types_per_minute(), pulse));
        }
 
-#if (0)
-       recompute_tempi (imaginary);
-
-       if (check_solved (imaginary)) {
-               return true;
-       } else {
-               dunp (imaginary, std::cout);
-       }
-#endif
-
        MetricSectionSorter cmp;
        imaginary.sort (cmp);
 
@@ -3219,7 +3177,7 @@ TempoMap::predict_tempo_position (TempoSection* section, const BBT_Time& bbt)
  * if sub_num is zero, the musical position will be taken from the supplied frame.
  */
 void
-TempoMap::gui_set_tempo_position (TempoSection* ts, const framepos_t& frame, const int& sub_num)
+TempoMap::gui_set_tempo_position (TempoSection* ts, const framepos_t frame, const int& sub_num)
 {
        Metrics future_map;
 
@@ -3248,25 +3206,24 @@ TempoMap::gui_set_tempo_position (TempoSection* ts, const framepos_t& frame, con
                        Glib::Threads::RWLock::WriterLock lm (lock);
                        TempoSection* tempo_copy = copy_metrics_and_point (_metrics, future_map, ts);
 
-                       if (solve_map_minute (future_map, tempo_copy, minute_at_frame (frame))) {
-                               if (sub_num != 0) {
-                                       /* We're moving the object that defines the grid while snapping to it...
-                                        * Placing the ts at the beat corresponding to the requested frame may shift the
-                                        * grid in such a way that the mouse is left hovering over a completerly different division,
-                                        * causing jittering when the mouse next moves (esp. large tempo deltas).
-                                        *
-                                        * This alters the snap behaviour slightly in that we snap to beat divisions
-                                        * in the future map rather than the existing one.
-                                        */
-                                       const double qn = exact_qn_at_frame_locked (future_map, frame, sub_num);
-                                       const framepos_t snapped_frame = frame_at_minute (minute_at_pulse_locked (future_map, qn / 4.0));
 
-                                       if (solve_map_minute (future_map, tempo_copy, minute_at_frame (snapped_frame))) {
-                                               solve_map_minute (_metrics, ts, minute_at_frame (snapped_frame));
-                                               ts->set_pulse (qn / 4.0);
-                                               recompute_meters (_metrics);
-                                       }
-                               } else {
+                       if (sub_num != 0) {
+                               /* We're moving the object that defines the grid while snapping to it...
+                                * Placing the ts at the beat corresponding to the requested frame may shift the
+                                * grid in such a way that the mouse is left hovering over a completerly different division,
+                                * causing jittering when the mouse next moves (esp. large tempo deltas).
+                                * We fudge around this by doing this in the musical domain and then swapping back for the recompute.
+                                */
+                               const double qn = exact_qn_at_frame_locked (_metrics, frame, sub_num);
+                               tempo_copy->set_position_lock_style (MusicTime);
+                               if (solve_map_pulse (future_map, tempo_copy, qn / 4.0)) {
+                                       ts->set_position_lock_style (MusicTime);
+                                       solve_map_pulse (_metrics, ts, qn / 4.0);
+                                       ts->set_position_lock_style (AudioTime);
+                                       recompute_meters (_metrics);
+                               }
+                       } else {
+                               if (solve_map_minute (future_map, tempo_copy, minute_at_frame (frame))) {
                                        solve_map_minute (_metrics, ts, minute_at_frame (frame));
                                        recompute_meters (_metrics);
                                }
@@ -3292,7 +3249,7 @@ TempoMap::gui_set_tempo_position (TempoSection* ts, const framepos_t& frame, con
  * leaving the meter position unchanged.
  */
 void
-TempoMap::gui_set_meter_position (MeterSection* ms, const framepos_t& frame)
+TempoMap::gui_set_meter_position (MeterSection* ms, const framepos_t frame)
 {
        Metrics future_map;
 
@@ -3391,7 +3348,7 @@ TempoMap::gui_change_tempo (TempoSection* ts, const Tempo& bpm)
 }
 
 void
-TempoMap::gui_stretch_tempo (TempoSection* ts, const framepos_t frame, const framepos_t end_frame)
+TempoMap::gui_stretch_tempo (TempoSection* ts, const framepos_t frame, const framepos_t end_frame, const double start_qnote, const double end_qnote)
 {
        /*
          Ts (future prev_t)   Tnext
@@ -3410,9 +3367,9 @@ TempoMap::gui_stretch_tempo (TempoSection* ts, const framepos_t frame, const fra
                        return;
                }
 
-               TempoSection* prev_t = copy_metrics_and_point (_metrics, future_map, ts);
+               TempoSection* ts_copy = copy_metrics_and_point (_metrics, future_map, ts);
 
-               if (!prev_t) {
+               if (!ts_copy) {
                        return;
                }
 
@@ -3420,32 +3377,31 @@ TempoMap::gui_stretch_tempo (TempoSection* ts, const framepos_t frame, const fra
                framepos_t const min_dframe = 2;
 
                double new_bpm;
-               if (prev_t->clamped()) {
-                       TempoSection* next_t = next_tempo_section_locked (future_map, prev_t);
-                       TempoSection* prev_to_prev_t = previous_tempo_section_locked (future_map, prev_t);
-                       /* the change in frames is the result of changing the slope of at most 2 previous tempo sections.
-                          constant to constant is straightforward, as the tempo prev to prev_t has constant slope.
-                       */
-                       double contribution = 0.0;
-                       if (next_t && prev_to_prev_t && prev_to_prev_t->type() == TempoSection::Ramp) {
-                               contribution = (prev_t->frame() - prev_to_prev_t->frame()) / (double) (next_t->frame() - prev_to_prev_t->frame());
-                       }
-                       framepos_t const fr_off = (end_frame - frame);
-                       const frameoffset_t prev_t_frame_contribution = fr_off - (contribution * (double) fr_off);
-
-                       if (frame > prev_to_prev_t->frame() + min_dframe && (frame + prev_t_frame_contribution) > prev_to_prev_t->frame() + min_dframe) {
-                               new_bpm = prev_t->note_types_per_minute() * ((frame - prev_to_prev_t->frame())
-                                                                                    / (double) ((frame + prev_t_frame_contribution) - prev_to_prev_t->frame()));
+               if (ts_copy->clamped()) {
+                       TempoSection* next_t = next_tempo_section_locked (future_map, ts_copy);
+                       TempoSection* prev_to_ts_copy = previous_tempo_section_locked (future_map, ts_copy);
+                        /* the change in frames is the result of changing the slope of at most 2 previous tempo sections.
+                          constant to constant is straightforward, as the tempo prev to ts_copy has constant slope.
+                        */                     double contribution = 0.0;
+                       if (next_t && prev_to_ts_copy && prev_to_ts_copy->type() == TempoSection::Ramp) {
+                               contribution = (ts_copy->pulse() - prev_to_ts_copy->pulse()) / (double) (next_t->pulse() - prev_to_ts_copy->pulse());
+                       }
+                       framepos_t const fr_off = end_frame - frame;
+                       frameoffset_t const ts_copy_frame_contribution = fr_off - (contribution * (double) fr_off);
+
+                       if (frame > prev_to_ts_copy->frame() + min_dframe && (frame + ts_copy_frame_contribution) > prev_to_ts_copy->frame() + min_dframe) {
+                               new_bpm = ts_copy->note_types_per_minute() * ((start_qnote - (prev_to_ts_copy->pulse() * 4.0))
+                                                                            / (end_qnote - (prev_to_ts_copy->pulse() * 4.0)));
                        } else {
-                               new_bpm = prev_t->note_types_per_minute();
+                               new_bpm = ts_copy->note_types_per_minute();
                        }
                } else {
-                       if (frame > prev_t->frame() + min_dframe && end_frame > prev_t->frame() + min_dframe) {
+                       if (frame > ts_copy->frame() + min_dframe && end_frame > ts_copy->frame() + min_dframe) {
 
-                               new_bpm = prev_t->note_types_per_minute() * ((frame - prev_t->frame())
-                                                                            / (double) (end_frame - prev_t->frame()));
+                               new_bpm = ts_copy->note_types_per_minute() * ((frame - ts_copy->frame())
+                                                                            / (double) (end_frame - ts_copy->frame()));
                        } else {
-                               new_bpm = prev_t->note_types_per_minute();
+                               new_bpm = ts_copy->note_types_per_minute();
                        }
 
                        new_bpm = min (new_bpm, (double) 1000.0);
@@ -3459,17 +3415,12 @@ TempoMap::gui_stretch_tempo (TempoSection* ts, const framepos_t frame, const fra
                        goto out;
                }
 
-               if (prev_t && prev_t->type() == TempoSection::Ramp) {
-                       prev_t->set_note_types_per_minute (new_bpm);
-               } else {
-                       prev_t->set_end_note_types_per_minute (new_bpm);
-                       prev_t->set_note_types_per_minute (new_bpm);
-               }
+               ts_copy->set_note_types_per_minute (new_bpm);
 
-               if (prev_t->clamped()) {
+               if (ts_copy->clamped()) {
                        TempoSection* prev = 0;
-                       if ((prev = previous_tempo_section_locked (future_map, prev_t)) != 0) {
-                               prev->set_end_note_types_per_minute (prev_t->note_types_per_minute());
+                       if ((prev = previous_tempo_section_locked (future_map, ts_copy)) != 0) {
+                               prev->set_end_note_types_per_minute (ts_copy->note_types_per_minute());
                        }
                }
 
@@ -3477,18 +3428,15 @@ TempoMap::gui_stretch_tempo (TempoSection* ts, const framepos_t frame, const fra
                recompute_meters (future_map);
 
                if (check_solved (future_map)) {
-                       if (prev_t && prev_t->type() == TempoSection::Ramp) {
-                               ts->set_note_types_per_minute (new_bpm);
-                       } else {
-                               ts->set_end_note_types_per_minute (new_bpm);
-                               ts->set_note_types_per_minute (new_bpm);
-                       }
+                       ts->set_note_types_per_minute (new_bpm);
+
                        if (ts->clamped()) {
                                TempoSection* prev = 0;
                                if ((prev = previous_tempo_section_locked (_metrics, ts)) != 0) {
                                        prev->set_end_note_types_per_minute (ts->note_types_per_minute());
                                }
                        }
+
                        recompute_tempi (_metrics);
                        recompute_meters (_metrics);
                }
@@ -3780,6 +3728,16 @@ TempoMap::gui_twist_tempi (TempoSection* ts, const Tempo& bpm, const framepos_t
 
        return can_solve;
 }
+
+/** Returns the frame position of the musical position zero */
+framepos_t
+TempoMap::music_origin ()
+{
+       Glib::Threads::RWLock::ReaderLock lm (lock);
+
+       return first_tempo().frame();
+}
+
 /** Returns the exact bbt-based beat corresponding to the bar, beat or quarter note subdivision nearest to
  * the supplied frame, possibly returning a negative value.
  *
@@ -3803,7 +3761,7 @@ TempoMap::gui_twist_tempi (TempoSection* ts, const Tempo& bpm, const framepos_t
  * This function is sensitive to tempo and meter.
  */
 double
-TempoMap::exact_beat_at_frame (const framepos_t& frame, const int32_t sub_num) const
+TempoMap::exact_beat_at_frame (const framepos_t frame, const int32_t sub_num) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -3811,7 +3769,7 @@ TempoMap::exact_beat_at_frame (const framepos_t& frame, const int32_t sub_num) c
 }
 
 double
-TempoMap::exact_beat_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t divisions) const
+TempoMap::exact_beat_at_frame_locked (const Metrics& metrics, const framepos_t frame, const int32_t divisions) const
 {
        return beat_at_pulse_locked (_metrics, exact_qn_at_frame_locked (metrics, frame, divisions) / 4.0);
 }
@@ -3839,7 +3797,7 @@ TempoMap::exact_beat_at_frame_locked (const Metrics& metrics, const framepos_t&
  * This function is tempo-sensitive.
  */
 double
-TempoMap::exact_qn_at_frame (const framepos_t& frame, const int32_t sub_num) const
+TempoMap::exact_qn_at_frame (const framepos_t frame, const int32_t sub_num) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -3847,7 +3805,7 @@ TempoMap::exact_qn_at_frame (const framepos_t& frame, const int32_t sub_num) con
 }
 
 double
-TempoMap::exact_qn_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t sub_num) const
+TempoMap::exact_qn_at_frame_locked (const Metrics& metrics, const framepos_t frame, const int32_t sub_num) const
 {
        double qn = pulse_at_minute_locked (metrics, minute_at_frame (frame)) * 4.0;
 
@@ -4162,11 +4120,11 @@ TempoMap::get_grid (vector<TempoMap::BBTPoint>& points,
        if (bar_mod == 0) {
                while (pos >= 0 && pos < upper) {
                        pos = frame_at_minute (minute_at_beat_locked (_metrics, cnt));
-                       const TempoSection tempo = tempo_section_at_minute_locked (_metrics, minute_at_frame (pos));
                        const MeterSection meter = meter_section_at_minute_locked (_metrics, minute_at_frame (pos));
                        const BBT_Time bbt = bbt_at_beat_locked (_metrics, cnt);
+                       const double qn = pulse_at_beat_locked (_metrics, cnt) * 4.0;
 
-                       points.push_back (BBTPoint (meter, tempo_at_minute_locked (_metrics, minute_at_frame (pos)), pos, bbt.bars, bbt.beats, tempo.c()));
+                       points.push_back (BBTPoint (meter, tempo_at_minute_locked (_metrics, minute_at_frame (pos)), pos, bbt.bars, bbt.beats, qn));
                        ++cnt;
                }
        } else {
@@ -4181,9 +4139,10 @@ TempoMap::get_grid (vector<TempoMap::BBTPoint>& points,
 
                while (pos >= 0 && pos < upper) {
                        pos = frame_at_minute (minute_at_bbt_locked (_metrics, bbt));
-                       const TempoSection tempo = tempo_section_at_minute_locked (_metrics, minute_at_frame (pos));
                        const MeterSection meter = meter_section_at_minute_locked (_metrics, minute_at_frame (pos));
-                       points.push_back (BBTPoint (meter, tempo_at_minute_locked (_metrics, minute_at_frame (pos)), pos, bbt.bars, bbt.beats, tempo.c()));
+                       const double qn = pulse_at_bbt_locked (_metrics, bbt) * 4.0;
+
+                       points.push_back (BBTPoint (meter, tempo_at_minute_locked (_metrics, minute_at_frame (pos)), pos, bbt.bars, bbt.beats, qn));
                        bbt.bars += bar_mod;
                }
        }
@@ -4379,7 +4338,7 @@ TempoMap::next_tempo_section_locked (const Metrics& metrics, TempoSection* ts) c
    do that stuff based on the beat_at_frame and frame_at_beat api
 */
 double
-TempoMap::frames_per_quarter_note_at (const framepos_t& frame, const framecnt_t& sr) const
+TempoMap::frames_per_quarter_note_at (const framepos_t frame, const framecnt_t sr) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -4570,7 +4529,7 @@ TempoMap::fix_legacy_end_session ()
                        }
 
                        if (prev_t) {
-                               if (prev_t->type() != TempoSection::Constant) {
+                               if (prev_t->end_note_types_per_minute() < 0.0) {
                                        prev_t->set_end_note_types_per_minute (t->note_types_per_minute());
                                }
                        }
@@ -4578,6 +4537,10 @@ TempoMap::fix_legacy_end_session ()
                        prev_t = t;
                }
        }
+
+       if (prev_t) {
+               prev_t->set_end_note_types_per_minute (prev_t->note_types_per_minute());
+       }
 }
 
 XMLNode&
@@ -4655,8 +4618,6 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                                        fix_legacy_end_session();
                                        break;
                                }
-
-                               break;
                        }
                }
 
@@ -4677,9 +4638,9 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                                TempoSection* ts;
                                TempoSection* prev_t;
                                if ((prev_m = dynamic_cast<MeterSection*>(*prev)) != 0 && (ms = dynamic_cast<MeterSection*>(*i)) != 0) {
-                                       if (prev_m->pulse() == ms->pulse()) {
-                                               cerr << string_compose (_("Multiple meter definitions found at %1"), prev_m->pulse()) << endmsg;
-                                               error << string_compose (_("Multiple meter definitions found at %1"), prev_m->pulse()) << endmsg;
+                                       if (prev_m->beat() == ms->beat()) {
+                                               cerr << string_compose (_("Multiple meter definitions found at %1"), prev_m->beat()) << endmsg;
+                                               error << string_compose (_("Multiple meter definitions found at %1"), prev_m->beat()) << endmsg;
                                                return -1;
                                        }
                                } else if ((prev_t = dynamic_cast<TempoSection*>(*prev)) != 0 && (ts = dynamic_cast<TempoSection*>(*i)) != 0) {