provide jdelay-based hardware/port insert latency measurement
[ardour.git] / libs / ardour / ardour / insert.h
1 /*
2     Copyright (C) 2000 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_insert_h__
21 #define __ardour_insert_h__
22
23 #include <vector>
24 #include <string>
25 #include <exception>
26
27 #include <sigc++/signal.h>
28 #include <ardour/ardour.h>
29 #include <ardour/redirect.h>
30 #include <ardour/types.h>
31
32 class XMLNode;
33 class MTDM;
34
35 namespace MIDI {
36         class Port;
37 }
38
39 namespace ARDOUR {
40
41 class Session;
42 class Route;
43 class Plugin;
44
45 class Insert : public Redirect
46 {
47   public:
48         Insert(Session& s, std::string name, Placement p);
49         Insert(Session& s, std::string name, Placement p, int imin, int imax, int omin, int omax);
50         
51         virtual ~Insert() { }
52
53         virtual void run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes) = 0;
54         virtual void activate () {}
55         virtual void deactivate () {}
56
57         virtual int32_t can_do (int32_t in, int32_t& out) = 0;
58         virtual int32_t configure_io (int32_t magic, int32_t in, int32_t out) = 0;
59 };
60
61 class PortInsert : public Insert 
62 {
63   public:
64         PortInsert (Session&, Placement);
65         PortInsert (Session&, const XMLNode&);
66         PortInsert (const PortInsert&);
67         ~PortInsert ();
68
69         XMLNode& state(bool full);
70         XMLNode& get_state(void);
71         int set_state(const XMLNode&);
72
73         void init ();
74         void run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes);
75
76         nframes_t latency();
77         
78         uint32_t output_streams() const;
79         uint32_t input_streams() const;
80
81         int32_t can_do (int32_t, int32_t& out);
82         int32_t configure_io (int32_t magic, int32_t in, int32_t out);
83         uint32_t bit_slot() const { return bitslot; }
84
85         void start_latency_detection ();
86         void stop_latency_detection ();
87
88         MTDM* mtdm () const { return _mtdm; }
89         void set_measured_latency (nframes_t);
90
91   private:
92         uint32_t   bitslot;
93         MTDM*     _mtdm;
94         bool      _latency_detect;
95         nframes_t _latency_flush_frames;
96         nframes_t _measured_latency;
97 };
98
99 class PluginInsert : public Insert
100 {
101   public:
102         PluginInsert (Session&, boost::shared_ptr<Plugin>, Placement);
103         PluginInsert (Session&, const XMLNode&);
104         PluginInsert (const PluginInsert&);
105         ~PluginInsert ();
106
107         static const string port_automation_node_name;
108         
109         XMLNode& state(bool);
110         XMLNode& get_state(void);
111         int set_state(const XMLNode&);
112
113         void run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes);
114         void silence (nframes_t nframes);
115         void activate ();
116         void deactivate ();
117
118         void set_block_size (nframes_t nframes);
119
120         uint32_t output_streams() const;
121         uint32_t input_streams() const;
122         uint32_t natural_output_streams() const;
123         uint32_t natural_input_streams() const;
124
125         int      set_count (uint32_t num);
126         uint32_t get_count () const { return _plugins.size(); }
127
128         int32_t can_do (int32_t, int32_t& out);
129         int32_t configure_io (int32_t magic, int32_t in, int32_t out);
130
131         bool is_generator() const;
132
133         void set_parameter (uint32_t port, float val);
134
135         AutoState get_port_automation_state (uint32_t port);
136         void set_port_automation_state (uint32_t port, AutoState);
137         void protect_automation ();
138
139         float default_parameter_value (uint32_t which);
140
141         boost::shared_ptr<Plugin> plugin(uint32_t num=0) const {
142                 if (num < _plugins.size()) { 
143                         return _plugins[num];
144                 } else {
145                         return _plugins[0]; // we always have one
146                 }
147         }
148
149         PluginType type ();
150
151         string describe_parameter (uint32_t);
152
153         nframes_t latency();
154
155         void transport_stopped (nframes_t now);
156         void automation_snapshot (nframes_t now, bool force);
157
158   private:
159
160         void parameter_changed (uint32_t, float);
161         
162         vector<boost::shared_ptr<Plugin> > _plugins;
163         void automation_run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes);
164         void connect_and_run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now = 0);
165
166         void init ();
167         void set_automatable ();
168         void auto_state_changed (uint32_t which);
169         void automation_list_creation_callback (uint32_t, AutomationList&);
170
171         boost::shared_ptr<Plugin> plugin_factory (boost::shared_ptr<Plugin>);
172 };
173
174 } // namespace ARDOUR
175
176 #endif /* __ardour_insert_h__ */