fix issue with initialization of a BBT_Time variable.
[ardour.git] / libs / ardour / tempo.cc
index aa8f46dd6c10ceac48894cb8778ccda81a3344ea..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,8 +159,6 @@ TempoSection::TempoSection (const XMLNode& node, framecnt_t sample_rate)
        , _locked_to_meter (false)
        , _clamped (false)
 {
-       _legacy_bbt = BBT_Time (0, 0, 0);
-
        BBT_Time bbt;
        std::string start_bbt;
        if (node.get_property ("start", start_bbt)) {
@@ -360,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) {
@@ -1020,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;
@@ -1030,7 +1028,8 @@ TempoMap::add_tempo (const Tempo& tempo, const double& pulse, const framepos_t&
        TempoSection* ts = 0;
        {
                Glib::Threads::RWLock::WriterLock lm (lock);
-               ts = add_tempo_locked (tempo, pulse, minute_at_frame (frame), pls, true, false);
+               /* 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);
 
                recompute_map (_metrics);
        }
@@ -1041,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;
@@ -1064,11 +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());
+                               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());
                                }
                        }
 
@@ -1091,10 +1090,11 @@ 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);
 
@@ -1102,11 +1102,7 @@ TempoMap::add_tempo_locked (const Tempo& tempo, double pulse, double minute
        for (Metrics::iterator i = _metrics.begin(); i != _metrics.end(); ++i) {
                TempoSection* const this_t = dynamic_cast<TempoSection*>(*i);
                if (this_t) {
-                       bool const ipm = t->position_lock_style() == MusicTime;
-                       bool const lm = t->locked_to_meter();
-
-                       if ((ipm && this_t->pulse() == t->pulse()) || (!ipm && this_t->frame() == t->frame())
-                           || (lm && this_t->pulse() == t->pulse())) {
+                       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());
                                }
@@ -1190,7 +1186,7 @@ TempoMap::add_meter_locked (const Meter& meter, const BBT_Time& bbt, framepos_t
        if (pls == AudioTime) {
                /* 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);
+               TempoSection* mlt = add_tempo_locked (tempo_at_time, pulse, time_minutes, AudioTime, true, true, false);
 
                if (!mlt) {
                        return 0;
@@ -1406,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()));
@@ -1599,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);
 
@@ -1696,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);
 
@@ -2590,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;
@@ -2684,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);
 
@@ -2751,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);
 
@@ -3200,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;
 
@@ -3272,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;
 
@@ -3371,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
@@ -3390,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;
                }
 
@@ -3400,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);
@@ -3439,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());
                        }
                }
 
@@ -3457,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);
                }
@@ -3793,7 +3761,7 @@ TempoMap::music_origin ()
  * 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);
 
@@ -3801,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);
 }
@@ -3829,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);
 
@@ -3837,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;
 
@@ -4370,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);
 
@@ -4561,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());
                                }
                        }
@@ -4569,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&
@@ -4646,8 +4618,6 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                                        fix_legacy_end_session();
                                        break;
                                }
-
-                               break;
                        }
                }