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