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