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