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 (visible, can_automate) parameter " << al->param_id().to_string() << ", # params = "
126 << _parameter_automation.size() << endl;
128 // FIXME: sane default behaviour?
129 _visible_parameter_automation.insert(al->param_id());
130 _can_automate_list.insert(al->param_id());
134 Automatable::what_has_automation (set<ParamID>& s) const
136 Glib::Mutex::Lock lm (_automation_lock);
137 map<ParamID,AutomationList*>::const_iterator li;
139 for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
140 s.insert ((*li).first);
145 Automatable::what_has_visible_automation (set<ParamID>& s) const
147 Glib::Mutex::Lock lm (_automation_lock);
148 set<ParamID>::const_iterator li;
150 for (li = _visible_parameter_automation.begin(); li != _visible_parameter_automation.end(); ++li) {
155 /** Returns NULL if we don't have an AutomationList for \a parameter.
158 Automatable::automation_list (ParamID parameter, bool create_if_missing)
160 std::map<ParamID,AutomationList*>::iterator i = _parameter_automation.find(parameter);
162 if (i != _parameter_automation.end()) {
165 } else if (create_if_missing) {
166 AutomationList* al = new AutomationList (parameter, FLT_MIN, FLT_MAX, default_parameter_value (parameter));
167 add_automation_parameter(al);
171 warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
176 const AutomationList*
177 Automatable::automation_list (ParamID parameter) const
179 std::map<ParamID,AutomationList*>::const_iterator i = _parameter_automation.find(parameter);
181 if (i != _parameter_automation.end()) {
184 warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
191 Automatable::describe_parameter (ParamID param)
193 /* derived classes like PluginInsert should override this */
195 if (param == ParamID(GainAutomation))
197 else if (param == ParamID(PanAutomation))
199 else if (param.type() == MidiCCAutomation)
200 return string_compose("MIDI CC %1", param.id());
202 return param.to_string();
206 Automatable::can_automate (ParamID what)
208 _can_automate_list.insert (what);
212 Automatable::mark_automation_visible (ParamID what, bool yn)
215 _visible_parameter_automation.insert (what);
217 set<ParamID>::iterator i;
219 if ((i = _visible_parameter_automation.find (what)) != _visible_parameter_automation.end()) {
220 _visible_parameter_automation.erase (i);
226 Automatable::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
228 map<ParamID,AutomationList*>::const_iterator li;
229 AutomationList::TimeComparator cmp;
231 next_event.when = max_frames;
233 for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
235 AutomationList::const_iterator i;
236 const AutomationList& alist (*((*li).second));
237 ControlEvent cp (now, 0.0f);
239 for (i = lower_bound (alist.const_begin(), alist.const_end(), &cp, cmp); i != alist.const_end() && (*i)->when < end; ++i) {
240 if ((*i)->when > now) {
245 if (i != alist.const_end() && (*i)->when < end) {
247 if ((*i)->when < next_event.when) {
248 next_event.when = (*i)->when;
253 return next_event.when != max_frames;
256 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
257 * had a single automation parameter, with it's type implicit. Derived objects should
258 * pass that type and it will be used for the untyped AutomationList found.
261 Automatable::set_automation_state (const XMLNode& node, ParamID legacy_param)
263 Glib::Mutex::Lock lm (_automation_lock);
265 _parameter_automation.clear ();
266 _visible_parameter_automation.clear ();
268 XMLNodeList nlist = node.children();
269 XMLNodeIterator niter;
271 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
273 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
274 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
278 if ((*niter)->name() == "AutomationList") {
280 const XMLProperty* id_prop = (*niter)->property("automation-id");
282 ParamID param = (id_prop ? ParamID(id_prop->value()) : legacy_param);
284 AutomationList* al = new AutomationList(**niter, param);
287 warning << "AutomationList node without automation-id property, "
288 << "using default: " << legacy_param.to_string() << endmsg;
289 al->set_param_id(legacy_param);
292 add_automation_parameter(al);
295 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
303 Automatable::get_automation_state ()
305 Glib::Mutex::Lock lm (_automation_lock);
306 XMLNode* node = new XMLNode (X_("Automation"));
308 cerr << "'" << _name << "'->get_automation_state, # params = " << _parameter_automation.size() << endl;
310 if (_parameter_automation.empty()) {
314 map<ParamID,AutomationList*>::iterator li;
316 for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
317 node->add_child_nocopy (li->second->get_state ());
324 Automatable::clear_automation ()
326 Glib::Mutex::Lock lm (_automation_lock);
328 map<ParamID,AutomationList*>::iterator li;
330 for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li)
335 Automatable::set_parameter_automation_state (ParamID param, AutoState s)
337 Glib::Mutex::Lock lm (_automation_lock);
339 AutomationList* al = automation_list (param, true);
341 if (s != al->automation_state()) {
342 al->set_automation_state (s);
343 _session.set_dirty ();
348 Automatable::get_parameter_automation_state (ParamID param)
350 Glib::Mutex::Lock lm (_automation_lock);
352 AutomationList* al = automation_list(param);
355 return al->automation_state();
362 Automatable::set_parameter_automation_style (ParamID param, AutoStyle s)
364 Glib::Mutex::Lock lm (_automation_lock);
366 AutomationList* al = automation_list (param, true);
368 if (s != al->automation_style()) {
369 al->set_automation_style (s);
370 _session.set_dirty ();
375 Automatable::get_parameter_automation_style (ParamID param)
377 Glib::Mutex::Lock lm (_automation_lock);
379 AutomationList* al = automation_list(param);
382 return al->automation_style();
384 return Absolute; // whatever
389 Automatable::protect_automation ()
391 set<ParamID> automated_params;
393 what_has_automation (automated_params);
395 for (set<ParamID>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
397 AutomationList* al = automation_list (*i);
399 switch (al->automation_state()) {
401 al->set_automation_state (Off);
404 al->set_automation_state (Play);