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 "timecode/time.h"
33 #include "ardour/types.h"
34 #include "midi++/parser.h"
35 #include "midi++/types.h"
53 * @brief The Slave interface can be used to sync ARDOURs tempo to an external source
54 * like MTC, MIDI Clock, etc.
56 * The name of the interface may be a bit misleading: A subclass of Slave actually
57 * acts as a time master for ARDOUR, that means ARDOUR will try to follow the
58 * speed and transport position of the implementation of Slave.
59 * Therefore it is rather that class, that makes ARDOUR a slave by connecting it
60 * to its external time master.
68 * This is the most important function to implement:
69 * Each process cycle, Session::follow_slave will call this method.
70 * and after the method call they should
72 * Session::follow_slave will then try to follow the given
73 * <em>position</em> using a delay locked loop (DLL),
74 * starting with the first given transport speed.
75 * If the values of speed and position contradict each other,
76 * ARDOUR will always follow the position and disregard the speed.
77 * Although, a correct speed is important so that ARDOUR
78 * can sync to the master time source quickly.
80 * For background information on delay locked loops,
81 * see http://www.kokkinizita.net/papers/usingdll.pdf
83 * The method has the following precondition:
86 * Slave::ok() should return true, otherwise playback will stop
87 * immediately and the method will not be called
90 * when the references speed and position are passed into the Slave
91 * they are uninitialized
95 * After the method call the following postconditions should be met:
98 * The first position value on transport start should be 0,
99 * otherwise ARDOUR will try to locate to the new position
100 * rather than move to it
103 * the references speed and position should be assigned
104 * to the Slaves current requested transport speed
105 * and transport position.
108 * Slave::resolution() should be greater than the maximum distance of
109 * ARDOURs transport position to the slaves requested transport position.
111 * <li>Slave::locked() should return true, otherwise Session::no_roll will be called</li>
112 * <li>Slave::starting() should be false, otherwise the transport will not move until it becomes true</li> *
115 * @param speed - The transport speed requested
116 * @param position - The transport position requested
117 * @return - The return value is currently ignored (see Session::follow_slave)
119 virtual bool speed_and_position (double& speed, framepos_t& position) = 0;
122 * reports to ARDOUR whether the Slave is currently synced to its external
125 * @return - when returning false, the transport will stop rolling
127 virtual bool locked() const = 0;
130 * reports to ARDOUR whether the slave is in a sane state
132 * @return - when returning false, the transport will be stopped and the slave
133 * disconnected from ARDOUR.
135 virtual bool ok() const = 0;
138 * reports to ARDOUR whether the slave is in the process of starting
141 * @return - when returning false, transport will not move until this method returns true
143 virtual bool starting() const { return false; }
146 * @return - the timing resolution of the Slave - If the distance of ARDOURs transport
147 * to the slave becomes greater than the resolution, sound will stop
149 virtual framecnt_t resolution() const = 0;
152 * @return - when returning true, ARDOUR will wait for seekahead_distance() before transport
155 virtual bool requires_seekahead () const = 0;
158 * @return the number of frames that this slave wants to seek ahead. Relevant
159 * only if requires_seekahead() returns true.
162 virtual framecnt_t seekahead_distance() const { return 0; }
165 * @return - when returning true, ARDOUR will use transport speed 1.0 no matter what
168 virtual bool is_always_synced() const { return false; }
171 * @return - whether ARDOUR should use the slave speed without any adjustments
173 virtual bool give_slave_full_control_over_transport_speed() const { return false; }
176 * @return - current time-delta between engine and sync-source
178 virtual std::string approximate_current_delta() const { return ""; }
182 /// We need this wrapper for testability, it's just too hard to mock up a session class
183 class ISlaveSessionProxy {
185 virtual ~ISlaveSessionProxy() {}
186 virtual TempoMap& tempo_map() const { return *((TempoMap *) 0); }
187 virtual framecnt_t frame_rate() const { return 0; }
188 virtual framepos_t audible_frame () const { return 0; }
189 virtual framepos_t transport_frame () const { return 0; }
190 virtual pframes_t frames_since_cycle_start () const { return 0; }
191 virtual framepos_t frame_time () const { return 0; }
193 virtual void request_locate (framepos_t /*frame*/, bool with_roll = false) {
196 virtual void request_transport_speed (double /*speed*/) {}
200 /// The Session Proxy for use in real Ardour
201 class SlaveSessionProxy : public ISlaveSessionProxy {
205 SlaveSessionProxy(Session &s) : session(s) {}
207 TempoMap& tempo_map() const;
208 framecnt_t frame_rate() const;
209 framepos_t audible_frame () const;
210 framepos_t transport_frame () const;
211 pframes_t frames_since_cycle_start () const;
212 framepos_t frame_time () const;
214 void request_locate (framepos_t frame, bool with_roll = false);
215 void request_transport_speed (double speed);
221 framepos_t timestamp;
234 class TimecodeSlave : public Slave {
238 virtual Timecode::TimecodeFormat apparent_timecode_format() const = 0;
240 /* this is intended to be used by a UI and polled from a timeout. it should
241 return a string describing the current position of the TC source. it
242 should NOT do any computation, but should use a cached value
243 of the TC source position.
245 virtual std::string approximate_current_position() const = 0;
248 class MTC_Slave : public TimecodeSlave {
250 MTC_Slave (Session&, MIDI::Port&);
253 void rebind (MIDI::Port&);
254 bool speed_and_position (double&, framepos_t&);
258 void handle_locate (const MIDI::byte*);
260 framecnt_t resolution () const;
261 bool requires_seekahead () const { return true; }
262 framecnt_t seekahead_distance() const;
263 bool give_slave_full_control_over_transport_speed() const;
264 std::string approximate_current_delta() const;
266 Timecode::TimecodeFormat apparent_timecode_format() const;
267 std::string approximate_current_position() const;
272 PBD::ScopedConnectionList port_connections;
273 bool can_notify_on_unknown_rate;
275 static const int frame_tolerance;
278 framepos_t mtc_frame; /* current time */
279 framepos_t last_inbound_frame; /* when we got it; audio clocked */
280 MIDI::byte last_mtc_fps_byte;
281 framepos_t window_begin;
282 framepos_t window_end;
283 framepos_t first_mtc_timestamp;
284 bool did_reset_tc_format;
285 Timecode::TimecodeFormat saved_tc_format;
286 Glib::Threads::Mutex reset_lock;
287 uint32_t reset_pending;
289 int transport_direction;
293 double speedup_due_to_tc_mismatch;
294 double quarter_frame_duration;
295 Timecode::TimecodeFormat mtc_timecode;
296 Timecode::TimecodeFormat a3e_timecode;
297 Timecode::Time timecode;
298 bool printed_timecode_warning;
299 frameoffset_t current_delta;
301 /* DLL - chase MTC */
302 double t0; ///< time at the beginning of the MTC quater frame
303 double t1; ///< calculated end of the MTC quater frame
304 double e2; ///< second order loop error
305 double b, c, omega; ///< DLL filter coefficients
307 /* DLL - sync engine */
308 int engine_dll_initstate;
309 double te0; ///< time at the beginning of the engine process
310 double te1; ///< calculated sync time
311 double ee2; ///< second order loop error
312 double be, ce, oe; ///< DLL filter coefficients
314 void reset (bool with_pos);
315 void queue_reset (bool with_pos);
318 void update_mtc_qtr (MIDI::Parser&, int, framepos_t);
319 void update_mtc_time (const MIDI::byte *, bool, framepos_t);
320 void update_mtc_status (MIDI::MTC_Status);
321 void read_current (SafeTime *) const;
322 void reset_window (framepos_t);
323 bool outside_window (framepos_t) const;
324 void init_mtc_dll(framepos_t, double);
325 void init_engine_dll (framepos_t, framepos_t);
329 class LTC_Slave : public TimecodeSlave {
331 LTC_Slave (Session&);
334 bool speed_and_position (double&, framepos_t&);
339 framecnt_t resolution () const;
340 bool requires_seekahead () const { return true; }
341 framecnt_t seekahead_distance() const;
342 bool give_slave_full_control_over_transport_speed() const;
344 Timecode::TimecodeFormat apparent_timecode_format() const;
345 std::string approximate_current_position() const;
348 int parse_ltc(const jack_nframes_t, const jack_default_audio_sample_t * const, const framecnt_t);
349 bool process_ltc(framepos_t, framecnt_t);
350 void init_ltc_dll(framepos_t, double);
351 void detect_ltc_fps(int, bool);
354 bool did_reset_tc_format;
355 Timecode::TimecodeFormat saved_tc_format;
358 Timecode::Time timecode;
359 double frames_per_ltc_frame;
361 framecnt_t last_timestamp;
362 framecnt_t last_ltc_frame;
363 framepos_t ltc_transport_pos;
366 int ltc_detect_fps_cnt;
367 int ltc_detect_fps_max;
368 Timecode::TimecodeFormat ltc_timecode;
369 Timecode::TimecodeFormat a3e_timecode;
370 bool printed_timecode_warning;
372 /* DLL - chase MTC */
373 double t0; ///< time at the beginning of the MTC quater frame
374 double t1; ///< calculated end of the MTC quater frame
375 double e2; ///< second order loop error
376 double b, c, omega; ///< DLL filter coefficients
380 class MIDIClock_Slave : public Slave {
382 MIDIClock_Slave (Session&, MIDI::Port&, int ppqn = 24);
384 /// Constructor for unit tests
385 MIDIClock_Slave (ISlaveSessionProxy* session_proxy = 0, int ppqn = 24);
388 void rebind (MIDI::Port&);
389 bool speed_and_position (double&, framepos_t&);
393 bool starting() const;
395 framecnt_t resolution () const;
396 bool requires_seekahead () const { return false; }
397 bool give_slave_full_control_over_transport_speed() const { return true; }
399 void set_bandwidth (double a_bandwith) { bandwidth = a_bandwith; }
402 ISlaveSessionProxy* session;
404 PBD::ScopedConnectionList port_connections;
406 /// pulses per quarter note for one MIDI clock frame (default 24)
409 /// the duration of one ppqn in frame time
410 double one_ppqn_in_frames;
412 /// the timestamp of the first MIDI clock message
413 framepos_t first_timestamp;
415 /// the time stamp and should-be transport position of the last inbound MIDI clock message
416 framepos_t last_timestamp;
417 double should_be_position;
419 /// the number of midi clock messages received (zero-based)
421 long midi_clock_count;
423 //the delay locked loop (DLL), see www.kokkinizita.net/papers/usingdll.pdf
425 /// time at the beginning of the MIDI clock frame
428 /// calculated end of the MIDI clock frame
431 /// loop error = real value - expected value
434 /// second order loop error
437 /// DLL filter bandwidth
440 /// DLL filter coefficients
444 void start (MIDI::Parser& parser, framepos_t timestamp);
445 void contineu (MIDI::Parser& parser, framepos_t timestamp);
446 void stop (MIDI::Parser& parser, framepos_t timestamp);
447 void position (MIDI::Parser& parser, MIDI::byte* message, size_t size);
448 // we can't use continue because it is a C++ keyword
449 void calculate_one_ppqn_in_frames_at(framepos_t time);
450 framepos_t calculate_song_position(uint16_t song_position_in_sixteenth_notes);
451 void calculate_filter_coefficients();
452 void update_midi_clock (MIDI::Parser& parser, framepos_t timestamp);
453 void read_current (SafeTime *) const;
454 bool stop_if_no_more_clock_events(framepos_t& pos, framepos_t now);
456 /// whether transport should be rolling
459 /// is true if the MIDI Start message has just been received until
460 /// the first MIDI Clock Event
464 class JACK_Slave : public Slave
467 JACK_Slave (jack_client_t*);
470 bool speed_and_position (double& speed, framepos_t& pos);
472 bool starting() const { return _starting; }
475 framecnt_t resolution () const { return 1; }
476 bool requires_seekahead () const { return false; }
477 void reset_client (jack_client_t* jack);
478 bool is_always_synced() const { return true; }
488 #endif /* __ardour_slave_h__ */