+ DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC_Slave 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++;
+ } else {
+ last_inbound_frame = 0;
+ current.guard1++;
+ current.timestamp = 0;
+ current.speed = 0;
+ current.guard2++;
+ }
+ first_mtc_timestamp = 0;
+ window_begin = 0;
+ window_end = 0;
+ transport_direction = 1;
+ current_delta = 0;
+}
+
+void
+MTC_Slave::handle_locate (const MIDI::byte* mmc_tc)
+{
+ MIDI::byte mtc[5];
+ DEBUG_TRACE (DEBUG::MTC, "MTC_Slave::handle_locate\n");
+
+ mtc[4] = last_mtc_fps_byte;
+ mtc[3] = mmc_tc[0] & 0xf; /* hrs only */
+ mtc[2] = mmc_tc[1];
+ mtc[1] = mmc_tc[2];
+ mtc[0] = mmc_tc[3];
+
+ update_mtc_time (mtc, true, 0);
+}
+
+void
+MTC_Slave::read_current (SafeTime *st) const
+{
+ int tries = 0;
+
+ do {
+ if (tries == 10) {
+ error << _("MTC Slave: atomic read of current time failed, sleeping!") << endmsg;
+ usleep (20);
+ tries = 0;
+ }
+ *st = current;
+ tries++;
+
+ } while (st->guard1 != st->guard2);
+}
+
+void
+MTC_Slave::init_mtc_dll(framepos_t tme, double qtr)
+{
+ omega = 2.0 * M_PI * qtr / 2.0 / double(session.frame_rate());
+ b = 1.4142135623730950488 * omega;
+ c = omega * omega;
+
+ e2 = qtr;
+ t0 = double(tme);
+ t1 = t0 + e2;
+ DEBUG_TRACE (DEBUG::MTC, string_compose ("[re-]init MTC DLL %1 %2 %3\n", t0, t1, e2));
+}
+
+
+/* called from MIDI parser */
+void
+MTC_Slave::update_mtc_qtr (Parser& /*p*/, int which_qtr, framepos_t now)
+{
+ busy_guard1++;
+ const double qtr_d = quarter_frame_duration;
+
+ mtc_frame_dll += qtr_d * (double) transport_direction;
+ mtc_frame = rint(mtc_frame_dll);
+
+ DEBUG_TRACE (DEBUG::MTC, string_compose ("qtr frame %1 at %2 -> mtc_frame: %3\n", which_qtr, now, mtc_frame));
+
+ double mtc_speed = 0;
+ if (first_mtc_timestamp != 0) {
+ /* update MTC DLL and calculate speed */
+ const double e = mtc_frame_dll - (double)transport_direction * ((double)now - (double)current.timestamp + t0);
+ t0 = t1;
+ t1 += b * e + e2;
+ e2 += c * e;
+
+ mtc_speed = (t1 - t0) / qtr_d;
+ DEBUG_TRACE (DEBUG::MTC, string_compose ("qtr frame DLL t0:%1 t1:%2 err:%3 spd:%4 ddt:%5\n", t0, t1, e, mtc_speed, e2 - qtr_d));
+
+ current.guard1++;
+ current.position = mtc_frame;
+ current.timestamp = now;
+ current.speed = mtc_speed;
+ current.guard2++;
+
+ last_inbound_frame = now;
+ }
+