Fix off-by-one in body_range().
[ardour.git] / libs / ardour / tempo.cc
index 137d25b217ff72a703f8a7a7482769d93fefe214..e64a1169333023bf72021c3f86b144cbe549d6d4 100644 (file)
 
 #include <algorithm>
 #include <stdexcept>
+#include <cmath>
 
 #include <unistd.h>
 
-#include <cmath>
-
 #include <glibmm/thread.h>
 #include "pbd/xml++.h"
 #include "evoral/types.hpp"
 #include "ardour/debug.h"
 #include "ardour/tempo.h"
-#include "ardour/utils.h"
 
 #include "i18n.h"
 #include <locale.h>
@@ -182,8 +180,8 @@ TempoSection::update_bbt_time_from_bar_offset (const Meter& meter)
        new_start.bars = start().bars;
        
        double ticks = BBT_Time::ticks_per_beat * meter.divisions_per_bar() * _bar_offset;
-       new_start.beats = (uint32_t) floor(ticks/BBT_Time::ticks_per_beat);
-       new_start.ticks = (uint32_t) fmod (ticks, BBT_Time::ticks_per_beat);
+       new_start.beats = (uint32_t) floor (ticks/BBT_Time::ticks_per_beat);
+       new_start.ticks = 0; /* (uint32_t) fmod (ticks, BBT_Time::ticks_per_beat); */
 
        /* remember the 1-based counting properties of beats */
        new_start.beats += 1;
@@ -750,12 +748,11 @@ TempoMap::recompute_map (bool reassign_tempo_bbt, framepos_t end)
 
        if (end < 0) {
 
-               if (_map.empty()) {
-                       /* compute 1 mins worth */
-                       end = _frame_rate * 60;
-               } else {
-                       end = _map.back().frame;
-               }
+               /* we will actually stop once we hit
+                  the last metric.
+               */
+               end = max_framepos;
+
        } else {
                if (!_map.empty ()) {
                        /* never allow the map to be shortened */
@@ -892,17 +889,17 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
 
        TempoSection* ts;
        MeterSection* ms;
-       double divisions_per_bar;
        double beat_frames;
        framepos_t bar_start_frame;
 
+       DEBUG_TRACE (DEBUG::TempoMath, string_compose ("Extend map to %1 from %2 = %3\n", end, current, current_frame));
+
        if (current.beats == 1) {
                bar_start_frame = current_frame;
        } else {
                bar_start_frame = 0;
        }
 
-       divisions_per_bar = meter->divisions_per_bar ();
        beat_frames = meter->frames_per_grid (*tempo,_frame_rate);
 
        while (current_frame < end) {
@@ -1003,11 +1000,10 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                                        meter->set_frame (current_frame);
                                }
                                
-                               divisions_per_bar = meter->divisions_per_bar ();
                                beat_frames = meter->frames_per_grid (*tempo, _frame_rate);
                                
                                DEBUG_TRACE (DEBUG::TempoMath, string_compose ("New metric with beat frames = %1 dpb %2 meter %3 tempo %4\n", 
-                                                                              beat_frames, divisions_per_bar, *((Meter*)meter), *((Tempo*)tempo)));
+                                                                              beat_frames, meter->divisions_per_bar(), *((Meter*)meter), *((Tempo*)tempo)));
                        
                                ++next_metric;
 
@@ -1017,7 +1013,7 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                                        goto set_metrics;
                                }
                        }
-               }
+               } 
 
                if (current.beats == 1) {
                        DEBUG_TRACE (DEBUG::TempoMath, string_compose ("Add Bar at %1|1 @ %2\n", current.bars, current_frame));
@@ -1027,6 +1023,15 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                        DEBUG_TRACE (DEBUG::TempoMath, string_compose ("Add Beat at %1|%2 @ %3\n", current.bars, current.beats, current_frame));
                        _map.push_back (BBTPoint (*meter, *tempo, (framepos_t) llrint(current_frame), current.bars, current.beats));
                }
