incomplete merge of master into windows (requires upcoming changes to master to be...
[ardour.git] / libs / ardour / midi_clock_slave.cc
index 7303059207b420cbb263b5f722ffd902a31482f8..a52854d954be24cfd56b7f49fda8100d5fc4cc7e 100644 (file)
 
 */
 
+#include <cmath>
 #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 "pbd/convert.h"
 
 #include "midi++/port.h"
-#include "midi++/jack.h"
+
+#include "ardour/debug.h"
+#include "ardour/midi_buffer.h"
+#include "ardour/midi_port.h"
 #include "ardour/slave.h"
-#include "ardour/session.h"
-#include "ardour/audioengine.h"
-#include "ardour/cycles.h"
 #include "ardour/tempo.h"
 
-
 #include "i18n.h"
 
 using namespace std;
@@ -42,9 +42,9 @@ using namespace ARDOUR;
 using namespace MIDI;
 using namespace PBD;
 
-MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
+MIDIClock_Slave::MIDIClock_Slave (Session& s, MidiPort& p, int ppqn)
        : ppqn (ppqn)
-       , bandwidth (30.0 / 60.0) // 1 BpM = 1 / 60 Hz
+       , bandwidth (10.0 / 60.0) // 1 BpM = 1 / 60 Hz
 {
        session = (ISlaveSessionProxy *) new SlaveSessionProxy(s);
        rebind (p);
@@ -54,9 +54,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 (10.0 / 60.0) // 1 BpM = 1 / 60 Hz
 {
-       session = session_proxy;
        reset ();
 }
 
@@ -66,46 +65,41 @@ MIDIClock_Slave::~MIDIClock_Slave()
 }
 
 void
-MIDIClock_Slave::rebind (MIDI::Port& p)
+MIDIClock_Slave::rebind (MidiPort& port)
 {
-       port_connections.drop_connections();
+       DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MIDIClock_Slave: connecting to port %1\n", port.name()));
 
-       port = &p;
+       port_connections.drop_connections ();
 
-#ifdef DEBUG_MIDI_CLOCK
-       std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
-#endif
+       port.self_parser().timing.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::update_midi_clock, this, _1, _2));
+       port.self_parser().start.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::start, this, _1, _2));
+       port.self_parser().contineu.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::contineu, this, _1, _2));
+       port.self_parser().stop.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::stop, this, _1, _2));
+       port.self_parser().position.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::position, this, _1, _2, 3));
 
-       port->input()->timing.connect (port_connections, boost::bind (&MIDIClock_Slave::update_midi_clock, this, _1, _2));
-       port->input()->start.connect (port_connections, boost::bind (&MIDIClock_Slave::start, this, _1, _2));
-       port->input()->contineu.connect (port_connections, boost::bind (&MIDIClock_Slave::contineu, this, _1, _2));
-       port->input()->stop.connect (port_connections, boost::bind (&MIDIClock_Slave::stop, this, _1, _2));
-       port->input()->position.connect (port_connections, boost::bind (&MIDIClock_Slave::position, this, _1, _2, 3));
 }
 
 void
-MIDIClock_Slave::calculate_one_ppqn_in_frames_at(nframes64_t time)
+MIDIClock_Slave::calculate_one_ppqn_in_frames_at(framepos_t time)
 {
        const Tempo& current_tempo = session->tempo_map().tempo_at(time);
-       const Meter& current_meter = session->tempo_map().meter_at(time);
-       double frames_per_beat =
-               current_tempo.frames_per_beat(session->frame_rate(),
-                                             current_meter);
+       double frames_per_beat = current_tempo.frames_per_beat(session->frame_rate());
 
        double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
        double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
 
        one_ppqn_in_frames = frames_per_quarter_note / double (ppqn);
+       // DEBUG_TRACE (DEBUG::MidiClock, string_compose ("at %1, one ppqn = %2\n", time, one_ppqn_in_frames));
 }
 
