2 Copyright (C) 2002 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __ardour_slave_h__
21 #define __ardour_slave_h__
25 #include <glibmm/threads.h>
27 #include <jack/jack.h>
29 #include "pbd/signals.h"
31 #include "ardour/types.h"
32 #include "midi++/parser.h"
33 #include "midi++/types.h"
51 * @brief The Slave interface can be used to sync ARDOURs tempo to an external source
52 * like MTC, MIDI Clock, etc.
54 * The name of the interface may be a bit misleading: A subclass of Slave actually
55 * acts as a time master for ARDOUR, that means ARDOUR will try to follow the
56 * speed and transport position of the implementation of Slave.
57 * Therefore it is rather that class, that makes ARDOUR a slave by connecting it
58 * to its external time master.
66 * This is the most important function to implement:
67 * Each process cycle, Session::follow_slave will call this method.
68 * and after the method call they should
70 * Session::follow_slave will then try to follow the given
71 * <em>position</em> using a delay locked loop (DLL),
72 * starting with the first given transport speed.
73 * If the values of speed and position contradict each other,
74 * ARDOUR will always follow the position and disregard the speed.
75 * Although, a correct speed is important so that ARDOUR
76 * can sync to the master time source quickly.
78 * For background information on delay locked loops,
79 * see http://www.kokkinizita.net/papers/usingdll.pdf
81 * The method has the following precondition:
84 * Slave::ok() should return true, otherwise playback will stop
85 * immediately and the method will not be called
88 * when the references speed and position are passed into the Slave
89 * they are uninitialized
93 * After the method call the following postconditions should be met:
96 * The first position value on transport start should be 0,
97 * otherwise ARDOUR will try to locate to the new position
98 * rather than move to it
101 * the references speed and position should be assigned
102 * to the Slaves current requested transport speed
103 * and transport position.
106 * Slave::resolution() should be greater than the maximum distance of
107 * ARDOURs transport position to the slaves requested transport position.
109 * <li>Slave::locked() should return true, otherwise Session::no_roll will be called</li>
110 * <li>Slave::starting() should be false, otherwise the transport will not move until it becomes true</li> *
113 * @param speed - The transport speed requested
114 * @param position - The transport position requested
115 * @return - The return value is currently ignored (see Session::follow_slave)
117 virtual bool speed_and_position (double& speed, framepos_t& position) = 0;
120 * reports to ARDOUR whether the Slave is currently synced to its external
123 * @return - when returning false, the transport will stop rolling
125 virtual bool locked() const = 0;
128 * reports to ARDOUR whether the slave is in a sane state
130 * @return - when returning false, the transport will be stopped and the slave
131 * disconnected from ARDOUR.
133 virtual bool ok() const = 0;
136 * reports to ARDOUR whether the slave is in the process of starting
139 * @return - when returning false, transport will not move until this method returns true
141 virtual bool starting() const { return false; }
144 * @return - the timing resolution of the Slave - If the distance of ARDOURs transport
145 * to the slave becomes greater than the resolution, sound will stop
147 virtual framecnt_t resolution() const = 0;
150 * @return - when returning true, ARDOUR will wait for seekahead_distance() before transport
153 virtual bool requires_seekahead () const = 0;
156 * @return the number of frames that this slave wants to seek ahead. Relevant
157 * only if requires_seekahead() returns true.
160 virtual framecnt_t seekahead_distance() const { return 0; }
163 * @return - when returning true, ARDOUR will use transport speed 1.0 no matter what
166 virtual bool is_always_synced() const { return false; }
169 * @return - whether ARDOUR should use the slave speed without any adjustments
171 virtual bool give_slave_full_control_over_transport_speed() const { return false; }
174 /// We need this wrapper for testability, it's just too hard to mock up a session class
175 class ISlaveSessionProxy {
177 virtual ~ISlaveSessionProxy() {}
178 virtual TempoMap& tempo_map() const { return *((TempoMap *) 0); }
179 virtual framecnt_t frame_rate() const { return 0; }
180 virtual framepos_t audible_frame () const { return 0; }
181 virtual framepos_t transport_frame () const { return 0; }
182 virtual pframes_t frames_since_cycle_start () const { return 0; }
183 virtual framepos_t frame_time () const { return 0; }
185 virtual void request_locate (framepos_t /*frame*/, bool with_roll = false) {
188 virtual void request_transport_speed (double /*speed*/) {}
192 /// The Session Proxy for use in real Ardour
193 class SlaveSessionProxy : public ISlaveSessionProxy {
197 SlaveSessionProxy(Session &s) : session(s) {}
199 TempoMap& tempo_map() const;
200 framecnt_t frame_rate() const;
201 framepos_t audible_frame () const;
202 framepos_t transport_frame () const;
203 pframes_t frames_since_cycle_start () const;
204 framepos_t frame_time () const;
206 void request_locate (framepos_t frame, bool with_roll = false);
207 void request_transport_speed (double speed);
213 framepos_t timestamp;
226 class MTC_Slave : public Slave {
228 MTC_Slave (Session&, MIDI::Port&);
231 void rebind (MIDI::Port&);
232 bool speed_and_position (double&, framepos_t&);
236 void handle_locate (const MIDI::byte*);
238 framecnt_t resolution () const;
239 bool requires_seekahead () const { return true; }
240 framecnt_t seekahead_distance() const;
241 bool give_slave_full_control_over_transport_speed() const;
246 PBD::ScopedConnectionList port_connections;
247 bool can_notify_on_unknown_rate;
249 static const int frame_tolerance;
252 framepos_t mtc_frame; /* current time */
253 framepos_t last_inbound_frame; /* when we got it; audio clocked */
254 MIDI::byte last_mtc_fps_byte;
255 framepos_t window_begin;
256 framepos_t window_end;
257 framepos_t first_mtc_timestamp;
258 bool did_reset_tc_format;
259 Timecode::TimecodeFormat saved_tc_format;
260 Glib::Threads::Mutex reset_lock;
261 uint32_t reset_pending;
263 int transport_direction;
267 double speedup_due_to_tc_mismatch;
268 double quarter_frame_duration;
269 Timecode::TimecodeFormat mtc_timecode;
270 Timecode::TimecodeFormat a3e_timecode;
271 bool printed_timecode_warning;
273 /* DLL - chase MTC */
274 double t0; ///< time at the beginning of the MTC quater frame
275 double t1; ///< calculated end of the MTC quater frame
276 double e2; ///< second order loop error
277 double b, c, omega; ///< DLL filter coefficients
279 /* DLL - sync engine */
280 int engine_dll_initstate;
281 double te0; ///< time at the beginning of the engine process
282 double te1; ///< calculated sync time
283 double ee2; ///< second order loop error
284 double be, ce, oe; ///< DLL filter coefficients
286 void reset (bool with_pos);
287 void queue_reset (bool with_pos);
290 void update_mtc_qtr (MIDI::Parser&, int, framepos_t);
291 void update_mtc_time (const MIDI::byte *, bool, framepos_t);
292 void update_mtc_status (MIDI::MTC_Status);
293 void read_current (SafeTime *) const;
294 void reset_window (framepos_t);
295 bool outside_window (framepos_t) const;
296 void init_mtc_dll(framepos_t, double);
297 void init_engine_dll (framepos_t, framepos_t);
301 class LTC_Slave : public Slave {
303 LTC_Slave (Session&);
306 bool speed_and_position (double&, framepos_t&);
311 framecnt_t resolution () const;
312 bool requires_seekahead () const { return true; }
313 framecnt_t seekahead_distance() const;
314 bool give_slave_full_control_over_transport_speed() const;
317 int parse_ltc(const jack_nframes_t nframes, const jack_default_audio_sample_t * const in, const framecnt_t posinfo);
321 bool did_reset_tc_format;
322 Timecode::TimecodeFormat saved_tc_format;
325 framecnt_t current_frames_per_ltc_frame;
326 framecnt_t monotonic_fcnt;
328 framepos_t ltc_transport_pos;
333 class MIDIClock_Slave : public Slave {
335 MIDIClock_Slave (Session&, MIDI::Port&, int ppqn = 24);
337 /// Constructor for unit tests
338 MIDIClock_Slave (ISlaveSessionProxy* session_proxy = 0, int ppqn = 24);
341 void rebind (MIDI::Port&);
342 bool speed_and_position (double&, framepos_t&);
346 bool starting() const;
348 framecnt_t resolution () const;
349 bool requires_seekahead () const { return false; }
350 bool give_slave_full_control_over_transport_speed() const { return true; }
352 void set_bandwidth (double a_bandwith) { bandwidth = a_bandwith; }
355 ISlaveSessionProxy* session;
357 PBD::ScopedConnectionList port_connections;
359 /// pulses per quarter note for one MIDI clock frame (default 24)
362 /// the duration of one ppqn in frame time
363 double one_ppqn_in_frames;
365 /// the timestamp of the first MIDI clock message
366 framepos_t first_timestamp;
368 /// the time stamp and should-be transport position of the last inbound MIDI clock message
369 framepos_t last_timestamp;
370 double should_be_position;
372 /// the number of midi clock messages received (zero-based)
374 long midi_clock_count;
376 //the delay locked loop (DLL), see www.kokkinizita.net/papers/usingdll.pdf
378 /// time at the beginning of the MIDI clock frame
381 /// calculated end of the MIDI clock frame
384 /// loop error = real value - expected value
387 /// second order loop error
390 /// DLL filter bandwidth
393 /// DLL filter coefficients
397 void start (MIDI::Parser& parser, framepos_t timestamp);
398 void contineu (MIDI::Parser& parser, framepos_t timestamp);
399 void stop (MIDI::Parser& parser, framepos_t timestamp);
400 void position (MIDI::Parser& parser, MIDI::byte* message, size_t size);
401 // we can't use continue because it is a C++ keyword
402 void calculate_one_ppqn_in_frames_at(framepos_t time);
403 framepos_t calculate_song_position(uint16_t song_position_in_sixteenth_notes);
404 void calculate_filter_coefficients();
405 void update_midi_clock (MIDI::Parser& parser, framepos_t timestamp);
406 void read_current (SafeTime *) const;
407 bool stop_if_no_more_clock_events(framepos_t& pos, framepos_t now);
409 /// whether transport should be rolling
412 /// is true if the MIDI Start message has just been received until
413 /// the first MIDI Clock Event
417 class JACK_Slave : public Slave
420 JACK_Slave (jack_client_t*);
423 bool speed_and_position (double& speed, framepos_t& pos);
425 bool starting() const { return _starting; }
428 framecnt_t resolution () const { return 1; }
429 bool requires_seekahead () const { return false; }
430 void reset_client (jack_client_t* jack);
431 bool is_always_synced() const { return true; }
441 #endif /* __ardour_slave_h__ */