Fix warnings
[ardour.git] / libs / ardour / tempo.cc
index 8d9f06db7527f54abede525b63cc6f22edac34e8..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;
+                       error << _("TempoSection XML node has an illegal \"beats-per-minute\" 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;
-                       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");
@@ -238,63 +215,78 @@ TempoSection::set_type (Type type)
        _type = type;
 }
 
-/** returns the tempo on note types per minute at the zero-based (relative to session) minute.
+/** returns the Tempo at the session-relative minute.
 */
-double
+Tempo
 TempoSection::tempo_at_minute (const double& m) const
 {
 
        if (_type == Constant || _c_func == 0.0) {
-               return note_types_per_minute();
+               return Tempo (note_types_per_minute(), note_type());
        }
 
-       return _tempo_at_time (m - minute());
+       return Tempo (_tempo_at_time (m - minute()), _note_type);
 }
 
-/** returns the zero-based minute (relative to session)
-   where the tempo in note types per minute occurs in this section.
-   pulse p is only used for constant tempi.
-   note that the tempo map may have multiple such values.
+/** returns the session relative minute where the supplied tempo in note types per minute occurs.
+ *  @param ntpm the tempo in mote types per minute used to calculate the returned minute
+ *  @param p the pulse used to calculate the returned minute for constant tempi
+ *  @return the minute at the supplied tempo
+ *
+ *  note that the note_type is currently ignored in this function. see below.
+ *
+*/
+
+/** 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.
 */
 double
-TempoSection::minute_at_tempo (const double& npm, const double& p) const
+TempoSection::minute_at_ntpm (const double& ntpm, const double& p) const
 {
        if (_type == Constant || _c_func == 0.0) {
                return ((p - pulse()) / pulses_per_minute()) + minute();
        }
 
-       return _time_at_tempo (npm) + minute();
+       return _time_at_tempo (ntpm) + minute();
 }
 
-/** returns the tempo in note types per minute at the supplied pulse.
-*/
-double
+/** returns the Tempo at the supplied whole-note pulse.
+ */
+Tempo
 TempoSection::tempo_at_pulse (const double& p) const
 {
 
        if (_type == Constant || _c_func == 0.0) {
-               return note_types_per_minute();
+               return Tempo (note_types_per_minute(), note_type());
        }
 
-       return _tempo_at_pulse (p - pulse());
+       return Tempo (_tempo_at_pulse (p - pulse()), _note_type);
 }
 
-/** returns the pulse where the tempo in note types per minute occurs given minute m.
-    minute m is only used for constant tempi.
-    note that the session tempo map may have multiple locations where a given tempo occurs.
+/** returns the whole-note pulse where a tempo in note types per minute occurs.
+ *  constant tempi require minute m.
+ *  @param ntpm the note types per minute value used to calculate the returned pulse
+ *  @param m the minute used to calculate the returned pulse if the tempo is constant
+ *  @return the whole-note pulse at the supplied tempo
+ *
+ *  note that note_type is currently ignored in this function. see minute_at_tempo().
+ *
+ *  for constant tempi, this is anaologous to pulse_at_minute().
 */
 double
-TempoSection::pulse_at_tempo (const double& npm, const double& m) const
+TempoSection::pulse_at_ntpm (const double& ntpm, const double& m) const
 {
        if (_type == Constant || _c_func == 0.0) {
-               const double pulses = ((m - minute()) * pulses_per_minute()) + pulse();
-               return pulses;
+               return ((m - minute()) * pulses_per_minute()) + pulse();
        }
 
-       return _pulse_at_tempo (npm) + pulse();
+       return _pulse_at_tempo (ntpm) + pulse();
 }
 
-/** returns the pulse at the supplied session-relative minute.
+/** returns the whole-note pulse at the supplied session-relative minute.
 */
 double
 TempoSection::pulse_at_minute (const double& m) const
@@ -306,7 +298,7 @@ TempoSection::pulse_at_minute (const double& m) const
        return _pulse_at_time (m - minute()) + pulse();
 }
 