-ARDOUR::nframes64_t
+ARDOUR::framepos_t
 MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes)
 {
-       nframes64_t song_position_frames = 0;
+       framepos_t song_position_frames = 0;
        for (uint16_t i = 1; i <= song_position_in_sixteenth_notes; ++i) {
                // one quarter note contains ppqn pulses, so a sixteenth note is ppqn / 4 pulses
                calculate_one_ppqn_in_frames_at(song_position_frames);
-               song_position_frames += one_ppqn_in_frames * (nframes64_t)(ppqn / 4);
+               song_position_frames += one_ppqn_in_frames * (framepos_t)(ppqn / 4);
        }
 
        return song_position_frames;
@@ -115,13 +109,13 @@ void
 MIDIClock_Slave::calculate_filter_coefficients()
 {
        // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
-       omega = 2.0 * 3.14159265358979323846 * bandwidth * one_ppqn_in_frames / session->frame_rate();
+       omega = 2.0 * M_PI * bandwidth * one_ppqn_in_frames / session->frame_rate();
        b = 1.4142135623730950488 * omega;
        c = omega * omega;
 }
 
 void
-MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, nframes64_t timestamp)
+MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, framepos_t timestamp)
 {
        // some pieces of hardware send MIDI Clock all the time
        if ( (!_starting) && (!_started) ) {
@@ -130,7 +124,7 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, nframes64_t timestamp)
 
        calculate_one_ppqn_in_frames_at(should_be_position);
 
-       nframes64_t elapsed_since_start = timestamp - first_timestamp;
+       framepos_t elapsed_since_start = timestamp - first_timestamp;
        double error = 0;
 
        if (_starting || last_timestamp == 0) {
@@ -158,8 +152,9 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, nframes64_t timestamp)
                // we use session->transport_frame() instead of t1 here
                // because t1 is used to calculate the transport speed,
                // so the loop will compensate for accumulating rounding errors
-               error = (double(should_be_position) - double(session->audible_frame()));
+               error = (double(should_be_position) - double(session->transport_frame()));
                e = error / double(session->frame_rate());
+               current_delta = error;
 
                // update DLL
                t0 = t1;
@@ -167,64 +162,58 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, nframes64_t timestamp)
                e2 += c * e;
        }
 
-       #ifdef DEBUG_MIDI_CLOCK
-               cerr
-                                 << "MIDI Clock #" << midi_clock_count
-                                 //<< "@" << timestamp
-                                 << " arrived at: " << elapsed_since_start << " (elapsed time) "
-                                 << " should-be transport: " << should_be_position
-                                 << " audible: " << session->audible_frame()
-                                 << " real transport: " << session->transport_frame()
-                                 << " error: " << error
-                                 //<< " engine: " << session->frame_time()
-                                 << " real delta: " << timestamp - last_timestamp
-                                 << " should-be delta: " << one_ppqn_in_frames
-                                 << " t1-t0: " << (t1 -t0) * session->frame_rate()
-                                 << " t0: " << t0 * session->frame_rate()
-                                 << " t1: " << t1 * session->frame_rate()
-                                 << " frame-rate: " << session->frame_rate()
-                                 << endl;
-
-               cerr      << "frames since cycle start: " << session->frames_since_cycle_start() << endl;
-       #endif // DEBUG_MIDI_CLOCK
+       DEBUG_TRACE (DEBUG::MidiClock, string_compose ("clock #%1 @ %2 arrived %3 (theoretical) audible %4 transport %5 error %6 "
+                                                      "read delta %7 should-be delta %8 t1-t0 %9 t0 %10 t1 %11 framerate %12 appspeed %13\n",
+                                                      midi_clock_count,
+                                                      elapsed_since_start,
+                                                      should_be_position,
+                                                      session->audible_frame(),
+                                                      session->transport_frame(),
+                                                      error,
+                                                      timestamp - last_timestamp,
+                                                      one_ppqn_in_frames,
+                                                      (t1 -t0) * session->frame_rate(),
+                                                      t0 * session->frame_rate(),
+                                                      t1 * session->frame_rate(),
+                                                      session->frame_rate(),
+                                                      ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames));
 
        last_timestamp = timestamp;
 }
 
 void
