- 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);
-}
-
-bool
-MTC_Slave::locked () const
-{
- return port->parser()->mtc_locked();
-}
-
-bool
-MTC_Slave::ok() const
-{
- return true;
-}
-
-bool
-MTC_Slave::speed_and_position (double& speed, framepos_t& pos)
-{
- framepos_t now = session.engine().frame_time();
- SafeTime last;
- framecnt_t elapsed;
- bool in_control = false;
-
- read_current (&last);
-
- if (last.timestamp == 0) {
- speed = 0;
- pos = last.position;
- DEBUG_TRACE (DEBUG::MTC, string_compose ("first call to MTC_Slave::speed_and_position, pos = %1\n", last.position));
- return true;
- }
-
- /* no timecode for 1/4 second ? conclude that its stopped */
-
- if (last_inbound_frame && now > last_inbound_frame && now - last_inbound_frame > session.frame_rate() / 4) {
- speed = 0;
- pos = last.position;
- session.request_locate (pos, false);
- session.request_transport_speed (0);
- queue_reset (false);
- DEBUG_TRACE (DEBUG::MTC, "MTC not seen for 1/4 second - reset pending\n");
- return false;
- }
-
- DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC::speed_and_position %1 %2\n", last.speed, last.position));
-
- if (give_slave_full_control_over_transport_speed()) {
- in_control = (session.slave_state() == Session::Running);
- framepos_t pic_want_locate = 0;
- //framepos_t slave_pos = session.audible_frame();
- framepos_t slave_pos = session.transport_frame();
- static double average_speed = 0;
-
- framepos_t ref_now = session.engine().frame_time_at_cycle_start();
- average_speed = pic->get_ratio (last.timestamp, last.position, ref_now, slave_pos, in_control, session.engine().frames_per_cycle());
-
- pic_want_locate = pic->want_locate();
-
- if (in_control && pic_want_locate) {
- last.speed = average_speed + (double) (pic_want_locate - session.transport_frame()) / (double)session.get_block_size();
- std::cout << "locate req " << pic_want_locate << " speed: " << average_speed << "\n";
- } else {
- last.speed = average_speed;
- }
- }
-
- if (last.speed == 0.0f) {
-
- elapsed = 0;
-
- } else {
-
- /* scale elapsed time by the current MTC speed */
-
- if (last.timestamp && (now > last.timestamp)) {
- elapsed = (framecnt_t) floor (last.speed * (now - last.timestamp));
- DEBUG_TRACE (DEBUG::MTC, string_compose ("last timecode received @ %1, now = %2, elapsed frames = %3 w/speed= %4\n",
- last.timestamp, now, elapsed, last.speed));
- } else {
- elapsed = 0; /* XXX is this right? */
- }
- }
-
- /* now add the most recent timecode value plus the estimated elapsed interval */
-
- if (in_control) {
- pos = session.transport_frame();
- } else {
- pos = last.position + elapsed;
- }
-
- speed = last.speed;
-
- DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC::speed_and_position FINAL %1 %2\n", last.speed, pos));
-
-
- DEBUG_TRACE (DEBUG::MTC, string_compose ("last = %1 elapsed = %2 pos = %3 speed = %4\n", last.position, elapsed, pos, speed));
-
- return true;
-}
-
-ARDOUR::framecnt_t
-MTC_Slave::resolution () const
-{
- return (framecnt_t) session.frames_per_timecode_frame();
-}
-
-void
-MTC_Slave::queue_reset (bool reset_pos)
-{
- Glib::Mutex::Lock lm (reset_lock);
- reset_pending++;
- if (reset_pos) {
- reset_position = true;
- }
-}
-
-void
-MTC_Slave::maybe_reset ()
-{
- Glib::Mutex::Lock lm (reset_lock);
-
- if (reset_pending) {
- reset (reset_position);
- reset_pending = 0;
- reset_position = false;
- }
-}
-
-void
-MTC_Slave::reset (bool with_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++;
- }
-
- window_begin = 0;
- window_end = 0;
- last_mtc_frame = 0;
- last_mtc_timestamp = 0;
-
- average_speed = 0;
- have_first_speed_accumulator = false;
- speed_accumulator_cnt = 0;
-
- pic->reset();
-}
-
-void
-MTC_Slave::reset_window (framepos_t root)
-{
-