allow linking unbundled versions of some libraries
[ardour.git] / libs / ardour / tempo.cc
index 9a4877c451055b0cd18b77578a8e60c36fdbc627..0bb2fea0cf3e0c6a22a1f25a07a916bdbab49045 100644 (file)
 
 #include <algorithm>
 #include <stdexcept>
+#include <cmath>
 
 #include <unistd.h>
 
-#include <cmath>
-
-#include <glibmm/thread.h>
+#include <glibmm/threads.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>
@@ -45,12 +43,6 @@ using Timecode::BBT_Time;
 Meter    TempoMap::_default_meter (4.0, 4.0);
 Tempo    TempoMap::_default_tempo (120.0);
 
-double 
-Tempo::frames_per_beat (framecnt_t sr) const
-{
-       return  (60.0 * sr) / _beats_per_minute;
-}
-
 /***********************************************************************/
 
 double 
@@ -313,7 +305,7 @@ TempoMap::remove_tempo (const TempoSection& tempo, bool complete_operation)
        bool removed = false;
 
        {
-               Glib::RWLock::WriterLock lm (lock);
+               Glib::Threads::RWLock::WriterLock lm (lock);
                Metrics::iterator i;
 
                for (i = metrics.begin(); i != metrics.end(); ++i) {
@@ -344,7 +336,7 @@ TempoMap::remove_meter (const MeterSection& tempo, bool complete_operation)
        bool removed = false;
 
        {
-               Glib::RWLock::WriterLock lm (lock);
+               Glib::Threads::RWLock::WriterLock lm (lock);
                Metrics::iterator i;
 
                for (i = metrics.begin(); i != metrics.end(); ++i) {
@@ -490,7 +482,7 @@ TempoMap::replace_tempo (const TempoSection& ts, const Tempo& tempo, const BBT_T
                add_tempo (tempo, where);
        } else {
                {
-                       Glib::RWLock::WriterLock lm (lock);
+                       Glib::Threads::RWLock::WriterLock lm (lock);
                        /* cannot move the first tempo section */
                        *((Tempo*)&first) = tempo;
                        recompute_map (false);
@@ -504,7 +496,7 @@ void
 TempoMap::add_tempo (const Tempo& tempo, BBT_Time where)
 {
        {
-               Glib::RWLock::WriterLock lm (lock);
+               Glib::Threads::RWLock::WriterLock lm (lock);
 
                /* new tempos always start on a beat */
                where.ticks = 0;
@@ -560,7 +552,7 @@ TempoMap::replace_meter (const MeterSection& ms, const Meter& meter, const BBT_T
                add_meter (meter, where);
        } else {
                {
-                       Glib::RWLock::WriterLock lm (lock);
+                       Glib::Threads::RWLock::WriterLock lm (lock);
                        /* cannot move the first meter section */
                        *((Meter*)&first) = meter;
                        recompute_map (true);
@@ -574,7 +566,7 @@ void
 TempoMap::add_meter (const Meter& meter, BBT_Time where)
 {
        {
-               Glib::RWLock::WriterLock lm (lock);
+               Glib::Threads::RWLock::WriterLock lm (lock);
 
                /* a new meter always starts a new bar on the first beat. so
                   round the start time appropriately. remember that
@@ -614,7 +606,7 @@ TempoMap::change_initial_tempo (double beats_per_minute, double note_type)
        for (Metrics::iterator i = metrics.begin(); i != metrics.end(); ++i) {
                if ((t = dynamic_cast<TempoSection*> (*i)) != 0) {
                        { 
-                               Glib::RWLock::WriterLock lm (lock);
+                               Glib::Threads::RWLock::WriterLock lm (lock);
                                *((Tempo*) t) = newtempo;
                                recompute_map (false);
                        }
@@ -664,7 +656,7 @@ TempoMap::change_existing_tempo_at (framepos_t where, double beats_per_minute, d
        /* reset */
 
        {
-               Glib::RWLock::WriterLock lm (lock);
+               Glib::Threads::RWLock::WriterLock lm (lock);
                /* cannot move the first tempo section */
                *((Tempo*)prev) = newtempo;
                recompute_map (false);
@@ -708,7 +700,7 @@ TempoMap::first_tempo () const
 void
 TempoMap::require_map_to (framepos_t pos)
 {
-       Glib::RWLock::WriterLock lm (lock);
+       Glib::Threads::RWLock::WriterLock lm (lock);
 
        if (_map.empty() || _map.back().frame < pos) {
                extend_map (pos);
@@ -718,7 +710,7 @@ TempoMap::require_map_to (framepos_t pos)
 void
 TempoMap::require_map_to (const BBT_Time& bbt)
 {
-       Glib::RWLock::WriterLock lm (lock);
+       Glib::Threads::RWLock::WriterLock lm (lock);
 
        /* since we have no idea where BBT is if its off the map, see the last
         * point in the map is past BBT, and if not add an arbitrary amount of
@@ -922,7 +914,7 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
 
                        if (!(current < (*next_metric)->start())) {
 
-                         set_metrics:
+               set_metrics:
                                if (((ts = dynamic_cast<TempoSection*> (*next_metric)) != 0)) {
 
                                        tempo = ts;
@@ -947,7 +939,7 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                                                double next_beat_frames = tempo->frames_per_beat (_frame_rate);                                 
                                                
                                                DEBUG_TRACE (DEBUG::TempoMath, string_compose ("bumped into non-beat-aligned tempo metric at %1 = %2, adjust next beat using %3\n",
-                                                                                              tempo->start(), current_frame, tempo->bar_offset()));
+                                                                                              tempo->start(), current_frame, tempo->bar_offset()));
                                                
                                                /* back up to previous beat */
                                                current_frame -= beat_frames;
@@ -957,7 +949,7 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                                                 * bar start 
                                                 */
                                                tempo->set_frame (bar_start_frame + 
-                                                                 llrint ((ts->bar_offset() * meter->divisions_per_bar() * beat_frames)));
+                                                                 llrint ((ts->bar_offset() * meter->divisions_per_bar() * beat_frames)));
                                                
                                                /* advance to the location of
                                                 * the new (adjusted) beat. do
@@ -982,7 +974,7 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                                        } else {
                                                
                                                DEBUG_TRACE (DEBUG::TempoMath, string_compose ("bumped into beat-aligned tempo metric at %1 = %2\n",
-                                                                                              tempo->start(), current_frame));
+                                                                                              tempo->start(), current_frame));
                                                tempo->set_frame (current_frame);
                                        }
 
@@ -995,7 +987,7 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                                         */
                                        
                                        DEBUG_TRACE (DEBUG::TempoMath, string_compose ("bumped into meter section at %1 vs %2 (%3)\n",
-                                                                                      meter->start(), current, current_frame));
+                                                                                      meter->start(), current, current_frame));
                                        
                                        assert (current.beats == 1);
 
@@ -1005,15 +997,16 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                                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, meter->divisions_per_bar(), *((Meter*)meter), *((Tempo*)tempo)));
+                                                                              beat_frames, meter->divisions_per_bar(), *((Meter*)meter), *((Tempo*)tempo)));
                        
                                ++next_metric;
 
                                if (next_metric != metrics.end() && ((*next_metric)->start() == current)) {
                                        /* same position so go back and set this one up before advancing
-                                       */
+                                        */
                                        goto set_metrics;
                                }
+
                        }
                } 
 
@@ -1030,7 +1023,7 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
                        /* 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));
+                                                                              current.bars, current.beats, current_frame));
                                break;
                        }
                }
@@ -1038,12 +1031,10 @@ TempoMap::_extend_map (TempoSection* tempo, MeterSection* meter,
 }
 
 TempoMetric
-TempoMap::metric_at (framepos_t frame) const
+TempoMap::metric_at (framepos_t frame, Metrics::const_iterator* last) const
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        TempoMetric m (first_meter(), first_tempo());
-       const Meter* meter;
-       const Tempo* tempo;
 
        /* at this point, we are *guaranteed* to have m.meter and m.tempo pointing
           at something, because we insert the default tempo and meter during
@@ -1058,14 +1049,11 @@ TempoMap::metric_at (framepos_t frame) const
                        break;
                }
 
-               if ((tempo = dynamic_cast<const TempoSection*>(*i)) != 0) {
-                       m.set_tempo (*tempo);
-               } else if ((meter = dynamic_cast<const MeterSection*>(*i)) != 0) {
-                       m.set_meter (*meter);
-               }
+               m.set_metric(*i);
 
-               m.set_frame ((*i)->frame ());
-               m.set_start ((*i)->start ());
+               if (last) {
+                       *last = i;
+               }
        }
        
        return m;
@@ -1074,10 +1062,8 @@ TempoMap::metric_at (framepos_t frame) const
 TempoMetric
 TempoMap::metric_at (BBT_Time bbt) const
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        TempoMetric m (first_meter(), first_tempo());
-       const Meter* meter;
-       const Tempo* tempo;
 
        /* at this point, we are *guaranteed* to have m.meter and m.tempo pointing
           at something, because we insert the default tempo and meter during
@@ -1094,14 +1080,7 @@ TempoMap::metric_at (BBT_Time bbt) const
                        break;
                }
 
-               if ((tempo = dynamic_cast<const TempoSection*>(*i)) != 0) {
-                       m.set_tempo (*tempo);
-               } else if ((meter = dynamic_cast<const MeterSection*>(*i)) != 0) {
-                       m.set_meter (*meter);
-               }
-
-               m.set_frame ((*i)->frame ());
-               m.set_start (section_start);
+               m.set_metric (*i);
        }
 
        return m;
@@ -1112,7 +1091,7 @@ TempoMap::bbt_time (framepos_t frame, BBT_Time& bbt)
 {
        require_map_to (frame);
 
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
 
        if (frame < 0) {
                bbt.bars = 1;
@@ -1128,7 +1107,7 @@ TempoMap::bbt_time (framepos_t frame, BBT_Time& bbt)
 void
 TempoMap::bbt_time_rt (framepos_t frame, BBT_Time& bbt)
 {
-       Glib::RWLock::ReaderLock lm (lock, Glib::TRY_LOCK);
+       Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
 
        if (!lm.locked()) {
                throw std::logic_error ("TempoMap::bbt_time_rt() could not lock tempo map");
@@ -1153,7 +1132,7 @@ TempoMap::bbt_time (framepos_t frame, BBT_Time& bbt, const BBTPointList::const_i
                bbt.ticks = 0;
        } else {
                bbt.ticks = llrint (((frame - (*i).frame) / (*i).tempo->frames_per_beat(_frame_rate)) *
-                                   BBT_Time::ticks_per_beat);
+                                   BBT_Time::ticks_per_beat);
        }
 }
 
@@ -1171,7 +1150,7 @@ TempoMap::frame_time (const BBT_Time& bbt)
 
        require_map_to (bbt);
 
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
 
        BBTPointList::const_iterator s = bbt_before_or_at (BBT_Time (1, 1, 0));
        BBTPointList::const_iterator e = bbt_before_or_at (BBT_Time (bbt.bars, bbt.beats, 0));
@@ -1187,18 +1166,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::Threads::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;
@@ -1207,16 +1183,9 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
        /* round back to the previous precise beat */
        BBTPointList::const_iterator wi = bbt_before_or_at (BBT_Time (when.bars, when.beats, 0));
        BBTPointList::const_iterator start (wi);
-       double tick_frames = 0;
 
        assert (wi != _map.end());
 
-       /* compute how much rounding we did because of non-zero ticks */
-
-       if (when.ticks != 0) {
-               tick_frames = (*wi).tempo->frames_per_beat (_frame_rate) * (when.ticks/BBT_Time::ticks_per_beat);
-       }
-       
        uint32_t bars = 0;
        uint32_t beats = 0;
 
@@ -1237,10 +1206,11 @@ TempoMap::bbt_duration_at_unlocked (const BBT_Time& when, const BBT_Time& bbt, i
        /* add any additional frames related to ticks in the added value */
 
        if (bbt.ticks != 0) {
-               tick_frames += (*wi).tempo->frames_per_beat (_frame_rate) * (bbt.ticks/BBT_Time::ticks_per_beat);
+               return ((*wi).frame - (*start).frame) + 
+                       (*wi).tempo->frames_per_beat (_frame_rate) * (bbt.ticks/BBT_Time::ticks_per_beat);
+       } else {
+               return ((*wi).frame - (*start).frame);
        }
-
-       return ((*wi).frame - (*start).frame) + llrint (tick_frames);
 }
 
 framepos_t
@@ -1260,7 +1230,7 @@ TempoMap::round_to_beat_subdivision (framepos_t fr, int sub_num, int dir)
 {
        require_map_to (fr);
 
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        BBTPointList::const_iterator i = bbt_before_or_at (fr);
        BBT_Time the_beat;
        uint32_t ticks_one_subdivisions_worth;
@@ -1379,7 +1349,7 @@ TempoMap::round_to_type (framepos_t frame, int dir, BBTPointType type)
 {
        require_map_to (frame);
 
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        BBTPointList::const_iterator fi;
 
        if (dir > 0) {
@@ -1525,7 +1495,7 @@ TempoMap::get_grid (TempoMap::BBTPointList::const_iterator& begin,
                    framepos_t lower, framepos_t upper) 
 {
        { 
-               Glib::RWLock::WriterLock lm (lock);
+               Glib::Threads::RWLock::WriterLock lm (lock);
                if (_map.empty() || (_map.back().frame < upper)) {
                        recompute_map (false, upper);
                }
@@ -1538,7 +1508,7 @@ TempoMap::get_grid (TempoMap::BBTPointList::const_iterator& begin,
 const TempoSection&
 TempoMap::tempo_section_at (framepos_t frame) const
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        Metrics::const_iterator i;
        TempoSection* prev = 0;
 
@@ -1584,7 +1554,7 @@ TempoMap::get_state ()
        XMLNode *root = new XMLNode ("TempoMap");
 
        {
-               Glib::RWLock::ReaderLock lm (lock);
+               Glib::Threads::RWLock::ReaderLock lm (lock);
                for (i = metrics.begin(); i != metrics.end(); ++i) {
                        root->add_child_nocopy ((*i)->get_state());
                }
@@ -1597,7 +1567,7 @@ int
 TempoMap::set_state (const XMLNode& node, int /*version*/)
 {
        {
-               Glib::RWLock::WriterLock lm (lock);
+               Glib::Threads::RWLock::WriterLock lm (lock);
 
                XMLNodeList nlist;
                XMLNodeConstIterator niter;
@@ -1650,6 +1620,28 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
                        metrics.sort (cmp);
                }
 
+               /* check for multiple tempo/meters at the same location, which
+                  ardour2 somehow allowed.
+               */
+
+               Metrics::iterator prev = metrics.end();
+               for (Metrics::iterator i = metrics.begin(); i != metrics.end(); ++i) {
+                       if (prev != metrics.end()) {
+                               if (dynamic_cast<MeterSection*>(*prev) && dynamic_cast<MeterSection*>(*i)) {
+                                       if ((*prev)->start() == (*i)->start()) {
+                                               error << string_compose (_("Multiple meter definitions found at %1"), (*prev)->start()) << endmsg;
+                                               return -1;
+                                       }
+                               } else if (dynamic_cast<TempoSection*>(*prev) && dynamic_cast<TempoSection*>(*i)) {
+                                       if ((*prev)->start() == (*i)->start()) {
+                                               error << string_compose (_("Multiple tempo definitions found at %1"), (*prev)->start()) << endmsg;
+                                               return -1;
+                                       }
+                               }
+                       }
+                       prev = i;
+               }
+
                recompute_map (true, -1);
        }
 
@@ -1661,7 +1653,7 @@ TempoMap::set_state (const XMLNode& node, int /*version*/)
 void
 TempoMap::dump (std::ostream& o) const
 {
-       Glib::RWLock::ReaderLock lm (lock, Glib::TRY_LOCK);
+       Glib::Threads::RWLock::ReaderLock lm (lock, Glib::Threads::TRY_LOCK);
        const MeterSection* m;
        const TempoSection* t;
 
@@ -1680,7 +1672,7 @@ TempoMap::dump (std::ostream& o) const
 int
 TempoMap::n_tempos() const
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        int cnt = 0;
 
        for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
@@ -1695,7 +1687,7 @@ TempoMap::n_tempos() const
 int
 TempoMap::n_meters() const
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        int cnt = 0;
 
        for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
@@ -1711,7 +1703,7 @@ void
 TempoMap::insert_time (framepos_t where, framecnt_t amount)
 {
        {
-               Glib::RWLock::WriterLock lm (lock);
+               Glib::Threads::RWLock::WriterLock lm (lock);
                for (Metrics::iterator i = metrics.begin(); i != metrics.end(); ++i) {
                        if ((*i)->frame() >= where && (*i)->movable ()) {
                                (*i)->set_frame ((*i)->frame() + amount);
@@ -1807,9 +1799,9 @@ TempoMap::insert_time (framepos_t where, framecnt_t amount)
 framepos_t
 TempoMap::framepos_plus_beats (framepos_t pos, Evoral::MusicalTime beats) const
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        Metrics::const_iterator next_tempo;
-       const TempoSection* tempo;
+       const TempoSection* tempo = 0;
 
        /* Find the starting tempo metric */
 
@@ -1844,8 +1836,9 @@ TempoMap::framepos_plus_beats (framepos_t pos, Evoral::MusicalTime beats) const
           next_tempo  -> first tempo after "pos", possibly metrics.end()
        */
 
-       DEBUG_TRACE (DEBUG::TempoMath, string_compose ("frame %1 plus %2 beats, start with tempo = %3 @ %4\n",
-                                                      pos, beats, *((Tempo*)tempo), tempo->frame()));
+       DEBUG_TRACE (DEBUG::TempoMath,
+                    string_compose ("frame %1 plus %2 beats, start with tempo = %3 @ %4\n",
+                                    pos, beats, *((const Tempo*)tempo), tempo->frame()));
 
        while (beats) {
 
@@ -1875,7 +1868,7 @@ TempoMap::framepos_plus_beats (framepos_t pos, Evoral::MusicalTime beats) const
                        tempo = dynamic_cast<const TempoSection*>(*next_tempo);
 
                        DEBUG_TRACE (DEBUG::TempoMath, string_compose ("\tnew tempo = %1 @ %2 fpb = %3\n",
-                                                                      *((Tempo*)tempo), tempo->frame(),
+                                                                      *((const Tempo*)tempo), tempo->frame(),
                                                                       tempo->frames_per_beat (_frame_rate)));
 
                        while (next_tempo != metrics.end ()) {
@@ -1892,11 +1885,11 @@ 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
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        Metrics::const_reverse_iterator prev_tempo;
        const TempoSection* tempo = 0;
 
@@ -1943,9 +1936,10 @@ TempoMap::framepos_minus_beats (framepos_t pos, Evoral::MusicalTime beats) const
                }
        }
 
-       DEBUG_TRACE (DEBUG::TempoMath, string_compose ("frame %1 minus %2 beats, start with tempo = %3 @ %4 prev at beg? %5\n",
-                                                      pos, beats, *((Tempo*)tempo), tempo->frame(),
-                                                      prev_tempo == metrics.rend()));
+       DEBUG_TRACE (DEBUG::TempoMath,
+                    string_compose ("frame %1 minus %2 beats, start with tempo = %3 @ %4 prev at beg? %5\n",
+                                    pos, beats, *((const Tempo*)tempo), tempo->frame(),
+                                    prev_tempo == metrics.rend()));
 
        /* We now have:
 
@@ -1980,9 +1974,10 @@ TempoMap::framepos_minus_beats (framepos_t pos, Evoral::MusicalTime beats) const
 
                        tempo = dynamic_cast<const TempoSection*>(*prev_tempo);
 
-                       DEBUG_TRACE (DEBUG::TempoMath, string_compose ("\tnew tempo = %1 @ %2 fpb = %3\n",
-                                                                      *((Tempo*)tempo), tempo->frame(),
-                                                                      tempo->frames_per_beat (_frame_rate)));
+                       DEBUG_TRACE (DEBUG::TempoMath,
+                                    string_compose ("\tnew tempo = %1 @ %2 fpb = %3\n",
+                                                    *((const Tempo*)tempo), tempo->frame(),
+                                                    tempo->frames_per_beat (_frame_rate)));
 
                        while (prev_tempo != metrics.rend ()) {
 
@@ -2005,7 +2000,7 @@ TempoMap::framepos_minus_beats (framepos_t pos, Evoral::MusicalTime beats) const
 framepos_t
 TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        Metrics::const_iterator i;
        const MeterSection* meter;
        const MeterSection* m;
@@ -2143,7 +2138,7 @@ TempoMap::framepos_plus_bbt (framepos_t pos, BBT_Time op) const
 Evoral::MusicalTime
 TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
 {
-       Glib::RWLock::ReaderLock lm (lock);
+       Glib::Threads::RWLock::ReaderLock lm (lock);
        Metrics::const_iterator next_tempo;
        const TempoSection* tempo = 0;
        framepos_t effective_pos = max (pos, (framepos_t) 0);
@@ -2172,18 +2167,27 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
 
        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()));
+       DEBUG_TRACE (DEBUG::TempoMath,
+                    string_compose ("frame %1 walk by %2 frames, start with tempo = %3 @ %4\n",
+                                    pos, distance, *((const 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);
@@ -2206,9 +2210,10 @@ TempoMap::framewalk_to_beats (framepos_t pos, framecnt_t distance) const
 
                        tempo = dynamic_cast<const TempoSection*>(*next_tempo);
 
-                       DEBUG_TRACE (DEBUG::TempoMath, string_compose ("\tnew tempo = %1 @ %2 fpb = %3\n",
-                                                                      *((Tempo*)tempo), tempo->frame(),
-                                                                      tempo->frames_per_beat (_frame_rate)));
+                       DEBUG_TRACE (DEBUG::TempoMath,
+                                    string_compose ("\tnew tempo = %1 @ %2 fpb = %3\n",
+                                                    *((const Tempo*)tempo), tempo->frame(),
+                                                    tempo->frames_per_beat (_frame_rate)));
 
                        while (next_tempo != metrics.end ()) {
 
@@ -2315,9 +2320,9 @@ operator<< (std::ostream& o, const MetricSection& section) {
        const MeterSection* ms;
 
        if ((ts = dynamic_cast<const TempoSection*> (&section)) != 0) {
-               o << *((Tempo*) ts);
+               o << *((const Tempo*) ts);
        } else if ((ms = dynamic_cast<const MeterSection*> (&section)) != 0) {
-               o << *((Meter*) ms);
+               o << *((const Meter*) ms);
        }
 
        return o;