more MTC debugging
[ardour.git] / libs / ardour / mtc_slave.cc
index 41aaaccbcf75a74417b442fdd648870fe02eebc8..8cc54d577d6d3dec533f041314ccfff35279d1c9 100644 (file)
@@ -21,7 +21,9 @@
 #include <poll.h>
 #include <sys/types.h>
 #include <unistd.h>
+
 #include "pbd/error.h"
+#include "pbd/stacktrace.h"
 #include "pbd/enumwriter.h"
 #include "pbd/failed_constructor.h"
 #include "pbd/pthread_utils.h"
@@ -37,7 +39,6 @@
 
 using namespace std;
 using namespace ARDOUR;
-using namespace sigc;
 using namespace MIDI;
 using namespace PBD;
 
@@ -57,7 +58,7 @@ MTC_Slave::MTC_Slave (Session& s, MIDI::Port& p)
        can_notify_on_unknown_rate = true;
        did_reset_tc_format = false;
 
-       pic = new PIController (1.0, 8);
+       pic = new PIChaser();
        
        last_mtc_fps_byte = session.get_mtc_timecode_bits ();
        mtc_frame = 0;
@@ -74,6 +75,7 @@ MTC_Slave::~MTC_Slave()
        if (did_reset_tc_format) {
                session.config.set_timecode_format (saved_tc_format);
        }
+
        delete pic;
        delete [] speed_accumulator;
 }
@@ -88,20 +90,19 @@ MTC_Slave::give_slave_full_control_over_transport_speed() const
 void
 MTC_Slave::rebind (MIDI::Port& p)
 {
-       for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
-               (*i).disconnect ();
-       }
-
+       port_connections.drop_connections ();
+       
        port = &p;
-
-       connections.push_back (port->input()->mtc_time.connect (mem_fun (*this, &MTC_Slave::update_mtc_time)));
-       connections.push_back (port->input()->mtc_qtr.connect (mem_fun (*this, &MTC_Slave::update_mtc_qtr)));
-       connections.push_back (port->input()->mtc_status.connect (mem_fun (*this, &MTC_Slave::update_mtc_status)));
+       
+       port->input()->mtc_time.connect_same_thread (port_connections,  boost::bind (&MTC_Slave::update_mtc_time, this, _1, _2, _3));
+       port->input()->mtc_qtr.connect_same_thread (port_connections, boost::bind (&MTC_Slave::update_mtc_qtr, this, _1, _2, _3));
+       port->input()->mtc_status.connect_same_thread (port_connections, boost::bind (&MTC_Slave::update_mtc_status, this, _1));
 }
 
 void
 MTC_Slave::update_mtc_qtr (Parser& /*p*/, int which_qtr, nframes_t now)
 {
+       DEBUG_TRACE (DEBUG::MTC, string_compose ("update MTC qtr does a reset, qtr = %1 now = %2\n", which_qtr, now));
        maybe_reset ();
 
        DEBUG_TRACE (DEBUG::MTC, string_compose ("qtr frame %1 at %2\n", which_qtr, now));
@@ -117,6 +118,7 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full, nframes_t now)
        */
 
        if (now) {
+               DEBUG_TRACE (DEBUG::MTC, string_compose ("update MTC time does a reset, was full ? %1 now = %2\n", was_full, now));
                maybe_reset ();
        }
 
@@ -182,15 +184,18 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full, nframes_t now)
 
        DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC time timestamp = %1 TC %2 = frame %3 (from full message ? %4)\n", 
                                                 now, timecode, mtc_frame, was_full));
-
+       
        if (was_full || outside_window (mtc_frame)) {
 
                session.timecode_to_sample (timecode, mtc_frame, true, false);
                session.request_locate (mtc_frame, false);
                session.request_transport_speed (0);
+               DEBUG_TRACE (DEBUG::MTC, string_compose ("reset MTC status to stopped, outside MTC window (%1 .. %2 vs. %3)\n",
+                                                        window_begin, window_end, mtc_frame));
                update_mtc_status (MIDI::MTC_Stopped);
-               reset_window (mtc_frame);
+               DEBUG_TRACE (DEBUG::MTC, string_compose ("outside, so window root reset to %1\n", mtc_frame));
                reset ();
+               reset_window (mtc_frame);
 
        } else {
                        
@@ -229,8 +234,18 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full, nframes_t now)
                                         * 
                                         * its not the average, but we will assign it to current.speed below
                                         */
+
+                                   static nframes64_t last_seen_timestamp = 0; 
+                                   static nframes64_t last_seen_position = 0; 
+
+                                   if ((now - last_seen_timestamp) < 300) {
+                                       mtc_frame = (mtc_frame + last_seen_position)/2;
+                                   }
+
+                                   last_seen_timestamp = now;
+                                   last_seen_position = mtc_frame;
+
                                        
