fix issue with initialization of a BBT_Time variable.
[ardour.git] / libs / ardour / tempo.cc
index 45ae54f4798b9b0293eeb5fa1aafe05b1decf8d5..5d3be149644012c923ff209a69b1eaa85ef7ea2d 100644 (file)
@@ -32,6 +32,7 @@
 #include "ardour/debug.h"
 #include "ardour/lmath.h"
 #include "ardour/tempo.h"
+#include "ardour/types_convert.h"
 
 #include "pbd/i18n.h"
 #include <locale.h>
@@ -54,11 +55,39 @@ 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;
 }
 
+/***********************************************************************/
+
+bool
+ARDOUR::bbt_time_to_string (const BBT_Time& bbt, std::string& str)
+{
+       char buf[256];
+       int retval = snprintf (buf, sizeof(buf), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, bbt.bars, bbt.beats,
+                              bbt.ticks);
+
+       if (retval <= 0 || retval >= (int)sizeof(buf)) {
+               return false;
+       }
+
+       str = buf;
+       return true;
+}
+
+bool
+ARDOUR::string_to_bbt_time (const std::string& str, BBT_Time& bbt)
+{
+       if (sscanf (str.c_str (), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &bbt.bars, &bbt.beats,
+                   &bbt.ticks) == 3) {
+               return true;
+       }
+       return false;
+}
+
+
 /***********************************************************************/
 
 double
@@ -82,6 +111,44 @@ Meter::frames_per_bar (const Tempo& tempo, framecnt_t sr) const
 
 /***********************************************************************/
 
+void
+MetricSection::add_state_to_node(XMLNode& node) const
+{
+       node.set_property ("pulse", _pulse);
+       node.set_property ("frame", frame());
+       node.set_property ("movable", !_initial);
+       node.set_property ("lock-style", _position_lock_style);
+}
+
+int
+MetricSection::set_state (const XMLNode& node, int /*version*/)
+{
+       node.get_property ("pulse", _pulse);
+
+       framepos_t frame;
+       if (node.get_property ("frame", frame)) {
+               set_minute (minute_at_frame (frame));
+       }
+
+       bool tmp;
+       if (!node.get_property ("movable", tmp)) {
+               error << _("TempoSection XML node has no \"movable\" property") << endmsg;
+               throw failed_constructor();
+       }
+       _initial = !tmp;
+
+       if (!node.get_property ("lock-style", _position_lock_style)) {
+               if (!initial()) {
+                       _position_lock_style = MusicTime;
+               } else {
+                       _position_lock_style = AudioTime;
+               }
+       }
+       return 0;
+}
+
+/***********************************************************************/
+
 const string TempoSection::xml_state_node_name = "Tempo";
 
 TempoSection::TempoSection (const XMLNode& node, framecnt_t sample_rate)
@@ -91,123 +158,74 @@ TempoSection::TempoSection (const XMLNode& node, framecnt_t sample_rate)
        , _active (true)
        , _locked_to_meter (false)
        , _clamped (false)
-       , _legacy_end (false)
 {
-       XMLProperty const * prop;
-       LocaleGuard lg;
        BBT_Time bbt;
-       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,
-                           &bbt.beats,
-                           &bbt.ticks) == 3) {
+       std::string start_bbt;
+       if (node.get_property ("start", start_bbt)) {
+               if (string_to_bbt_time (start_bbt, bbt)) {
                        /* legacy session - start used to be in bbt*/
                        _legacy_bbt = bbt;
-                       pulse = -1.0;
+                       set_pulse(-1.0);
                        info << _("Legacy session detected. TempoSection XML node will be altered.") << endmsg;
                }
        }
 
-       if ((prop = node.property ("pulse")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &pulse) != 1) {
-                       error << _("TempoSection XML node has an illegal \"pulse\" value") << endmsg;
-               }
-       }
-
-       set_pulse (pulse);
+       // Don't worry about return value, exception will be thrown on error
+       MetricSection::set_state (node, Stateful::loading_state_version);
 
-       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;
+       if (node.get_property ("beats-per-minute", _note_types_per_minute)) {
+               if (_note_types_per_minute < 0.0) {
+                       error << _("TempoSection XML node has an illegal \"beats_per_minute\" value") << endmsg;
                        throw failed_constructor();
-               } else {
-                       set_minute (minute_at_frame (frame));
                }
        }
 
