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