2 Copyright (C) 2001,2007 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.
20 #include <ardour/ardour.h>
25 #include <pbd/error.h>
26 #include <pbd/enumwriter.h>
27 #include <ardour/session.h>
28 #include <ardour/automatable.h>
33 using namespace ARDOUR;
37 Automatable::Automatable(Session& _session, const string& name)
38 : SessionObject(_session, name)
39 , _last_automation_snapshot(0)
43 Automatable::old_set_automation_state (const XMLNode& node)
45 const XMLProperty *prop;
47 if ((prop = node.property ("path")) != 0) {
48 load_automation (prop->value());
50 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
53 if ((prop = node.property ("visible")) != 0) {
57 _visible_parameter_automation.clear ();
59 sstr << prop->value();
65 mark_automation_visible (ParamID(PluginAutomation, what), true);
73 Automatable::load_automation (const string& path)
77 if (path[0] == '/') { // legacy
80 fullpath = _session.automation_dir();
83 ifstream in (fullpath.c_str());
86 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
90 Glib::Mutex::Lock lm (_automation_lock);
92 _parameter_automation.clear ();
99 in >> port; if (!in) break;
100 in >> when; if (!in) goto bad;
101 in >> value; if (!in) goto bad;
103 /* FIXME: this is legacy and only used for plugin inserts? I think? */
104 AutomationList* al = automation_list (ParamID(PluginAutomation, port), true);
105 al->add (when, value);
106 tosave.insert (ParamID(PluginAutomation, port));
112 error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
113 _parameter_automation.clear ();
118 Automatable::add_automation_parameter(AutomationList* al)
120 _parameter_automation[al->param_id()] = al;
122 /* let derived classes do whatever they need with this */
123 automation_list_creation_callback (al->param_id(), *al);
125 cerr << _name << ": added parameter " << al->param_id().to_string() << endl;
127 // FIXME: sane default behaviour?
128 _visible_parameter_automation.insert(al->param_id());
129 _can_automate_list.insert(al->param_id());
133 Automatable::what_has_automation (set<ParamID>& s) const
135 Glib::Mutex::Lock lm (_automation_lock);
136 map<ParamID,AutomationList*>::const_iterator li;
138 for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
139 s.insert ((*li).first);
144 Automatable::what_has_visible_automation (set<ParamID>& s) const
146 Glib::Mutex::Lock lm (_automation_lock);
147 set<ParamID>::const_iterator li;
149 for (li = _visible_parameter_automation.begin(); li != _visible_parameter_automation.end(); ++li) {
154 /** Returns NULL if we don't have an AutomationList for \a parameter.
157 Automatable::automation_list (ParamID parameter, bool create_if_missing)
159 std::map<ParamID,AutomationList*>::iterator i = _parameter_automation.find(parameter);
161 if (i != _parameter_automation.end()) {
164 } else if (create_if_missing) {
165 AutomationList* al = new AutomationList (parameter, FLT_MIN, FLT_MAX, default_parameter_value (parameter));
166 add_automation_parameter(al);
170 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
175 const AutomationList*
176 Automatable::automation_list (ParamID parameter) const
178 std::map<ParamID,AutomationList*>::const_iterator i = _parameter_automation.find(parameter);
180 if (i != _parameter_automation.end()) {
183 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
190 Automatable::describe_parameter (ParamID param)
192 /* derived classes like PluginInsert should override this */
194 if (param == ParamID(GainAutomation))
196 else if (param == ParamID(PanAutomation))
198 else if (param.type() == MidiCCAutomation)
199 return string_compose("MIDI CC %1", param.id());
201 return param.to_string();
205 Automatable::can_automate (ParamID what)
207 _can_automate_list.insert (what);
211 Automatable::mark_automation_visible (ParamID what, bool yn)
214 _visible_parameter_automation.insert (what);
216 set<ParamID>::iterator i;
218 if ((i = _visible_parameter_automation.find (what)) != _visible_parameter_automation.end()) {
219 _visible_parameter_automation.erase (i);
225 Automatable::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
227 map<ParamID,AutomationList*>::const_iterator li;
228 AutomationList::TimeComparator cmp;
230 next_event.when = max_frames;
232 for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
234 AutomationList::const_iterator i;
235 const AutomationList& alist (*((*li).second));
236 ControlEvent cp (now, 0.0f);
238 for (i = lower_bound (alist.const_begin(), alist.const_end(), &cp, cmp); i != alist.const_end() && (*i)->when < end; ++i) {
239 if ((*i)->when > now) {
244 if (i != alist.const_end() && (*i)->when < end) {
246 if ((*i)->when < next_event.when) {
247 next_event.when = (*i)->when;
252 return next_event.when != max_frames;
255 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
256 * had a single automation parameter, with it's type implicit. Derived objects should
257 * pass that type and it will be used for the untyped AutomationList found.
260 Automatable::set_automation_state (const XMLNode& node, ParamID legacy_param)
262 Glib::Mutex::Lock lm (_automation_lock);
264 _parameter_automation.clear ();
265 _visible_parameter_automation.clear ();
267 XMLNodeList nlist = node.children();
268 XMLNodeIterator niter;
270 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
272 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
273 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
277 if ((*niter)->name() == "AutomationList") {
279 const XMLProperty* id_prop = (*niter)->property("automation-id");
281 ParamID param = (id_prop ? ParamID(id_prop->value()) : legacy_param);
283 AutomationList* al = new AutomationList(**niter, param);
286 warning << "AutomationList node without automation-id property, "
287 << "using default: " << legacy_param.to_string() << endmsg;
288 al->set_param_id(legacy_param);
291 add_automation_parameter(al);
294 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
302 Automatable::get_automation_state ()
304 Glib::Mutex::Lock lm (_automation_lock);
305 XMLNode* node = new XMLNode (X_("Automation"));
307 cerr << "'" << _name << "'->get_automation_state, # params = " << _parameter_automation.size() << endl;
309 if (_parameter_automation.empty()) {
313 map<ParamID,AutomationList*>::iterator li;
315 for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
316 node->add_child_nocopy (li->second->get_state ());
323 Automatable::clear_automation ()
325 Glib::Mutex::Lock lm (_automation_lock);
327 map<ParamID,AutomationList*>::iterator li;
329 for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li)
334 Automatable::set_parameter_automation_state (ParamID param, AutoState s)
336 Glib::Mutex::Lock lm (_automation_lock);
338 AutomationList* al = automation_list (param, true);
340 if (s != al->automation_state()) {
341 al->set_automation_state (s);
342 _session.set_dirty ();
347 Automatable::get_parameter_automation_state (ParamID param)
349 Glib::Mutex::Lock lm (_automation_lock);
351 AutomationList* al = automation_list(param);
354 return al->automation_state();
361 Automatable::set_parameter_automation_style (ParamID param, AutoStyle s)
363 Glib::Mutex::Lock lm (_automation_lock);
365 AutomationList* al = automation_list (param, true);
367 if (s != al->automation_style()) {
368 al->set_automation_style (s);
369 _session.set_dirty ();
374 Automatable::get_parameter_automation_style (ParamID param)
376 Glib::Mutex::Lock lm (_automation_lock);
378 AutomationList* al = automation_list(param);
381 return al->automation_style();
383 return Absolute; // whatever
388 Automatable::protect_automation ()
390 set<ParamID> automated_params;
392 what_has_automation (automated_params);
394 for (set<ParamID>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
396 AutomationList* al = automation_list (*i);
398 switch (al->automation_state()) {
400 al->set_automation_state (Off);
403 al->set_automation_state (Play);