Fix warnings
[ardour.git] / libs / ardour / tempo.cc
index 51b911187807248c42980916c0a0fe7b072a3265..41c08ae675b11bdca0d5eee1664d03e1671fc028 100644 (file)
@@ -45,7 +45,7 @@ using Timecode::BBT_Time;
 /* _default tempo is 4/4 qtr=120 */
 
 Meter    TempoMap::_default_meter (4.0, 4.0);
-Tempo    TempoMap::_default_tempo (120.0);
+Tempo    TempoMap::_default_tempo (120.0, 4.0);
 
 framepos_t
 MetricSection::frame_at_minute (const double& time) const
@@ -96,8 +96,6 @@ TempoSection::TempoSection (const XMLNode& node, framecnt_t sample_rate)
        BBT_Time bbt;
        double pulse;
        uint32_t frame;
-       double minute;
-       bool had_beats_per_minute = false;
 
        _legacy_bbt = BBT_Time (0, 0, 0);
 
@@ -124,37 +122,18 @@ TempoSection::TempoSection (const XMLNode& node, framecnt_t sample_rate)
        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;
+                       throw failed_constructor();
                } else {
                        set_minute (minute_at_frame (frame));
                }
        }
 
-       if ((prop = node.property ("minute")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &minute) != 1) {
-                       error << _("TempoSection XML node has an illegal \"minute\" value") << endmsg;
-               } else {
-                       set_minute (minute);
-               }
-       }
-
-       /* replace old beats-per-minute with note-types-per-minute */
+       /* XX replace old beats-per-minute name with note-types-per-minute */
        if ((prop = node.property ("beats-per-minute")) != 0) {
-               info << _("Renaming legacy \"beats-per-minute\" XML node to note-types-per-minute") << endmsg;
-               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-minutee\" value") << endmsg;
-                       throw failed_constructor();
-               }
-               had_beats_per_minute = true;
-       }
-
-       if ((prop = node.property ("note-types-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 \"note-types-per-minute\" value") << endmsg;
+                       error << _("TempoSection XML node has an illegal \"beats-per-minute\" value") << endmsg;
                        throw failed_constructor();
                }
-       } else if (!had_beats_per_minute) {
-               error << _("TempoSection XML node has no \"note-types-per-minute\" or \"beats-per-minute\" property") << endmsg;
-               throw failed_constructor();
        }
 
        if ((prop = node.property ("note-type")) == 0) {
@@ -215,10 +194,8 @@ TempoSection::get_state() const
        root->add_property ("pulse", buf);
        snprintf (buf, sizeof (buf), "%li", frame());
        root->add_property ("frame", buf);
-       snprintf (buf, sizeof (buf), "%lf", minute());
-       root->add_property ("minute", buf);
        snprintf (buf, sizeof (buf), "%lf", _note_types_per_minute);
-       root->add_property ("note-types-per-minute", buf);
+       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", movable()?"yes":"no");
@@ -260,12 +237,11 @@ TempoSection::tempo_at_minute (const double& m) const
  *
 */
 
-/** user feedback dictates that if tempoA (120, 4.0) precedes tempoB (120, 8.0),
- *  there will be no ramp between the two even if set to ramped.
+/** if tempoA (120, 4.0) precedes tempoB (120, 8.0),
+ *  there should be no ramp between the two even if we are ramped.
  *  in other words a ramp should only place a curve on note_types_per_minute.
  *  we should be able to use Tempo note type here, but the above
  *  complicates things a bit.
- *  we would ideally like to use arbitrary Tempo structs here.
 */
 double
 TempoSection::minute_at_ntpm (const double& ntpm, const double& p) const
@@ -334,6 +310,12 @@ TempoSection::minute_at_pulse (const double& p) const
        return _time_at_pulse (p - pulse()) + minute();
 }
 
+/** returns thw whole-note pulse at session frame position f.
+ *  @param f the frame position.
+ *  @return the position in whole-note pulses corresponding to f
+ *
+ *  for use with musical units whose granularity is coarser than frames (e.g. ticks)
+*/
 double
 TempoSection::pulse_at_frame (const framepos_t& f) const
 {
@@ -522,7 +504,6 @@ MeterSection::MeterSection (const XMLNode& node, const framecnt_t sample_rate)
        double beat = 0.0;
        framepos_t frame = 0;
        pair<double, BBT_Time> start;
-       double minute = 0.0;
 
        if ((prop = node.property ("start")) != 0) {
                if (sscanf (prop->value().c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32,
@@ -568,17 +549,11 @@ MeterSection::MeterSection (const XMLNode& node, const framecnt_t sample_rate)
        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));
                }
        }
