fix up MTC message delivered internally when locate is noticed (removes stupid messag...
[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 class Slave {
40   public:
41         Slave() { }
42         virtual ~Slave() {}
43
44         virtual bool speed_and_position (float&, nframes_t&) = 0;
45         virtual bool locked() const = 0;
46         virtual bool ok() const = 0;
47         virtual bool starting() const { return false; }
48         virtual nframes_t resolution() const = 0;
49         virtual bool requires_seekahead () const = 0;
50         virtual bool is_always_synced() const { return false; }
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&, nframes_t&);
61
62         bool      locked() const;
63         bool      ok() const;
64         void      handle_locate (const MIDI::byte*);
65
66         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         bool        can_notify_on_unknown_rate;
74
75         struct SafeTime {
76
77
78             int guard1;
79             //SMPTE_Time  mtc;
80             nframes_t   position;
81             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         nframes_t   mtc_frame;               /* current time */
93         nframes_t   last_inbound_frame;      /* when we got it; audio clocked */
94         MIDI::byte  last_mtc_fps_byte;
95
96         float            mtc_speed;
97         nframes_t   first_mtc_frame;
98         nframes_t   first_mtc_time;
99
100         static const int32_t accumulator_size = 128;
101         float   accumulator[accumulator_size];
102         int32_t accumulator_index;
103         bool    have_first_accumulated_speed;
104
105         void reset ();
106         void update_mtc_qtr (MIDI::Parser&);
107         void update_mtc_time (const MIDI::byte *, bool);
108         void update_mtc_status (MIDI::Parser::MTC_Status);
109         void read_current (SafeTime *) const;
110 };
111
112 class ADAT_Slave : public Slave 
113 {
114   public:
115         ADAT_Slave () {}
116         ~ADAT_Slave () {}
117         
118         bool speed_and_position (float& speed, nframes_t& pos) {
119                 speed = 0;
120                 pos = 0;
121                 return false;
122         }
123
124         bool locked() const { return false; }
125         bool ok() const { return false; }
126         nframes_t resolution() const { return 1; }
127         bool requires_seekahead () const { return true; }
128 };
129
130 class JACK_Slave : public Slave 
131 {
132   public:
133         JACK_Slave (jack_client_t*);
134         ~JACK_Slave ();
135         
136         bool speed_and_position (float& speed, nframes_t& pos);
137
138         bool starting() const { return _starting; }
139         bool locked() const;
140         bool ok() const;
141         nframes_t resolution() const { return 1; }
142         bool requires_seekahead () const { return false; }
143         void reset_client (jack_client_t* jack);
144         bool is_always_synced() const { return true; }
145
146   private:
147         jack_client_t* jack;
148         float speed;
149         bool _starting;
150 };
151
152 } /* namespace */
153
154 #endif /* __ardour_slave_h__ */