MIDClock_Slave: Introduce a 3% deadzone around speed 1.0 to lock in the speed
[ardour.git] / libs / ardour / midi_clock_slave.cc
index 3a43580d648c79fe028311fae3c884268799aa9d..16f45440620f85b921e3e56f32fcd6a8c81f1a73 100644 (file)
@@ -42,9 +42,11 @@ using namespace ARDOUR;
 using namespace MIDI;
 using namespace PBD;
 
+#define DEBUG_MIDI_CLOCK 1
+
 MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
        : ppqn (ppqn)
-       , bandwidth (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
+       , bandwidth (1.0 / 60.0) // 1 BpM = 1 / 60 Hz
 {
        session = (ISlaveSessionProxy *) new SlaveSessionProxy(s);
        rebind (p);
@@ -54,9 +56,8 @@ MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
 MIDIClock_Slave::MIDIClock_Slave (ISlaveSessionProxy* session_proxy, int ppqn)
        : session(session_proxy)
        , ppqn (ppqn)
-       , bandwidth (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
+       , bandwidth (1.0 / 60.0) // 1 BpM = 1 / 60 Hz
 {
-       session = session_proxy;
        reset ();
 }
 
@@ -185,14 +186,14 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, nframes64_t timestamp)
                                  << " frame-rate: " << session->frame_rate()
                                  << endl;
 
-               cerr      << "frames since cycle start: " << session->frames_since_cycle_start() << endl;
+               //cerr      << "frames since cycle start: " << session->frames_since_cycle_start() << endl;
        #endif // DEBUG_MIDI_CLOCK
 
        last_timestamp = timestamp;
 }
 
 void
-MIDIClock_Slave::start (Parser& /*parser*/, nframes64_t /*timestamp*/)
+MIDIClock_Slave::start (Parser& /*parser*/, nframes64_t timestamp)
 {
        #ifdef DEBUG_MIDI_CLOCK
                cerr << "MIDIClock_Slave got start message at time "  <<  timestamp << " engine time: " << session->frame_time() << endl;
@@ -216,7 +217,7 @@ MIDIClock_Slave::reset ()
        _starting = false;
        _started  = false;
 
-       session->request_locate(0, false);
+        if (session) session->request_locate(0, false);
 }
 
 void
@@ -345,6 +346,10 @@ MIDIClock_Slave::speed_and_position (double& speed, nframes64_t& pos)
 
        // calculate speed
        speed = ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames;
+       
+       // provide a 3% deadzone to lock the speed
+       if (fabs(speed - 1.0) <= 0.03)
+               speed = 1.0;
 
        // calculate position
        if (engine_now > last_timestamp) {