-       if ((prop = node.property ("minute")) != 0) {
-               if (sscanf (prop->value().c_str(), "%lf", &minute) != 1) {
-                       error << _("MeterSection XML node has an illegal \"frame\" value") << endmsg;
-               } else {
-                       set_minute (minute);
-               }
-       }
 
        /* beats-per-bar is old; divisions-per-bar is new */
 
@@ -641,8 +616,6 @@ MeterSection::get_state() const
        root->add_property ("note-type", buf);
        snprintf (buf, sizeof (buf), "%li", frame());
        root->add_property ("frame", buf);
-       snprintf (buf, sizeof (buf), "%lf", minute());
-       root->add_property ("minute", 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);
@@ -1360,6 +1333,7 @@ TempoMap::recompute_tempi (Metrics& metrics)
                        prev_t = t;
                }
        }
+       assert (prev_t);
        prev_t->set_c_func (0.0);
 }
 
@@ -1597,6 +1571,7 @@ TempoMap::minute_at_beat_locked (const Metrics& metrics, const double& beat) con
                        prev_m = m;
                }
        }
+       assert (prev_m);
 
        TempoSection* t;
 
@@ -1610,6 +1585,7 @@ TempoMap::minute_at_beat_locked (const Metrics& metrics, const double& beat) con
                }
 
        }
+       assert (prev_t);
 
        return prev_t->minute_at_pulse (((beat - prev_m->beat()) / prev_m->note_divisor()) + prev_m->pulse());
 }
@@ -1827,6 +1803,7 @@ TempoMap::beat_at_pulse_locked (const Metrics& metrics, const double& pulse) con
                        prev_m = m;
                }
        }
+       assert (prev_m);
 
        double const ret = ((pulse - prev_m->pulse()) * prev_m->note_divisor()) + prev_m->beat();
        return ret;
@@ -1973,6 +1950,7 @@ TempoMap::bbt_at_beat_locked (const Metrics& metrics, const double& b) const
                        prev_m = m;
                }
        }
+       assert (prev_m);
 
        const double beats_in_ms = beats - prev_m->beat();
        const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
@@ -2110,6 +2088,8 @@ TempoMap::bbt_at_pulse_locked (const Metrics& metrics, const double& pulse) cons
                }
        }
 
+       assert (prev_m);
+
        const double beats_in_ms = (pulse - prev_m->pulse()) * prev_m->note_divisor();
        const uint32_t bars_in_ms = (uint32_t) floor (beats_in_ms / prev_m->divisions_per_bar());
        const uint32_t total_bars = bars_in_ms + (prev_m->bbt().bars - 1);
@@ -2346,7 +2326,7 @@ TempoMap::minute_at_quarter_note_locked (const Metrics& metrics, const double qu
  *
  */
 double
-TempoMap::quarter_note_at_beat (const double beat)
+TempoMap::quarter_note_at_beat (const double beat) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -2371,7 +2351,7 @@ TempoMap::quarter_note_at_beat_locked (const Metrics& metrics, const double beat
  *
  */
 double
-TempoMap::beat_at_quarter_note (const double quarter_note)
+TempoMap::beat_at_quarter_note (const double quarter_note) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -2439,6 +2419,7 @@ TempoMap::quarter_notes_between_frames_locked (const Metrics& metrics, const fra
                        prev_t = t;
                }
        }
+       assert (prev_t);
        const double start_qn = prev_t->pulse_at_frame (start);
 
        for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
@@ -3279,7 +3260,9 @@ TempoMap::gui_dilate_tempo (TempoSection* ts, const framepos_t& frame, const fra
                TempoSection* prev_to_prev_t = 0;
                const frameoffset_t fr_off = end_frame - frame;
 
-               if (prev_t && prev_t->pulse() > 0.0) {
+               assert (prev_t);
+
+               if (prev_t->pulse() > 0.0) {
                        prev_to_prev_t = const_cast<TempoSection*>(&tempo_section_at_minute_locked (future_map, minute_at_frame (prev_t->frame() - 1)));
                }
 
@@ -3426,7 +3409,7 @@ TempoMap::gui_dilate_tempo (TempoSection* ts, const framepos_t& frame, const fra
  * This function is sensitive to tempo and meter.
  */
 double
-TempoMap::exact_beat_at_frame (const framepos_t& frame, const int32_t sub_num)
+TempoMap::exact_beat_at_frame (const framepos_t& frame, const int32_t sub_num) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -3434,7 +3417,7 @@ TempoMap::exact_beat_at_frame (const framepos_t& frame, const int32_t sub_num)
 }
 
 double
-TempoMap::exact_beat_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t divisions)
+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);
 }
