2 Copyright (C) 2001 Paul Davis
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.
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.
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.
28 #include <sigc++/bind.h>
30 #include <pbd/xml++.h>
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>
41 using namespace ARDOUR;
44 const string Redirect::state_node_name = "Redirect";
45 sigc::signal<void,Redirect*> Redirect::RedirectCreated;
47 Redirect::Redirect (Session& s, const string& name, Placement p,
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)
59 Redirect::~Redirect ()
64 boost::shared_ptr<Redirect>
65 Redirect::clone (boost::shared_ptr<const Redirect> other)
67 boost::shared_ptr<const Send> send;
68 boost::shared_ptr<const PortInsert> port_insert;
69 boost::shared_ptr<const PluginInsert> plugin_insert;
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));
78 fatal << _("programming error: unknown Redirect type in Redirect::Clone!\n")
82 return boost::shared_ptr<Redirect>();
86 Redirect::set_sort_key (uint32_t key)
92 Redirect::set_placement (Placement p, void *src)
94 if (_placement != p) {
96 placement_changed (this, src); /* EMIT SIGNAL */
101 Redirect::set_placement (const string& str, void *src)
103 if (str == _("pre")) {
104 set_placement (PreFader, this);
105 } else if (str == _("post")) {
106 set_placement (PostFader, this);
108 error << string_compose(_("Redirect: unknown placement string \"%1\" (ignored)"), str) << endmsg;
114 <Automation [optionally with visible="...." ]>
116 <AutomationList id=N>
127 Redirect::set_automation_state (const XMLNode& node)
129 Glib::Mutex::Lock lm (_automation_lock);
131 parameter_automation.clear ();
133 XMLNodeList nlist = node.children();
134 XMLNodeIterator niter;
136 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
139 if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
140 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
144 AutomationList& al = automation_list (param);
145 if (al.set_state (*(*niter)->children().front())) {
153 error << string_compose(_("%1: cannot load automation data from XML"), _name) << endmsg;
154 parameter_automation.clear ();
159 Redirect::get_automation_state ()
161 Glib::Mutex::Lock lm (_automation_lock);
162 XMLNode* node = new XMLNode (X_("Automation"));
165 if (parameter_automation.empty()) {
169 map<uint32_t,AutomationList*>::iterator li;
171 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
177 snprintf (buf, sizeof (buf), "parameter-%" PRIu32, li->first);
178 child = new XMLNode (buf);
179 child->add_child_nocopy (li->second->get_state ());
186 Redirect::get_state (void)
192 Redirect::state (bool full_state)
194 XMLNode* node = new XMLNode (state_node_name);
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));
202 node->add_child_copy (*_extra_xml);
207 XMLNode& automation = get_automation_state();
209 for (set<uint32_t>::iterator x = visible_parameter_automation.begin(); x != visible_parameter_automation.end(); ++x) {
210 if (x != visible_parameter_automation.begin()) {
216 automation.add_property ("visible", sstr.str());
218 node->add_child_nocopy (automation);
226 Redirect::set_state (const XMLNode& node)
228 const XMLProperty *prop;
230 if (node.name() != state_node_name) {
231 error << string_compose(_("incorrect XML node \"%1\" passed to Redirect object"), node.name()) << endmsg;
235 XMLNodeList nlist = node.children();
236 XMLNodeIterator niter;
237 bool have_io = false;
239 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
241 if ((*niter)->name() == IO::state_node_name) {
243 IO::set_state (**niter);
246 } else if ((*niter)->name() == X_("Automation")) {
251 if ((prop = (*niter)->property ("path")) != 0) {
252 old_set_automation_state (*(*niter));
254 set_automation_state (*(*niter));
257 if ((prop = (*niter)->property ("visible")) != 0) {
261 visible_parameter_automation.clear ();
263 sstr << prop->value();
269 mark_automation_visible (what, true);
273 } else if ((*niter)->name() == "extra") {
274 _extra_xml = new XMLNode (*(*niter));
279 error << _("XML node describing an IO is missing an IO node") << endmsg;
283 if ((prop = node.property ("active")) == 0) {
284 error << _("XML node describing a redirect is missing the `active' field") << endmsg;
288 if (_active != (prop->value() == "yes")) {
290 active_changed (this, this); /* EMIT_SIGNAL */
293 if ((prop = node.property ("placement")) == 0) {
294 error << _("XML node describing a redirect is missing the `placement' field") << endmsg;
298 set_placement (prop->value(), this);
304 Redirect::old_set_automation_state (const XMLNode& node)
306 const XMLProperty *prop;
308 if ((prop = node.property ("path")) != 0) {
309 load_automation (prop->value());
311 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
314 if ((prop = node.property ("visible")) != 0) {
318 visible_parameter_automation.clear ();
320 sstr << prop->value();
326 mark_automation_visible (what, true);
334 Redirect::load_automation (string path)
338 if (path[0] == '/') { // legacy
341 fullpath = _session.automation_dir();
344 ifstream in (fullpath.c_str());
347 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
351 Glib::Mutex::Lock lm (_automation_lock);
352 set<uint32_t> tosave;
353 parameter_automation.clear ();
360 in >> port; if (!in) break;
361 in >> when; if (!in) goto bad;
362 in >> value; if (!in) goto bad;
364 AutomationList& al = automation_list (port);
365 al.add (when, value);
366 tosave.insert (port);
372 error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
373 parameter_automation.clear ();
379 Redirect::what_has_automation (set<uint32_t>& s) const
381 Glib::Mutex::Lock lm (_automation_lock);
382 map<uint32_t,AutomationList*>::const_iterator li;
384 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
385 s.insert ((*li).first);
390 Redirect::what_has_visible_automation (set<uint32_t>& s) const
392 Glib::Mutex::Lock lm (_automation_lock);
393 set<uint32_t>::const_iterator li;
395 for (li = visible_parameter_automation.begin(); li != visible_parameter_automation.end(); ++li) {
400 Redirect::automation_list (uint32_t parameter)
402 AutomationList* al = parameter_automation[parameter];
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);
414 Redirect::describe_parameter (uint32_t which)
416 /* derived classes will override this */
421 Redirect::can_automate (uint32_t what)
423 can_automate_list.insert (what);
427 Redirect::mark_automation_visible (uint32_t what, bool yn)
430 visible_parameter_automation.insert (what);
432 set<uint32_t>::iterator i;
434 if ((i = visible_parameter_automation.find (what)) != visible_parameter_automation.end()) {
435 visible_parameter_automation.erase (i);
441 Redirect::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
443 map<uint32_t,AutomationList*>::const_iterator li;
444 AutomationList::TimeComparator cmp;
446 next_event.when = max_frames;
448 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
450 AutomationList::const_iterator i;
451 const AutomationList& alist (*((*li).second));
452 ControlEvent cp (now, 0.0f);
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) {
460 if (i != alist.const_end() && (*i)->when < end) {
462 if ((*i)->when < next_event.when) {
463 next_event.when = (*i)->when;
468 return next_event.when != max_frames;
472 Redirect::set_active (bool yn, void* src)
475 active_changed (this, src);
476 _session.set_dirty ();