rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[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
180         // may not exist for legacy sessions
181         if ((prop = node.property ("name")) != 0) {
182                 set_name(prop->value());
183         }
184
185         XMLNodeList nlist = node.children();
186         XMLNodeIterator niter;
187
188         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
189
190                 if ((*niter)->name() == X_("Automation")) {
191
192
193                         XMLProperty *prop;
194                         
195                         if ((prop = (*niter)->property ("path")) != 0) {
196                                 old_set_automation_state (*(*niter));
197                         } else {
198                                 set_automation_state (*(*niter), Parameter(PluginAutomation));
199                         }
200
201                         if ((prop = (*niter)->property ("visible")) != 0) {
202                                 uint32_t what;
203                                 stringstream sstr;
204
205                                 _visible_controls.clear ();
206                                 
207                                 sstr << prop->value();
208                                 while (1) {
209                                         sstr >> what;
210                                         if (sstr.fail()) {
211                                                 break;
212                                         }
213                                         // FIXME: other automation types?
214                                         mark_automation_visible (Parameter(PluginAutomation, what), true);
215                                 }
216                         }
217
218                 } else if ((*niter)->name() == "extra") {
219                         _extra_xml = new XMLNode (*(*niter));
220                 }
221         }
222
223         if ((prop = node.property ("active")) == 0) {
224                 error << _("XML node describing a processor is missing the `active' field") << endmsg;
225                 return -1;
226         }
227
228         if (_active != (prop->value() == "yes")) {
229                 _active = !_active;
230                 ActiveChanged (); /* EMIT_SIGNAL */
231         }
232         
233         if ((prop = node.property ("placement")) == 0) {
234                 error << _("XML node describing a processor is missing the `placement' field") << endmsg;
235                 return -1;
236         }
237
238         /* hack to handle older sessions before we only used EnumWriter */
239
240         string pstr;
241
242         if (prop->value() == "pre") {
243                 pstr = "PreFader";
244         } else if (prop->value() == "post") {
245                 pstr = "PostFader";
246         } else {
247                 pstr = prop->value();
248         }
249
250         Placement p = Placement (string_2_enum (pstr, p));
251         set_placement (p);
252
253         return 0;
254 }
255