Fix AU preset handling
[ardour.git] / libs / ardour / ltc_slave.cc
index b4c4a1446bfde6e091949abaa72bc978aadbcc87..9f8411103f80ca0569177b920bd8d1d0502decda 100644 (file)
 */
 #include <iostream>
 #include <errno.h>
-#include <poll.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #include "pbd/error.h"
+#include "pbd/failed_constructor.h"
+#include "pbd/pthread_utils.h"
 
 #include "ardour/debug.h"
-#include "ardour/slave.h"
+#include "ardour/profile.h"
+#include "ardour/transport_master.h"
 #include "ardour/session.h"
 #include "ardour/audioengine.h"
 #include "ardour/audio_port.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
@@ -39,60 +41,99 @@ using namespace MIDI;
 using namespace PBD;
 using namespace Timecode;
 
-#define FLYWHEEL_TIMEOUT ( 1 * session.frame_rate() )
+#define ENGINE AudioEngine::instance()
+#define FLYWHEEL_TIMEOUT ( 1 * ENGINE->sample_rate() )
+
+/* XXX USE Config->get_ltc_input */
+
+LTC_TransportMaster::LTC_TransportMaster (std::string const & name)
+       : TimecodeTransportMaster (name, LTC)
+       , did_reset_tc_format (false)
+       , decoder (0)
+       , samples_per_ltc_frame (0)
+       , fps_detected (false)
+       , monotonic_cnt (0)
+       , delayedlocked (10)
+       , ltc_detect_fps_cnt (0)
+       , ltc_detect_fps_max (0)
+       , sync_lock_broken (false)
+{
+       if ((_port = AudioEngine::instance()->register_input_port (DataType::AUDIO, string_compose ("%1 in", _name))) == 0) {
+               throw failed_constructor();
+       }
+
+       DEBUG_TRACE (DEBUG::Slave, string_compose ("LTC registered %1\n", _port->name()));
+
+       memset(&prev_sample, 0, sizeof(LTCFrameExt));
+
+       resync_latency();
 
-LTC_Slave::LTC_Slave (Session& s)
-       : session (s)
+       AudioEngine::instance()->Xrun.connect_same_thread (port_connections, boost::bind (&LTC_TransportMaster::resync_xrun, this));
+       AudioEngine::instance()->GraphReordered.connect_same_thread (port_connections, boost::bind (&LTC_TransportMaster::resync_latency, this));
+}
+
+void
+LTC_TransportMaster::init ()
 {
-       frames_per_ltc_frame = session.frames_per_timecode_frame();
-       timecode.rate = session.timecode_frames_per_second();
-       timecode.drop  = session.timecode_drop_frames();
+       reset (true);
+}
 
-       did_reset_tc_format = false;
-       delayedlocked = 10;
-       monotonic_cnt = 0;
-       fps_detected=false;
+void
+LTC_TransportMaster::set_session (Session *s)
+{
+       config_connection.disconnect ();
+       _session = s;
 
-       ltc_timecode = session.config.get_timecode_format();
-       a3e_timecode = session.config.get_timecode_format();
-       printed_timecode_warning = false;
-       ltc_detect_fps_cnt = ltc_detect_fps_max = 0;
-       memset(&prev_frame, 0, sizeof(LTCFrameExt));
+       if (_session) {
 
-       decoder = ltc_decoder_create((int) frames_per_ltc_frame, 128 /*queue size*/);
+               samples_per_ltc_frame = _session->samples_per_timecode_frame();
+               timecode.rate = _session->timecode_frames_per_second();
+               timecode.drop  = _session->timecode_drop_frames();
+               printed_timecode_warning = false;
+               ltc_timecode = _session->config.get_timecode_format();
+               a3e_timecode = _session->config.get_timecode_format();
 
-       session.config.ParameterChanged.connect_same_thread (config_connection, boost::bind (&LTC_Slave::parameter_changed, this, _1));
-       parse_timecode_offset();
-       reset();
-       resync_latency();
-       session.Xrun.connect_same_thread (port_connections, boost::bind (&LTC_Slave::resync_xrun, this));
-       session.engine().GraphReordered.connect_same_thread (port_connections, boost::bind (&LTC_Slave::resync_latency, this));
+               if (Config->get_use_session_timecode_format() && _session) {
+                       samples_per_timecode_frame = _session->samples_per_timecode_frame();
+               }
+
+               if (decoder) {
+                       ltc_decoder_free (decoder);
+               }
+
+               decoder = ltc_decoder_create((int) samples_per_ltc_frame, 128 /*queue size*/);
+
+               parse_timecode_offset();
+               reset();
+
+               _session->config.ParameterChanged.connect_same_thread (config_connection, boost::bind (&LTC_TransportMaster::parameter_changed, this, _1));
+       }
 }
 
