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 Redirect::clone (const Redirect& other)
67 const PortInsert *port_insert;
68 const PluginInsert *plugin_insert;
70 if ((send = dynamic_cast<const Send*>(&other)) != 0) {
71 return new Send (*send);
72 } else if ((port_insert = dynamic_cast<const PortInsert*>(&other)) != 0) {
73 return new PortInsert (*port_insert);
74 } else if ((plugin_insert = dynamic_cast<const PluginInsert*>(&other)) != 0) {
75 return new PluginInsert (*plugin_insert);
77 fatal << _("programming error: unknown Redirect type in Redirect::Clone!\n")
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);
147 for (set<uint32_t>::iterator i = tosave.begin(); i != tosave.end(); ++i) {
148 automation_list (*i).save_state (_("loaded from disk"));
154 error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
155 parameter_automation.clear ();
160 Redirect::save_automation (string path)
162 Glib::Mutex::Lock lm (_automation_lock);
165 if (parameter_automation.empty()) {
169 fullpath = _session.automation_dir();
172 ofstream out (fullpath.c_str());
175 error << string_compose(_("%1: cannot open %2 to store automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
179 AutomationList::const_iterator i;
180 map<uint32_t,AutomationList*>::iterator li;
182 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
183 for (i = (*li).second->begin(); i != (*li).second->end(); ++i) {
185 out << (*li).first << ' ' << (*i)->when << ' ' << (*i)->value << endl;
192 if (i != (*li).second->end()) {
193 unlink (fullpath.c_str());
194 error << string_compose(_("%1: could not save automation state to %2"), _name, fullpath) << endmsg;
199 if (li != parameter_automation.end()) {
200 unlink (fullpath.c_str());
201 error << string_compose(_("%1: could not save automation state to %2"), _name, fullpath) << endmsg;
209 Redirect::get_state (void)
215 Redirect::state (bool full_state)
218 XMLNode* node = new XMLNode (state_node_name);
221 node->add_property("active", active() ? "yes" : "no");
222 node->add_property("placement", placement_as_string (placement()));
223 node->add_child_nocopy (IO::state (full_state));
226 node->add_child_copy (*_extra_xml);
234 snprintf (buf, sizeof(buf), "%" PRIu64, id());
235 path = _session.snap_name();
236 path += "-redirect-";
238 path += ".automation";
240 /* XXX we didn't ask for a state save, we asked for the current state.
244 switch (save_automation (path)) {
246 error << string_compose(_("Could not get state from Redirect (%1). Problem with save_automation"), _name) << endmsg;
250 XMLNode *aevents = node->add_child("Automation");
252 for (set<uint32_t>::iterator x = visible_parameter_automation.begin(); x != visible_parameter_automation.end(); ++x) {
253 if (x != visible_parameter_automation.begin()) {
259 aevents->add_property ("path", path);
260 aevents->add_property ("visible", sstr.str());
269 Redirect::what_has_automation (set<uint32_t>& s) const
271 Glib::Mutex::Lock lm (_automation_lock);
272 map<uint32_t,AutomationList*>::const_iterator li;
274 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
275 s.insert ((*li).first);
280 Redirect::what_has_visible_automation (set<uint32_t>& s) const
282 Glib::Mutex::Lock lm (_automation_lock);
283 set<uint32_t>::const_iterator li;
285 for (li = visible_parameter_automation.begin(); li != visible_parameter_automation.end(); ++li) {
291 Redirect::set_state (const XMLNode& node)
293 const XMLProperty *prop;
295 if (node.name() != state_node_name) {
296 error << string_compose(_("incorrect XML node \"%1\" passed to Redirect object"), node.name()) << endmsg;
300 XMLNodeList nlist = node.children();
301 XMLNodeIterator niter;
302 bool have_io = false;
304 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
306 if ((*niter)->name() == IO::state_node_name) {
308 IO::set_state (**niter);
311 } else if ((*niter)->name() == "Automation") {
315 if ((prop = (*niter)->property ("path")) != 0) {
316 load_automation (prop->value());
318 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
321 if ((prop = (*niter)->property ("visible")) != 0) {
325 visible_parameter_automation.clear ();
327 sstr << prop->value();
333 mark_automation_visible (what, true);
337 } else if ((*niter)->name() == "extra") {
338 _extra_xml = new XMLNode (*(*niter));
343 error << _("XML node describing an IO is missing an IO node") << endmsg;
347 if ((prop = node.property ("active")) == 0) {
348 error << _("XML node describing a redirect is missing the `active' field") << endmsg;
352 if (_active != (prop->value() == "yes")) {
354 active_changed (this, this); /* EMIT_SIGNAL */
357 if ((prop = node.property ("placement")) == 0) {
358 error << _("XML node describing a redirect is missing the `placement' field") << endmsg;
362 set_placement (prop->value(), this);
368 Redirect::automation_list (uint32_t parameter)
370 AutomationList* al = parameter_automation[parameter];
373 al = parameter_automation[parameter] = new AutomationList (default_parameter_value (parameter));
374 /* let derived classes do whatever they need with this */
375 automation_list_creation_callback (parameter, *al);
382 Redirect::describe_parameter (uint32_t which)
384 /* derived classes will override this */
389 Redirect::can_automate (uint32_t what)
391 can_automate_list.insert (what);
395 Redirect::mark_automation_visible (uint32_t what, bool yn)
398 visible_parameter_automation.insert (what);
400 set<uint32_t>::iterator i;
402 if ((i = visible_parameter_automation.find (what)) != visible_parameter_automation.end()) {
403 visible_parameter_automation.erase (i);
409 Redirect::find_next_event (jack_nframes_t now, jack_nframes_t end, ControlEvent& next_event) const
411 map<uint32_t,AutomationList*>::const_iterator li;
412 AutomationList::TimeComparator cmp;
414 next_event.when = max_frames;
416 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
418 AutomationList::const_iterator i;
419 const AutomationList& alist (*((*li).second));
420 ControlEvent cp (now, 0.0f);
422 for (i = lower_bound (alist.const_begin(), alist.const_end(), &cp, cmp); i != alist.const_end() && (*i)->when < end; ++i) {
423 if ((*i)->when > now) {
428 if (i != alist.const_end() && (*i)->when < end) {
430 if ((*i)->when < next_event.when) {
431 next_event.when = (*i)->when;
436 return next_event.when != max_frames;
440 Redirect::store_state (RedirectState& state) const
442 state.active = _active;
446 Redirect::restore_state (StateManager::State& state)
448 RedirectState* rstate = dynamic_cast<RedirectState*> (&state);
449 set_active (rstate->active, this);
454 Redirect::state_factory (std::string why) const
456 RedirectState* state = new RedirectState (why);
458 store_state (*state);
464 Redirect::set_active (bool yn, void* src)
467 save_state (_("active_changed"));
468 active_changed (this, src);
469 _session.set_dirty ();