0079c10e0a952c2458d78d1597658f5ae94ba3d1
[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     $Id$
19 */
20
21 #ifndef __ardour_slave_h__
22 #define __ardour_slave_h__
23
24 #include <vector>
25
26 #include <jack/jack.h>
27
28 #include <sigc++/signal.h>
29 #include <ardour/ardour.h>
30 #include <midi++/parser.h>
31 #include <midi++/types.h>
32
33 namespace MIDI {
34         class Port;
35 }
36
37 namespace ARDOUR {
38 class Session;
39
40 class Slave {
41   public:
42         Slave() { }
43         virtual ~Slave() {}
44
45         virtual bool speed_and_position (float&, jack_nframes_t&) = 0;
46         virtual bool locked() const = 0;
47         virtual bool ok() const = 0;
48         virtual bool starting() const { return false; }
49         virtual jack_nframes_t resolution() const = 0;
50         virtual bool requires_seekahead () const = 0;
51 };
52
53
54 class MTC_Slave : public Slave, public sigc::trackable {
55   public:
56         MTC_Slave (Session&, MIDI::Port&);
57         ~MTC_Slave ();
58
59         void rebind (MIDI::Port&);
60         bool speed_and_position (float&, jack_nframes_t&);
61
62         bool      locked() const;
63         bool      ok() const;
64         void      handle_locate (const MIDI::byte*);
65
66         jack_nframes_t resolution() const;
67         bool requires_seekahead () const { return true; }
68
69   private:
70         Session&    session;
71         MIDI::Port* port;
72         std::vector<sigc::connection> connections;
73
74         struct SafeTime {
75
76
77             int guard1;
78             //SMPTE_Time  mtc;
79             jack_nframes_t   position;
80             jack_nframes_t   timestamp;
81             int guard2;
82
83             SafeTime() {
84                     guard1 = 0;
85                     guard2 = 0;
86                     timestamp = 0;
87             }
88         };
89
90         SafeTime         current;
91         jack_nframes_t   mtc_frame;               /* current time */
92         jack_nframes_t   last_inbound_frame;      /* when we got it; audio clocked */
93
94         float            mtc_speed;
95         jack_nframes_t   first_mtc_frame;
96         jack_nframes_t   first_mtc_time;
97
98         static const int32_t accumulator_size = 128;
99         float   accumulator[accumulator_size];
100         int32_t accumulator_index;
101         bool    have_first_accumulated_speed;
102
103         void reset ();
104         void update_mtc_qtr (MIDI::Parser&);
105         void update_mtc_time (const MIDI::byte *, bool);
106         void update_mtc_status (MIDI::Parser::MTC_Status);
107         void read_current (SafeTime *) const;
108 };
109
110 class ADAT_Slave : public Slave 
111 {
112   public:
113         ADAT_Slave () {}
114         ~ADAT_Slave () {}
115         
116         bool speed_and_position (float& speed, jack_nframes_t& pos) {
117                 speed = 0;
118                 pos = 0;
119                 return false;
120         }
121
122         bool locked() const { return false; }
123         bool ok() const { return false; }
124         jack_nframes_t resolution() const { return 1; }
125         bool requires_seekahead () const { return true; }
126 };
127
128 class JACK_Slave : public Slave 
129 {
130   public:
131         JACK_Slave (jack_client_t*);
132         ~JACK_Slave ();
133         
134         bool speed_and_position (float& speed, jack_nframes_t& pos);
135
136         bool starting() const { return _starting; }
137         bool locked() const;
138         bool ok() const;
139         jack_nframes_t resolution() const { return 1; }
140         bool requires_seekahead () const { return false; }
141
142   private:
143         jack_client_t* jack;
144         float speed;
145         bool _starting;
146 };
147
148 } /* namespace */
149
150 #endif /* __ardour_slave_h__ */