* changed transport speed in session and slaves from float to double
[ardour.git] / libs / ardour / ardour / slave.h
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef __ardour_slave_h__
21 #define __ardour_slave_h__
22
23 #include <vector>
24
25 #include <jack/jack.h>
26
27 #include <sigc++/signal.h>
28 #include <ardour/ardour.h>
29 #include <midi++/parser.h>
30 #include <midi++/types.h>
31
32 namespace MIDI {
33         class Port;
34 }
35
36 namespace ARDOUR {
37 class Session;
38
39 /**
40  * @class Slave
41  * 
42  * @brief The Slave interface can be used to sync ARDOURs tempo to an external source
43  * like MTC, MIDI Clock, etc.
44  * 
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.
50  */
51 class Slave {
52   public:
53         Slave() { }
54         virtual ~Slave() {}
55
56         /**
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
60          * 
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.
68          * 
69          * For background information on delay locked loops, 
70          * see http://www.kokkinizita.net/papers/usingdll.pdf
71          * 
72          * The method has the following precondition:
73          * <ul>
74          *       <li>
75          *       Slave::ok() should return true, otherwise playback will stop 
76          *       immediately and the method will not be called
77          *   </li> 
78          *   <li>
79          *     when the references speed and position are passed into the Slave
80          *     they are uninitialized
81          *   </li>
82          * </ul>
83          * 
84          * After the method call the following postconditions should be met:
85          * <ul>
86          *        <li>
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
90          *    </li>
91          *        <li>
92          *      the references speed and position should be assigned 
93          *      to the Slaves current requested transport speed
94          *      and transport position.
95          *    </li>
96          *   <li>
97          *     Slave::resolution() should be greater than the maximum distance of 
98          *     ARDOURs transport position to the slaves requested transport position.
99          *   </li>
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>         *   
102          * </ul>
103          * 
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)
107          */
108         virtual bool speed_and_position (double& speed, nframes_t& position) = 0;
109         
110         /**
111          * reports to ARDOUR whether the Slave is currently synced to its external 
112          * time source.
113          * 
114          * @return - when returning false, the transport will stop rolling
115          */
116         virtual bool locked() const = 0;
117         
118         /**
119          * reports to ARDOUR whether the slave is in a sane state
120          * 
121          * @return - when returning false, the transport will be stopped and the slave 
122          * disconnected from ARDOUR.
123          */
124         virtual bool ok() const = 0;
125         
126         /**
127          * reports to ARDOUR whether the slave is in the process of starting
128          * to roll
129          * 
130          * @return - when returning false, transport will not move until this method returns true
131          */
132         virtual bool starting() const { return false; }
133         
134         /**
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
137          */
138         virtual nframes_t resolution() const = 0;
139         
140         /**
141          * @return - when returning true, ARDOUR will wait for one second before transport
142          * starts rolling
143          */
144         virtual bool requires_seekahead () const = 0;
145         
146         /**
147          * @return - when returning true, ARDOUR will use transport speed 1.0 no matter what 
148          *           the slave returns
149          */
150         virtual bool is_always_synced() const { return false; }
151         
152         /**
153          * @return - whether ARDOUR should use the slave speed without any adjustments 
154          */
155         virtual bool give_slave_full_control_over_transport_speed() const { return false; }
156 };
157
158 struct SafeTime {
159     int guard1;
160     nframes_t   position;
161     nframes_t   timestamp;
162     int guard2;
163
164     SafeTime() {
165             guard1 = 0;
166             guard2 = 0;
167             timestamp = 0;
168     }
169 };
170
171 class MTC_Slave : public Slave, public sigc::trackable {
172   public:
173         MTC_Slave (Session&, MIDI::Port&);
174         ~MTC_Slave ();
175
176         void rebind (MIDI::Port&);
177         bool speed_and_position (double&, nframes_t&);
178
179         bool locked() const;
180         bool ok() const;
181         void handle_locate (const MIDI::byte*);
182
183         nframes_t resolution() const;
184         bool requires_seekahead () const { return true; }
185
186   private:
187         Session&    session;
188         MIDI::Port* port;
189         std::vector<sigc::connection> connections;
190         bool        can_notify_on_unknown_rate;
191
192         SafeTime    current;
193         nframes_t   mtc_frame;               /* current time */
194         nframes_t   last_inbound_frame;      /* when we got it; audio clocked */
195         MIDI::byte  last_mtc_fps_byte;
196
197         double      mtc_speed;
198         nframes_t   first_mtc_frame;
199         nframes_t   first_mtc_time;
200
201         static const int32_t accumulator_size = 128;
202         double   accumulator[accumulator_size];
203         int32_t accumulator_index;
204         bool    have_first_accumulated_speed;
205
206         void reset ();
207         void update_mtc_qtr (MIDI::Parser&);
208         void update_mtc_time (const MIDI::byte *, bool);
209         void update_mtc_status (MIDI::Parser::MTC_Status);
210         void read_current (SafeTime *) const;
211 };
212
213 class MIDIClock_Slave : public Slave, public sigc::trackable {
214   public:
215         MIDIClock_Slave (Session&, MIDI::Port&, int ppqn = 24);
216         ~MIDIClock_Slave ();
217
218         void rebind (MIDI::Port&);
219         bool speed_and_position (double&, nframes_t&);
220
221         bool locked() const;
222         bool ok() const;
223         bool starting() const;
224
225         nframes_t resolution() const;
226         bool requires_seekahead () const { return false; }
227         bool give_slave_full_control_over_transport_speed() const { return true; }
228         
229   private:
230         Session&    session;
231         MIDI::Port* port;
232         std::vector<sigc::connection> connections;
233
234         /// pulses per quarter note for one MIDI clock frame (default 24)
235         int         ppqn;
236         
237         /// the duration of one ppqn in frame time
238         double      one_ppqn_in_frames;
239
240         /// the timestamp of the first MIDI clock message
241         nframes_t   first_timestamp;
242         
243         /// the time stamp and transport position of the last inbound MIDI clock message
244         nframes_t   last_timestamp;
245         double      last_position;
246         
247         //the delay locked loop (DLL), see www.kokkinizita.net/papers/usingdll.pdf
248         
249         /// time at the beginning of the MIDI clock frame
250         double t0;
251         
252         /// calculated end of the MIDI clock frame
253         double t1;
254         
255         /// loop error = real value - expected value
256         double e;
257         
258         /// second order loop error
259         double e2;
260         
261         /// DLL filter bandwidth
262         double bandwidth;
263         
264         /// DLL filter coefficients
265         double b, c, omega;
266         
267         void reset ();
268         void start (MIDI::Parser& parser, nframes_t timestamp);
269         void stop (MIDI::Parser& parser, nframes_t timestamp);
270         // we can't use continue because it is a C++ keyword
271         void contineu (MIDI::Parser& parser, nframes_t timestamp);
272         void calculate_one_ppqn_in_frames_at(nframes_t time);
273         void calculate_filter_coefficients();
274         void update_midi_clock (MIDI::Parser& parser, nframes_t timestamp);
275         void read_current (SafeTime *) const;
276         bool stop_if_no_more_clock_events(nframes_t& pos, nframes_t now);
277
278         /// whether transport should be rolling
279         bool _started;
280         
281         /// is true if the MIDI Start message has just been received until
282         /// the first MIDI Clock Event
283         bool _starting;
284 };
285
286 class ADAT_Slave : public Slave
287 {
288   public:
289         ADAT_Slave () {}
290         ~ADAT_Slave () {}
291
292         bool speed_and_position (double& speed, nframes_t& pos) {
293                 speed = 0;
294                 pos = 0;
295                 return false;
296         }
297
298         bool locked() const { return false; }
299         bool ok() const { return false; }
300         nframes_t resolution() const { return 1; }
301         bool requires_seekahead () const { return true; }
302 };
303
304 class JACK_Slave : public Slave
305 {
306   public:
307         JACK_Slave (jack_client_t*);
308         ~JACK_Slave ();
309
310         bool speed_and_position (double& speed, nframes_t& pos);
311
312         bool starting() const { return _starting; }
313         bool locked() const;
314         bool ok() const;
315         nframes_t resolution() const { return 1; }
316         bool requires_seekahead () const { return false; }
317         void reset_client (jack_client_t* jack);
318         bool is_always_synced() const { return true; }
319
320   private:
321         jack_client_t* jack;
322         double speed;
323         bool _starting;
324 };
325
326 } /* namespace */
327
328 #endif /* __ardour_slave_h__ */