merge from 2.0-ongoing @ 3581
[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         : Automatable(session, name)
63         , _active(false)
64         , _next_ab_is_active(false)
65         , _configured(false)
66         , _placement(p)
67         , _gui(0)
68 {
69 }
70
71 boost::shared_ptr<Processor>
72 Processor::clone (boost::shared_ptr<const Processor> other)
73 {
74         boost::shared_ptr<const Send> send;
75         boost::shared_ptr<const PortInsert> port_insert;
76         boost::shared_ptr<const PluginInsert> plugin_insert;
77
78         if ((send = boost::dynamic_pointer_cast<const Send>(other)) != 0) {
79                 return boost::shared_ptr<Processor> (new Send (*send));
80         } else if ((port_insert = boost::dynamic_pointer_cast<const PortInsert>(other)) != 0) {
81                 return boost::shared_ptr<Processor> (new PortInsert (*port_insert));
82         } else if ((plugin_insert = boost::dynamic_pointer_cast<const PluginInsert>(other)) != 0) {
83                 return boost::shared_ptr<Processor> (new PluginInsert (*plugin_insert));
84         } else {
85                 fatal << _("programming error: unknown Processor type in Processor::Clone!\n")
86                       << endmsg;
87                 /*NOTREACHED*/
88         }
89         return boost::shared_ptr<Processor>();
90 }
91
92 void
93 Processor::set_sort_key (uint32_t key)
94 {
95         _sort_key = key;
96 }
97         
98 void
99 Processor::set_placement (Placement p)
100 {
101         if (_placement != p) {
102                 _placement = p;
103                  PlacementChanged (); /* EMIT SIGNAL */
104         }
105 }
106
107 void
108 Processor::set_active (bool yn)
109 {
110         _active = yn; 
111         ActiveChanged (); 
112 }
113
114 XMLNode&
115 Processor::get_state (void)
116 {
117         return state (true);
118 }
119
120 /* NODE STRUCTURE 
121    
122     <Automation [optionally with visible="...." ]>
123        <parameter-N>
124          <AutomationList id=N>
125            <events>
126            X1 Y1
127            X2 Y2
128            ....
129            </events>
130        </parameter-N>
131     <Automation>
132 */
133
134 XMLNode&
135 Processor::state (bool full_state)
136 {
137         XMLNode* node = new XMLNode (state_node_name);
138         stringstream sstr;
139         
140         // FIXME: This conflicts with "id" used by plugin for name in legacy sessions (ugh).
141         // Do we need to serialize this?
142         /*
143         char buf[64];
144         id().print (buf, sizeof (buf));
145         node->add_property("id", buf);
146         */
147
148         node->add_property("name", _name);
149         node->add_property("active", active() ? "yes" : "no");  
150         node->add_property("placement", enum_2_string (_placement));
151
152         if (_extra_xml){
153                 node->add_child_copy (*_extra_xml);
154         }
155         
156         if (full_state) {
157
158                 XMLNode& automation = Automatable::get_automation_state(); 
159                 
160                 for (set<Parameter>::iterator x = _visible_controls.begin(); x != _visible_controls.end(); ++x) {
161                         if (x != _visible_controls.begin()) {
162                                 sstr << ' ';
163                         }
164                         sstr << *x;
165                 }
166
167                 automation.add_property ("visible", sstr.str());
168
169                 node->add_child_nocopy (automation);
170         }
171
172         return *node;
173 }
174
175 int
176 Processor::set_state (const XMLNode& node)
177 {
178         const XMLProperty *prop;
179         const XMLProperty *legacy_active = 0;
180         const XMLProperty *legacy_placement = 0;
181
182         // may not exist for legacy sessions
183         if ((prop = node.property ("name")) != 0) {
184                 set_name(prop->value());
185         }
186
187         XMLNodeList nlist = node.children();
188         XMLNodeIterator niter;
189
190         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
191
192                 if ((*niter)->name() == X_("Automation")) {
193
194
195                         XMLProperty *prop;
196                         
197                         if ((prop = (*niter)->property ("path")) != 0) {
198                                 old_set_automation_state (*(*niter));
199                         } else {
200                                 set_automation_state (*(*niter), 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 (Parameter(PluginAutomation, 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                         if ( !(legacy_placement = (*niter)->property("placement"))) {
227                                 error << string_compose(_("No %1 property flag in element %2"), "placement", (*niter)->name()) << endl;
228                         }
229                 }
230         }
231
232         if ((prop = node.property ("active")) == 0) {
233                 warning << _("XML node describing a processor is missing the `active' field, trying legacy active flag from child node") << endmsg;
234                 if (legacy_active) {
235                         prop = legacy_active;
236                 } else {
237                         error << _("No child node with active property") << endmsg;
238                         return -1;
239                 }
240         }
241
242         if (_active != (prop->value() == "yes")) {
243                 _active = !_active;
244                 ActiveChanged (); /* EMIT_SIGNAL */
245         }       
246
247         if ((prop = node.property ("placement")) == 0) {
248                 warning << _("XML node describing a processor is missing the `placement' field, trying legacy placement flag from child node") << endmsg;
249                 if (legacy_placement) {
250                         prop = legacy_placement; 
251                 } else {
252                         error << _("No child node with placement property") << endmsg;
253                         return -1;
254                 }
255         }
256
257         /* hack to handle older sessions before we only used EnumWriter */
258
259         string pstr;
260
261         if (prop->value() == "pre") {
262                 pstr = "PreFader";
263         } else if (prop->value() == "post") {
264                 pstr = "PostFader";
265         } else {
266                 pstr = prop->value();
267         }
268
269         Placement p = Placement (string_2_enum (pstr, p));
270         set_placement (p);
271
272         return 0;
273 }
274
275 bool
276 Processor::configure_io (ChanCount in, ChanCount out)
277 {
278         /* this class assumes static output stream count.
279            Derived classes must override, and must set "out"
280            to reflect "in" before calling this.
281         */
282
283         _configured_input = in; 
284         _configured = true;
285
286         return true;
287 }