-LTC_Slave::~LTC_Slave()
+LTC_TransportMaster::~LTC_TransportMaster()
 {
        port_connections.drop_connections();
        config_connection.disconnect();
 
        if (did_reset_tc_format) {
-               session.config.set_timecode_format (saved_tc_format);
+               _session->config.set_timecode_format (saved_tc_format);
        }
 
        ltc_decoder_free(decoder);
 }
 
 void
-LTC_Slave::parse_timecode_offset() {
+LTC_TransportMaster::parse_timecode_offset() {
        Timecode::Time offset_tc;
-       Timecode::parse_timecode_format(session.config.get_slave_timecode_offset(), offset_tc);
-       offset_tc.rate = session.timecode_frames_per_second();
-       offset_tc.drop = session.timecode_drop_frames();
-       session.timecode_to_sample(offset_tc, timecode_offset, false, false);
+       Timecode::parse_timecode_format(_session->config.get_slave_timecode_offset(), offset_tc);
+       offset_tc.rate = _session->timecode_frames_per_second();
+       offset_tc.drop = _session->timecode_drop_frames();
+       _session->timecode_to_sample(offset_tc, timecode_offset, false, false);
        timecode_negative_offset = offset_tc.negative;
 }
 
 void
-LTC_Slave::parameter_changed (std::string const & p)
+LTC_TransportMaster::parameter_changed (std::string const & p)
 {
        if (p == "slave-timecode-offset"
                        || p == "timecode-format"
@@ -101,59 +142,71 @@ LTC_Slave::parameter_changed (std::string const & p)
        }
 }
 
-ARDOUR::framecnt_t
-LTC_Slave::resolution () const
+ARDOUR::samplecnt_t
+LTC_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
+LTC_TransportMaster::resolution () const
 {
-       return (framecnt_t) (session.frame_rate() / 1000);
+       return (samplecnt_t) (ENGINE->sample_rate() / 1000);
 }
 
 bool
-LTC_Slave::locked () const
+LTC_TransportMaster::locked () const
 {
        return (delayedlocked < 5);
 }
 
 bool
-LTC_Slave::ok() const
+LTC_TransportMaster::ok() const
 {
        return true;
 }
 
 void
-LTC_Slave::resync_xrun()
+LTC_TransportMaster::resync_xrun()
 {
        DEBUG_TRACE (DEBUG::LTC, "LTC resync_xrun()\n");
-       engine_dll_initstate = 0;
+       sync_lock_broken = false;
 }
 
 void
-LTC_Slave::resync_latency()
+LTC_TransportMaster::resync_latency()
 {
        DEBUG_TRACE (DEBUG::LTC, "LTC resync_latency()\n");
-       engine_dll_initstate = 0;
+       sync_lock_broken = false;
 
-       if (!session.deletion_in_progress() && session.ltc_output_io()) { /* check if Port exits */
-               boost::shared_ptr<Port> ltcport = session.ltc_input_port();
-               ltcport->get_connected_latency_range (ltc_slave_latency, false);
+       if (!_port) {
+               _port->get_connected_latency_range (ltc_slave_latency, false);
        }
 }
 
 void
-LTC_Slave::reset()
+LTC_TransportMaster::reset (bool with_ts)
 {
        DEBUG_TRACE (DEBUG::LTC, "LTC reset()\n");
-       last_timestamp = 0;
-       current_delta = 0;
+       if (with_ts) {
+               current.update (current.position, 0, current.speed);
+               _current_delta = 0;
+       }
        transport_direction = 0;
-       ltc_speed = 0;
-       engine_dll_initstate = 0;
+       sync_lock_broken = false;
+       monotonic_cnt = 0;
 }
 
 void
-LTC_Slave::parse_ltc(const pframes_t nframes, const Sample* const in, const framecnt_t posinfo)
+LTC_TransportMaster::parse_ltc (const ARDOUR::pframes_t nframes, const Sample* const in, const ARDOUR::samplecnt_t posinfo)
 {
        pframes_t i;
        unsigned char sound[8192];
+
        if (nframes > 8192) {
                /* TODO warn once or wrap, loop conversion below
                 * does jack/A3 support > 8192 spp anyway?
@@ -162,57 +215,58 @@ LTC_Slave::parse_ltc(const pframes_t nframes, const Sample* const in, const fram
        }
 
        for (i = 0; i < nframes; i++) {
-               const int snd=(int)rint((127.0*in[i])+128.0);
+               const int snd=(int) rint ((127.0*in[i])+128.0);
                sound[i] = (unsigned char) (snd&0xff);
        }
-       ltc_decoder_write(decoder, sound, nframes, posinfo);
+
+       ltc_decoder_write (decoder, sound, nframes, posinfo);
+
        return;
 }
 
 bool
-LTC_Slave::equal_ltc_frame_time(LTCFrame *a, LTCFrame *b) {
-       if (       a->frame_units != b->frame_units
-               || a->frame_tens  != b->frame_tens
-               || a->dfbit       != b->dfbit
-               || a->secs_units  != b->secs_units
-               || a->secs_tens   != b->secs_tens
-               || a->mins_units  != b->mins_units
-               || a->mins_tens   != b->mins_tens
-               || a->hours_units != b->hours_units
-               || a->hours_tens  != b->hours_tens
-            ) {
+LTC_TransportMaster::equal_ltc_sample_time(LTCFrame *a, LTCFrame *b) {
+       if (a->frame_units != b->frame_units ||
+           a->frame_tens  != b->frame_tens ||
+           a->dfbit       != b->dfbit ||
+           a->secs_units  != b->secs_units ||
+           a->secs_tens   != b->secs_tens ||
+           a->mins_units  != b->mins_units ||
+           a->mins_tens   != b->mins_tens ||
+           a->hours_units != b->hours_units ||
+           a->hours_tens  != b->hours_tens) {
                return false;
        }
        return true;
 }
 
 bool
-LTC_Slave::detect_discontinuity(LTCFrameExt *frame, int fps, bool fuzzy) {
+LTC_TransportMaster::detect_discontinuity(LTCFrameExt *sample, int fps, bool fuzzy) {
        bool discontinuity_detected = false;
 
        if (fuzzy && (
-                 ( frame->reverse && prev_frame.ltc.frame_units == 0)
-               ||(!frame->reverse && frame->ltc.frame_units == 0)
+                 ( sample->reverse && prev_sample.ltc.frame_units == 0)
+               ||(!sample->reverse && sample->ltc.frame_units == 0)
                )) {
-               memcpy(&prev_frame, frame, sizeof(LTCFrameExt));
+               memcpy(&prev_sample, sample, sizeof(LTCFrameExt));
                return false;
        }
 
-       if (frame->reverse) {
-               ltc_frame_decrement(&prev_frame.ltc, fps, LTC_TV_525_60, 0);
+       if (sample->reverse) {
+               ltc_frame_decrement(&prev_sample.ltc, fps, LTC_TV_525_60, 0);
        } else {
-               ltc_frame_increment(&prev_frame.ltc, fps, LTC_TV_525_60, 0);
+               ltc_frame_increment(&prev_sample.ltc, fps, LTC_TV_525_60, 0);
        }
-       if (!equal_ltc_frame_time(&prev_frame.ltc, &frame->ltc)) {
+       if (!equal_ltc_sample_time(&prev_sample.ltc, &sample->ltc)) {
                discontinuity_detected = true;
        }
 
-    memcpy(&prev_frame, frame, sizeof(LTCFrameExt));
+    memcpy(&prev_sample, sample, sizeof(LTCFrameExt));
     return discontinuity_detected;
 }
 
 bool
-LTC_Slave::detect_ltc_fps(int frameno, bool df)
+LTC_TransportMaster::detect_ltc_fps(int frameno, bool df)
 {
        bool fps_changed = false;
        double detected_fps = 0;
@@ -227,7 +281,7 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
                        detected_fps = ltc_detect_fps_max + 1;
                        if (df) {
                                /* LTC df -> indicates fractional framerate */
-                               if (Config->get_timecode_source_2997()) {
+                               if (fr2997()) {
                                        detected_fps = detected_fps * 999.0 / 1000.0;
                                } else {
                                        detected_fps = detected_fps * 1000.0 / 1001.0;
@@ -247,15 +301,15 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
        if (detected_fps != 0 && (detected_fps != timecode.rate || df != timecode.drop)) {
                timecode.rate = detected_fps;
                timecode.drop = df;
-               frames_per_ltc_frame = double(session.frame_rate()) / timecode.rate;
-               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC reset to FPS: %1%2 ; audio-frames per LTC: %3\n",
-                               detected_fps, df?"df":"ndf", frames_per_ltc_frame));
+               samples_per_ltc_frame = double(_session->sample_rate()) / timecode.rate;
+               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC reset to FPS: %1%2 ; audio-samples per LTC: %3\n",
+                               detected_fps, df?"df":"ndf", samples_per_ltc_frame));
                fps_changed=true;
        }
 
        /* poll and check session TC */
        TimecodeFormat tc_format = apparent_timecode_format();
-       TimecodeFormat cur_timecode = session.config.get_timecode_format();
+       TimecodeFormat cur_timecode = _session->config.get_timecode_format();
 
        if (Config->get_timecode_sync_frame_rate()) {
                /* enforce time-code */
@@ -270,7 +324,7 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
                                                Timecode::timecode_format_name(tc_format))
                                        << endmsg;
                        }
-                       session.config.set_timecode_format (tc_format);
+                       _session->config.set_timecode_format (tc_format);
                }
        } else {
                /* only warn about TC mismatch */
@@ -290,51 +344,69 @@ LTC_Slave::detect_ltc_fps(int frameno, bool df)
        ltc_timecode = tc_format;
        a3e_timecode = cur_timecode;
 
+       if (Config->get_use_session_timecode_format() && _session) {
+               samples_per_timecode_frame = _session->samples_per_timecode_frame();
+       } else {
+               samples_per_timecode_frame = ENGINE->sample_rate() / Timecode::timecode_to_frames_per_second (ltc_timecode);
+       }
+
        return fps_changed;
 }
 
 void