+
+               if (next_metric == metrics.end()) {
+                       /* no more metrics - we've timestamped them all, stop here */
+                       if (end == max_framepos) {
+                               DEBUG_TRACE (DEBUG::TempoMath, string_compose ("stop extending map now that we've reach the end @ %1|%2 = %3\n",
+                                                                              current.bars, current.beats, current_frame));
+                               break;
+                       }
+               }
        }
 }
 
@@ -1106,6 +1111,15 @@ TempoMap::bbt_time (framepos_t frame, BBT_Time& bbt)
        require_map_to (frame);
 
        Glib::RWLock::ReaderLock lm (lock);
+
+       if (frame < 0) {
+               bbt.bars = 1;
+               bbt.beats = 1;
+               bbt.ticks = 0;
+               warning << string_compose (_("tempo map asked for BBT time at frame %1\n"), frame) << endmsg;
+               return;
+       }
+
        return bbt_time (frame, bbt, bbt_before_or_at (frame));
 }
 
@@ -1144,6 +1158,15 @@ TempoMap::bbt_time (framepos_t frame, BBT_Time& bbt, const BBTPointList::const_i
 framepos_t
 TempoMap::frame_time (const BBT_Time& bbt)
 {
+       if (bbt.bars < 1) {
+               warning << string_compose (_("tempo map asked for frame time at bar < 1  (%1)\n"), bbt) << endmsg;
+               return 0;
+       }
+       
+       if (bbt.beats < 1) {
+               throw std::logic_error ("beats are counted from one");
+       }
+
        require_map_to (bbt);
 
        Glib::RWLock::ReaderLock lm (lock);
@@ -1162,18 +1185,15 @@ TempoMap::frame_time (const BBT_Time& bbt)
 framecnt_t
 TempoMap::bbt_duration_at (framepos_t pos, const BBT_Time& bbt, int dir)
 {
-       Glib::RWLock::ReaderLock lm (lock);
-       framecnt_t frames = 0;
        BBT_Time when;
-
        bbt_time (pos, when);
-       frames = bbt_duration_at_unlocked (when, bbt,dir);
-
-       return frames;
+       
+       Glib::RWLock::ReaderLock lm (lock);
+       return bbt_duration_at_unlocked (when, bbt, dir);
 }
 
 framecnt_t
-TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int dir
+TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, int /*dir*/
 {
        if (bbt.bars == 0 && bbt.beats == 0 && bbt.ticks == 0) {
                return 0;
@@ -1578,7 +1598,6 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                XMLNodeConstIterator niter;
                Metrics old_metrics (metrics);
                MeterSection* last_meter = 0;
-
                metrics.clear();
 
                nlist = node.children();
@@ -1626,7 +1645,7 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                        metrics.sort (cmp);
                }
 
-               recompute_map (true);
+               recompute_map (true, -1);
        }
 
        PropertyChanged (PropertyChange ());
@@ -1785,7 +1804,7 @@ TempoMap::framepos_plus_beats (framepos_t pos, Evoral::MusicalTime beats) const
 {
        Glib::RWLock::ReaderLock lm (lock);
        Metrics::const_iterator next_tempo;
-       const TempoSection* tempo;
+       const TempoSection* tempo = 0;
 
        /* Find the starting tempo metric */
 
@@ -1868,7 +1887,7 @@ TempoMap::framepos_plus_beats (framepos_t pos, Evoral::MusicalTime beats) const
        return pos;
 }
 
-/** Subtract some (fractional) beats to a frame position, and return the result in frames */
+/** Subtract some (fractional) beats from a frame position, and return the result in frames */
 framepos_t
 TempoMap::framepos_minus_beats (framepos_t pos, Evoral::MusicalTime beats) const
 {
@@ -1988,6 +2007,7 @@ TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
        const TempoSection* tempo;
        const TempoSection* t;
        double frames_per_beat;
+       framepos_t effective_pos = max (pos, (framepos_t) 0);
 
        meter = &first_meter ();
        tempo = &first_tempo ();
@@ -1999,7 +2019,7 @@ TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
 
        for (i = metrics.begin(); i != metrics.end(); ++i) {
 
-               if ((*i)->frame() > pos) {
+               if ((*i)->frame() > effective_pos) {
                        break;
                }
 
@@ -2120,8 +2140,9 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
 {
        Glib::RWLock::ReaderLock lm (lock);
        Metrics::const_iterator next_tempo;
-       const TempoSection* tempo;
-       
+       const TempoSection* tempo = 0;
+       framepos_t effective_pos = max (pos, (framepos_t) 0);
+
        /* Find the relevant initial tempo metric  */
 
        for (next_tempo = metrics.begin(); next_tempo != metrics.end(); ++next_tempo) {
@@ -2130,7 +2151,7 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
 
                if ((t = dynamic_cast<const TempoSection*>(*next_tempo)) != 0) {
 
-                       if ((*next_tempo)->frame() > pos) {
+                       if ((*next_tempo)->frame() > effective_pos) {
                                break;
                        }
 
@@ -2144,24 +2165,44 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
           next_tempo -> the next tempo after "pos", possibly metrics.end()
        */
 
+       assert (tempo);
+
+       DEBUG_TRACE (DEBUG::TempoMath, string_compose ("frame %1 walk by %2 frames, start with tempo = %3 @ %4\n",
+                                                      pos, distance, *((Tempo*)tempo), tempo->frame()));
+       
        Evoral::MusicalTime beats = 0;
 
        while (distance) {
 
                /* End of this section */
-               framepos_t const end = ((next_tempo == metrics.end()) ? max_framepos : (*next_tempo)->frame ());
-
-               /* Distance to the end in frames */
-               framecnt_t const distance_to_end = end - pos;
+               framepos_t end;
+               /* Distance to `end' in frames */
+               framepos_t distance_to_end;
+
+               if (next_tempo == metrics.end ()) {
+                       /* We can't do (end - pos) if end is max_framepos, as it will overflow if pos is -ve */
+                       end = max_framepos;
+                       distance_to_end = max_framepos;
+               } else {
+                       end = (*next_tempo)->frame ();
+                       distance_to_end = end - pos;
+               }
 
                /* Amount to subtract this time */
                double const sub = min (distance, distance_to_end);
 
+               DEBUG_TRACE (DEBUG::TempoMath, string_compose ("to reach end at %1 (end ? %2), distance= %3 sub=%4\n", end, (next_tempo == metrics.end()),
+                                                              distance_to_end, sub));
+
                /* Update */
                pos += sub;
                distance -= sub;
+               assert (tempo);
                beats += sub / tempo->frames_per_beat (_frame_rate);
-               
+
+               DEBUG_TRACE (DEBUG::TempoMath, string_compose ("now at %1, beats = %2 distance left %3\n",
+                                                              pos, beats, distance));
+
                /* Move on if there's anything to move to */
 
                if (next_tempo != metrics.end()) {
@@ -2180,7 +2221,16 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
                                        break;
                                }
                        }
+
+                       if (next_tempo == metrics.end()) {
+                               DEBUG_TRACE (DEBUG::TempoMath, "no more tempo sections\n");
+                       } else {
+                               DEBUG_TRACE (DEBUG::TempoMath, string_compose ("next tempo section is %1 @ %2\n",
+                                                                              **next_tempo, (*next_tempo)->frame()));
+                       }
+
                }
+               assert (tempo);
        }
 
        return beats;
@@ -2193,6 +2243,13 @@ TempoMap::bbt_before_or_at (framepos_t pos)
 
        BBTPointList::const_iterator i;
 
+       if (pos < 0) {
+               /* not really correct, but we should catch pos < 0 at a higher
+                  level 
+               */
+               return _map.begin();
+       }
+
        i = lower_bound (_map.begin(), _map.end(), pos);
        assert (i != _map.end());
        if ((*i).frame > pos) {