Optimize plugin-processing for non-automated params
[ardour.git] / libs / ardour / ardour / plugin_insert.h
1 /*
2     Copyright (C) 2000,2007 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_plugin_insert_h__
21 #define __ardour_plugin_insert_h__
22
23 #include <vector>
24 #include <string>
25
26 #include <boost/weak_ptr.hpp>
27
28 #include "pbd/timing.h"
29
30 #include "ardour/ardour.h"
31 #include "ardour/libardour_visibility.h"
32 #include "ardour/chan_mapping.h"
33 #include "ardour/fixed_delay.h"
34 #include "ardour/io.h"
35 #include "ardour/types.h"
36 #include "ardour/parameter_descriptor.h"
37 #include "ardour/plugin.h"
38 #include "ardour/processor.h"
39 #include "ardour/readonly_control.h"
40 #include "ardour/sidechain.h"
41 #include "ardour/automation_control.h"
42
43 class XMLNode;
44
45 namespace ARDOUR {
46
47 class Session;
48 class Route;
49 class Plugin;
50
51 /** Plugin inserts: send data through a plugin
52  */
53 class LIBARDOUR_API PluginInsert : public Processor
54 {
55 public:
56         PluginInsert (Session&, boost::shared_ptr<Plugin> = boost::shared_ptr<Plugin>());
57         ~PluginInsert ();
58
59         static const std::string port_automation_node_name;
60
61         int set_state(const XMLNode&, int version);
62         void update_id (PBD::ID);
63         void set_owner (SessionObject*);
64         void set_state_dir (const std::string& d = "");
65
66         void run (BufferSet& in, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool);
67         void silence (samplecnt_t nframes, samplepos_t start_sample);
68
69         void activate ();
70         void deactivate ();
71         void flush ();
72
73         void enable (bool yn);
74         bool enabled () const;
75         bool bypassable () const;
76
77         bool reset_parameters_to_default ();
78         bool can_reset_all_parameters ();
79
80         bool write_immediate_event (size_t size, const uint8_t* buf);
81
82         void automation_run (samplepos_t, pframes_t, bool only_active = false);
83         bool find_next_event (double, double, Evoral::ControlEvent&, bool only_active = true) const;
84
85         int set_block_size (pframes_t nframes);
86
87         ChanMapping input_map (uint32_t num) const {
88                 if (num < _in_map.size()) {
89                         return _in_map.find (num)->second;
90                 } else {
91                         return ChanMapping ();
92                 }
93         }
94
95         ChanMapping output_map (uint32_t num) const {
96                 if (num < _out_map.size()) {
97                         return _out_map.find (num)->second;
98                 } else {
99                         return ChanMapping ();
100                 }
101         }
102
103         ChanMapping thru_map () const {
104                 return _thru_map;
105         }
106
107         bool pre_seed (const ChanCount&, const ChanCount&, const ChanMapping&, const ChanMapping&, const ChanMapping&);
108
109         ChanMapping input_map () const; ///< combined (all instances) input map
110         ChanMapping no_sc_input_map () const; ///< combined (all instances) input map w/o sidechain sinks
111         ChanMapping output_map () const; ///< combined (all instances) output map
112         bool has_midi_bypass () const;
113         bool has_midi_thru () const;
114         bool inplace () const { return ! _no_inplace; }
115
116 #ifdef MIXBUS
117         bool is_channelstrip () const;
118         bool is_nonbypassable () const;
119 #else
120         bool is_channelstrip () const { return false; }
121         bool is_nonbypassable () const { return false; }
122 #endif
123
124         void set_input_map (uint32_t, ChanMapping);
125         void set_output_map (uint32_t, ChanMapping);
126         void set_thru_map (ChanMapping);
127         bool reset_map (bool emit = true);
128         bool sanitize_maps ();
129         bool check_inplace ();
130         bool configured () const { return _configured; }
131
132         // these are ports visible on the outside
133         ChanCount output_streams() const;
134         ChanCount input_streams() const;
135         ChanCount internal_streams() const; // with side-chain
136
137         // actual ports of all plugins.
138         // n * natural_i/o or result of reconfigurable i/o
139         ChanCount internal_output_streams() const;
140         ChanCount internal_input_streams() const;
141
142         // a single plugin's internal i/o
143         ChanCount natural_output_streams() const;
144         ChanCount natural_input_streams() const;
145
146         /** plugin ports marked as sidechain */
147         ChanCount sidechain_input_pins() const;
148
149         /** Plugin-Insert IO sidechain ports */
150         ChanCount sidechain_input_ports() const {
151                 if (_sidechain) {
152                         return _sidechain->input ()->n_ports ();
153                 } else {
154                         return ChanCount ();
155                 }
156         }
157
158         const ChanCount& required_buffers () const { return _required_buffers; }
159         const ChanCount& preset_out () const { return _preset_out; }
160
161         // allow to override output_streams(), implies "Custom Mode"
162
163         // only the owning route may call these (with process lock held)
164         // route is not a friend class, it owns us
165         bool set_count      (uint32_t num);
166         void set_sinks      (const ChanCount&); // reconfigurable I/O ONLY
167         void set_outputs    (const ChanCount&);
168         void set_strict_io  (bool b);
169         void set_custom_cfg (bool b);
170         bool set_preset_out (const ChanCount&);
171         bool add_sidechain  (uint32_t n_audio = 1, uint32_t n_midi = 0);
172         bool del_sidechain ();
173         void update_sidechain_name ();
174         boost::shared_ptr<SideChain> sidechain () const { return _sidechain; }
175         // end C++ class slavery!
176
177         uint32_t get_count  () const { return _plugins.size(); }
178         bool     strict_io  () const { return _strict_io; }
179         bool     custom_cfg () const { return _custom_cfg; }
180
181         bool can_support_io_configuration (const ChanCount& in, ChanCount& out);
182         bool configure_io (ChanCount in, ChanCount out);
183
184         bool has_no_inputs() const;
185         bool has_no_audio_inputs() const;
186
187         bool is_instrument () const;
188
189         bool has_output_presets (
190                         ChanCount in = ChanCount (DataType::MIDI, 1),
191                         ChanCount out = ChanCount (DataType::AUDIO, 2)
192                         );
193
194         void realtime_handle_transport_stopped ();
195         void realtime_locate ();
196         void monitoring_changed ();
197
198         bool load_preset (Plugin::PresetRecord);
199
200         bool provides_stats () const;
201         bool get_stats (uint64_t& min, uint64_t& max, double& avg, double& dev) const;
202         void clear_stats ();
203
204         /** A control that manipulates a plugin parameter (control port). */
205         struct PluginControl : public AutomationControl
206         {
207                 PluginControl (PluginInsert*                     p,
208                                const Evoral::Parameter&          param,
209                                const ParameterDescriptor&        desc,
210                                boost::shared_ptr<AutomationList> list=boost::shared_ptr<AutomationList>());
211
212                 double get_value (void) const;
213                 void catch_up_with_external_value (double val);
214                 XMLNode& get_state();
215
216         private:
217                 PluginInsert* _plugin;
218                 void actually_set_value (double val, PBD::Controllable::GroupControlDisposition group_override);
219         };
220
221         /** A control that manipulates a plugin property (message). */
222         struct PluginPropertyControl : public AutomationControl
223         {
224                 PluginPropertyControl (PluginInsert*                     p,
225                                        const Evoral::Parameter&          param,
226                                        const ParameterDescriptor&        desc,
227                                        boost::shared_ptr<AutomationList> list=boost::shared_ptr<AutomationList>());
228
229                 double get_value (void) const;
230                 XMLNode& get_state();
231         protected:
232                 void actually_set_value (double value, PBD::Controllable::GroupControlDisposition);
233
234         private:
235                 PluginInsert* _plugin;
236                 Variant       _value;
237         };
238
239         boost::shared_ptr<Plugin> plugin(uint32_t num=0) const {
240                 if (num < _plugins.size()) {
241                         return _plugins[num];
242                 } else {
243                         return _plugins[0]; // we always have one
244                 }
245         }
246
247         samplecnt_t plugin_latency () const;
248
249         bool has_sidechain () const {
250                 return _sidechain ? true : false;
251         }
252
253         boost::shared_ptr<IO> sidechain_input () const {
254                 if (_sidechain) {
255                         return _sidechain->input ();
256                 }
257                 return boost::shared_ptr<IO> ();
258         }
259
260         PluginType type ();
261
262         boost::shared_ptr<ReadOnlyControl> control_output (uint32_t) const;
263
264         std::string describe_parameter (Evoral::Parameter param);
265
266         samplecnt_t signal_latency () const;
267
268         boost::shared_ptr<Plugin> get_impulse_analysis_plugin();
269
270         void collect_signal_for_analysis (samplecnt_t nframes);
271
272         bool strict_io_configured () const {
273                 return _match.strict_io;
274         }
275
276         bool splitting () const {
277                 return _match.method == Split;
278         }
279
280         void configured_io (ChanCount &in, ChanCount &out) const {
281                 in = _configured_in;
282                 out = _configured_out;
283         }
284
285         PBD::Signal2<void,BufferSet*, BufferSet*> AnalysisDataGathered;
286         PBD::Signal0<void> PluginIoReConfigure;
287         PBD::Signal0<void> PluginMapChanged;
288         PBD::Signal0<void> PluginConfigChanged;
289
290         /** Enumeration of the ways in which we can match our insert's
291          *  IO to that of the plugin(s).
292          */
293         enum MatchingMethod {
294                 Impossible,  ///< we can't
295                 Delegate,    ///< we are delegating to the plugin, and it can handle it
296                 NoInputs,    ///< plugin has no inputs, so anything goes
297                 ExactMatch,  ///< our insert's inputs are the same as the plugin's
298                 Replicate,   ///< we have multiple instances of the plugin
299                 Split,       ///< we copy one of our insert's inputs to multiple plugin inputs
300                 Hide,        ///< we `hide' some of the plugin's inputs by feeding them silence
301         };
302
303         /** Description of how we can match our plugin's IO to our own insert IO */
304         struct Match {
305                 Match () : method (Impossible), plugins (0), strict_io (false), custom_cfg (false) {}
306                 Match (MatchingMethod m, int32_t p,
307                                 bool strict = false, bool custom = false, ChanCount h = ChanCount ())
308                         : method (m), plugins (p), hide (h), strict_io (strict), custom_cfg (custom) {}
309
310                 MatchingMethod method; ///< method to employ
311                 int32_t plugins;       ///< number of copies of the plugin that we need
312                 ChanCount hide;        ///< number of channels to hide
313                 bool strict_io;        ///< force in == out
314                 bool custom_cfg;       ///< custom config (if not strict)
315         };
316
317 protected:
318         XMLNode& state ();
319
320 private:
321         /* disallow copy construction */
322         PluginInsert (const PluginInsert&);
323
324         void parameter_changed_externally (uint32_t, float);
325
326         void set_parameter (Evoral::Parameter param, float val);
327
328         float default_parameter_value (const Evoral::Parameter& param);
329
330         typedef std::vector<boost::shared_ptr<Plugin> > Plugins;
331         Plugins _plugins;
332
333         boost::shared_ptr<SideChain> _sidechain;
334         uint32_t _sc_playback_latency;
335         uint32_t _sc_capture_latency;
336         uint32_t _plugin_signal_latency;
337
338         boost::weak_ptr<Plugin> _impulseAnalysisPlugin;
339
340         samplecnt_t _signal_analysis_collected_nframes;
341         samplecnt_t _signal_analysis_collect_nframes_max;
342
343         BufferSet _signal_analysis_inputs;
344         BufferSet _signal_analysis_outputs;
345
346         FixedDelay _delaybuffers;
347
348         ChanCount _configured_in;
349         ChanCount _configured_internal; // with side-chain
350         ChanCount _configured_out;
351         ChanCount _custom_out;
352         ChanCount _custom_sinks;
353         ChanCount _preset_out;
354         ChanCount _cached_sidechain_pins;
355         ChanCount _required_buffers;
356
357         bool _configured;
358         bool _no_inplace;
359         bool _strict_io;
360         bool _custom_cfg;
361         bool _maps_from_state;
362         bool _mapping_changed;
363
364         Match private_can_support_io_configuration (ChanCount const &, ChanCount &) const;
365         Match internal_can_support_io_configuration (ChanCount const &, ChanCount &) const;
366         Match automatic_can_support_io_configuration (ChanCount const &, ChanCount &) const;
367
368         /** details of the match currently being used */
369         Match _match;
370
371         /* ordered map [plugin instance ID] => ARDOUR::ChanMapping
372          * TODO: consider replacing with boost::flat_map<> or std::vector<>.
373          */
374         class PinMappings : public std::map <uint32_t, ARDOUR::ChanMapping> {
375                 public:
376                         /* this emulates C++11's  std::map::at()
377                          * return mapping for given plugin instance */
378                         inline ARDOUR::ChanMapping const& p (const uint32_t i) const {
379 #ifndef NDEBUG
380                                 const_iterator x = find (i);
381                                 assert (x != end ());
382                                 return x->second;
383 #else
384                                 return find(i)->second;
385 #endif
386                         }
387         };
388
389         PinMappings _in_map;
390         PinMappings _out_map;
391         ChanMapping _thru_map; // out-idx <=  in-idx
392
393         void automate_and_run (BufferSet& bufs, samplepos_t start, samplepos_t end, double speed, pframes_t nframes);
394         void connect_and_run (BufferSet& bufs, samplepos_t start, samplecnt_t end, double speed, pframes_t nframes, samplecnt_t offset, bool with_auto);
395         void bypass (BufferSet& bufs, pframes_t nframes);
396         void inplace_silence_unconnected (BufferSet&, const PinMappings&, samplecnt_t nframes, samplecnt_t offset) const;
397
398         void create_automatable_parameters ();
399         void control_list_automation_state_changed (Evoral::Parameter, AutoState);
400         void set_parameter_state_2X (const XMLNode& node, int version);
401         void set_control_ids (const XMLNode&, int version);
402
403         void enable_changed ();
404         void bypassable_changed ();
405
406         boost::shared_ptr<Plugin> plugin_factory (boost::shared_ptr<Plugin>);
407         void add_plugin (boost::shared_ptr<Plugin>);
408
409         void add_sidechain_from_xml (const XMLNode& node, int version);
410
411         void start_touch (uint32_t param_id);
412         void end_touch (uint32_t param_id);
413
414         void latency_changed ();
415         bool _latency_changed;
416         uint32_t _bypass_port;
417
418         typedef std::map<uint32_t, boost::shared_ptr<ReadOnlyControl> >CtrlOutMap;
419         CtrlOutMap _control_outputs;
420
421         void preset_load_set_value (uint32_t, float);
422
423         PBD::TimingStats _timing_stats;
424         volatile gint _stat_reset;
425 };
426
427 } // namespace ARDOUR
428
429 std::ostream& operator<<(std::ostream& o, const ARDOUR::PluginInsert::Match& m);
430
431 #endif /* __ardour_plugin_insert_h__ */