-                                       average_speed = pic->get_ratio (session.audible_frame() - mtc_frame);
                                        
                                } else {
 
@@ -270,6 +285,7 @@ MTC_Slave::update_mtc_time (const byte *msg, bool was_full, nframes_t now)
        }
 
        if (window_root >= 0) {
+               DEBUG_TRACE (DEBUG::MTC, string_compose ("window root reset to %1\n", window_root));
                reset_window (window_root);
        }
 }
@@ -412,6 +428,24 @@ MTC_Slave::speed_and_position (double& speed, nframes64_t& pos)
 
        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()) {
+           bool in_control = (session.slave_state() == Session::Running);
+           nframes64_t pic_want_locate = 0; 
+           //nframes64_t slave_pos = session.audible_frame();
+           nframes64_t slave_pos = session.transport_frame();
+           static double average_speed = 0;
+
+           average_speed = pic->get_ratio (last.timestamp, last.position, slave_pos, in_control );
+           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;
@@ -431,7 +465,7 @@ MTC_Slave::speed_and_position (double& speed, nframes64_t& pos)
 
        /* now add the most recent timecode value plus the estimated elapsed interval */
 
-       pos =  elapsed + last.position;
+       pos = last.position + elapsed; 
        speed = last.speed;
 
        DEBUG_TRACE (DEBUG::MTC, string_compose ("MTC::speed_and_position FINAL %1 %2\n", last.speed, pos));
@@ -448,6 +482,8 @@ MTC_Slave::resolution() const
 void
 MTC_Slave::queue_reset ()
 {
+       DEBUG_TRACE (DEBUG::MTC, "queue reset of MTC slave state\n");
+       PBD::stacktrace (cerr, 35);
        Glib::Mutex::Lock lm (reset_lock);
        reset_pending++;
 }
@@ -458,6 +494,7 @@ MTC_Slave::maybe_reset ()
        reset_lock.lock ();
 
        if (reset_pending) {
+               DEBUG_TRACE (DEBUG::MTC, "actually reset\n");
                reset ();
                reset_pending = 0;
        } 
@@ -468,6 +505,8 @@ MTC_Slave::maybe_reset ()
 void
 MTC_Slave::reset ()
 {
+       DEBUG_TRACE (DEBUG::MTC, "*****************\n\n\n MTC SLAVE reset ********************\n\n\n");
+       PBD::stacktrace (cerr, 35);
        port->input()->reset_mtc_state ();
 
        last_inbound_frame = 0;
@@ -486,7 +525,7 @@ MTC_Slave::reset ()
        have_first_speed_accumulator = false;
        speed_accumulator_cnt = 0;
 
-       pic->out_of_bounds();
+       pic->reset();
 }
 
 void
@@ -498,18 +537,21 @@ MTC_Slave::reset_window (nframes64_t root)
           ahead of the window root (taking direction into account).
        */
 
+       DEBUG_TRACE (DEBUG::MTC, string_compose ("trying to reset MTC window with state = %1\n", enum_2_string (port->input()->mtc_running())));
+
        switch (port->input()->mtc_running()) {
        case MTC_Forward:
+               DEBUG_TRACE (DEBUG::MTC, "set MTC window while running forward\n");
                window_begin = root;
                if (session.slave_state() == Session::Running) {
                        window_end = root + (session.frames_per_timecode_frame() * frame_tolerance);
                } else {
                        window_end = root + seekahead_distance ();
                }
-               DEBUG_TRACE (DEBUG::MTC, string_compose ("legal MTC window now %1 .. %2\n", window_begin, window_end));
                break;
 
        case MTC_Backward:
+               DEBUG_TRACE (DEBUG::MTC, "set MTC window while running backward\n");
                if (session.slave_state() == Session::Running) {
                        nframes_t d = session.frames_per_timecode_frame() * frame_tolerance;
                        if (root > d) {
@@ -527,13 +569,15 @@ MTC_Slave::reset_window (nframes64_t root)
                        }
                }
                window_end = root;
-               DEBUG_TRACE (DEBUG::MTC, string_compose ("legal MTC window now %1 .. %2\n", window_begin, window_end));
                break;
                
        default:
+               DEBUG_TRACE (DEBUG::MTC, "not touching MTC window - MTC_Stopped\n");
                /* do nothing */
                break;
        }
+
+       DEBUG_TRACE (DEBUG::MTC, string_compose ("legal MTC window now %1 .. %2\n", window_begin, window_end));
 }
 
 nframes64_t