-/** returns the minute (relative to session start) at the supplied pulse.
+/** returns the session-relative minute at the supplied whole-note pulse.
 */
 double
 TempoSection::minute_at_pulse (const double& p) const
@@ -318,6 +310,32 @@ 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
+{
+       if (_type == Constant || _c_func == 0.0) {
+               return (minute_at_frame (f - frame()) * pulses_per_minute()) + pulse();
+       }
+
+       return _pulse_at_time (minute_at_frame (f - frame())) + pulse();
+}
+
+framepos_t
+TempoSection::frame_at_pulse (const double& p) const
+{
+       if (_type == Constant || _c_func == 0.0) {
+               return frame_at_minute (((p - pulse()) / pulses_per_minute()) + minute());
+       }
+
+       return frame_at_minute (_time_at_pulse (p - pulse()) + minute());
+}
+
 /*
 Ramp Overview
 
@@ -439,16 +457,16 @@ TempoSection::_tempo_at_time (const double& time) const
 
 /* time in minutes at tempo in note types per minute */
 double
-TempoSection::_time_at_tempo (const double& tempo) const
+TempoSection::_time_at_tempo (const double& npm) const
 {
-       return log (tempo / note_types_per_minute()) / _c_func;
+       return log (npm / note_types_per_minute()) / _c_func;
 }
 
 /* pulse at tempo in note types per minute */
 double
-TempoSection::_pulse_at_tempo (const double& tempo) const
+TempoSection::_pulse_at_tempo (const double& npm) const
 {
-       return ((tempo - note_types_per_minute()) / _c_func) / _note_type;
+       return ((npm - note_types_per_minute()) / _c_func) / _note_type;
 }
 
 /* tempo in note types per minute at pulse */
@@ -486,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,
@@ -532,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 */
 
@@ -605,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);
@@ -1312,18 +1321,19 @@ TempoMap::recompute_tempi (Metrics& metrics)
                                if (t->position_lock_style() == AudioTime) {
                                        prev_t->set_c_func (prev_t->compute_c_func_minute (t->note_types_per_minute(), t->minute()));
                                        if (!t->locked_to_meter()) {
-                                               t->set_pulse (prev_t->pulse_at_tempo (t->note_types_per_minute(), t->minute()));
+                                               t->set_pulse (prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute()));
                                        }
 
                                } else {
                                        prev_t->set_c_func (prev_t->compute_c_func_pulse (t->note_types_per_minute(), t->pulse()));
-                                       t->set_minute (prev_t->minute_at_tempo (t->note_types_per_minute(), t->pulse()));
+                                       t->set_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()));
 
                                }
                        }
                        prev_t = t;
                }
        }
+       assert (prev_t);
        prev_t->set_c_func (0.0);
 }
 
@@ -1561,6 +1571,7 @@ TempoMap::minute_at_beat_locked (const Metrics& metrics, const double& beat) con
                        prev_m = m;
                }
        }
+       assert (prev_m);
 
        TempoSection* t;
 
@@ -1574,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());
 }
@@ -1606,18 +1618,13 @@ TempoMap::tempo_at_minute_locked (const Metrics& metrics, const double& minute)
                        }
                        if ((prev_t) && t->minute() > minute) {
                                /* t is the section past frame */
-                               const double ret_bpm = prev_t->tempo_at_minute (minute);
-                               const Tempo ret_tempo (ret_bpm, prev_t->note_type());
-                               return ret_tempo;
+                               return prev_t->tempo_at_minute (minute);
                        }
                        prev_t = t;
                }
        }
 
