revert VST debug hacks
[ardour.git] / libs / ardour / redirect.cc
1 /*
2     Copyright (C) 2001 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     $Id$
19 */
20
21 #include <fstream>
22 #include <algorithm>
23 #include <string>
24 #include <cerrno>
25 #include <unistd.h>
26 #include <sstream>
27
28 #include <sigc++/bind.h>
29
30 #include <pbd/xml++.h>
31
32 #include <ardour/redirect.h>
33 #include <ardour/session.h>
34 #include <ardour/utils.h>
35 #include <ardour/send.h>
36 #include <ardour/insert.h>
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 const string Redirect::state_node_name = "Redirect";
45 sigc::signal<void,Redirect*> Redirect::RedirectCreated;
46
47 Redirect::Redirect (Session& s, const string& name, Placement p,
48
49                     int input_min, int input_max, int output_min, int output_max)
50         : IO (s, name, input_min, input_max, output_min, output_max)
51 {
52         _placement = p;
53         _active = false;
54         _sort_key = 0;
55         _gui = 0;
56         _extra_xml = 0;
57 }
58
59 Redirect::~Redirect ()
60 {
61         notify_callbacks ();
62 }
63
64 boost::shared_ptr<Redirect>
65 Redirect::clone (boost::shared_ptr<const Redirect> other)
66 {
67         boost::shared_ptr<const Send> send;
68         boost::shared_ptr<const PortInsert> port_insert;
69         boost::shared_ptr<const PluginInsert> plugin_insert;
70
71         if ((send = boost::dynamic_pointer_cast<const Send>(other)) != 0) {
72                 return boost::shared_ptr<Redirect> (new Send (*send));
73         } else if ((port_insert = boost::dynamic_pointer_cast<const PortInsert>(other)) != 0) {
74                 return boost::shared_ptr<Redirect> (new PortInsert (*port_insert));
75         } else if ((plugin_insert = boost::dynamic_pointer_cast<const PluginInsert>(other)) != 0) {
76                 return boost::shared_ptr<Redirect> (new PluginInsert (*plugin_insert));
77         } else {
78                 fatal << _("programming error: unknown Redirect type in Redirect::Clone!\n")
79                       << endmsg;
80                 /*NOTREACHED*/
81         }
82         return boost::shared_ptr<Redirect>();
83 }
84
85 void
86 Redirect::set_sort_key (uint32_t key)
87 {
88         _sort_key = key;
89 }
90         
91 void
92 Redirect::set_placement (Placement p, void *src)
93 {
94         if (_placement != p) {
95                 _placement = p;
96                  placement_changed (this, src); /* EMIT SIGNAL */
97         }
98 }
99
100 void
101 Redirect::set_placement (const string& str, void *src)
102 {
103         if (str == _("pre")) {
104                 set_placement (PreFader, this);
105         } else if (str == _("post")) {
106                 set_placement (PostFader, this);
107         } else {
108                 error << string_compose(_("Redirect: unknown placement string \"%1\" (ignored)"), str) << endmsg;
109         }
110 }
111
112 /* NODE STRUCTURE 
113    
114     <Automation [optionally with visible="...." ]>
115        <parameter-N>
116          <AutomationList id=N>
117            <events>
118            X1 Y1
119            X2 Y2
120            ....
121            </events>
122        </parameter-N>
123     <Automation>
124 */
125
126 int
127 Redirect::set_automation_state (const XMLNode& node)
128 {
129         Glib::Mutex::Lock lm (_automation_lock);
130
131         parameter_automation.clear ();
132
133         XMLNodeList nlist = node.children();
134         XMLNodeIterator niter;
135
136         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
137                 uint32_t param;
138
139                 if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
140                         error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
141                         continue;
142                 }
143
144                 AutomationList& al = automation_list (param);
145                 if (al.set_state (*(*niter)->children().front())) {
146                         goto bad;
147                 }
148         }
149
150         return 0;
151
152   bad:
153         error << string_compose(_("%1: cannot load automation data from XML"), _name) << endmsg;
154         parameter_automation.clear ();
155         return -1;
156 }
157
158 XMLNode&
159 Redirect::get_automation_state ()
160 {
161         Glib::Mutex::Lock lm (_automation_lock);
162         XMLNode* node = new XMLNode (X_("Automation"));
163         string fullpath;
164
165         if (parameter_automation.empty()) {
166                 return *node;
167         }
168
169         map<uint32_t,AutomationList*>::iterator li;
170         
171         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
172         
173                 XMLNode* child;
174                 
175                 char buf[64];
176                 stringstream str;
177                 snprintf (buf, sizeof (buf), "parameter-%" PRIu32, li->first);
178                 child = new XMLNode (buf);
179                 child->add_child_nocopy (li->second->get_state ());
180         }
181
182         return *node;
183 }
184
185 XMLNode&
186 Redirect::get_state (void)
187 {
188         return state (true);
189 }
190
191 XMLNode&
192 Redirect::state (bool full_state)
193 {
194         XMLNode* node = new XMLNode (state_node_name);
195         stringstream sstr;
196
197         node->add_property("active", active() ? "yes" : "no");  
198         node->add_property("placement", placement_as_string (placement()));
199         node->add_child_nocopy (IO::state (full_state));
200
201         if (_extra_xml){
202                 node->add_child_copy (*_extra_xml);
203         }
204         
205         if (full_state) {
206
207                 XMLNode& automation = get_automation_state(); 
208                 
209                 for (set<uint32_t>::iterator x = visible_parameter_automation.begin(); x != visible_parameter_automation.end(); ++x) {
210                         if (x != visible_parameter_automation.begin()) {
211                                 sstr << ' ';
212                         }
213                         sstr << *x;
214                 }
215
216                 automation.add_property ("visible", sstr.str());
217
218                 node->add_child_nocopy (automation);
219         }
220
221         return *node;
222 }
223
224
225 int
226 Redirect::set_state (const XMLNode& node)
227 {
228         const XMLProperty *prop;
229
230         if (node.name() != state_node_name) {
231                 error << string_compose(_("incorrect XML node \"%1\" passed to Redirect object"), node.name()) << endmsg;
232                 return -1;
233         }
234
235         XMLNodeList nlist = node.children();
236         XMLNodeIterator niter;
237         bool have_io = false;
238
239         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
240
241                 if ((*niter)->name() == IO::state_node_name) {
242
243                         IO::set_state (**niter);
244                         have_io = true;
245
246                 } else if ((*niter)->name() == X_("Automation")) {
247
248
249                         XMLProperty *prop;
250                         
251                         if ((prop = (*niter)->property ("path")) != 0) {
252                                 old_set_automation_state (*(*niter));
253                         } else {
254                                 set_automation_state (*(*niter));
255                         }
256
257                         if ((prop = (*niter)->property ("visible")) != 0) {
258                                 uint32_t what;
259                                 stringstream sstr;
260
261                                 visible_parameter_automation.clear ();
262                                 
263                                 sstr << prop->value();
264                                 while (1) {
265                                         sstr >> what;
266                                         if (sstr.fail()) {
267                                                 break;
268                                         }
269                                         mark_automation_visible (what, true);
270                                 }
271                         }
272
273                 } else if ((*niter)->name() == "extra") {
274                         _extra_xml = new XMLNode (*(*niter));
275                 }
276         }
277
278         if (!have_io) {
279                 error << _("XML node describing an IO is missing an IO node") << endmsg;
280                 return -1;
281         }
282
283         if ((prop = node.property ("active")) == 0) {
284                 error << _("XML node describing a redirect is missing the `active' field") << endmsg;
285                 return -1;
286         }
287
288         if (_active != (prop->value() == "yes")) {
289                 _active = !_active;
290                 active_changed (this, this); /* EMIT_SIGNAL */
291         }
292         
293         if ((prop = node.property ("placement")) == 0) {
294                 error << _("XML node describing a redirect is missing the `placement' field") << endmsg;
295                 return -1;
296         }
297
298         set_placement (prop->value(), this);
299
300         return 0;
301 }
302
303 int
304 Redirect::old_set_automation_state (const XMLNode& node)
305 {
306         const XMLProperty *prop;
307                         
308         if ((prop = node.property ("path")) != 0) {
309                 load_automation (prop->value());
310         } else {
311                 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
312         }
313         
314         if ((prop = node.property ("visible")) != 0) {
315                 uint32_t what;
316                 stringstream sstr;
317                 
318                 visible_parameter_automation.clear ();
319                 
320                 sstr << prop->value();
321                 while (1) {
322                         sstr >> what;
323                         if (sstr.fail()) {
324                                 break;
325                         }
326                         mark_automation_visible (what, true);
327                 }
328         }
329
330         return 0;
331 }
332
333 int
334 Redirect::load_automation (string path)
335 {
336         string fullpath;
337
338         if (path[0] == '/') { // legacy
339                 fullpath = path;
340         } else {
341                 fullpath = _session.automation_dir();
342                 fullpath += path;
343         }
344         ifstream in (fullpath.c_str());
345
346         if (!in) {
347                 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
348                 return 1;
349         }
350
351         Glib::Mutex::Lock lm (_automation_lock);
352         set<uint32_t> tosave;
353         parameter_automation.clear ();
354
355         while (in) {
356                 double when;
357                 double value;
358                 uint32_t port;
359
360                 in >> port;     if (!in) break;
361                 in >> when;  if (!in) goto bad;
362                 in >> value; if (!in) goto bad;
363                 
364                 AutomationList& al = automation_list (port);
365                 al.add (when, value);
366                 tosave.insert (port);
367         }
368         
369         return 0;
370
371   bad:
372         error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
373         parameter_automation.clear ();
374         return -1;
375 }
376
377
378 void
379 Redirect::what_has_automation (set<uint32_t>& s) const
380 {
381         Glib::Mutex::Lock lm (_automation_lock);
382         map<uint32_t,AutomationList*>::const_iterator li;
383         
384         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
385                 s.insert  ((*li).first);
386         }
387 }
388
389 void
390 Redirect::what_has_visible_automation (set<uint32_t>& s) const
391 {
392         Glib::Mutex::Lock lm (_automation_lock);
393         set<uint32_t>::const_iterator li;
394         
395         for (li = visible_parameter_automation.begin(); li != visible_parameter_automation.end(); ++li) {
396                 s.insert  (*li);
397         }
398 }
399 AutomationList&
400 Redirect::automation_list (uint32_t parameter)
401 {
402         AutomationList* al = parameter_automation[parameter];
403
404         if (al == 0) {
405                 al = parameter_automation[parameter] = new AutomationList (default_parameter_value (parameter));
406                 /* let derived classes do whatever they need with this */
407                 automation_list_creation_callback (parameter, *al);
408         }
409
410         return *al;
411 }
412
413 string
414 Redirect::describe_parameter (uint32_t which)
415 {
416         /* derived classes will override this */
417         return "";
418 }
419
420 void
421 Redirect::can_automate (uint32_t what)
422 {
423         can_automate_list.insert (what);
424 }
425
426 void
427 Redirect::mark_automation_visible (uint32_t what, bool yn)
428 {
429         if (yn) {
430                 visible_parameter_automation.insert (what);
431         } else {
432                 set<uint32_t>::iterator i;
433
434                 if ((i = visible_parameter_automation.find (what)) != visible_parameter_automation.end()) {
435                         visible_parameter_automation.erase (i);
436                 }
437         }
438 }
439
440 bool
441 Redirect::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
442 {
443         map<uint32_t,AutomationList*>::const_iterator li;       
444         AutomationList::TimeComparator cmp;
445
446         next_event.when = max_frames;
447         
448         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
449                 
450                 AutomationList::const_iterator i;
451                 const AutomationList& alist (*((*li).second));
452                 ControlEvent cp (now, 0.0f);
453                 
454                 for (i = lower_bound (alist.const_begin(), alist.const_end(), &cp, cmp); i != alist.const_end() && (*i)->when < end; ++i) {
455                         if ((*i)->when > now) {
456                                 break; 
457                         }
458                 }
459                 
460                 if (i != alist.const_end() && (*i)->when < end) {
461                         
462                         if ((*i)->when < next_event.when) {
463                                 next_event.when = (*i)->when;
464                         }
465                 }
466         }
467
468         return next_event.when != max_frames;
469 }
470
471 void
472 Redirect::set_active (bool yn, void* src)
473 {
474         _active = yn; 
475         active_changed (this, src); 
476         _session.set_dirty ();
477 }
478