Display recorded controller data (fix show all/existing 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 #include <string>
21
22 #include <sigc++/bind.h>
23
24 #include <pbd/failed_constructor.h>
25 #include <pbd/enumwriter.h>
26 #include <pbd/xml++.h>
27
28 #include <ardour/processor.h>
29 #include <ardour/plugin.h>
30 #include <ardour/port.h>
31 #include <ardour/route.h>
32 #include <ardour/ladspa_plugin.h>
33 #include <ardour/buffer_set.h>
34 #include <ardour/send.h>
35 #include <ardour/port_insert.h>
36 #include <ardour/plugin_insert.h>
37
38 #ifdef VST_SUPPORT
39 #include <ardour/vst_plugin.h>
40 #endif
41
42 #ifdef HAVE_AUDIOUNITS
43 #include <ardour/audio_unit.h>
44 #endif
45
46 #include <ardour/audioengine.h>
47 #include <ardour/session.h>
48 #include <ardour/types.h>
49
50 #include "i18n.h"
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55
56 sigc::signal<void,Processor*> Processor::ProcessorCreated;
57
58 // Always saved as Processor, but may be IOProcessor or Send in legacy sessions
59 const string Processor::state_node_name = "Processor";
60
61 Processor::Processor(Session& session, const string& name, Placement p)
62         : SessionObject(session, name)
63         , AutomatableControls(session)
64         , _active(false)
65         , _next_ab_is_active(false)
66         , _configured(false)
67         , _placement(p)
68         , _gui(0)
69 {
70 }
71
72 boost::shared_ptr<Processor>
73 Processor::clone (boost::shared_ptr<const Processor> other)
74 {
75         boost::shared_ptr<const Send> send;
76         boost::shared_ptr<const PortInsert> port_insert;
77         boost::shared_ptr<const PluginInsert> plugin_insert;
78
79         if ((send = boost::dynamic_pointer_cast<const Send>(other)) != 0) {
80                 return boost::shared_ptr<Processor> (new Send (*send));
81         } else if ((port_insert = boost::dynamic_pointer_cast<const PortInsert>(other)) != 0) {
82                 return boost::shared_ptr<Processor> (new PortInsert (*port_insert));
83         } else if ((plugin_insert = boost::dynamic_pointer_cast<const PluginInsert>(other)) != 0) {
84                 return boost::shared_ptr<Processor> (new PluginInsert (*plugin_insert));
85         } else {
86                 fatal << _("programming error: unknown Processor type in Processor::Clone!\n")
87                       << endmsg;
88                 /*NOTREACHED*/
89         }
90         return boost::shared_ptr<Processor>();
91 }
92
93 void
94 Processor::set_sort_key (uint32_t key)
95 {
96         _sort_key = key;
97 }
98         
99 void
100 Processor::set_placement (Placement p)
101 {
102         if (_placement != p) {
103                 _placement = p;
104                  PlacementChanged (); /* EMIT SIGNAL */
105         }
106 }
107
108 void
109 Processor::set_active (bool yn)
110 {
111         _active = yn; 
112         ActiveChanged (); 
113 }
114
115 XMLNode&
116 Processor::get_state (void)
117 {
118         return state (true);
119 }
120
121 /* NODE STRUCTURE 
122    
123     <Automation [optionally with visible="...." ]>
124        <parameter-N>
125          <AutomationList id=N>
126            <events>
127            X1 Y1
128            X2 Y2
129            ....
130            </events>
131        </parameter-N>
132     <Automation>
133 */
134
135 XMLNode&
136 Processor::state (bool full_state)
137 {
138         XMLNode* node = new XMLNode (state_node_name);
139         stringstream sstr;
140         
141         // FIXME: This conflicts with "id" used by plugin for name in legacy sessions (ugh).
142         // Do we need to serialize this?
143         /*
144         char buf[64];
145         id().print (buf, sizeof (buf));
146         node->add_property("id", buf);
147         */
148
149         node->add_property("name", _name);
150         node->add_property("active", active() ? "yes" : "no");  
151         node->add_property("placement", enum_2_string (_placement));
152
153         if (_extra_xml){
154                 node->add_child_copy (*_extra_xml);
155         }
156         
157         if (full_state) {
158
159                 XMLNode& automation = Automatable::get_automation_state(); 
160                 
161                 for (set<Parameter>::iterator x = _visible_controls.begin(); x != _visible_controls.end(); ++x) {
162                         if (x != _visible_controls.begin()) {
163                                 sstr << ' ';
164                         }
165                         sstr << *x;
166                 }
167
168                 automation.add_property ("visible", sstr.str());
169
170                 node->add_child_nocopy (automation);
171         }
172
173         return *node;
174 }
175
176 int
177 Processor::set_state (const XMLNode& node)
178 {
179         const XMLProperty *prop;
180         const XMLProperty *legacy_active = 0;
181         const XMLProperty *legacy_placement = 0;
182
183         // may not exist for legacy sessions
184         if ((prop = node.property ("name")) != 0) {
185                 set_name(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
196                         XMLProperty *prop;
197                         
198                         if ((prop = (*niter)->property ("path")) != 0) {
199                                 old_set_automation_state (*(*niter));
200                         } else {
201                                 set_automation_state (*(*niter), Parameter(PluginAutomation));
202                         }
203
204                         if ((prop = (*niter)->property ("visible")) != 0) {
205                                 uint32_t what;
206                                 stringstream sstr;
207
208                                 _visible_controls.clear ();
209                                 
210                                 sstr << prop->value();
211                                 while (1) {
212                                         sstr >> what;
213                                         if (sstr.fail()) {
214                                                 break;
215                                         }
216                                         // FIXME: other automation types?
217                                         mark_automation_visible (Parameter(PluginAutomation, what), true);
218                                 }
219                         }
220
221                 } else if ((*niter)->name() == "extra") {
222                         _extra_xml = new XMLNode (*(*niter));
223                 } else if ((*niter)->name() == "Redirect") {
224                         if ( !(legacy_active = (*niter)->property("active"))) {
225                                 error << string_compose(_("No %1 property flag in element %2"), "active", (*niter)->name()) << endl;
226                         }
227                         if ( !(legacy_placement = (*niter)->property("placement"))) {
228                                 error << string_compose(_("No %1 property flag in element %2"), "placement", (*niter)->name()) << endl;
229                         }
230                 }
231         }
232
233         if ((prop = node.property ("active")) == 0) {
234                 warning << _("XML node describing a processor is missing the `active' field, trying legacy active flag from child node") << endmsg;
235                 if (legacy_active) {
236                         prop = legacy_active;
237                 } else {
238                         error << _("No child node with active property") << endmsg;
239                         return -1;
240                 }
241         }
242
243         if (_active != (prop->value() == "yes")) {
244                 _active = !_active;
245                 ActiveChanged (); /* EMIT_SIGNAL */
246         }       
247
248         if ((prop = node.property ("placement")) == 0) {
249                 warning << _("XML node describing a processor is missing the `placement' field, trying legacy placement flag from child node") << endmsg;
250                 if (legacy_placement) {
251                         prop = legacy_placement; 
252                 } else {
253                         error << _("No child node with placement property") << endmsg;
254                         return -1;
255                 }
256         }
257
258         /* hack to handle older sessions before we only used EnumWriter */
259
260         string pstr;
261
262         if (prop->value() == "pre") {
263                 pstr = "PreFader";
264         } else if (prop->value() == "post") {
265                 pstr = "PostFader";
266         } else {
267                 pstr = prop->value();
268         }
269
270         Placement p = Placement (string_2_enum (pstr, p));
271         set_placement (p);
272
273         return 0;
274 }
275
276 bool
277 Processor::configure_io (ChanCount in, ChanCount out)
278 {
279         /* this class assumes static output stream count.
280            Derived classes must override, and must set "out"
281            to reflect "in" before calling this.
282         */
283
284         _configured_input = in; 
285         _configured = true;
286
287         return true;
288 }