-       const double ret = prev_t->note_types_per_minute();
-       const Tempo ret_tempo (ret, prev_t->note_type ());
-
-       return ret_tempo;
+       return Tempo (prev_t->note_types_per_minute(), prev_t->note_type());
 }
 
 /** returns the frame at which the supplied tempo occurs, or
@@ -1660,7 +1667,7 @@ TempoMap::minute_at_tempo_locked (const Metrics& metrics, const Tempo& tempo) co
                                const double prev_t_bpm = prev_t->note_types_per_minute();
 
                                if ((t_bpm > tempo_bpm && prev_t_bpm < tempo_bpm) || (t_bpm < tempo_bpm && prev_t_bpm > tempo_bpm)) {
-                                       return prev_t->minute_at_tempo (tempo_bpm, prev_t->pulse());
+                                       return prev_t->minute_at_ntpm (prev_t->note_types_per_minute(), prev_t->pulse());
                                }
                        }
                        prev_t = t;
@@ -1685,18 +1692,13 @@ TempoMap::tempo_at_pulse_locked (const Metrics& metrics, const double& pulse) co
                        }
                        if ((prev_t) && t->pulse() > pulse) {
                                /* t is the section past frame */
-                               const double ret_bpm = prev_t->tempo_at_pulse (pulse);
-                               const Tempo ret_tempo (ret_bpm, prev_t->note_type());
-                               return ret_tempo;
+                               return prev_t->tempo_at_pulse (pulse);
                        }
                        prev_t = t;
                }
        }
 
-       const double ret = prev_t->note_types_per_minute();
-       const Tempo ret_tempo (ret, prev_t->note_type ());
-
-       return ret_tempo;
+       return Tempo (prev_t->note_types_per_minute(), prev_t->note_type());
 }
 
 double
@@ -1726,7 +1728,7 @@ TempoMap::pulse_at_tempo_locked (const Metrics& metrics, const Tempo& tempo) con
                                const double prev_t_bpm = prev_t->note_types_per_minute();
 
                                if ((t_bpm > tempo_bpm && prev_t_bpm < tempo_bpm) || (t_bpm < tempo_bpm && prev_t_bpm > tempo_bpm)) {
-                                       return prev_t->pulse_at_tempo (tempo_bpm, prev_t->minute());
+                                       return prev_t->pulse_at_ntpm (prev_t->note_types_per_minute(), prev_t->minute());
                                }
                        }
                        prev_t = t;
@@ -1801,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;
@@ -1947,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());
@@ -2084,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);
@@ -2126,7 +2132,7 @@ TempoMap::bbt_at_frame (framepos_t frame)
                bbt.bars = 1;
                bbt.beats = 1;
                bbt.ticks = 0;
-               warning << string_compose (_("tempo map asked for BBT time at frame %1\n"), frame) << endmsg;
+               warning << string_compose (_("tempo map was asked for BBT time at frame %1\n"), frame) << endmsg;
                return bbt;
        }
        Glib::Threads::RWLock::ReaderLock lm (lock);
@@ -2255,7 +2261,7 @@ TempoMap::minute_at_bbt_locked (const Metrics& metrics, const BBT_Time& bbt) con
  *
 */
 double
-TempoMap::quarter_note_at_frame (const framepos_t frame)
+TempoMap::quarter_note_at_frame (const framepos_t frame) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -2273,7 +2279,7 @@ TempoMap::quarter_note_at_minute_locked (const Metrics& metrics, const double mi
 }
 
 double
-TempoMap::quarter_note_at_frame_rt (const framepos_t frame)
+TempoMap::quarter_note_at_frame_rt (const framepos_t frame) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
 
@@ -2295,7 +2301,7 @@ TempoMap::quarter_note_at_frame_rt (const framepos_t frame)
  *
 */
 framepos_t
-TempoMap::frame_at_quarter_note (const double quarter_note)
+TempoMap::frame_at_quarter_note (const double quarter_note) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -2320,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);
 
@@ -2345,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);
 
@@ -2372,7 +2378,7 @@ TempoMap::beat_at_quarter_note_locked (const Metrics& metrics, const double quar
  *
  */
 framecnt_t
-TempoMap::frames_between_quarter_notes (const double start, const double end)
+TempoMap::frames_between_quarter_notes (const double start, const double end) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
@@ -2380,12 +2386,61 @@ TempoMap::frames_between_quarter_notes (const double start, const double end)
 }
 
 double
