incomplete merge of master into windows (requires upcoming changes to master to be...
[ardour.git] / libs / ardour / midi_clock_slave.cc
index 4b6efdd9491bf1b5ba3556d13b96f3b97a806da6..a52854d954be24cfd56b7f49fda8100d5fc4cc7e 100644 (file)
@@ -1,5 +1,6 @@
 /*
-    Copyright (C) 2002-4 Paul Davis
+    Copyright (C) 2008 Paul Davis
+    Author: Hans Baier
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 */
 
+#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/error.h"
+#include "pbd/failed_constructor.h"
+#include "pbd/pthread_utils.h"
+#include "pbd/convert.h"
 
-#include <midi++/port.h>
-#include <ardour/slave.h>
-#include <ardour/session.h>
-#include <ardour/audioengine.h>
-#include <ardour/cycles.h>
-#include <ardour/tempo.h>
+#include "midi++/port.h"
+
+#include "ardour/debug.h"
+#include "ardour/midi_buffer.h"
+#include "ardour/midi_port.h"
+#include "ardour/slave.h"
+#include "ardour/tempo.h"
 
 #include "i18n.h"
 
+using namespace std;
 using namespace ARDOUR;
-using namespace sigc;
 using namespace MIDI;
 using namespace PBD;
 
-MIDIClock_Slave::MIDIClock_Slave (Session& s, MIDI::Port& p, int ppqn)
-       : session (s)
-       , ppqn (ppqn)
+MIDIClock_Slave::MIDIClock_Slave (Session& s, MidiPort& p, int ppqn)
+       : ppqn (ppqn)
+       , bandwidth (10.0 / 60.0) // 1 BpM = 1 / 60 Hz
 {
+       session = (ISlaveSessionProxy *) new SlaveSessionProxy(s);
        rebind (p);
        reset ();
 }
 
+MIDIClock_Slave::MIDIClock_Slave (ISlaveSessionProxy* session_proxy, int ppqn)
+       : session(session_proxy)
+       , ppqn (ppqn)
+       , bandwidth (10.0 / 60.0) // 1 BpM = 1 / 60 Hz
+{
+       reset ();
+}
+
 MIDIClock_Slave::~MIDIClock_Slave()
 {
+       delete session;
 }
 
 void
-MIDIClock_Slave::rebind (MIDI::Port& p)
+MIDIClock_Slave::rebind (MidiPort& port)
 {
-       for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
-               (*i).disconnect ();
-       }
+       DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MIDIClock_Slave: connecting to port %1\n", port.name()));
 
-       port = &p;
+       port_connections.drop_connections ();
 
-       std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
+       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));
 
-       connections.push_back (port->input()->timing.connect (mem_fun (*this, &MIDIClock_Slave::update_midi_clock)));
-       connections.push_back (port->input()->start.connect  (mem_fun (*this, &MIDIClock_Slave::start)));
-       connections.push_back (port->input()->stop.connect   (mem_fun (*this, &MIDIClock_Slave::stop)));
 }
 
 void
-MIDIClock_Slave::update_midi_clock (Parser& parser)
+MIDIClock_Slave::calculate_one_ppqn_in_frames_at(framepos_t time)
 {
-       // ignore clock events if no start event received
-       if(!_started)
-               return;
+       const Tempo& current_tempo = session->tempo_map().tempo_at(time);
+       double frames_per_beat = current_tempo.frames_per_beat(session->frame_rate());
 
-       nframes_t now = session.engine().frame_time();
+       double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
+       double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
 
-       SafeTime last;
-       read_current (&last);
+       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));
+}
 
-       const Tempo& current_tempo = session.tempo_map().tempo_at(now);
-       const Meter& current_meter = session.tempo_map().meter_at(now);
-       double frames_per_beat =
-               current_tempo.frames_per_beat(session.nominal_frame_rate(),
-                                             current_meter);
+ARDOUR::framepos_t
+MIDIClock_Slave::calculate_song_position(uint16_t song_position_in_sixteenth_notes)
+{
+       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 * (framepos_t)(ppqn / 4);
+       }
 
-       double quarter_notes_per_beat = 4.0 / current_tempo.note_type();
-       double frames_per_quarter_note = frames_per_beat / quarter_notes_per_beat;
+       return song_position_frames;
+}
 
-       one_ppqn_in_frames = frames_per_quarter_note / ppqn;
+void
+MIDIClock_Slave::calculate_filter_coefficients()
+{
+       // omega = 2 * PI * Bandwidth / MIDI clock frame frequency in Hz
+       omega = 2.0 * M_PI * bandwidth * one_ppqn_in_frames / session->frame_rate();
+       b = 1.4142135623730950488 * omega;
+       c = omega * omega;
+}
 