@@ -3462,7 +3445,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)
+TempoMap::exact_qn_at_frame (const framepos_t& frame, const int32_t sub_num) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -3470,7 +3453,7 @@ TempoMap::exact_qn_at_frame (const framepos_t& frame, const int32_t sub_num)
 }
 
 double
-TempoMap::exact_qn_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t sub_num)
+TempoMap::exact_qn_at_frame_locked (const Metrics& metrics, const framepos_t& frame, const int32_t sub_num) const
 {
        double qn = quarter_note_at_minute_locked (metrics, minute_at_frame (frame));
 
@@ -3511,7 +3494,7 @@ TempoMap::bbt_duration_at (framepos_t pos, const BBT_Time& bbt, int dir)
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
        BBT_Time pos_bbt = bbt_at_minute_locked (_metrics, minute_at_frame (pos));
-       const framecnt_t offset = frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
+
        const double divisions = meter_section_at_minute_locked (_metrics, minute_at_frame (pos)).divisions_per_bar();
 
        if (dir > 0) {
@@ -3528,31 +3511,47 @@ TempoMap::bbt_duration_at (framepos_t pos, const BBT_Time& bbt, int dir)
                        pos_bbt.bars += 1;
                        pos_bbt.beats -= divisions;
                }
+               const framecnt_t pos_bbt_frame = frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
+
+               return pos_bbt_frame - pos;
 
-               return frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt)) - offset;
        } else {
-               pos_bbt.bars -= bbt.bars;
+
+               if (pos_bbt.bars <= bbt.bars) {
+                       pos_bbt.bars = 1;
+               } else {
+                       pos_bbt.bars -= bbt.bars;
+               }
 
                if (pos_bbt.ticks < bbt.ticks) {
-                       if (pos_bbt.beats == 1) {
-                               pos_bbt.bars--;
-                               pos_bbt.beats = divisions;
+                       if (pos_bbt.bars > 1) {
+                               if (pos_bbt.beats == 1) {
+                                       pos_bbt.bars--;
+                                       pos_bbt.beats = divisions;
+                               } else {
+                                       pos_bbt.beats--;
+                               }
+                               pos_bbt.ticks = BBT_Time::ticks_per_beat - (bbt.ticks - pos_bbt.ticks);
                        } else {
-                               pos_bbt.beats--;
+                               pos_bbt.beats = 1;
+                               pos_bbt.ticks = 0;
                        }
-                       pos_bbt.ticks = BBT_Time::ticks_per_beat - (bbt.ticks - pos_bbt.ticks);
                } else {
                        pos_bbt.ticks -= bbt.ticks;
                }
 
                if (pos_bbt.beats <= bbt.beats) {
-                       pos_bbt.bars--;
-                       pos_bbt.beats = divisions - (bbt.beats - pos_bbt.beats);
+                       if (pos_bbt.bars > 1) {
+                               pos_bbt.bars--;
+                               pos_bbt.beats = divisions - (bbt.beats - pos_bbt.beats);
+                       } else {
+                               pos_bbt.beats = 1;
+                       }
                } else {
                        pos_bbt.beats -= bbt.beats;
                }
 
-               return offset - frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
+               return pos - frame_at_minute (minute_at_bbt_locked (_metrics, pos_bbt));
        }
 
        return 0;
@@ -3946,6 +3945,7 @@ TempoMap::frames_per_quarter_note_at (const framepos_t& frame, const framecnt_t&
                        ts_at = t;
                }
        }
+       assert (ts_at);
 
        if (ts_after) {
                return  (60.0 * _frame_rate) / ts_at->tempo_at_minute (minute_at_frame (frame)).quarter_notes_per_minute();