-TempoMap::minutes_between_quarter_notes_locked (const Metrics& metrics, const double start, const double end)
+TempoMap::minutes_between_quarter_notes_locked (const Metrics& metrics, const double start, const double end) const
 {
 
        return minute_at_pulse_locked (metrics, end / 4.0) - minute_at_pulse_locked (metrics, start / 4.0);
 }
 
+double
+TempoMap::quarter_notes_between_frames (const framecnt_t start, const framecnt_t end) const
+{
+       Glib::Threads::RWLock::ReaderLock lm (lock);
+
+       return quarter_notes_between_frames_locked (_metrics, start, end);
+}
+
+double
+TempoMap::quarter_notes_between_frames_locked (const Metrics& metrics, const framecnt_t start, const framecnt_t end) const
+{
+       const TempoSection* prev_t = 0;
+
+       for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
+               TempoSection* t;
+
+               if ((*i)->is_tempo()) {
+                       t = static_cast<TempoSection*> (*i);
+                       if (!t->active()) {
+                               continue;
+                       }
+                       if (prev_t && t->frame() > start) {
+                               break;
+                       }
+                       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) {
+               TempoSection* t;
+
+               if ((*i)->is_tempo()) {
+                       t = static_cast<TempoSection*> (*i);
+                       if (!t->active()) {
+                               continue;
+                       }
+                       if (prev_t && t->frame() > end) {
+                               break;
+                       }
+                       prev_t = t;
+               }
+       }
+       const double end_qn = prev_t->pulse_at_frame (end);
+
+       return (end_qn - start_qn) * 4.0;
+}
+
 bool
 TempoMap::check_solved (const Metrics& metrics) const
 {
@@ -2407,7 +2462,7 @@ TempoMap::check_solved (const Metrics& metrics) const
                                }
 
                                /* precision check ensures tempo and frames align.*/
-                               if (t->frame() != frame_at_minute (prev_t->minute_at_tempo (t->note_types_per_minute(), t->pulse()))) {
+                               if (t->frame() != frame_at_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()))) {
                                        if (!t->locked_to_meter()) {
                                                return false;
                                        }
@@ -2511,11 +2566,11 @@ TempoMap::solve_map_minute (Metrics& imaginary, TempoSection* section, const dou
                                }
                                if (t->position_lock_style() == MusicTime) {
                                        prev_t->set_c_func (prev_t->compute_c_func_pulse (t->note_types_per_minute(), t->pulse()));
-                                       t->set_minute (prev_t->minute_at_tempo (t->note_types_per_minute(), t->pulse()));
+                                       t->set_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()));
                                } else {
                                        prev_t->set_c_func (prev_t->compute_c_func_minute (t->note_types_per_minute(), t->minute()));
                                        if (!t->locked_to_meter()) {
-                                               t->set_pulse (prev_t->pulse_at_tempo (t->note_types_per_minute(), t->minute()));
+                                               t->set_pulse (prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute()));
                                        }
                                }
                        }
@@ -2526,7 +2581,7 @@ TempoMap::solve_map_minute (Metrics& imaginary, TempoSection* section, const dou
        if (section_prev) {
                section_prev->set_c_func (section_prev->compute_c_func_minute (section->note_types_per_minute(), minute));
                if (!section->locked_to_meter()) {
-                       section->set_pulse (section_prev->pulse_at_tempo (section->note_types_per_minute(), minute));
+                       section->set_pulse (section_prev->pulse_at_ntpm (section->note_types_per_minute(), minute));
                }
        }
 
@@ -2579,11 +2634,11 @@ TempoMap::solve_map_pulse (Metrics& imaginary, TempoSection* section, const doub
                                }
                                if (t->position_lock_style() == MusicTime) {
                                        prev_t->set_c_func (prev_t->compute_c_func_pulse (t->note_types_per_minute(), t->pulse()));
-                                       t->set_minute (prev_t->minute_at_tempo (t->note_types_per_minute(), t->pulse()));
+                                       t->set_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()));
                                } else {
                                        prev_t->set_c_func (prev_t->compute_c_func_minute (t->note_types_per_minute(), t->minute()));
                                        if (!t->locked_to_meter()) {
-                                               t->set_pulse (prev_t->pulse_at_tempo (t->note_types_per_minute(), t->minute()));
+                                               t->set_pulse (prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute()));
                                        }
                                }
                        }