-       /*
-          Also compensate for audio latency.
-       */
+void
+MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, framepos_t timestamp)
+{
+       // some pieces of hardware send MIDI Clock all the time
+       if ( (!_starting) && (!_started) ) {
+               return;
+       }
+
+       calculate_one_ppqn_in_frames_at(should_be_position);
 
-       midi_clock_frame += (long) (one_ppqn_in_frames)
-                           + session.worst_output_latency();
+       framepos_t elapsed_since_start = timestamp - first_timestamp;
+       double error = 0;
 
-       /*
-       std::cerr << "got MIDI Clock message at time " << now  
-                 << " result: " << midi_clock_frame 
-                 << " open_ppqn_in_frames: " << one_ppqn_in_frames << std::endl;
-        */
-       
-       if (first_midi_clock_frame == 0) {
-               first_midi_clock_frame = midi_clock_frame;
-               first_midi_clock_time = now;
+       if (_starting || last_timestamp == 0) {
+               midi_clock_count = 0;
+
+               first_timestamp = timestamp;
+               elapsed_since_start = should_be_position;
+
+               // calculate filter coefficients
+               calculate_filter_coefficients();
+
+               // initialize DLL
+               e2 = double(one_ppqn_in_frames) / double(session->frame_rate());
+               t0 = double(elapsed_since_start) / double(session->frame_rate());
+               t1 = t0 + e2;
+
+               // let ardour go after first MIDI Clock Event
+               _starting = false;
+       } else {
+               midi_clock_count++;
+               should_be_position  += one_ppqn_in_frames;
+               calculate_filter_coefficients();
+
+               // calculate loop error
+               // 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->transport_frame()));
+               e = error / double(session->frame_rate());
+               current_delta = error;
+
+               // update DLL
+               t0 = t1;
+               t1 += b * e + e2;
+               e2 += c * e;
        }
 
-       current.guard1++;
-       current.position = midi_clock_frame;
-       current.timestamp = now;
-       current.guard2++;
+       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;
+}
 
-       last_inbound_frame = now;
+void
+MIDIClock_Slave::start (Parser& /*parser*/, framepos_t timestamp)
+{
+       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::start (Parser& parser)
+MIDIClock_Slave::reset ()
 {
-       std::cerr << "MIDIClock_Slave got start message" << endl;
+       should_be_position = session->transport_frame();
+       last_timestamp = 0;
 
-       midi_clock_speed = 1.0f;
-       _started = true;
+       _starting = true;
+       _started  = true;
+
+       // session->request_locate(0, false);
+       current_delta = 0;
 }
 
 void
-MIDIClock_Slave::stop (Parser& parser)
+MIDIClock_Slave::contineu (Parser& /*parser*/, framepos_t /*timestamp*/)
 {
-       std::cerr << "MIDIClock_Slave got stop message" << endl;
+       DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got continue message\n");
 
-       midi_clock_speed = 0.0f;
-       midi_clock_frame = 0;
-       _started = false;
+       if (!_started) {
+               _starting = true;
+               _started  = true;
+       }
+}
 
-       current.guard1++;
-       current.position = midi_clock_frame;
-       current.timestamp = 0;
-       current.guard2++;
+
+void
+MIDIClock_Slave::stop (Parser& /*parser*/, framepos_t /*timestamp*/)
+{
+       DEBUG_TRACE (DEBUG::MidiClock, "MIDIClock_Slave got stop message\n");
+
+       if (_started || _starting) {
+               _starting = false;
+               _started  = false;
+               // locate to last MIDI clock position
+               session->request_transport_speed(0.0);
+
+               // we need to go back to the last MIDI beat (6 ppqn)
+               // and lets hope the tempo didnt change in the meantime :)
+
+               // begin at the should be position, because
+               // that is the position of the last MIDI Clock
+               // message and that is probably what the master
+               // expects where we are right now
+               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 :)
+               stop_position -= (midi_clock_count % 6) * one_ppqn_in_frames;
+
+               session->request_locate(stop_position, false);
+               should_be_position = stop_position;
+               last_timestamp = 0;
+       }
 }
 
 void
-MIDIClock_Slave::read_current (SafeTime *st) const
+MIDIClock_Slave::position (Parser& /*parser*/, MIDI::byte* message, size_t size)
 {
-       int tries = 0;
-       do {
-               if (tries == 10) {
-                       error << _("MIDI Clock Slave: atomic read of current time failed, sleeping!") << endmsg;
-                       usleep (20);
-                       tries = 0;
-               }
-
-               *st = current;
-               tries++;
-
-       } while (st->guard1 != st->guard2);
+       // we are note supposed to get position messages while we are running
+       // so lets be robust and ignore those
+       if (_started || _starting) {
+               return;
+       }
+
+       assert(size == 3);
+       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);
+       framepos_t position_in_frames = calculate_song_position(position_in_sixteenth_notes);
+
+       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;
+       last_timestamp = 0;
+
 }
 
 bool