-LTC_Slave::process_ltc(framepos_t const /*now*/)
+LTC_TransportMaster::process_ltc(samplepos_t const now)
 {
-       LTCFrameExt frame;
-       enum LTC_TV_STANDARD tv_standard = LTC_TV_625_50;
-       while (ltc_decoder_read(decoder, &frame)) {
+       LTCFrameExt sample;
+       LTC_TV_STANDARD tv_standard = LTC_TV_625_50;
+
+       while (ltc_decoder_read (decoder, &sample)) {
+
                SMPTETimecode stime;
 
-               ltc_frame_to_time(&stime, &frame.ltc, 0);
+               ltc_frame_to_time (&stime, &sample.ltc, 0);
                timecode.negative  = false;
                timecode.subframes  = 0;
 
                /* set timecode.rate and timecode.drop: */
-               bool ltc_is_static = equal_ltc_frame_time(&prev_frame.ltc, &frame.ltc);
 
-               if (detect_discontinuity(&frame, ceil(timecode.rate), !fps_detected)) {
-                       if (fps_detected) { ltc_detect_fps_cnt = ltc_detect_fps_max = 0; }
-                       fps_detected=false;
+               const bool ltc_is_stationary = equal_ltc_sample_time (&prev_sample.ltc, &sample.ltc);
+
+               if (detect_discontinuity (&sample, ceil(timecode.rate), !fps_detected)) {
+
+                       if (fps_detected) {
+                               ltc_detect_fps_cnt = ltc_detect_fps_max = 0;
+                       }
+
+                       fps_detected = false;
                }
 
-               if (!ltc_is_static && detect_ltc_fps(stime.frame, (frame.ltc.dfbit)? true : false)) {
+               if (!ltc_is_stationary && detect_ltc_fps (stime.frame, (sample.ltc.dfbit)? true : false)) {
                        reset();
                        fps_detected=true;
                }
 
-#if 0 // Devel/Debug
-               fprintf(stdout, "LTC %02d:%02d:%02d%c%02d | %8lld %8lld%s\n",
-                       stime.hours,
-                       stime.mins,
-                       stime.secs,
-                       (frame.ltc.dfbit) ? '.' : ':',
-                       stime.frame,
-                       frame.off_start,
-                       frame.off_end,
-                       frame.reverse ? " R" : "  "
-                       );
+#ifndef NDEBUG
+               if (DEBUG_ENABLED (DEBUG::LTC)) {
+                       /* use fprintf for simpler correct formatting of times
+                        */
+                       fprintf (stderr, "LTC@%ld %02d:%02d:%02d%c%02d | %8lld %8lld%s\n",
+                                now,
+                                stime.hours,
+                                stime.mins,
+                                stime.secs,
+                                (sample.ltc.dfbit) ? '.' : ':',
+                                stime.frame,
+                                sample.off_start,
+                                sample.off_end,
+                                sample.reverse ? " R" : "  "
+                               );
+               }
 #endif
 
-               /* when a full LTC frame is decoded, the timecode the LTC frame
+               /* when a full LTC sample is decoded, the timecode the LTC sample
                 * is referring has just passed.
                 * So we send the _next_ timecode which
-                * is expected to start at the end of the current frame
+                * is expected to start at the end of the current sample
                 */
                int fps_i = ceil(timecode.rate);
 
@@ -354,17 +426,17 @@ LTC_Slave::process_ltc(framepos_t const /*now*/)
                                break;
                }
 
-               if (!frame.reverse) {
-                       ltc_frame_increment(&frame.ltc, fps_i, tv_standard, 0);
-                       ltc_frame_to_time(&stime, &frame.ltc, 0);
+               if (!sample.reverse) {
+                       ltc_frame_increment(&sample.ltc, fps_i, tv_standard, 0);
+                       ltc_frame_to_time(&stime, &sample.ltc, 0);
                        transport_direction = 1;
-                       frame.off_start -= ltc_frame_alignment(session.frames_per_timecode_frame(), tv_standard);
-                       frame.off_end -= ltc_frame_alignment(session.frames_per_timecode_frame(), tv_standard);
+                       sample.off_start -= ltc_frame_alignment(samples_per_timecode_frame, tv_standard);
+                       sample.off_end -= ltc_frame_alignment(samples_per_timecode_frame, tv_standard);
                } else {
-                       ltc_frame_decrement(&frame.ltc, fps_i, tv_standard, 0);
-                       int off = frame.off_end - frame.off_start;
-                       frame.off_start += off - ltc_frame_alignment(session.frames_per_timecode_frame(), tv_standard);
-                       frame.off_end += off - ltc_frame_alignment(session.frames_per_timecode_frame(), tv_standard);
+                       ltc_frame_decrement(&sample.ltc, fps_i, tv_standard, 0);
+                       int off = sample.off_end - sample.off_start;
+                       sample.off_start += off - ltc_frame_alignment(samples_per_timecode_frame, tv_standard);
+                       sample.off_end += off - ltc_frame_alignment(samples_per_timecode_frame, tv_standard);
                        transport_direction = -1;
                }
 
@@ -373,225 +445,176 @@ LTC_Slave::process_ltc(framepos_t const /*now*/)
                timecode.seconds = stime.secs;
                timecode.frames  = stime.frame;
 
-               /* map LTC timecode to session TC setting */
-               framepos_t ltc_frame; ///< audio-frame corresponding to LTC frame
-               Timecode::timecode_to_sample (timecode, ltc_frame, true, false,
-                       double(session.frame_rate()),
-                       session.config.get_subframes_per_frame(),
-                       timecode_negative_offset, timecode_offset
-                       );
-
-               framepos_t cur_timestamp = frame.off_end + 1;
-               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC F: %1 LF: %2  N: %3 L: %4\n", ltc_frame, last_ltc_frame, cur_timestamp, last_timestamp));
-               if (frame.off_end + 1 <= last_timestamp || last_timestamp == 0) {
-                       DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed: UNCHANGED: %1\n", ltc_speed));
+               samplepos_t ltc_sample; // audio-sample corresponding to position of LTC frame
+
+               if (_session && Config->get_use_session_timecode_format()) {
+                       Timecode::timecode_to_sample (timecode, ltc_sample, true, false, (double)ENGINE->sample_rate(), _session->config.get_subframes_per_frame(), timecode_negative_offset, timecode_offset);
                } else {
-                       ltc_speed = double(ltc_frame - last_ltc_frame) / double(cur_timestamp - last_timestamp);
-                       DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed: %1\n", ltc_speed));
+                       Timecode::timecode_to_sample (timecode, ltc_sample, true, false, (double)ENGINE->sample_rate(), 100, timecode_negative_offset, timecode_offset);
                }
 
-               if (fabs(ltc_speed) > 10.0) {
-                       ltc_speed = 0;
-               }
+               ltc_sample += ltc_slave_latency.max;
+
+               /* This LTC frame spans sample time between sample.off_start  .. sample.off_end
+                *
+                * NOTE: these sample times are NOT the ones that LTC is representing. They are
+                * derived our own audioengine's monotonic audio clock.
+                *
+                * So we expect the next frame to span sample.off_end+1 and ... <don't care for now>.
+                * That isn't the time we will necessarily receive the LTC frame, but the decoder
+                * should tell us that its span begins there.
+                *
+                */
 
-               last_timestamp = frame.off_end + 1;
-               last_ltc_frame = ltc_frame;
-       } /* end foreach decoded LTC frame */
-}
+               samplepos_t cur_timestamp = sample.off_end + 1;
+               double ltc_speed = current.speed;
 
-void
-LTC_Slave::init_engine_dll (framepos_t pos, int32_t inc)
-{
-       double omega = 2.0 * M_PI * double(inc) / double(session.frame_rate());
-       b = 1.4142135623730950488 * omega;
-       c = omega * omega;
-
-       e2 = double(ltc_speed * inc);
-       t0 = double(pos);
-       t1 = t0 + e2;
-       DEBUG_TRACE (DEBUG::LTC, string_compose ("[re-]init Engine DLL %1 %2 %3\n", t0, t1, e2));
-}
+               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC S: %1 LS: %2  N: %3 L: %4 span %5..%6\n", ltc_sample, current.position, cur_timestamp, current.timestamp, sample.off_start, sample.off_end));
 
-/* main entry point from session_process.cc
- * called from process callback context
- * so it is OK to use get_buffer()
- */
-bool
-LTC_Slave::speed_and_position (double& speed, framepos_t& pos)
-{
-       bool engine_init_called = false;
-       framepos_t now = session.engine().sample_time_at_cycle_start();
-       framepos_t sess_pos = session.transport_frame(); // corresponds to now
-       framecnt_t nframes = session.engine().samples_per_cycle();
+               if (cur_timestamp <= current.timestamp || current.timestamp == 0) {
+                       DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed: UNCHANGED: %1\n", current.speed));
+               } else {
+                       ltc_speed = double (ltc_sample - current.position) / double (cur_timestamp - current.timestamp);
+
+                       /* provide a .1% deadzone to lock the speed */
+                       if (fabs (ltc_speed - 1.0) <= 0.001) {
+                               ltc_speed = 1.0;
+                       }
 
-       Sample* in;
+                       if (fabs (ltc_speed) > 10.0) {
+                               ltc_speed = 0;
+                       }
 
-       boost::shared_ptr<Port> ltcport = session.ltc_input_port();
+                       DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed: %1\n", ltc_speed));
+               }
+               DEBUG_TRACE (DEBUG::LTC, string_compose ("update current to %1 %2 %3\n", ltc_sample, cur_timestamp, ltc_speed));
+               current.update (ltc_sample, cur_timestamp, ltc_speed);
 
-       in = (Sample*) AudioEngine::instance()->port_engine().get_buffer (ltcport->port_handle(), nframes);
+       } /* end foreach decoded LTC sample */
+}
 
-       frameoffset_t skip = now - (monotonic_cnt + nframes);
+void
+LTC_TransportMaster::pre_process (ARDOUR::pframes_t nframes, samplepos_t now, boost::optional<samplepos_t> session_pos)
+{
+       Sample* in = (Sample*) AudioEngine::instance()->port_engine().get_buffer (_port->port_handle(), nframes);
+       sampleoffset_t skip = now - (monotonic_cnt + nframes);
        monotonic_cnt = now;
-       DEBUG_TRACE (DEBUG::LTC, string_compose ("speed_and_position - TID:%1 | latency: %2 | skip %3\n", ::pthread_self(), ltc_slave_latency.max, skip));
 
-       if (last_timestamp == 0) {
-               engine_dll_initstate = 0;
-               delayedlocked++;
-       }
-       else if (engine_dll_initstate != transport_direction && ltc_speed != 0) {
-               engine_dll_initstate = transport_direction;
-               init_engine_dll(last_ltc_frame + rint(ltc_speed * double(2 * nframes + now - last_timestamp)),
-                               session.engine().samples_per_cycle());
-               engine_init_called = true;
-       }
+       DEBUG_TRACE (DEBUG::LTC, string_compose ("pre-process - TID:%1 | latency: %2 | skip %3 | session ? %4| last %5 | dir %6 | sp %7\n",
+                                                pthread_name(), ltc_slave_latency.max, skip, (_session ? 'y' : 'n'), current.timestamp, transport_direction, current.speed));
 
-       if (in) {
-               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC Process eng-tme: %1 eng-pos: %2\n", now, sess_pos));
-               /* when the jack-graph changes and if ardour performs
-                * locates, the audioengine is stopped (skipping frames) while
-                * jack [time] moves along.
-                */
-               if (skip > 0) {
-                       DEBUG_TRACE (DEBUG::LTC, string_compose("engine skipped %1 frames. Feeding silence to LTC parser.\n", skip));
-                       if (skip >= 8192) skip = 8192;
-                       unsigned char sound[8192];
-                       memset(sound, 0, sizeof(char) * skip);
-                       ltc_decoder_write(decoder, sound, nframes, now);
-               } else if (skip != 0) {
-                       /* this should never happen. it may if monotonic_cnt, now overflow on 64bit */
-                       DEBUG_TRACE (DEBUG::LTC, string_compose("engine skipped %1 frames\n", skip));
-                       reset();
+       if (current.timestamp == 0) {
+               if (delayedlocked < 10) {
+                       ++delayedlocked;
                }
 
-               parse_ltc(nframes, in, now - ltc_slave_latency.max );
-               process_ltc(now);
-       }
+       } else if (current.speed != 0) {
 
-       if (last_timestamp == 0) {
-               DEBUG_TRACE (DEBUG::LTC, "last timestamp == 0\n");
-               speed = 0;
-               pos = session.transport_frame();
-               return true;
-       } else if (ltc_speed != 0) {
-               delayedlocked = 0;
        }
 
-       if (abs(now - last_timestamp) > FLYWHEEL_TIMEOUT) {
-               DEBUG_TRACE (DEBUG::LTC, "flywheel timeout\n");
+       DEBUG_TRACE (DEBUG::LTC, string_compose ("pre-process with audio clock time: %1\n", now));
+
+       /* if the audioengine failed to take the process lock, it won't
+          call this method, and time will appear to skip. Reset the
+          LTC decoder's state by giving it some silence.
+       */
+
+       if (skip > 0) {
+               DEBUG_TRACE (DEBUG::LTC, string_compose("engine skipped %1 samples. Feeding silence to LTC parser.\n", skip));
+               if (skip >= 8192) skip = 8192;
+               unsigned char sound[8192];
+               memset (sound, 0x80, sizeof(char) * skip);
+               ltc_decoder_write (decoder, sound, nframes, now);
+       } else if (skip != 0) {
+               /* this should never happen. it may if monotonic_cnt, now overflow on 64bit */
+               DEBUG_TRACE (DEBUG::LTC, string_compose("engine skipped %1 samples\n", skip));
                reset();
-               speed = 0;
-               pos = session.transport_frame();
-               return true;
        }
 
-       /* it take 2 cycles from naught to rolling.
-        * during these to initial cycles the speed == 0
-        *
-        * the first cycle:
-        * DEBUG::Slave: slave stopped, move to NNN
-        * DEBUG::Transport: Request forced locate to NNN
-        * DEBUG::Slave: slave state 0 @ NNN speed 0 cur delta VERY-LARGE-DELTA avg delta 1800
-        * DEBUG::Slave: silent motion
-        * DEBUG::Transport: realtime stop @ NNN
-        * DEBUG::Transport: Butler transport work, todo = PostTransportStop,PostTransportLocate,PostTransportClearSubstate
-        *
-        * [engine skips frames to locate, jack time keeps rolling on]
-        *
-        * the second cycle:
-        *
-        * DEBUG::LTC: [re-]init Engine DLL
-        * DEBUG::Slave: slave stopped, move to NNN+
-        * ...
-        *
-        * we need to seek two cycles ahead: 2 * nframes
-        */
-       if (engine_dll_initstate == 0) {
-               DEBUG_TRACE (DEBUG::LTC, "engine DLL not initialized. ltc_speed\n");
-               speed = 0;
-               pos = last_ltc_frame + rint(ltc_speed * double(2 * nframes + now - last_timestamp));
-               return true;
-       }
+       /* Now feed the incoming LTC signal into the decoder */
 
-       /* interpolate position according to speed and time since last LTC-frame*/
-       double speed_flt = ltc_speed;
-       double elapsed = (now - last_timestamp) * speed_flt;
-
-       if (!engine_init_called) {
-               const double e = elapsed + double (last_ltc_frame - sess_pos);
-               t0 = t1;
-               t1 += b * e + e2;
-               e2 += c * e;
-               speed_flt = (t1 - t0) / double(session.engine().samples_per_cycle());
-               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC engine DLL t0:%1 t1:%2 err:%3 spd:%4 ddt:%5\n", t0, t1, e, speed_flt, e2 - session.engine().samples_per_cycle() ));
-       } else {
-               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC adjusting elapsed (no DLL) from %1 by %2\n", elapsed, (2 * nframes * ltc_speed)));
-               speed_flt = 0;
-               elapsed += 2.0 * nframes * ltc_speed; /* see note above */
-       }
+       parse_ltc (nframes, in, now);
 
-       pos = last_ltc_frame + rint(elapsed);
-       speed = speed_flt;
-       current_delta = (pos - sess_pos);
+       /* and pull out actual LTC frame data */
 
-       if (((pos < 0) || (labs(current_delta) > 2 * session.frame_rate()))) {
-               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC large drift: %1\n", current_delta));
-               reset();
-               speed = 0;
-               pos = session.transport_frame();
-               return true;
+       process_ltc (now);
+
+       if (current.timestamp == 0) {
+               DEBUG_TRACE (DEBUG::LTC, "last timestamp == 0\n");
+               return;
+       } else if (current.speed != 0) {
+               DEBUG_TRACE (DEBUG::LTC, string_compose ("speed non-zero (%1)\n", current.speed));
+               if (delayedlocked > 1) {
+                       delayedlocked--;
+               } else if (_current_delta == 0) {
+                       delayedlocked = 0;
+               }
        }
 
-       DEBUG_TRACE (DEBUG::LTC, string_compose ("LTCsync spd: %1 pos: %2 | last-pos: %3 elapsed: %4 delta: %5\n",
-                                                speed, pos, last_ltc_frame, elapsed, current_delta));
+       if (abs (now - current.timestamp) > FLYWHEEL_TIMEOUT) {
+               DEBUG_TRACE (DEBUG::LTC, "flywheel timeout\n");
+               reset();
+               /* don't change position from last known */
 
-       /* provide a .1% deadzone to lock the speed */
-       if (fabs(speed - 1.0) <= 0.001) {
-               speed = 1.0;
+               return;
        }
 
-       return true;
+       if (!sync_lock_broken && current.speed != 0 && delayedlocked == 0 && fabs(current.speed) != 1.0) {
+               sync_lock_broken = true;
+               DEBUG_TRACE (DEBUG::LTC, string_compose ("LTC speed not locked based on %1\n", current.speed));
+       }
+
+       if (session_pos) {
+               const samplepos_t current_pos = current.position + ((now - current.timestamp) * current.speed);
+               _current_delta = current_pos - *session_pos;
+       } else {
+               _current_delta = 0;
+       }
 }
 
 Timecode::TimecodeFormat
-LTC_Slave::apparent_timecode_format () const
+LTC_TransportMaster::apparent_timecode_format () const
 {
        if      (timecode.rate == 24 && !timecode.drop)
                return timecode_24;
        else if (timecode.rate == 25 && !timecode.drop)
                return timecode_25;
        else if (rint(timecode.rate * 100) == 2997 && !timecode.drop)
-               return (Config->get_timecode_source_2997() ? timecode_2997000 : timecode_2997);
+               return (fr2997() ? timecode_2997000 : timecode_2997);
        else if (rint(timecode.rate * 100) == 2997 &&  timecode.drop)
-               return (Config->get_timecode_source_2997() ? timecode_2997000drop : timecode_2997drop);
+               return (fr2997() ? timecode_2997000drop : timecode_2997drop);
        else if (timecode.rate == 30 &&  timecode.drop)
-               return timecode_2997drop; // timecode_30drop; // LTC counting to 30 frames w/DF *means* 29.97 df
+               return timecode_2997drop; // timecode_30drop; // LTC counting to 30 samples w/DF *means* 29.97 df
        else if (timecode.rate == 30 && !timecode.drop)
                return timecode_30;
 
        /* XXX - unknown timecode format */
-       return session.config.get_timecode_format();
+       return _session->config.get_timecode_format();
 }
 
 std::string
-LTC_Slave::approximate_current_position() const
+LTC_TransportMaster::position_string() const
 {
-       if (last_timestamp == 0) {
+       if (!_collect || current.timestamp == 0) {
                return " --:--:--:--";
        }
        return Timecode::timecode_format_time(timecode);
 }
 
 std::string
-LTC_Slave::approximate_current_delta() const
+LTC_TransportMaster::delta_string() const
 {
        char delta[80];
-       if (last_timestamp == 0 || engine_dll_initstate == 0) {
-               snprintf(delta, sizeof(delta), "\u2012\u2012\u2012\u2012");
-       } else if ((monotonic_cnt - last_timestamp) > 2 * frames_per_ltc_frame) {
-               snprintf(delta, sizeof(delta), "%s", _("flywheel"));
+
+       if (!_collect || current.timestamp == 0) {
+               snprintf (delta, sizeof(delta), "\u2012\u2012\u2012\u2012");
+       } else if ((monotonic_cnt - current.timestamp) > 2 * samples_per_ltc_frame) {
+               snprintf (delta, sizeof(delta), "%s", _("flywheel"));
        } else {
-               snprintf(delta, sizeof(delta), "\u0394<span foreground=\"green\" face=\"monospace\" >%s%s%" PRIi64 "</span>sm",
-                               LEADINGZERO(abs(current_delta)), PLUSMINUS(-current_delta), abs(current_delta));
+               snprintf (delta, sizeof(delta), "\u0394<span foreground=\"%s\" face=\"monospace\" >%s%s%lld</span>sm",
+                               sync_lock_broken ? "red" : "green",
+                               LEADINGZERO(::llabs(_current_delta)), PLUSMINUS(-_current_delta), ::llabs(_current_delta));
        }
-       return std::string(delta);
+
+       return delta;
 }