* forgot to add the midi clock slave cc file
authorHans Baier <hansfbaier@googlemail.com>
Tue, 5 Aug 2008 00:14:03 +0000 (00:14 +0000)
committerHans Baier <hansfbaier@googlemail.com>
Tue, 5 Aug 2008 00:14:03 +0000 (00:14 +0000)
git-svn-id: svn://localhost/ardour2/branches/3.0@3653 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/ardour/midi_clock_slave.cc [new file with mode: 0644]

diff --git a/libs/ardour/midi_clock_slave.cc b/libs/ardour/midi_clock_slave.cc
new file mode 100644 (file)
index 0000000..841dc55
--- /dev/null
@@ -0,0 +1,270 @@
+/*
+    Copyright (C) 2002-4 Paul Davis
+
+    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
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#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 <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 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)
+{
+       rebind (p);
+       reset ();
+}
+
+MIDIClock_Slave::~MIDIClock_Slave()
+{
+}
+
+void
+MIDIClock_Slave::rebind (MIDI::Port& p)
+{
+       for (vector<sigc::connection>::iterator i = connections.begin(); i != connections.end(); ++i) {
+               (*i).disconnect ();
+       }
+
+       port = &p;
+
+       std::cerr << "MIDIClock_Slave: connecting to port " << port->name() << std::endl;
+       port->input()->trace(true, &std::cout, string(port->name()));
+
+       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)
+{
+
+       nframes_t now = session.engine().frame_time();
+       _starting = false;
+
+       std::cerr << "got MIDI Clock message at time " << now << std::endl;
+
+       SafeTime last;
+       read_current (&last);
+
+       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);
+
+       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 / ppqn;
+
+       /*
+          Also compensate for audio latency.
+       */
+
+       midi_clock_frame += (long) (one_ppqn_in_frames)
+                           + session.worst_output_latency();
+
+       if (first_midi_clock_frame == 0) {
+               first_midi_clock_frame = midi_clock_frame;
+               first_midi_clock_time = now;
+       }
+
+       current.guard1++;
+       current.position = midi_clock_frame;
+       current.timestamp = now;
+       current.guard2++;
+
+       last_inbound_frame = now;
+}
+
+void
+MIDIClock_Slave::start (Parser& parser)
+{
+       std::cerr << "MIDIClock_Slave got start message" << endl;
+
+       midi_clock_speed = 1.0f;
+       _starting = true;
+}
+
+void
+MIDIClock_Slave::stop (Parser& parser)
+{
+       std::cerr << "MIDIClock_Slave got stop message" << endl;
+
+       midi_clock_speed = 0.0f;
+       midi_clock_frame = 0;
+       _starting = false;
+
+       current.guard1++;
+       current.position = midi_clock_frame;
+       current.timestamp = 0;
+       current.guard2++;
+}
+
+void
+MIDIClock_Slave::read_current (SafeTime *st) const
+{
+       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);
+}
+
+bool
+MIDIClock_Slave::locked () const
+{
+       return true;
+}
+
+bool
+MIDIClock_Slave::ok() const
+{
+       return true;
+}
+
+bool
+MIDIClock_Slave::speed_and_position (float& speed, nframes_t& pos)
+{
+       //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;
+       }
+
+       /* 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();
+               return false;
+       }
+
+       speed_now = (float) ((last.position - first_midi_clock_frame) / (double) (now - first_midi_clock_time));
+
+       accumulator[accumulator_index++] = speed_now;
+
+       if (accumulator_index >= accumulator_size) {
+               have_first_accumulated_speed = true;
+               accumulator_index = 0;
+       }
+
+       if (have_first_accumulated_speed) {
+               float total = 0;
+
+               for (int32_t i = 0; i < accumulator_size; ++i) {
+                       total += accumulator[i];
+               }
+
+               midi_clock_speed = total / accumulator_size;
+
+       } else {
+
+               midi_clock_speed = speed_now;
+
+       }
+
+       if (midi_clock_speed == 0.0f) {
+
+               elapsed = 0;
+
+       } else {
+
+               /* scale elapsed time by the current MIDI Clock speed */
+
+               if (last.timestamp && (now > last.timestamp)) {
+                       elapsed = (nframes_t) floor (midi_clock_speed * (now - last.timestamp));
+               } else {
+                       elapsed = 0; /* XXX is this right? */
+               }
+       }
+
+       /* now add the most recent timecode value plus the estimated elapsed interval */
+
+       pos =  elapsed + last.position;
+
+       speed = midi_clock_speed;
+       return true;
+}
+
+ARDOUR::nframes_t
+MIDIClock_Slave::resolution() const
+{
+       return (nframes_t) one_ppqn_in_frames;
+}
+
+void
+MIDIClock_Slave::reset ()
+{
+       /* 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;
+
+       accumulator_index = 0;
+       have_first_accumulated_speed = false;
+       midi_clock_speed = 0;
+}