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