@@ -2593,7 +2648,7 @@ TempoMap::solve_map_pulse (Metrics& imaginary, TempoSection* section, const doub
 
        if (section_prev) {
                section_prev->set_c_func (section_prev->compute_c_func_pulse (section->note_types_per_minute(), pulse));
-               section->set_minute (section_prev->minute_at_tempo (section->note_types_per_minute(), pulse));
+               section->set_minute (section_prev->minute_at_ntpm (section->note_types_per_minute(), pulse));
        }
 
 #if (0)
@@ -3205,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)));
                }
 
@@ -3352,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);
 
@@ -3360,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);
 }
@@ -3388,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);
 
@@ -3396,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));
 
@@ -3437,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) {
@@ -3454,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;
@@ -3872,9 +3945,10 @@ 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)) / ts_at->note_type()) * 4.0);
+               return  (60.0 * _frame_rate) / ts_at->tempo_at_minute (minute_at_frame (frame)).quarter_notes_per_minute();
        }
        /* must be treated as constant tempo */
        return ts_at->frames_per_quarter_note (_frame_rate);
@@ -4154,9 +4228,9 @@ TempoMap::dump (const Metrics& metrics, std::ostream& o) const
                                o << "  previous     : " << prev_t->note_types_per_minute()
                                  << " | " << prev_t->pulse() << " | " << prev_t->frame() << " | " << prev_t->minute() << std::endl;
                                o << "  calculated   : " << prev_t->tempo_at_pulse (t->pulse())
-                                 << " | " << prev_t->pulse_at_tempo (t->note_types_per_minute(), t->minute())
-                                 << " | " << frame_at_minute (prev_t->minute_at_tempo (t->note_types_per_minute(), t->pulse()))
-                                 << " | " << prev_t->minute_at_tempo (t->note_types_per_minute(), t->pulse()) << std::endl;
+                                 << " | " << prev_t->pulse_at_ntpm (t->note_types_per_minute(), t->minute())
+                                 << " | " << frame_at_minute (prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()))
+                                 << " | " << prev_t->minute_at_ntpm (t->note_types_per_minute(), t->pulse()) << std::endl;
                        }
                        prev_t = t;
                } else if ((m = dynamic_cast<const MeterSection*>(*i)) != 0) {
@@ -4284,11 +4358,12 @@ TempoMap::remove_time (framepos_t where, framecnt_t amount)
  *  pos can be -ve, if required.
  */
 framepos_t
-TempoMap::framepos_plus_qn (framepos_t frame, Evoral::Beats quarter_note) const
+TempoMap::framepos_plus_qn (framepos_t frame, Evoral::Beats beats) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
+       const double frame_qn = quarter_notes_between_frames_locked (_metrics, 0, frame);
 
-       return frame_at_minute (minute_at_quarter_note_locked (_metrics, quarter_note_at_minute_locked (_metrics, minute_at_frame (frame)) + quarter_note.to_double()));
+       return frame_at_minute (minute_at_quarter_note_locked (_metrics, frame_qn + beats.to_double()));
 }
 
 framepos_t
@@ -4323,7 +4398,7 @@ TempoMap::framewalk_to_qn (framepos_t pos, framecnt_t distance) const
 {
        Glib::Threads::RWLock::ReaderLock lm (lock);
 
-       return Evoral::Beats (quarter_note_at_minute_locked (_metrics, minute_at_frame (pos + distance)) - quarter_note_at_minute_locked (_metrics, minute_at_frame (pos)));
+       return Evoral::Beats (quarter_notes_between_frames_locked (_metrics, pos, pos + distance));
 }
 
 struct bbtcmp {