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