assert() to help find some possible causes of #2991. Fix some confusion with GTK...
[ardour.git] / libs / ardour / tempo.cc
index b8144d6e3c27264877a168f625ec7120fcc25731..9dcce637e63d839481003c359ffe034e91047fb3 100644 (file)
@@ -24,7 +24,6 @@
 
 #include <cmath>
 
-#include <sigc++/bind.h>
 
 #include <glibmm/thread.h>
 #include "pbd/xml++.h"
@@ -1235,25 +1234,25 @@ TempoMap::round_to_type (nframes64_t frame, int dir, BBTPointType type)
 
                        /* "true" rounding */
 
-                       /* round to nearest beat */
-                       if (bbt.ticks >= (Meter::ticks_per_beat/2)) {
-                               try {
-                                       bbt = bbt_add (bbt, one_beat, metric);
-                               }
-                               catch (...) {
-                                       return frame;
-                               }
-                       } 
+                       float midbar_beats;
+                       float midbar_ticks;
 
-                       /* round to nearest bar */
-                       if (bbt.beats >= metric.meter().beats_per_bar()/2) {
-                               try {
-                                       bbt = bbt_add (bbt, one_bar, metric);
-                               }
-                               catch (...) {
-                                       return frame;
-                               }
-                       } 
+                       midbar_beats = metric.meter().beats_per_bar() / 2;
+                       midbar_ticks = Meter::ticks_per_beat * fmod (midbar_beats, 1.0f);
+                       midbar_beats = floor (midbar_beats);
+                       
+                       BBT_Time midbar (bbt.bars, lrintf (midbar_beats), lrintf (midbar_ticks));
+
+                       if (bbt < midbar) {
+                               /* round down */
+                               bbt.beats = 1;
+                               bbt.ticks = 0;
+                       } else {
+                               /* round up */
+                               bbt.bars++;
+                               bbt.beats = 1;
+                               bbt.ticks = 0;
+                       }
                }
                /* force beats & ticks to their values at the start of a bar */
                bbt.beats = 1;
@@ -1684,11 +1683,14 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& other) const
        return bbt_add (start, other, metric);
 }
 
+/**
+ * add the BBT interval @param increment to  @param start and return the result
+ */
 BBT_Time
-TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& other, const TempoMetric& metric) const
+TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& increment, const TempoMetric& /*metric*/) const
 {
        BBT_Time result = start;
-       BBT_Time op = other;
+       BBT_Time op = increment; /* argument is const, but we need to modify it */
        uint32_t ticks = result.ticks + op.ticks;
 
        if (ticks >= Meter::ticks_per_beat) {
@@ -1780,11 +1782,14 @@ TempoMap::bbt_add (const BBT_Time& start, const BBT_Time& other, const TempoMetr
        return result;
 }
 
+/**
+ * subtract the BBT interval @param decrement from @param start and return the result
+ */
 BBT_Time
-TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& other) const
+TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& decrement) const
 {
        BBT_Time result = start;
-       BBT_Time op = other;
+       BBT_Time op = decrement; /* argument is const, but we need to modify it */
 
        if (op.ticks > result.ticks) {
                /* subtract an extra beat later; meanwhile set ticks to the right "carry" value */
@@ -1817,7 +1822,8 @@ TempoMap::bbt_subtract (const BBT_Time& start, const BBT_Time& other) const
        */
        
        const MeterSection* meter = 0;
-       list<const MeterSection*>::const_reverse_iterator next_meter;
+       list<const MeterSection*>::reverse_iterator next_meter; // older versions of GCC don't 
+                                                               // support const_reverse_iterator::operator!=()
        
        for (next_meter = meter_sections.rbegin(); next_meter != meter_sections.rend(); ++next_meter) {