X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fmidi_clock_slave.cc;h=f2dc4f9abb9fea1dc35f449ff8a3e107a8bdd148;hb=cf52d6e4b40111eb04b244ec054055a4ec15dbe0;hp=ef2d22ab18e61088d43a421084c6a0103fed21d5;hpb=2c1b2256b6450ce9237678efab7ae40f2b8fb1b1;p=ardour.git diff --git a/libs/ardour/midi_clock_slave.cc b/libs/ardour/midi_clock_slave.cc index ef2d22ab18..f2dc4f9abb 100644 --- a/libs/ardour/midi_clock_slave.cc +++ b/libs/ardour/midi_clock_slave.cc @@ -20,7 +20,6 @@ #include #include -#include #include #include #include "pbd/error.h" @@ -31,19 +30,21 @@ #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" +#include "pbd/i18n.h" using namespace std; 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 (1.0 / 60.0) // 1 BpM = 1 / 60 Hz + , bandwidth (2.0 / 60.0) // 1 BpM = 1 / 60 Hz { session = (ISlaveSessionProxy *) new SlaveSessionProxy(s); rebind (p); @@ -53,7 +54,7 @@ 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 (1.0 / 60.0) // 1 BpM = 1 / 60 Hz + , bandwidth (2.0 / 60.0) // 1 BpM = 1 / 60 Hz { reset (); } @@ -64,26 +65,25 @@ 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 (); - DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MIDIClock_Slave: connecting to port %1\n", port->name())); + 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->parser()->timing.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::update_midi_clock, this, _1, _2)); - port->parser()->start.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::start, this, _1, _2)); - port->parser()->contineu.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::contineu, this, _1, _2)); - port->parser()->stop.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::stop, this, _1, _2)); - port->parser()->position.connect_same_thread (port_connections, boost::bind (&MIDIClock_Slave::position, this, _1, _2, 3)); } void MIDIClock_Slave::calculate_one_ppqn_in_frames_at(framepos_t time) { - const Tempo& current_tempo = session->tempo_map().tempo_at(time); - double frames_per_beat = current_tempo.frames_per_beat(session->frame_rate()); + const Tempo& current_tempo = session->tempo_map().tempo_at_frame (time); + double const frames_per_beat = session->tempo_map().frames_per_beat_at (time, 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; @@ -122,6 +122,8 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, framepos_t timestamp) return; } + pframes_t cycle_offset = timestamp - session->sample_time_at_cycle_start(); + calculate_one_ppqn_in_frames_at(should_be_position); framepos_t elapsed_since_start = timestamp - first_timestamp; @@ -133,6 +135,8 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, framepos_t timestamp) first_timestamp = timestamp; elapsed_since_start = should_be_position; + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("first clock message after start received @ %1\n", timestamp)); + // calculate filter coefficients calculate_filter_coefficients(); @@ -149,11 +153,12 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, framepos_t timestamp) calculate_filter_coefficients(); // calculate loop error - // we use session->audible_frame() instead of t1 here + // 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()) + double(cycle_offset))); e = error / double(session->frame_rate()); + current_delta = error; // update DLL t0 = t1; @@ -161,21 +166,23 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, framepos_t timestamp) e2 += c * e; } - 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)); + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("clock #%1 @ %2 should-be %3 transport %4 error %5 appspeed %6 " + "read-delta %7 should-be delta %8 t1-t0 %9 t0 %10 t1 %11 framerate %12 engine %13\n", + midi_clock_count, // # + elapsed_since_start, // @ + should_be_position, // should-be + session->transport_frame(), // transport + error, // error + ((t1 - t0) * session->frame_rate()) / one_ppqn_in_frames, // appspeed + timestamp - last_timestamp, // read delta + one_ppqn_in_frames, // should-be delta + (t1 - t0) * session->frame_rate(), // t1-t0 + t0 * session->frame_rate(), // t0 + t1 * session->frame_rate(), // t1 + session->frame_rate(), // framerate + session->frame_time() + + )); last_timestamp = timestamp; } @@ -183,7 +190,7 @@ MIDIClock_Slave::update_midi_clock (Parser& /*parser*/, framepos_t timestamp) 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())); + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MIDIClock_Slave got start message at time %1 engine time %2 transport_frame %3\n", timestamp, session->frame_time(), session->transport_frame())); if (!_started) { reset(); @@ -198,6 +205,8 @@ MIDIClock_Slave::start (Parser& /*parser*/, framepos_t timestamp) void MIDIClock_Slave::reset () { + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("MidiClock_Slave reset(): calculated filter bandwidth is %1 for period size %2\n", bandwidth, session->frames_per_cycle())); + should_be_position = session->transport_frame(); last_timestamp = 0; @@ -251,7 +260,7 @@ MIDIClock_Slave::stop (Parser& /*parser*/, framepos_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 @@ -260,8 +269,8 @@ 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); @@ -328,8 +337,8 @@ MIDIClock_Slave::speed_and_position (double& speed, framepos_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) + // provide a 0.1% deadzone to lock the speed + if (fabs(speed - 1.0) <= 0.001) speed = 1.0; // calculate position @@ -343,8 +352,7 @@ MIDIClock_Slave::speed_and_position (double& speed, framepos_t& pos) pos = should_be_position; } - DEBUG_TRACE (DEBUG::MidiClock, string_compose ("speed_and_position: %1 & %2 <-> %3 (transport)\n", speed, pos, session->transport_frame())); - current_delta = pos - session->transport_frame(); + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("speed_and_position: speed %1 should-be %2 transport %3 \n", speed, pos, session->transport_frame())); return true; } @@ -363,7 +371,7 @@ MIDIClock_Slave::approximate_current_delta() const if (last_timestamp == 0 || _starting) { snprintf(delta, sizeof(delta), "\u2012\u2012\u2012\u2012"); } else { - snprintf(delta, sizeof(delta), "\u0394%s%s%" PRIi64 " sm", + snprintf(delta, sizeof(delta), "\u0394%s%s%" PRIi64 "sm", LEADINGZERO(abs(current_delta)), PLUSMINUS(-current_delta), abs(current_delta)); } return std::string(delta);