Wouldn't it be nice if plugin presets had a description/comment?
[ardour.git] / libs / ardour / processor.cc
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 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <string>
25
26 #include "pbd/xml++.h"
27 #include "pbd/types_convert.h"
28
29 #include "ardour/automatable.h"
30 #include "ardour/chan_count.h"
31 #include "ardour/debug.h"
32 #include "ardour/processor.h"
33 #include "ardour/types.h"
34
35 #ifdef WINDOWS_VST_SUPPORT
36 #include "ardour/windows_vst_plugin.h"
37 #endif
38
39 #ifdef AUDIOUNIT_SUPPORT
40 #include "ardour/audio_unit.h"
41 #endif
42
43 #include "ardour/audioengine.h"
44 #include "ardour/session.h"
45 #include "ardour/types.h"
46
47 #include "pbd/i18n.h"
48
49 using namespace std;
50 using namespace ARDOUR;
51 using namespace PBD;
52
53 namespace ARDOUR { class Session; }
54
55 // Always saved as Processor, but may be IOProcessor or Send in legacy sessions
56 const string Processor::state_node_name = "Processor";
57
58 Processor::Processor(Session& session, const string& name)
59         : SessionObject(session, name)
60         , Automatable (session)
61         , _pending_active(false)
62         , _active(false)
63         , _next_ab_is_active(false)
64         , _configured(false)
65         , _display_to_user (true)
66         , _pre_fader (false)
67         , _ui_pointer (0)
68         , _window_proxy (0)
69         , _pinmgr_proxy (0)
70         , _owner (0)
71         , _input_latency (0)
72         , _output_latency (0)
73         , _capture_offset (0)
74         , _playback_offset (0)
75         , _loop_location (0)
76 {
77 }
78
79 Processor::Processor (const Processor& other)
80         : Evoral::ControlSet (other)
81         , SessionObject (other.session(), other.name())
82         , Automatable (other.session())
83         , Latent (other)
84         , _pending_active(other._pending_active)
85         , _active(other._active)
86         , _next_ab_is_active(false)
87         , _configured(false)
88         , _display_to_user (true)
89         , _pre_fader (false)
90         , _ui_pointer (0)
91         , _window_proxy (0)
92         , _pinmgr_proxy (0)
93         , _owner (0)
94         , _input_latency (0)
95         , _output_latency (0)
96         , _capture_offset (0)
97         , _playback_offset (0)
98         , _loop_location (other._loop_location)
99 {
100 }
101
102 Processor::~Processor ()
103 {
104         DEBUG_TRACE (DEBUG::Destruction, string_compose ("processor %1 destructor\n", _name));
105 }
106
107 XMLNode&
108 Processor::get_state (void)
109 {
110         return state ();
111 }
112
113 /* NODE STRUCTURE
114
115     <Automation [optionally with visible="...." ]>
116        <parameter-N>
117          <AutomationList id=N>
118            <events>
119            X1 Y1
120            X2 Y2
121            ....
122            </events>
123        </parameter-N>
124     <Automation>
125 */
126
127 XMLNode&
128 Processor::state ()
129 {
130         XMLNode* node = new XMLNode (state_node_name);
131
132         node->set_property("id", id());
133         node->set_property("name", name());
134         node->set_property("active", active());
135
136         if (_extra_xml){
137                 node->add_child_copy (*_extra_xml);
138         }
139
140         if (!skip_saving_automation) {
141                 XMLNode& automation = Automatable::get_automation_xml_state();
142                 if (!automation.children().empty() || !automation.properties().empty()) {
143                         node->add_child_nocopy (automation);
144                 } else {
145                         delete &automation;
146                 }
147         }
148
149         node->set_property("user-latency", _user_latency);
150
151         return *node;
152 }
153
154 int
155 Processor::set_state_2X (const XMLNode & node, int /*version*/)
156 {
157         XMLProperty const * prop;
158
159         XMLNodeList children = node.children ();
160
161         for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
162
163                 if ((*i)->name() == X_("IO")) {
164
165                         if ((prop = (*i)->property ("name")) != 0) {
166                                 set_name (prop->value ());
167                         }
168
169                         set_id (**i);
170
171                         //note:  in A2, active state was stored in the Redirect node, not the child IO node
172                         /*
173                          * if ((prop = (*i)->property ("active")) != 0) {
174                                 bool const a = string_is_affirmative (prop->value ());
175                                 if (_active != a) {
176                                         if (a) {
177                                                 activate ();
178                                         } else {
179                                                 deactivate ();
180                                         }
181                                 }
182                         }*/
183
184                 }
185         }
186
187         return 0;
188 }
189
190 int
191 Processor::set_state (const XMLNode& node, int version)
192 {
193         if (version < 3000) {
194                 return set_state_2X (node, version);
195         }
196
197         bool ignore_name;
198         // Only testing for the presence of the property not value
199         if (!node.get_property("ignore-name", ignore_name)) {
200                 string name;
201                 // may not exist for legacy 3.0 sessions
202                 if (node.get_property ("name", name)) {
203                         /* don't let derived classes have a crack at set_name,
204                            as some (like Send) will screw with the one we suggest.
205                         */
206                         Processor::set_name (name);
207                 }
208
209                 set_id (node);
210         }
211
212         XMLNodeList nlist = node.children();
213         XMLNodeIterator niter;
214
215         Stateful::save_extra_xml (node);
216
217         XMLProperty const * prop = 0;
218         XMLProperty const * legacy_active = 0;
219
220         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
221
222                 if ((*niter)->name() == X_("Automation")) {
223
224                         if ((prop = (*niter)->property ("path")) != 0) {
225                                 old_set_automation_state (*(*niter));
226                         } else {
227                                 set_automation_xml_state (*(*niter), Evoral::Parameter(PluginAutomation));
228                         }
229
230                 } else if ((*niter)->name() == "Redirect") {
231                         if ( !(legacy_active = (*niter)->property("active"))) {
232                                 error << string_compose(_("No %1 property flag in element %2"), "active", (*niter)->name()) << endl;
233                         }
234                 }
235         }
236
237         if ((prop = node.property ("active")) == 0) {
238                 if (legacy_active) {
239                         prop = legacy_active;
240                 } else {
241                         error << _("No child node with active property") << endmsg;
242                         return -1;
243                 }
244         }
245
246         bool const a = string_to<bool> (prop->value ()) && !_session.get_bypass_all_loaded_plugins();
247         if (_active != a) {
248                 if (a) {
249                         activate ();
250                 } else {
251                         deactivate ();
252                 }
253         }
254
255         node.get_property ("user-latency", _user_latency);
256
257         return 0;
258 }
259
260 /** @pre Caller must hold process lock */
261 bool
262 Processor::configure_io (ChanCount in, ChanCount out)
263 {
264         /* This class assumes 1:1 input:output.static output stream count.
265            Derived classes must override and set _configured_output appropriately
266            if this is not the case
267         */
268
269         _configured_input = in;
270         _configured_output = out;
271         _configured = true;
272
273         ConfigurationChanged (in, out); /* EMIT SIGNAL */
274
275         return true;
276 }
277
278 bool
279 Processor::map_loop_range (samplepos_t& start, samplepos_t& end) const
280 {
281         if (!_loop_location) {
282                 return false;
283         }
284         if (start >= end) {
285                 /* no backwards looping */
286                 return false;
287         }
288
289         const samplepos_t loop_end = _loop_location->end ();
290         if (start < loop_end) {
291                 return false;
292         }
293
294         const samplepos_t loop_start   = _loop_location->start ();
295         const samplecnt_t looplen      = loop_end - loop_start;
296         const sampleoffset_t start_off = (start - loop_start) % looplen;
297         const samplepos_t start_pos    = loop_start + start_off;
298
299         assert (start >= start_pos);
300         end -= start - start_pos;
301         start = start_pos;
302         assert (end > start);
303         return true;
304 }
305
306 void
307 Processor::set_display_to_user (bool yn)
308 {
309         _display_to_user = yn;
310 }
311
312 void
313 Processor::set_pre_fader (bool p)
314 {
315         _pre_fader = p;
316 }
317
318 void
319 Processor::set_owner (SessionObject* o)
320 {
321         _owner = o;
322 }
323
324 SessionObject*
325 Processor::owner() const
326 {
327         return _owner;
328 }