@@ -170,87 +289,83 @@ MIDIClock_Slave::ok() const
 }
 
 bool
-MIDIClock_Slave::speed_and_position (float& speed, nframes_t& pos)
+MIDIClock_Slave::starting() const
 {
-       //std::cerr << "MIDIClock_Slave speed and position() called" << endl;
-       nframes_t now = session.engine().frame_time();
-       nframes_t frame_rate = session.frame_rate();
-       nframes_t elapsed;
-       float speed_now;
-
-       SafeTime last;
-       read_current (&last);
-
-       if (first_midi_clock_time == 0) {
-               speed = 0;
-               pos = last.position;
-               return true;
-       }
+       return false;
+}
 
+bool
+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_inbound_frame && now > last_inbound_frame && now - last_inbound_frame > frame_rate / 4) {
-               midi_clock_speed = 0;
-               pos = last.position;
-               session.request_locate (pos, false);
-               session.request_transport_speed (0);
-               this->stop(*port->input());
-               reset();
+       if (last_timestamp &&
+           now > last_timestamp &&
+           now - last_timestamp > session->frame_rate() / 4) {
+               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);
+               return true;
+       } else {
                return false;
        }
+}
 
-       speed_now = (float) ((last.position - first_midi_clock_frame) / (double) (now - first_midi_clock_time));
-
-       cerr << "speed_and_position: speed_now: " << speed_now ;
-       
-       midi_clock_speed = speed_now;
+bool
+MIDIClock_Slave::speed_and_position (double& speed, framepos_t& pos)
+{
+       if (!_started || _starting) {
+               speed = 0.0;
+               pos   = should_be_position;
+               return true;
+       }
 
-       if (midi_clock_speed == 0.0f) {
+       framepos_t engine_now = session->frame_time();
 
-               elapsed = 0;
+       if (stop_if_no_more_clock_events(pos, engine_now)) {
+               return false;
+       }
 
-       } else {
+       // calculate speed
+       speed = ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames;
 
-               /* scale elapsed time by the current MIDI Clock speed */
+       // provide a 0.1% deadzone to lock the speed
+       if (fabs(speed - 1.0) <= 0.001)
+               speed = 1.0;
 
-               if (last.timestamp && (now > last.timestamp)) {
-                       elapsed = (nframes_t) floor (midi_clock_speed * (now - last.timestamp));
-               } else {
-                       elapsed = 0; /* XXX is this right? */
-               }
+       // calculate position
+       if (engine_now > last_timestamp) {
+               // we are in between MIDI clock messages
+               // so we interpolate position according to 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;
        }
 
-       /* now add the most recent timecode value plus the estimated elapsed interval */
+       DEBUG_TRACE (DEBUG::MidiClock, string_compose ("speed_and_position: %1 & %2 <-> %3 (transport)\n", speed, pos, session->transport_frame()));
 
-       pos =  elapsed + last.position;
-
-       speed = midi_clock_speed;
-       
-       cerr << " final speed: " << speed << " position: " << pos << endl;
        return true;
 }
 
-ARDOUR::nframes_t
+ARDOUR::framecnt_t
 MIDIClock_Slave::resolution() const
 {
-       return (nframes_t) one_ppqn_in_frames;
+       // one beat
+       return (framecnt_t) one_ppqn_in_frames * ppqn;
 }
 
-void
-MIDIClock_Slave::reset ()
+std::string
+MIDIClock_Slave::approximate_current_delta() const
 {
-       /* XXX massive thread safety issue here. MTC could
-          be being updated as we call this. but this
-          supposed to be a realtime-safe call.
-       */
-
-       last_inbound_frame = 0;
-       current.guard1++;
-       current.position = 0;
-       current.timestamp = 0;
-       current.guard2++;
-       first_midi_clock_frame = 0;
-       first_midi_clock_time = 0;
-
-       midi_clock_speed = 0;
+       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);
 }
+