Tempo ramps - correct previous commit.
[ardour.git] / libs / ardour / tempo.cc
index 19e2dc5b702ca77550e71c7280d77ddfc199cad2..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())));
        }
@@ -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);
@@ -953,10 +960,9 @@ TempoMap::replace_meter (const MeterSection& ms, const Meter& meter, const BBT_T
                        add_meter_locked (meter, bbt_to_beats_locked (_metrics, where), where, true);
                } else {
                        MeterSection& first (first_meter());
-                       const PositionLockStyle pl = ms.position_lock_style();
                        /* cannot move the first meter section */
                        *static_cast<Meter*>(&first) = meter;
-                       first.set_position_lock_style (pl);
+                       first.set_position_lock_style (AudioTime);
                }
                recompute_map (_metrics);
        }
@@ -1293,13 +1299,16 @@ TempoMap::recompute_meters (Metrics& metrics)
                                meter->set_pulse (pulse);
                        } else {
                                double pulse = 0.0;
+                               pair<double, BBT_Time> new_beat;
                                if (prev_m) {
                                        pulse = prev_m->pulse() + ((meter->bbt().bars - prev_m->bbt().bars) *  prev_m->divisions_per_bar() / prev_m->note_divisor());
+                                       new_beat = make_pair (((pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat(), meter->bbt());
                                } else {
                                        /* shouldn't happen - the first is audio-locked */
                                        pulse = pulse_at_beat_locked (metrics, meter->beat());
+                                       new_beat = make_pair (pulse, meter->bbt());
                                }
-                               pair<double, BBT_Time> new_beat (((pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat(), meter->bbt());
+
                                meter->set_beat (new_beat);
                                meter->set_frame (frame_at_pulse_locked (metrics, pulse));
                                meter->set_pulse (pulse);
@@ -2608,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;
        }
 }
@@ -2779,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 ()
 {
@@ -2843,59 +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 (!m->movable()) {
-                                       pair<double, BBT_Time> bbt = make_pair (0.0, BBT_Time (1, 1, 0));
-                                       m->set_beat (bbt);
-                                       m->set_pulse (0.0);
-                                       m->set_frame (0);
-                                       m->set_position_lock_style (AudioTime);
-                                       prev_m = m;
-                                       continue;
-                               }
-                               if (prev_m && prev_m->pulse() < 0.0) {
-                                       /*XX we cannot possibly make this work??. */
-                                       pair<double, BBT_Time> start = make_pair (((m->bbt().bars - 1) * 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) * 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 && prev_t->pulse() < 0.0) {
-                                       double const beat = ((t->legacy_bbt().bars - 1) * prev_m->note_divisor()) + (t->legacy_bbt().beats - 1) + (t->legacy_bbt().ticks / BBT_Time::ticks_per_beat);
-                                       if (prev_m) {
-                                               t->set_pulse (beat / prev_m->note_divisor());
-                                       } else {
-                                               /* really shouldn't happen but.. */
-                                               t->set_pulse (beat / 4.0);
-                                       }
+                       if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
+                               if (t->legacy_bbt().bars != 0) {
+                                       fix_legacy_session();
+                                       break;
                                }
-                               prev_t = t;
+                               break;
                        }
                }
+
                /* check for multiple tempo/meters at the same location, which
                   ardour2 somehow allowed.
                */