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 ()
63 boost::shared_ptr<Redirect>
64 Redirect::clone (boost::shared_ptr<const Redirect> other)
66 boost::shared_ptr<const Send> send;
67 boost::shared_ptr<const PortInsert> port_insert;
68 boost::shared_ptr<const PluginInsert> plugin_insert;
70 if ((send = boost::dynamic_pointer_cast<const Send>(other)) != 0) {
71 return boost::shared_ptr<Redirect> (new Send (*send));
72 } else if ((port_insert = boost::dynamic_pointer_cast<const PortInsert>(other)) != 0) {
73 return boost::shared_ptr<Redirect> (new PortInsert (*port_insert));
74 } else if ((plugin_insert = boost::dynamic_pointer_cast<const PluginInsert>(other)) != 0) {
75 return boost::shared_ptr<Redirect> (new PluginInsert (*plugin_insert));
77 fatal << _("programming error: unknown Redirect type in Redirect::Clone!\n")
81 return boost::shared_ptr<Redirect>();
85 Redirect::set_sort_key (uint32_t key)
91 Redirect::set_placement (Placement p, void *src)
93 if (_placement != p) {
95 placement_changed (this, src); /* EMIT SIGNAL */
100 Redirect::set_placement (const string& str, void *src)
102 if (str == _("pre")) {
103 set_placement (PreFader, this);
104 } else if (str == _("post")) {
105 set_placement (PostFader, this);
107 error << string_compose(_("Redirect: unknown placement string \"%1\" (ignored)"), str) << endmsg;
112 Redirect::load_automation (string path)
116 if (path[0] == '/') { // legacy
119 fullpath = _session.automation_dir();
122 ifstream in (fullpath.c_str());
125 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
129 Glib::Mutex::Lock lm (_automation_lock);
130 set<uint32_t> tosave;
131 parameter_automation.clear ();
138 in >> port; if (!in) break;
139 in >> when; if (!in) goto bad;
140 in >> value; if (!in) goto bad;
142 AutomationList& al = automation_list (port);
143 al.add (when, value);
144 tosave.insert (port);
150 error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
151 parameter_automation.clear ();
156 Redirect::save_automation (string path)
158 Glib::Mutex::Lock lm (_automation_lock);
161 if (parameter_automation.empty()) {
165 fullpath = _session.automation_dir();
168 ofstream out (fullpath.c_str());
171 error << string_compose(_("%1: cannot open %2 to store automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
175 AutomationList::const_iterator i;
176 map<uint32_t,AutomationList*>::iterator li;
178 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
179 for (i = (*li).second->begin(); i != (*li).second->end(); ++i) {
181 out << (*li).first << ' ' << (*i)->when << ' ' << (*i)->value << endl;
188 if (i != (*li).second->end()) {
189 unlink (fullpath.c_str());
190 error << string_compose(_("%1: could not save automation state to %2"), _name, fullpath) << endmsg;
195 if (li != parameter_automation.end()) {
196 unlink (fullpath.c_str());
197 error << string_compose(_("%1: could not save automation state to %2"), _name, fullpath) << endmsg;
205 Redirect::get_state (void)
211 Redirect::state (bool full_state)
214 XMLNode* node = new XMLNode (state_node_name);
217 node->add_property("active", active() ? "yes" : "no");
218 node->add_property("placement", placement_as_string (placement()));
219 node->add_child_nocopy (IO::state (full_state));
222 node->add_child_copy (*_extra_xml);
230 path = _session.snap_name();
231 path += "-redirect-";
232 id().print (buf, sizeof (buf));
234 path += ".automation";
236 /* XXX we didn't ask for a state save, we asked for the current state.
240 switch (save_automation (path)) {
242 error << string_compose(_("Could not get state from Redirect (%1). Problem with save_automation"), _name) << endmsg;
246 XMLNode *aevents = node->add_child("Automation");
248 for (set<uint32_t>::iterator x = visible_parameter_automation.begin(); x != visible_parameter_automation.end(); ++x) {
249 if (x != visible_parameter_automation.begin()) {
255 aevents->add_property ("path", path);
256 aevents->add_property ("visible", sstr.str());
265 Redirect::what_has_automation (set<uint32_t>& s) const
267 Glib::Mutex::Lock lm (_automation_lock);
268 map<uint32_t,AutomationList*>::const_iterator li;
270 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
271 s.insert ((*li).first);
276 Redirect::what_has_visible_automation (set<uint32_t>& s) const
278 Glib::Mutex::Lock lm (_automation_lock);
279 set<uint32_t>::const_iterator li;
281 for (li = visible_parameter_automation.begin(); li != visible_parameter_automation.end(); ++li) {
287 Redirect::set_state (const XMLNode& node)
289 const XMLProperty *prop;
291 if (node.name() != state_node_name) {
292 error << string_compose(_("incorrect XML node \"%1\" passed to Redirect object"), node.name()) << endmsg;
296 XMLNodeList nlist = node.children();
297 XMLNodeIterator niter;
298 bool have_io = false;
300 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
302 if ((*niter)->name() == IO::state_node_name) {
304 IO::set_state (**niter);
307 } else if ((*niter)->name() == "Automation") {
311 if ((prop = (*niter)->property ("path")) != 0) {
312 load_automation (prop->value());
314 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
317 if ((prop = (*niter)->property ("visible")) != 0) {
321 visible_parameter_automation.clear ();
323 sstr << prop->value();
329 mark_automation_visible (what, true);
333 } else if ((*niter)->name() == "extra") {
334 _extra_xml = new XMLNode (*(*niter));
339 error << _("XML node describing an IO is missing an IO node") << endmsg;
343 if ((prop = node.property ("active")) == 0) {
344 error << _("XML node describing a redirect is missing the `active' field") << endmsg;
348 if (_active != (prop->value() == "yes")) {
350 active_changed (this, this); /* EMIT_SIGNAL */
353 if ((prop = node.property ("placement")) == 0) {
354 error << _("XML node describing a redirect is missing the `placement' field") << endmsg;
358 set_placement (prop->value(), this);
364 Redirect::automation_list (uint32_t parameter)
366 AutomationList* al = parameter_automation[parameter];
369 al = parameter_automation[parameter] = new AutomationList (default_parameter_value (parameter));
370 /* let derived classes do whatever they need with this */
371 automation_list_creation_callback (parameter, *al);
378 Redirect::describe_parameter (uint32_t which)
380 /* derived classes will override this */
385 Redirect::can_automate (uint32_t what)
387 can_automate_list.insert (what);
391 Redirect::mark_automation_visible (uint32_t what, bool yn)
394 visible_parameter_automation.insert (what);
396 set<uint32_t>::iterator i;
398 if ((i = visible_parameter_automation.find (what)) != visible_parameter_automation.end()) {
399 visible_parameter_automation.erase (i);
405 Redirect::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
407 map<uint32_t,AutomationList*>::const_iterator li;
408 AutomationList::TimeComparator cmp;
410 next_event.when = max_frames;
412 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
414 AutomationList::const_iterator i;
415 const AutomationList& alist (*((*li).second));
416 ControlEvent cp (now, 0.0f);
418 for (i = lower_bound (alist.const_begin(), alist.const_end(), &cp, cmp); i != alist.const_end() && (*i)->when < end; ++i) {
419 if ((*i)->when > now) {
424 if (i != alist.const_end() && (*i)->when < end) {
426 if ((*i)->when < next_event.when) {
427 next_event.when = (*i)->when;
432 return next_event.when != max_frames;
436 Redirect::set_active (bool yn, void* src)
439 active_changed (this, src);
440 _session.set_dirty ();