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 <jack/jack.h>
27 #include <sigc++/signal.h>
28 #include "ardour/ardour.h"
29 #include "ardour/session.h"
30 #include "ardour/audioengine.h"
31 #include "midi++/parser.h"
32 #include "midi++/types.h"
42 * @brief The Slave interface can be used to sync ARDOURs tempo to an external source
43 * like MTC, MIDI Clock, etc.
45 * The name of the interface may be a bit misleading: A subclass of Slave actually
46 * acts as a time master for ARDOUR, that means ARDOUR will try to follow the
47 * speed and transport position of the implementation of Slave.
48 * Therefore it is rather that class, that makes ARDOUR a slave by connecting it
49 * to its external time master.
57 * This is the most important function to implement:
58 * Each process cycle, Session::follow_slave will call this method.
59 * and after the method call they should
61 * Session::follow_slave will then try to follow the given
62 * <emph>position</emph> using a delay locked loop (DLL),
63 * starting with the first given transport speed.
64 * If the values of speed and position contradict each other,
65 * ARDOUR will always follow the position and disregard the speed.
66 * Although, a correct speed is important so that ARDOUR
67 * can sync to the master time source quickly.
69 * For background information on delay locked loops,
70 * see http://www.kokkinizita.net/papers/usingdll.pdf
72 * The method has the following precondition:
75 * Slave::ok() should return true, otherwise playback will stop
76 * immediately and the method will not be called
79 * when the references speed and position are passed into the Slave
80 * they are uninitialized
84 * After the method call the following postconditions should be met:
87 * The first position value on transport start should be 0,
88 * otherwise ARDOUR will try to locate to the new position
89 * rather than move to it
92 * the references speed and position should be assigned
93 * to the Slaves current requested transport speed
94 * and transport position.
97 * Slave::resolution() should be greater than the maximum distance of
98 * ARDOURs transport position to the slaves requested transport position.
100 * <li>Slave::locked() should return true, otherwise Session::no_roll will be called</li>
101 * <li>Slave::starting() should be false, otherwise the transport will not move until it becomes true</li> *
104 * @param speed - The transport speed requested
105 * @param position - The transport position requested
106 * @return - The return value is currently ignored (see Session::follow_slave)
108 virtual bool speed_and_position (double& speed, nframes_t& position) = 0;
111 * reports to ARDOUR whether the Slave is currently synced to its external
114 * @return - when returning false, the transport will stop rolling
116 virtual bool locked() const = 0;
119 * reports to ARDOUR whether the slave is in a sane state
121 * @return - when returning false, the transport will be stopped and the slave
122 * disconnected from ARDOUR.
124 virtual bool ok() const = 0;
127 * reports to ARDOUR whether the slave is in the process of starting
130 * @return - when returning false, transport will not move until this method returns true
132 virtual bool starting() const { return false; }
135 * @return - the timing resolution of the Slave - If the distance of ARDOURs transport
136 * to the slave becomes greater than the resolution, sound will stop
138 virtual nframes_t resolution() const = 0;
141 * @return - when returning true, ARDOUR will wait for one second before transport
144 virtual bool requires_seekahead () const = 0;
147 * @return - when returning true, ARDOUR will use transport speed 1.0 no matter what
150 virtual bool is_always_synced() const { return false; }
153 * @return - whether ARDOUR should use the slave speed without any adjustments
155 virtual bool give_slave_full_control_over_transport_speed() const { return false; }
158 /// We need this wrapper for testability, it's just too hard to mock up a session class
159 class ISlaveSessionProxy {
161 virtual TempoMap& tempo_map() const { return *((TempoMap *) 0); }
162 virtual nframes_t frame_rate() const { return 0; }
163 virtual nframes_t audible_frame () const { return 0; }
164 virtual nframes_t transport_frame () const { return 0; }
165 virtual nframes_t frames_since_cycle_start () const { return 0; }
166 virtual nframes_t frame_time () const { return 0; }
168 virtual void request_locate (nframes_t /*frame*/, bool with_roll = false) {
171 virtual void request_transport_speed (double /*speed*/) {}
175 /// The Session Proxy for use in real Ardour
176 class SlaveSessionProxy : public ISlaveSessionProxy {
180 SlaveSessionProxy(Session &s) : session(s) {};
181 TempoMap& tempo_map() const { return session.tempo_map(); }
182 nframes_t frame_rate() const { return session.frame_rate(); }
183 nframes_t audible_frame () const { return session.audible_frame(); }
184 nframes_t transport_frame () const { return session.transport_frame(); }
185 nframes_t frames_since_cycle_start () const { return session.engine().frames_since_cycle_start(); }
186 nframes_t frame_time () const { return session.engine().frame_time(); }
188 void request_locate (nframes_t frame, bool with_roll = false) { session.request_locate(frame, with_roll); }
189 void request_transport_speed (double speed) { session.request_transport_speed(speed); }
205 class MTC_Slave : public Slave, public sigc::trackable {
207 MTC_Slave (Session&, MIDI::Port&);
210 void rebind (MIDI::Port&);
211 bool speed_and_position (double&, nframes_t&);
215 void handle_locate (const MIDI::byte*);
217 nframes_t resolution() const;
218 bool requires_seekahead () const { return true; }
223 std::vector<sigc::connection> connections;
224 bool can_notify_on_unknown_rate;
227 nframes_t mtc_frame; /* current time */
228 nframes_t last_inbound_frame; /* when we got it; audio clocked */
229 MIDI::byte last_mtc_fps_byte;
232 nframes_t first_mtc_frame;
233 nframes_t first_mtc_time;
235 static const int32_t accumulator_size = 128;
236 double accumulator[accumulator_size];
237 int32_t accumulator_index;
238 bool have_first_accumulated_speed;
241 void update_mtc_qtr (MIDI::Parser&);
242 void update_mtc_time (const MIDI::byte *, bool);
243 void update_mtc_status (MIDI::Parser::MTC_Status);
244 void read_current (SafeTime *) const;
247 class MIDIClock_Slave : public Slave, public sigc::trackable {
249 MIDIClock_Slave (Session&, MIDI::Port&, int ppqn = 24);
251 /// Constructor for unit tests
252 MIDIClock_Slave (ISlaveSessionProxy* session_proxy, int ppqn = 24);
255 void rebind (MIDI::Port&);
256 bool speed_and_position (double&, nframes_t&);
260 bool starting() const;
262 nframes_t resolution() const;
263 bool requires_seekahead () const { return false; }
264 bool give_slave_full_control_over_transport_speed() const { return true; }
266 void set_bandwidth (double a_bandwith) { bandwidth = a_bandwith; }
269 ISlaveSessionProxy* session;
271 std::vector<sigc::connection> connections;
273 /// pulses per quarter note for one MIDI clock frame (default 24)
276 /// the duration of one ppqn in frame time
277 double one_ppqn_in_frames;
279 /// the timestamp of the first MIDI clock message
280 nframes_t first_timestamp;
282 /// the time stamp and should-be transport position of the last inbound MIDI clock message
283 nframes_t last_timestamp;
284 double should_be_position;
286 /// the number of midi clock messages received (zero-based)
288 long midi_clock_count;
290 //the delay locked loop (DLL), see www.kokkinizita.net/papers/usingdll.pdf
292 /// time at the beginning of the MIDI clock frame
295 /// calculated end of the MIDI clock frame
298 /// loop error = real value - expected value
301 /// second order loop error
304 /// DLL filter bandwidth
307 /// DLL filter coefficients
311 void start (MIDI::Parser& parser, nframes_t timestamp);
312 void contineu (MIDI::Parser& parser, nframes_t timestamp);
313 void stop (MIDI::Parser& parser, nframes_t timestamp);
314 void position (MIDI::Parser& parser, MIDI::byte* message, size_t size);
315 // we can't use continue because it is a C++ keyword
316 void calculate_one_ppqn_in_frames_at(nframes_t time);
317 nframes_t calculate_song_position(uint16_t song_position_in_sixteenth_notes);
318 void calculate_filter_coefficients();
319 void update_midi_clock (MIDI::Parser& parser, nframes_t timestamp);
320 void read_current (SafeTime *) const;
321 bool stop_if_no_more_clock_events(nframes_t& pos, nframes_t now);
323 /// whether transport should be rolling
326 /// is true if the MIDI Start message has just been received until
327 /// the first MIDI Clock Event
331 class ADAT_Slave : public Slave
337 bool speed_and_position (double& speed, nframes_t& pos) {
343 bool locked() const { return false; }
344 bool ok() const { return false; }
345 nframes_t resolution() const { return 1; }
346 bool requires_seekahead () const { return true; }
349 class JACK_Slave : public Slave
352 JACK_Slave (jack_client_t*);
355 bool speed_and_position (double& speed, nframes_t& pos);
357 bool starting() const { return _starting; }
360 nframes_t resolution() const { return 1; }
361 bool requires_seekahead () const { return false; }
362 void reset_client (jack_client_t* jack);
363 bool is_always_synced() const { return true; }
373 #endif /* __ardour_slave_h__ */