-       /* XX replace old beats-per-minute name with note-types-per-minute */
-       if ((prop = node.property ("beats-per-minute")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &_note_types_per_minute) != 1 || _note_types_per_minute < 0.0) {
-                       error << _("TempoSection XML node has an illegal \"beats-per-minute\" value") << endmsg;
+       if (node.get_property ("note-type", _note_type)) {
+               if (_note_type < 1.0) {
+                       error << _("TempoSection XML node has an illegal \"note-type\" value") << endmsg;
                        throw failed_constructor();
                }
-       }
-
-       if ((prop = node.property ("note-type")) == 0) {
+       } else {
                /* older session, make note type be quarter by default */
                _note_type = 4.0;
-       } else {
-               if (sscanf (prop->value().c_str(), "%lf", &_note_type) != 1 || _note_type < 1.0) {
-                       error << _("TempoSection XML node has an illegal \"note-type\" value") << endmsg;
-                       throw failed_constructor();
-               }
        }
 
-       if ((prop = node.property ("clamped")) == 0) {
-               warning << _("TempoSection XML node has no \"clamped\" property") << endmsg;
-               set_clamped (false);
-       } else {
-               set_clamped (string_is_affirmative (prop->value()));
+       if (!node.get_property ("clamped", _clamped)) {
+               _clamped = false;
        }
 
-       /* XX replace old end-beats-per-minute name with note-types-per-minute */
-       if ((prop = node.property ("end-beats-per-minute")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &_end_note_types_per_minute) != 1 || _end_note_types_per_minute < 0.0) {
-                       info << _("TempoSection XML node has an illegal \"in-beats-per-minute\" value") << endmsg;
-                       //throw failed_constructor();
-                       _end_note_types_per_minute = _note_types_per_minute;
-                       _legacy_end = true;
+       if (node.get_property ("end-beats-per-minute", _end_note_types_per_minute)) {
+               if (_end_note_types_per_minute < 0.0) {
+                       info << _("TempoSection XML node has an illegal \"end-beats-per-minute\" value") << endmsg;
+                       throw failed_constructor();
                }
-       } else {
-               _legacy_end = true;
        }
 
-       if ((prop = node.property ("tempo-type")) != 0) {
-               TempoSection::Type old_type;
+       TempoSection::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.
+               */
 
-               old_type = Type (string_2_enum (prop->value(), old_type));
                if (old_type == TempoSection::Constant) {
                        _end_note_types_per_minute = _note_types_per_minute;
+               } else {
+                       _end_note_types_per_minute = -1.0;
                }
        }
 
-       if ((prop = node.property ("movable")) == 0) {
-               error << _("TempoSection XML node has no \"movable\" property") << endmsg;
-               throw failed_constructor();
-       }
-
-       set_initial (!string_is_affirmative (prop->value()));
-
-       if ((prop = node.property ("active")) == 0) {
+       if (!node.get_property ("active", _active)) {
                warning << _("TempoSection XML node has no \"active\" property") << endmsg;
-               set_active(true);
-       } else {
-               set_active (string_is_affirmative (prop->value()));
-       }
-
-       if ((prop = node.property ("lock-style")) == 0) {
-               if (!initial()) {
-                       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())));
+               _active = true;
        }
 
-       if ((prop = node.property ("locked-to-meter")) == 0) {
+       if (!node.get_property ("locked-to-meter", _locked_to_meter)) {
                if (initial()) {
                        set_locked_to_meter (true);
                } else {
                        set_locked_to_meter (false);
                }
-       } else {
-               set_locked_to_meter (string_is_affirmative (prop->value()));
        }
 
        /* 5.5 marked initial tempo as not locked to meter. this should always be true anyway */
@@ -220,27 +238,15 @@ XMLNode&
 TempoSection::get_state() const
 {
        XMLNode *root = new XMLNode (xml_state_node_name);
-       char buf[256];
-       LocaleGuard lg;
-
-       snprintf (buf, sizeof (buf), "%lf", pulse());
-       root->add_property ("pulse", buf);
-       snprintf (buf, sizeof (buf), "%li", frame());
-       root->add_property ("frame", buf);
-       snprintf (buf, sizeof (buf), "%lf", _note_types_per_minute);
-       root->add_property ("beats-per-minute", buf);
-       snprintf (buf, sizeof (buf), "%lf", _note_type);
-       root->add_property ("note-type", buf);
-       snprintf (buf, sizeof (buf), "%s", _clamped?"yes":"no");
-       root->add_property ("clamped", buf);
-       snprintf (buf, sizeof (buf), "%lf", _end_note_types_per_minute);
-       root->add_property ("end-beats-per-minute", buf);
-       snprintf (buf, sizeof (buf), "%s", !initial()?"yes":"no");
-       root->add_property ("movable", buf);
-       snprintf (buf, sizeof (buf), "%s", active()?"yes":"no");
-       root->add_property ("active", buf);
-       root->add_property ("lock-style", enum_2_string (position_lock_style()));
-       root->add_property ("locked-to-meter", locked_to_meter()?"yes":"no");
+
+       MetricSection::add_state_to_node (*root);
+
+       root->set_property ("beats-per-minute", _note_types_per_minute);
+       root->set_property ("note-type", _note_type);
+       root->set_property ("clamped", _clamped);
+       root->set_property ("end-beats-per-minute", _end_note_types_per_minute);
+       root->set_property ("active", _active);
+       root->set_property ("locked-to-meter", _locked_to_meter);
 
        return *root;
 }
@@ -352,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) {
@@ -542,130 +548,73 @@ 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())
 {
-       XMLProperty const * prop;
-       LocaleGuard lg;
-       BBT_Time bbt;
-       double pulse = 0.0;
-       double beat = 0.0;
-       framepos_t frame = 0;
        pair<double, BBT_Time> start;
+       start.first = 0.0;
 
-       if ((prop = node.property ("start")) != 0) {
-               if (sscanf (prop->value().c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
-                   &bbt.bars,
-                   &bbt.beats,
-                   &bbt.ticks) < 3) {
-                       error << _("MeterSection XML node has an illegal \"start\" value") << endmsg;
-               } else {
+       std::string bbt_str;
+       if (node.get_property ("start", bbt_str)) {
+               if (string_to_bbt_time (bbt_str, start.second)) {
                        /* legacy session - start used to be in bbt*/
                        info << _("Legacy session detected - MeterSection XML node will be altered.") << endmsg;
-                       pulse = -1.0;
-               }
-       }
-
-       if ((prop = node.property ("pulse")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &pulse) != 1) {
-                       error << _("MeterSection XML node has an illegal \"pulse\" value") << endmsg;
+                       set_pulse (-1.0);
+               } else {
+                       error << _("MeterSection XML node has an illegal \"start\" value") << endmsg;
                }
        }
-       set_pulse (pulse);
 
-       if ((prop = node.property ("beat")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &beat) != 1) {
-                       error << _("MeterSection XML node has an illegal \"beat\" value") << endmsg;
-               }
-       }
+       MetricSection::set_state (node, Stateful::loading_state_version);
 
-       start.first = beat;
+       node.get_property ("beat", start.first);
 
-       if ((prop = node.property ("bbt")) == 0) {
+       if (node.get_property ("bbt", bbt_str)) {
+               if (!string_to_bbt_time (bbt_str, start.second)) {
+                       error << _("MeterSection XML node has an illegal \"bbt\" value") << endmsg;
+                       throw failed_constructor();
+               }
+       } else {
                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();
        }
 
-       start.second = bbt;
        set_beat (start);
 
-       if ((prop = node.property ("frame")) != 0) {
-               if (sscanf (prop->value().c_str(), "%li", &frame) != 1) {
-                       error << _("MeterSection XML node has an illegal \"frame\" value") << endmsg;
-                       throw failed_constructor();
-               } else {
-                       set_minute (minute_at_frame (frame));
-               }
-       }
-
        /* beats-per-bar is old; divisions-per-bar is new */
 
-       if ((prop = node.property ("divisions-per-bar")) == 0) {
-               if ((prop = node.property ("beats-per-bar")) == 0) {
+       if (!node.get_property ("divisions-per-bar", _divisions_per_bar)) {
+               if (!node.get_property ("beats-per-bar", _divisions_per_bar)) {
                        error << _("MeterSection XML node has no \"beats-per-bar\" or \"divisions-per-bar\" property") << endmsg;
                        throw failed_constructor();
                }
        }
-       if (sscanf (prop->value().c_str(), "%lf", &_divisions_per_bar) != 1 || _divisions_per_bar < 0.0) {
+
+       if (_divisions_per_bar < 0.0) {
                error << _("MeterSection XML node has an illegal \"divisions-per-bar\" value") << endmsg;
                throw failed_constructor();
        }
 
-       if ((prop = node.property ("note-type")) == 0) {
+       if (!node.get_property ("note-type", _note_type)) {
                error << _("MeterSection XML node has no \"note-type\" property") << endmsg;
                throw failed_constructor();
        }
-       if (sscanf (prop->value().c_str(), "%lf", &_note_type) != 1 || _note_type < 0.0) {
-               error << _("MeterSection XML node has an illegal \"note-type\" value") << endmsg;
-               throw failed_constructor();
-       }
 
-       if ((prop = node.property ("movable")) == 0) {
-               error << _("MeterSection XML node has no \"movable\" property") << endmsg;
+       if (_note_type < 0.0) {
+               error << _("MeterSection XML node has an illegal \"note-type\" value") << endmsg;
                throw failed_constructor();
        }
-
-       set_initial (!string_is_affirmative (prop->value()));
-
-       if ((prop = node.property ("lock-style")) == 0) {
-               warning << _("MeterSection XML node has no \"lock-style\" property") << endmsg;
-               if (!initial()) {
-                       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&
 MeterSection::get_state() const
 {
        XMLNode *root = new XMLNode (xml_state_node_name);
-       char buf[256];
-       LocaleGuard lg;
-
-       snprintf (buf, sizeof (buf), "%lf", pulse());
-       root->add_property ("pulse", buf);
-       snprintf (buf, sizeof (buf), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
-                 bbt().bars,
-                 bbt().beats,
-                 bbt().ticks);
-       root->add_property ("bbt", buf);
-       snprintf (buf, sizeof (buf), "%lf", beat());
-       root->add_property ("beat", buf);
-       snprintf (buf, sizeof (buf), "%lf", _note_type);
-       root->add_property ("note-type", buf);
-       snprintf (buf, sizeof (buf), "%li", frame());
-       root->add_property ("frame", buf);
-       root->add_property ("lock-style", enum_2_string (position_lock_style()));
-       snprintf (buf, sizeof (buf), "%lf", _divisions_per_bar);
-       root->add_property ("divisions-per-bar", buf);
-       snprintf (buf, sizeof (buf), "%s", !initial()?"yes":"no");
-       root->add_property ("movable", buf);
+
+       MetricSection::add_state_to_node (*root);
+
+       std::string bbt_str;
+       bbt_time_to_string (_bbt, bbt_str);
+       root->set_property ("bbt", bbt_str);
+       root->set_property ("beat", beat());
+       root->set_property ("note-type", _note_type);
+       root->set_property ("divisions-per-bar", _divisions_per_bar);
 
        return *root;
 }
@@ -1069,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;
@@ -1077,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) {
+               /* 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);
 
-                       if ((*i)->is_tempo()) {
-                               TempoSection* const this_t = static_cast<TempoSection*> (*i);
-
-                               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);
        }
 
@@ -1107,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;
@@ -1130,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());
                                }
                        }
 
@@ -1173,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());
@@ -1193,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);
        }
 
 
@@ -1217,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());
@@ -1244,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;
 
@@ -1270,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.
@@ -1278,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).
@@ -1469,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()));
@@ -1662,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);
 
@@ -1759,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);
 
@@ -1926,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.
@@ -2653,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;
@@ -2747,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);
 
@@ -2814,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);
 
@@ -3263,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;
 
@@ -3292,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);
                                }
@@ -3336,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;
 
@@ -3435,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
@@ -3454,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;
                }
 
@@ -3464,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);
@@ -3503,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());
                        }
                }
 
@@ -3521,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);
                }
@@ -3824,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.
  *
@@ -3847,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);
 
@@ -3855,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);
 }
@@ -3883,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);
 
@@ -3891,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;
 
@@ -4206,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 {
@@ -4225,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;
                }
        }
@@ -4423,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);
 
@@ -4562,6 +4477,8 @@ TempoMap::fix_legacy_session ()
                        if (!t->active()) {
                                continue;
                        }
+                       /* Ramp type never existed in the era of this tempo section */
+                       t->set_end_note_types_per_minute (t->note_types_per_minute());
 
                        if (t->initial()) {
                                t->set_pulse (0.0);
@@ -4612,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());
                                }
                        }
@@ -4620,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&
@@ -4684,11 +4605,6 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                        }
                }
 
-               if (niter == nlist.end()) {
-                       MetricSectionSorter cmp;
-                       _metrics.sort (cmp);
-               }
-
                /* check for legacy sessions where bbt was the base musical unit for tempo */
                for (Metrics::const_iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
                        TempoSection* t;
@@ -4698,15 +4614,18 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                                        break;
                                }
 
-                               if (t->legacy_end()) {
+                               if (t->end_note_types_per_minute() < 0.0) {
                                        fix_legacy_end_session();
                                        break;
                                }
-
-                               break;
                        }
                }
 
+               if (niter == nlist.end()) {
+                       MetricSectionSorter cmp;
+                       _metrics.sort (cmp);
+               }
+
                /* check for multiple tempo/meters at the same location, which
                   ardour2 somehow allowed.
                */
@@ -4719,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) {