rework Stateful::set_state() patch to avoid default version argument
[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 <sigc++/bind.h>
27
28 #include "pbd/failed_constructor.h"
29 #include "pbd/enumwriter.h"
30 #include "pbd/xml++.h"
31
32 #include "ardour/processor.h"
33 #include "ardour/plugin.h"
34 #include "ardour/port.h"
35 #include "ardour/route.h"
36 #include "ardour/ladspa_plugin.h"
37 #include "ardour/buffer_set.h"
38 #include "ardour/send.h"
39 #include "ardour/port_insert.h"
40 #include "ardour/plugin_insert.h"
41
42 #ifdef VST_SUPPORT
43 #include "ardour/vst_plugin.h"
44 #endif
45
46 #ifdef HAVE_AUDIOUNITS
47 #include "ardour/audio_unit.h"
48 #endif
49
50 #include "ardour/audioengine.h"
51 #include "ardour/session.h"
52 #include "ardour/types.h"
53
54 #include "i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59
60 sigc::signal<void,Processor*> Processor::ProcessorCreated;
61
62 // Always saved as Processor, but may be IOProcessor or Send in legacy sessions
63 const string Processor::state_node_name = "Processor";
64
65 Processor::Processor(Session& session, const string& name)
66         : SessionObject(session, name)
67         , AutomatableControls(session)
68         , _pending_active(false)
69         , _active(false)
70         , _next_ab_is_active(false)
71         , _configured(false)
72         , _gui(0)
73 {
74 }
75
76 Processor::Processor (Session& session, const XMLNode& node)
77         : SessionObject(session, "renameMe")
78         , AutomatableControls(session)
79         , _pending_active(false)
80         , _active(false)
81         , _next_ab_is_active(false)
82         , _configured(false)
83         , _gui(0)
84 {
85         set_state (node, Stateful::loading_state_version);
86         _pending_active = _active;
87 }
88
89 XMLNode&
90 Processor::get_state (void)
91 {
92         return state (true);
93 }
94
95 /* NODE STRUCTURE
96
97     <Automation [optionally with visible="...." ]>
98        <parameter-N>
99          <AutomationList id=N>
100            <events>
101            X1 Y1
102            X2 Y2
103            ....
104            </events>
105        </parameter-N>
106     <Automation>
107 */
108
109 XMLNode&
110 Processor::state (bool full_state)
111 {
112         XMLNode* node = new XMLNode (state_node_name);
113         stringstream sstr;
114         char buf[64];
115
116         id().print (buf, sizeof (buf));
117         node->add_property("id", buf);
118         node->add_property("name", _name);
119         node->add_property("active", active() ? "yes" : "no");
120
121         if (_extra_xml){
122                 node->add_child_copy (*_extra_xml);
123         }
124
125         if (full_state) {
126                 XMLNode& automation = Automatable::get_automation_state();
127                 if (!automation.children().empty()
128                                 || !automation.properties().empty()
129                                 || !_visible_controls.empty()) {
130
131                         for (set<Evoral::Parameter>::iterator x = _visible_controls.begin();
132                                         x != _visible_controls.end(); ++x) {
133                                 if (x != _visible_controls.begin()) {
134                                         sstr << ' ';
135                                 }
136                                 sstr << *x;
137                         }
138
139                         automation.add_property ("visible", sstr.str());
140                         node->add_child_nocopy (automation);
141                 }
142         }
143
144         return *node;
145 }
146
147 int
148 Processor::set_state_2X (const XMLNode & node, int version)
149 {
150         XMLProperty const * prop;
151
152         XMLNodeList children = node.children ();
153
154         for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
155
156                 if ((*i)->name() == X_("IO")) {
157
158                         if ((prop = (*i)->property ("name")) != 0) {
159                                 set_name (prop->value ());
160                         }
161
162                         if ((prop = (*i)->property ("id")) != 0) {
163                                 _id = prop->value ();
164                         }
165
166                         if ((prop = (*i)->property ("active")) != 0) {
167                                 if (_active != string_is_affirmative (prop->value())) {
168                                         _active = !_active;
169                                         ActiveChanged (); /* EMIT_SIGNAL */
170                                 }
171                         }
172                 }
173         }
174
175         return 0;
176 }
177
178 int
179 Processor::set_state (const XMLNode& node, int version)
180 {
181         if (version < 3000) {
182                 return set_state_2X (node, version);
183         }
184         
185         const XMLProperty *prop;
186         const XMLProperty *legacy_active = 0;
187
188         // may not exist for legacy 3.0 sessions
189         if ((prop = node.property ("name")) != 0) {
190                 set_name(prop->value());
191         }
192
193         // may not exist for legacy 3.0 sessions
194         if ((prop = node.property ("id")) != 0) {
195                 _id = prop->value();
196         }
197
198         XMLNodeList nlist = node.children();
199         XMLNodeIterator niter;
200
201         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
202
203                 if ((*niter)->name() == X_("Automation")) {
204
205                         XMLProperty *prop;
206
207                         if ((prop = (*niter)->property ("path")) != 0) {
208                                 old_set_automation_state (*(*niter));
209                         } else {
210                                 set_automation_state (*(*niter), Evoral::Parameter(PluginAutomation));
211                         }
212
213                         if ((prop = (*niter)->property ("visible")) != 0) {
214                                 uint32_t what;
215                                 stringstream sstr;
216
217                                 _visible_controls.clear ();
218
219                                 sstr << prop->value();
220                                 while (1) {
221                                         sstr >> what;
222                                         if (sstr.fail()) {
223                                                 break;
224                                         }
225                                         // FIXME: other automation types?
226                                         mark_automation_visible (Evoral::Parameter(PluginAutomation, 0, what), true);
227                                 }
228                         }
229
230                 } else if ((*niter)->name() == "Extra") {
231                         _extra_xml = new XMLNode (*(*niter));
232                 } else if ((*niter)->name() == "Redirect") {
233                         if ( !(legacy_active = (*niter)->property("active"))) {
234                                 error << string_compose(_("No %1 property flag in element %2"), "active", (*niter)->name()) << endl;
235                         }
236                 }
237         }
238
239         if ((prop = node.property ("active")) == 0) {
240                 warning << _("XML node describing a processor is missing the `active' field,"
241                            "trying legacy active flag from child node") << endmsg;
242                 if (legacy_active) {
243                         prop = legacy_active;
244                 } else {
245                         error << _("No child node with active property") << endmsg;
246                         return -1;
247                 }
248         }
249
250         if (_active != string_is_affirmative (prop->value())) {
251                 _active = !_active;
252                 ActiveChanged (); /* EMIT_SIGNAL */
253         }
254
255         return 0;
256 }
257
258 bool
259 Processor::configure_io (ChanCount in, ChanCount out)
260 {
261         /* This class assumes 1:1 input:output.static output stream count.
262            Derived classes must override and set _configured_output appropriately
263            if this is not the case
264         */
265
266         _configured_input = in;
267         _configured_output = out;
268         _configured = true;
269
270         ConfigurationChanged.emit (in, out);
271
272         return true;
273 }