Consistent AU factory Preset IDs
[ardour.git] / libs / ardour / mtc_slave.cc
index 7a6d6a6ed6f481e0d931ec0ed8526cb93eb45266..f8afe66d93d6986d59a3a5a91ff2155bd4f31c26 100644 (file)
@@ -32,8 +32,6 @@
 #include "ardour/session.h"
 #include "ardour/transport_master.h"
 
-#include <glibmm/timer.h>
-
 #include "pbd/i18n.h"
 
 using namespace std;
@@ -134,6 +132,8 @@ MTC_TransportMaster::pre_process (MIDI::pframes_t nframes, samplepos_t now, boos
 {
        /* Read and parse incoming MIDI */
 
+       maybe_reset ();
+
        _midi_port->read_and_parse_entire_midi_buffer_with_no_speed_adjustment (nframes, parser, now);
 
        if (session_pos) {
@@ -164,6 +164,16 @@ MTC_TransportMaster::parameter_changed (std::string const & p)
        }
 }
 
+ARDOUR::samplecnt_t
+MTC_TransportMaster::update_interval() const
+{
+       if (timecode.rate) {
+               return AudioEngine::instance()->sample_rate() / timecode.rate;
+       }
+
+       return AudioEngine::instance()->sample_rate(); /* useless but what other answer is there? */
+}
+
 ARDOUR::samplecnt_t
 MTC_TransportMaster::resolution () const
 {
@@ -224,18 +234,9 @@ MTC_TransportMaster::reset (bool with_position)
        DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC_TransportMaster reset %1\n", with_position?"with position":"without position"));
 
        if (with_position) {
-               last_inbound_frame = 0;
-               current.guard1++;
-               current.position = 0;
-               current.timestamp = 0;
-               current.speed = 0;
-               current.guard2++;
+               current.update (0, 0, 0);
        } else {
-               last_inbound_frame = 0;
-               current.guard1++;
-               current.timestamp = 0;
-               current.speed = 0;
-               current.guard2++;
+               current.update (current.position, 0, 0);
        }
        first_mtc_timestamp = 0;
        window_begin = 0;
@@ -259,23 +260,6 @@ MTC_TransportMaster::handle_locate (const MIDI::byte* mmc_tc)
        update_mtc_time (mtc, true, 0);
 }
 
-void
-MTC_TransportMaster::read_current (SafeTime *st) const
-{
-       int tries = 0;
-
-       do {
-               if (tries == 10) {
-                       error << _("MTC Slave: atomic read of current time failed, sleeping!") << endmsg;
-                       Glib::usleep (20);
-                       tries = 0;
-               }
-               *st = current;
-               tries++;
-
-       } while (st->guard1.load (boost::memory_order_acquire) != st->guard2.load (boost::memory_order_acquire));
-}
-
 void
 MTC_TransportMaster::init_mtc_dll(samplepos_t tme, double qtr)
 {
@@ -500,9 +484,6 @@ MTC_TransportMaster::update_mtc_time (const MIDI::byte *msg, bool was_full, samp
                }
        }
 
-       if (now) {
-               last_inbound_frame = now;
-       }
        busy_guard2++;
 }
 
@@ -568,62 +549,6 @@ MTC_TransportMaster::reset_window (samplepos_t root)
        DEBUG_TRACE (DEBUG::MTC, string_compose ("reset MTC window @ %3, now %1 .. %2\n", window_begin, window_end, root));
 }
 
-/* main entry point from session_process.cc
-xo * in process callback context */
-bool
-MTC_TransportMaster::speed_and_position (double& speed, samplepos_t& pos, samplepos_t now)
-{
-       SafeTime last;
-
-       if (!_collect) {
-               return false;
-       }
-
-       read_current (&last);
-
-       DEBUG_TRACE (DEBUG::MTC, string_compose ("speed&pos: timestamp %1 speed %2 dir %4 now %5 last-in %6\n",
-                                                last.timestamp,
-                                                last.speed,
-                                                transport_direction,
-                                                now,
-                                                last_inbound_frame));
-
-       if (last.timestamp == 0) {
-               return false;
-       }
-
-       if (last_inbound_frame && now > last_inbound_frame && now - last_inbound_frame > labs(seekahead_distance())) {
-               /* no timecode for two cycles - conclude that it's stopped */
-
-               if (!Config->get_transport_masters_just_roll_when_sync_lost()) {
-                       speed = 0;
-                       pos = last.position;
-                       _current_delta = 0;
-                       queue_reset (false);
-                       DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC not seen for 2 samples - reset pending, pos = %1\n", pos));
-                       return false;
-               }
-       }
-
-
-       DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC::speed_and_position mtc-tme: %1 mtc-pos: %2 mtc-spd: %3\n", last.timestamp, last.position, last.speed));
-
-       speed = last.speed;
-
-       /* provide a .1% deadzone to lock the speed */
-       if (fabs (speed - 1.0) <= 0.001) {
-               speed = 1.0;
-       }
-
-       pos =  last.position;
-       pos += (now - last.timestamp) * speed;
-
-       DEBUG_TRACE (DEBUG::MTC, string_compose ("MTCsync spd: %1 pos: %2 | last-pos: %3 | elapsed: %4\n",
-                                                speed, pos, last.position, (now - last.timestamp)));
-
-       return true;
-}
-
 Timecode::TimecodeFormat
 MTC_TransportMaster::apparent_timecode_format () const
 {
@@ -634,7 +559,7 @@ std::string
 MTC_TransportMaster::position_string() const
 {
        SafeTime last;
-       read_current (&last);
+       current.safe_read (last);
        if (last.timestamp == 0 || reset_pending) {
                return " --:--:--:--";
        }
@@ -650,7 +575,7 @@ MTC_TransportMaster::delta_string () const
 {
        char delta[80];
        SafeTime last;
-       read_current (&last);
+       current.safe_read (last);
 
        delta[0] = '\0';