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_controls.clear ();
59 sstr << prop->value();
65 mark_automation_visible (Parameter(PluginAutomation, what), true);
69 _last_automation_snapshot = 0;
75 Automatable::load_automation (const string& path)
79 if (path[0] == '/') { // legacy
82 fullpath = _session.automation_dir();
85 ifstream in (fullpath.c_str());
88 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
92 Glib::Mutex::Lock lm (_automation_lock);
93 set<Parameter> tosave;
96 _last_automation_snapshot = 0;
103 in >> port; if (!in) break;
104 in >> when; if (!in) goto bad;
105 in >> value; if (!in) goto bad;
107 /* FIXME: this is legacy and only used for plugin inserts? I think? */
108 boost::shared_ptr<AutomationControl> c = control (Parameter(PluginAutomation, port), true);
109 c->list()->add (when, value);
110 tosave.insert (Parameter(PluginAutomation, port));
116 error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
122 Automatable::add_control(boost::shared_ptr<AutomationControl> ac)
124 Parameter param = ac->parameter();
126 _controls[param] = ac;
128 cerr << _name << ": added parameter " << param.to_string() << endl;
130 _can_automate_list.insert(param);
132 // Sync everything (derived classes) up to initial values
133 auto_state_changed(param);
137 Automatable::what_has_automation (set<Parameter>& s) const
139 Glib::Mutex::Lock lm (_automation_lock);
140 Controls::const_iterator li;
142 // FIXME: correct semantics?
143 for (li = _controls.begin(); li != _controls.end(); ++li) {
144 s.insert ((*li).first);
149 Automatable::what_has_visible_automation (set<Parameter>& s) const
151 Glib::Mutex::Lock lm (_automation_lock);
152 set<Parameter>::const_iterator li;
154 for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
159 /** Returns NULL if we don't have an AutomationList for \a parameter.
161 boost::shared_ptr<AutomationControl>
162 Automatable::control (Parameter parameter, bool create_if_missing)
164 Controls::iterator i = _controls.find(parameter);
166 if (i != _controls.end()) {
169 } else if (create_if_missing) {
170 boost::shared_ptr<AutomationList> al (new AutomationList (
171 parameter, FLT_MIN, FLT_MAX, default_parameter_value (parameter)));
172 boost::shared_ptr<AutomationControl> ac (new AutomationControl(_session, al));
174 cerr << "WARNING: Default AutomationControl created for " << parameter.to_string() << endl;
178 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
179 return boost::shared_ptr<AutomationControl>();
183 boost::shared_ptr<const AutomationControl>
184 Automatable::control (Parameter parameter) const
186 Controls::const_iterator i = _controls.find(parameter);
188 if (i != _controls.end()) {
191 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
192 return boost::shared_ptr<AutomationControl>();
198 Automatable::describe_parameter (Parameter param)
200 /* derived classes like PluginInsert should override this */
202 if (param == Parameter(GainAutomation))
204 else if (param.type() == PanAutomation)
205 return (string_compose(_("Pan %1"), param.id()));
206 else if (param.type() == MidiCCAutomation)
207 return string_compose("CC %1", param.id());
209 return param.to_string();
213 Automatable::can_automate (Parameter what)
215 _can_automate_list.insert (what);
219 Automatable::mark_automation_visible (Parameter what, bool yn)
222 _visible_controls.insert (what);
224 set<Parameter>::iterator i;
226 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
227 _visible_controls.erase (i);
233 Automatable::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
235 Controls::const_iterator li;
236 AutomationList::TimeComparator cmp;
238 next_event.when = max_frames;
240 for (li = _controls.begin(); li != _controls.end(); ++li) {
242 AutomationList::const_iterator i;
243 boost::shared_ptr<const AutomationList> alist (li->second->list());
244 ControlEvent cp (now, 0.0f);
246 for (i = lower_bound (alist->const_begin(), alist->const_end(), &cp, cmp); i != alist->const_end() && (*i)->when < end; ++i) {
247 if ((*i)->when > now) {
252 if (i != alist->const_end() && (*i)->when < end) {
254 if ((*i)->when < next_event.when) {
255 next_event.when = (*i)->when;
260 return next_event.when != max_frames;
263 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
264 * had a single automation parameter, with it's type implicit. Derived objects should
265 * pass that type and it will be used for the untyped AutomationList found.
268 Automatable::set_automation_state (const XMLNode& node, Parameter legacy_param)
270 Glib::Mutex::Lock lm (_automation_lock);
272 /* Don't clear controls, since some may be special derived Controllable classes */
274 _visible_controls.clear ();
276 XMLNodeList nlist = node.children();
277 XMLNodeIterator niter;
279 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
281 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
282 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
286 if ((*niter)->name() == "AutomationList") {
288 const XMLProperty* id_prop = (*niter)->property("automation-id");
290 Parameter param = (id_prop ? Parameter(id_prop->value()) : legacy_param);
292 boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
295 warning << "AutomationList node without automation-id property, "
296 << "using default: " << legacy_param.to_string() << endmsg;
297 al->set_parameter(legacy_param);
300 boost::shared_ptr<AutomationControl> existing = control(param);
302 existing->set_list(al);
304 add_control(boost::shared_ptr<AutomationControl>(new AutomationControl(_session, al)));
307 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
311 _last_automation_snapshot = 0;
317 Automatable::get_automation_state ()
319 Glib::Mutex::Lock lm (_automation_lock);
320 XMLNode* node = new XMLNode (X_("Automation"));
322 if (_controls.empty()) {
326 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li) {
327 node->add_child_nocopy (li->second->list()->get_state ());
334 Automatable::clear_automation ()
336 Glib::Mutex::Lock lm (_automation_lock);
338 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
339 li->second->list()->clear();
343 Automatable::set_parameter_automation_state (Parameter param, AutoState s)
345 Glib::Mutex::Lock lm (_automation_lock);
347 boost::shared_ptr<AutomationControl> c = control (param, true);
349 if (s != c->list()->automation_state()) {
350 c->list()->set_automation_state (s);
351 _session.set_dirty ();
356 Automatable::get_parameter_automation_state (Parameter param, bool lock)
358 AutoState result = Off;
361 _automation_lock.lock();
363 boost::shared_ptr<AutomationControl> c = control(param);
366 result = c->list()->automation_state();
369 _automation_lock.unlock();
375 Automatable::set_parameter_automation_style (Parameter param, AutoStyle s)
377 Glib::Mutex::Lock lm (_automation_lock);
379 boost::shared_ptr<AutomationControl> c = control(param, true);
381 if (s != c->list()->automation_style()) {
382 c->list()->set_automation_style (s);
383 _session.set_dirty ();
388 Automatable::get_parameter_automation_style (Parameter param)
390 Glib::Mutex::Lock lm (_automation_lock);
392 boost::shared_ptr<AutomationControl> c = control(param);
395 return c->list()->automation_style();
397 return Absolute; // whatever
402 Automatable::protect_automation ()
404 set<Parameter> automated_params;
406 what_has_automation (automated_params);
408 for (set<Parameter>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
410 boost::shared_ptr<AutomationControl> c = control(*i);
412 switch (c->list()->automation_state()) {
414 c->list()->set_automation_state (Off);
417 c->list()->set_automation_state (Play);
426 Automatable::automation_snapshot (nframes_t now)
428 if (_last_automation_snapshot > now || (now - _last_automation_snapshot) > _session.automation_interval()) {
430 for (Controls::iterator i = _controls.begin(); i != _controls.end(); ++i) {
431 if (i->second->list()->automation_write()) {
432 i->second->list()->rt_add (now, i->second->user_value());
436 _last_automation_snapshot = now;
441 Automatable::transport_stopped (nframes_t now)
443 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li) {
445 boost::shared_ptr<AutomationControl> c = li->second;
447 c->list()->reposition_for_rt_add (now);
449 if (c->list()->automation_state() != Off) {
450 c->set_value(c->list()->eval(now));