80fac38a2a2c8035f019f1754c395134a8a8f33c
[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 class Slave can be used to sync ardours tempo to an external source
43  * like MTC, MIDI Clock, etc.
44  * 
45  * The name of the class may be a bit misleading: A subclass of Slave actually
46  * acts as a master for Ardour, that means Ardour will try to follow the
47  * speed and transport position of the implementation of Slave.
48  * Therefor 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          *     (Otherwise Session:average_slave_delta will become negative, and
100          *      the transport will move silently)
101          *   </li>
102          *       <li>Slave::locked() should return true, otherwise Session::no_roll will be called</li>
103          *       <li>Slave::starting() should be false, otherwise the transport will not move until it becomes true</li>         *   
104          * </ul>
105          * 
106          * @param speed - The transport speed requested
107          * @param position - The transport position requested
108          */
109         virtual bool speed_and_position (float& speed, nframes_t& position) = 0;
110         
111         /**
112          * reports to ardour whether the Slave is currently synced to its external 
113          * time source.
114          * 
115          * @return - when returning false, the transport will stop rolling
116          */
117         virtual bool locked() const = 0;
118         
119         /**
120          * reports to ardour whether the slave is in a sane state
121          * 
122          * @return - when returning false, the transport will be stopped and the slave 
123          * disconnected from ardour.
124          */
125         virtual bool ok() const = 0;
126         
127         /**
128          * reports to ardour whether the slave is in the process of starting
129          * to roll
130          * 
131          * @return - when returning false, transport will not move until this method returns true
132          */
133         virtual bool starting() const { return false; }
134         
135         /**
136          * @return - the timing resolution of the Slave - If the distance of ardours transport
137          * to the slave becomes negative or greater than the resolution, sound will stop
138          * (Session::follow_slave label silent_motion)
139          */
140         virtual nframes_t resolution() const = 0;
141         
142         /**
143          * @return - when returning true, ardour will wait for one second before transport
144          * starts rolling
145          */
146         virtual bool requires_seekahead () const = 0;
147         
148         /**
149          * @return - when returning true, ardour will use transport speed 1.0 no matter what 
150          *           the slave returns
151          */
152         virtual bool is_always_synced() const { return false; }
153 };
154
155 struct SafeTime {
156     int guard1;
157     nframes_t   position;
158     nframes_t   timestamp;
159     int guard2;
160
161     SafeTime() {
162             guard1 = 0;
163             guard2 = 0;
164             timestamp = 0;
165     }
166 };
167
168 class MTC_Slave : public Slave, public sigc::trackable {
169   public:
170         MTC_Slave (Session&, MIDI::Port&);
171         ~MTC_Slave ();
172
173         void rebind (MIDI::Port&);
174         bool speed_and_position (float&, nframes_t&);
175
176         bool locked() const;
177         bool ok() const;
178         void handle_locate (const MIDI::byte*);
179
180         nframes_t resolution() const;
181         bool requires_seekahead () const { return true; }
182
183   private:
184         Session&    session;
185         MIDI::Port* port;
186         std::vector<sigc::connection> connections;
187         bool        can_notify_on_unknown_rate;
188
189         SafeTime    current;
190         nframes_t   mtc_frame;               /* current time */
191         nframes_t   last_inbound_frame;      /* when we got it; audio clocked */
192
193         float       mtc_speed;
194         nframes_t   first_mtc_frame;
195         nframes_t   first_mtc_time;
196
197         static const int32_t accumulator_size = 128;
198         float   accumulator[accumulator_size];
199         int32_t accumulator_index;
200         bool    have_first_accumulated_speed;
201
202         void reset ();
203         void update_mtc_qtr (MIDI::Parser&);
204         void update_mtc_time (const MIDI::byte *, bool);
205         void update_mtc_status (MIDI::Parser::MTC_Status);
206         void read_current (SafeTime *) const;
207 };
208
209 class MIDIClock_Slave : public Slave, public sigc::trackable {
210   public:
211         MIDIClock_Slave (Session&, MIDI::Port&, int ppqn = 24);
212         ~MIDIClock_Slave ();
213
214         void rebind (MIDI::Port&);
215         bool speed_and_position (float&, nframes_t&);
216
217         bool locked() const;
218         bool ok() const;
219         bool starting() const { return false; }
220
221         nframes_t resolution() const;
222         bool requires_seekahead () const { return false; }
223
224   private:
225         Session&    session;
226         MIDI::Port* port;
227         std::vector<sigc::connection> connections;
228
229         /// pulses per quarter note for one MIDI clock frame (default 24)
230         int         ppqn;
231         
232         /// the duration of one ppqn in frame time
233         double      one_ppqn_in_frames;
234
235         /// the time stamp and transport position of the last inbound MIDI clock message
236         SafeTime    current;
237         
238         /// The duration of the current MIDI clock frame in frames
239         nframes_t   current_midi_clock_frame_duration;
240         /// the timestamp of the last inbound MIDI clock message
241         nframes_t   last_inbound_frame;             
242
243         /// how many MIDI clock frames to average over
244         static const int32_t accumulator_size = 4;
245         float   accumulator[accumulator_size];
246         int32_t accumulator_index;
247         
248         /// the running average of current_midi_clock_frame_duration
249         float   average_midi_clock_frame_duration;
250
251         void reset ();
252         void start (MIDI::Parser& parser, nframes_t timestamp);
253         void stop (MIDI::Parser& parser, nframes_t timestamp);
254         void update_midi_clock (MIDI::Parser& parser, nframes_t timestamp);
255         void read_current (SafeTime *) const;
256
257         /// whether transport should be rolling
258         bool _started;
259         
260         /// is true if the MIDI Start message has just been received until
261         /// the first call of speed_and_position(...)
262         bool _starting;
263 };
264
265 class ADAT_Slave : public Slave
266 {
267   public:
268         ADAT_Slave () {}
269         ~ADAT_Slave () {}
270
271         bool speed_and_position (float& speed, nframes_t& pos) {
272                 speed = 0;
273                 pos = 0;
274                 return false;
275         }
276
277         bool locked() const { return false; }
278         bool ok() const { return false; }
279         nframes_t resolution() const { return 1; }
280         bool requires_seekahead () const { return true; }
281 };
282
283 class JACK_Slave : public Slave
284 {
285   public:
286         JACK_Slave (jack_client_t*);
287         ~JACK_Slave ();
288
289         bool speed_and_position (float& speed, nframes_t& pos);
290
291         bool starting() const { return _starting; }
292         bool locked() const;
293         bool ok() const;
294         nframes_t resolution() const { return 1; }
295         bool requires_seekahead () const { return false; }
296         void reset_client (jack_client_t* jack);
297         bool is_always_synced() const { return true; }
298
299   private:
300         jack_client_t* jack;
301         float speed;
302         bool _starting;
303 };
304
305 } /* namespace */
306
307 #endif /* __ardour_slave_h__ */