-MIDIClock_Slave::start (Parser& /*parser*/, nframes64_t /*timestamp*/)
+MIDIClock_Slave::start (Parser& /*parser*/, framepos_t timestamp)
 {
-       #ifdef DEBUG_MIDI_CLOCK
-               cerr << "MIDIClock_Slave got start message at time "  <<  timestamp << " engine time: " << session->frame_time() << endl;
-       #endif
+       DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MIDIClock_Slave got start message at time %1 engine time %2\n", timestamp, session->frame_time()));
 
        if (!_started) {
                reset();
 
                _started = true;
                _starting = true;
+
+               should_be_position = session->transport_frame();
        }
 }
 
 void
 MIDIClock_Slave::reset ()
 {
-
-       should_be_position = 0;
+       should_be_position = session->transport_frame();
        last_timestamp = 0;
 
-       _starting = false;
-       _started  = false;
+       _starting = true;
+       _started  = true;
 
-       session->request_locate(0, false);
+       // session->request_locate(0, false);
+       current_delta = 0;
 }
 
 void
-MIDIClock_Slave::contineu (Parser& /*parser*/, nframes64_t /*timestamp*/)
+MIDIClock_Slave::contineu (Parser& /*parser*/, framepos_t /*timestamp*/)
 {
-       #ifdef DEBUG_MIDI_CLOCK
-               std::cerr << "MIDIClock_Slave got continue message" << endl;
-       #endif
+       DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got continue message\n");
+
        if (!_started) {
                _starting = true;
                _started  = true;
@@ -233,11 +222,9 @@ MIDIClock_Slave::contineu (Parser& /*parser*/, nframes64_t /*timestamp*/)
 
 
 void
-MIDIClock_Slave::stop (Parser& /*parser*/, nframes64_t /*timestamp*/)
+MIDIClock_Slave::stop (Parser& /*parser*/, framepos_t /*timestamp*/)
 {
-       #ifdef DEBUG_MIDI_CLOCK
-               std::cerr << "MIDIClock_Slave got stop message" << endl;
-       #endif
+       DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got stop message\n");
 
        if (_started || _starting) {
                _starting = false;
@@ -252,7 +239,7 @@ MIDIClock_Slave::stop (Parser& /*parser*/, nframes64_t /*timestamp*/)
                // that is the position of the last MIDI Clock
                // message and that is probably what the master
                // expects where we are right now
-               nframes64_t stop_position = should_be_position;
+               framepos_t stop_position = should_be_position;
 
                // find out the last MIDI beat: go back #midi_clocks mod 6
                // and lets hope the tempo didnt change in those last 6 beats :)
@@ -265,7 +252,7 @@ MIDIClock_Slave::stop (Parser& /*parser*/, nframes64_t /*timestamp*/)
 }
 
 void
-MIDIClock_Slave::position (Parser& /*parser*/, byte* message, size_t size)
+MIDIClock_Slave::position (Parser& /*parser*/, MIDI::byte* message, size_t size)
 {
        // we are note supposed to get position messages while we are running
        // so lets be robust and ignore those
@@ -274,16 +261,14 @@ MIDIClock_Slave::position (Parser& /*parser*/, byte* message, size_t size)
        }
 
        assert(size == 3);
-       byte lsb = message[1];
-       byte msb = message[2];
+       MIDI::byte lsb = message[1];
+       MIDI::byte msb = message[2];
        assert((lsb <= 0x7f) && (msb <= 0x7f));
 
        uint16_t position_in_sixteenth_notes = (uint16_t(msb) << 7) | uint16_t(lsb);
-       nframes64_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
+       framepos_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
 
-       #ifdef DEBUG_MIDI_CLOCK
-       cerr << "Song Position: " << position_in_sixteenth_notes << " frames: " << position_in_frames << endl;
-       #endif
+       DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Song Position: %1 frames: %2\n", position_in_sixteenth_notes, position_in_frames));
 
        session->request_locate(position_in_frames, false);
        should_be_position  = position_in_frames;
@@ -310,15 +295,13 @@ MIDIClock_Slave::starting() const
 }
 
 bool
-MIDIClock_Slave::stop_if_no_more_clock_events(nframes64_t& pos, nframes64_t now)
+MIDIClock_Slave::stop_if_no_more_clock_events(framepos_t& pos, framepos_t now)
 {
        /* no timecode for 1/4 second ? conclude that its stopped */
        if (last_timestamp &&
            now > last_timestamp &&
            now - last_timestamp > session->frame_rate() / 4) {
-        #ifdef DEBUG_MIDI_CLOCK
-                       cerr << "No MIDI Clock frames received for some time, stopping!" << endl;
-        #endif
+               DEBUG_TRACE (DEBUG::MidiClock, "No MIDI Clock frames received for some time, stopping!\n");
                pos = should_be_position;
                session->request_transport_speed (0);
                session->request_locate (should_be_position, false);
@@ -329,7 +312,7 @@ MIDIClock_Slave::stop_if_no_more_clock_events(nframes64_t& pos, nframes64_t now)
 }
 
 bool
-MIDIClock_Slave::speed_and_position (double& speed, nframes64_t& pos)
+MIDIClock_Slave::speed_and_position (double& speed, framepos_t& pos)
 {
        if (!_started || _starting) {
                speed = 0.0;
@@ -337,7 +320,7 @@ MIDIClock_Slave::speed_and_position (double& speed, nframes64_t& pos)
                return true;
        }
 
-       nframes64_t engine_now = session->frame_time();
+       framepos_t engine_now = session->frame_time();
 
        if (stop_if_no_more_clock_events(pos, engine_now)) {
                return false;
@@ -346,28 +329,43 @@ MIDIClock_Slave::speed_and_position (double& speed, nframes64_t& pos)
        // calculate speed
        speed = ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames;
 
+       // provide a 0.1% deadzone to lock the speed
+       if (fabs(speed - 1.0) <= 0.001)
+               speed = 1.0;
+
        // calculate position
        if (engine_now > last_timestamp) {
                // we are in between MIDI clock messages
                // so we interpolate position according to speed
-               nframes64_t elapsed = engine_now - last_timestamp;
-               pos = (nframes64_t) (should_be_position + double(elapsed) * speed);
+               framecnt_t elapsed = engine_now - last_timestamp;
+               pos = (framepos_t) (should_be_position + double(elapsed) * speed);
        } else {
                // A new MIDI clock message has arrived this cycle
                pos = should_be_position;
        }
 
-       #ifdef DEBUG_MIDI_CLOCK
-       cerr << "speed_and_position: " << speed << " & " << pos << " <-> " << session->transport_frame() << " (transport)" << endl;
-       #endif
+       DEBUG_TRACE (DEBUG::MidiClock, string_compose ("speed_and_position: %1 & %2 <-> %3 (transport)\n", speed, pos, session->transport_frame()));
 
        return true;
 }
 
-ARDOUR::nframes_t
+ARDOUR::framecnt_t
 MIDIClock_Slave::resolution() const
 {
        // one beat
-       return (nframes_t) one_ppqn_in_frames * ppqn;
+       return (framecnt_t) one_ppqn_in_frames * ppqn;
+}
+
+std::string
+MIDIClock_Slave::approximate_current_delta() const
+{
+       char delta[80];
+       if (last_timestamp == 0 || _starting) {
+               snprintf(delta, sizeof(delta), "\u2012\u2012\u2012\u2012");
+       } 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));
+       }
+       return std::string(delta);
 }