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"
28 #include "midi++/names.h"
30 #include "ardour/automatable.h"
31 #include "ardour/amp.h"
32 #include "ardour/event_type_map.h"
33 #include "ardour/midi_track.h"
34 #include "ardour/panner.h"
35 #include "ardour/plugin_insert.h"
36 #include "ardour/session.h"
41 using namespace ARDOUR;
44 nframes_t Automatable::_automation_interval = 0;
46 Automatable::Automatable(Session& session)
48 , _last_automation_snapshot(0)
52 Automatable::Automatable (const Automatable& other)
54 , _a_session (other._a_session)
55 , _last_automation_snapshot (0)
57 Glib::Mutex::Lock lm (other._control_lock);
59 for (Controls::const_iterator i = other._controls.begin(); i != other._controls.end(); ++i) {
60 boost::shared_ptr<Evoral::Control> ac (control_factory (i->first));
61 _controls[ac->parameter()] = ac;
65 Automatable::old_set_automation_state (const XMLNode& node)
67 const XMLProperty *prop;
69 if ((prop = node.property ("path")) != 0) {
70 load_automation (prop->value());
72 warning << _("Automation node has no path property") << endmsg;
75 if ((prop = node.property ("visible")) != 0) {
79 _visible_controls.clear ();
81 sstr << prop->value();
87 mark_automation_visible (Evoral::Parameter(PluginAutomation, 0, what), true);
91 _last_automation_snapshot = 0;
97 Automatable::load_automation (const string& path)
101 if (path[0] == '/') { // legacy
104 fullpath = _a_session.automation_dir();
107 ifstream in (fullpath.c_str());
110 warning << string_compose(_("cannot open %2 to load automation data (%3)")
111 , fullpath, strerror (errno)) << endmsg;
115 Glib::Mutex::Lock lm (control_lock());
116 set<Evoral::Parameter> tosave;
119 _last_automation_snapshot = 0;
126 in >> port; if (!in) break;
127 in >> when; if (!in) goto bad;
128 in >> value; if (!in) goto bad;
130 Evoral::Parameter param(PluginAutomation, 0, port);
131 /* FIXME: this is legacy and only used for plugin inserts? I think? */
132 boost::shared_ptr<Evoral::Control> c = control (param, true);
133 c->list()->add (when, value);
134 tosave.insert (param);
140 error << string_compose(_("cannot load automation data from %2"), fullpath) << endmsg;
146 Automatable::add_control(boost::shared_ptr<Evoral::Control> ac)
148 Evoral::Parameter param = ac->parameter();
150 ControlSet::add_control(ac);
151 _can_automate_list.insert(param);
152 auto_state_changed(param); // sync everything up
156 Automatable::what_has_visible_data(set<Evoral::Parameter>& s) const
158 Glib::Mutex::Lock lm (control_lock());
159 set<Evoral::Parameter>::const_iterator li;
161 for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
167 Automatable::describe_parameter (Evoral::Parameter param)
169 /* derived classes like PluginInsert should override this */
171 if (param == Evoral::Parameter(GainAutomation)) {
173 } else if (param.type() == PanAutomation) {
174 /* ID's are zero-based, present them as 1-based */
175 return (string_compose(_("Pan %1"), param.id() + 1));
176 } else if (param.type() == MidiCCAutomation) {
177 return string_compose("%2 [%3]",
178 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);
179 } else if (param.type() == MidiPgmChangeAutomation) {
180 return string_compose("Program [%1]", int(param.channel()) + 1);
181 } else if (param.type() == MidiPitchBenderAutomation) {
182 return string_compose("Bender [%1]", int(param.channel()) + 1);
183 } else if (param.type() == MidiChannelPressureAutomation) {
184 return string_compose("Pressure [%1]", int(param.channel()) + 1);
186 return EventTypeMap::instance().to_symbol(param);
191 Automatable::can_automate (Evoral::Parameter what)
193 _can_automate_list.insert (what);
197 Automatable::mark_automation_visible (Evoral::Parameter what, bool yn)
200 _visible_controls.insert (what);
202 set<Evoral::Parameter>::iterator i;
204 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
205 _visible_controls.erase (i);
210 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
211 * had a single automation parameter, with it's type implicit. Derived objects should
212 * pass that type and it will be used for the untyped AutomationList found.
215 Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy_param)
217 Glib::Mutex::Lock lm (control_lock());
219 /* Don't clear controls, since some may be special derived Controllable classes */
221 _visible_controls.clear ();
223 XMLNodeList nlist = node.children();
224 XMLNodeIterator niter;
226 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
228 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
229 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
233 if ((*niter)->name() == "AutomationList") {
235 const XMLProperty* id_prop = (*niter)->property("automation-id");
237 Evoral::Parameter param = (id_prop
238 ? EventTypeMap::instance().new_parameter(id_prop->value())
241 if (param.type() == NullAutomation) {
242 warning << "Automation has null type" << endl;
246 boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
249 warning << "AutomationList node without automation-id property, "
250 << "using default: " << EventTypeMap::instance().to_symbol(legacy_param) << endmsg;
253 boost::shared_ptr<Evoral::Control> existing = control(param);
255 existing->set_list(al);
257 boost::shared_ptr<Evoral::Control> newcontrol = control_factory(param);
258 add_control(newcontrol);
259 newcontrol->set_list(al);
263 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
267 _last_automation_snapshot = 0;
273 Automatable::get_automation_state ()
275 Glib::Mutex::Lock lm (control_lock());
276 XMLNode* node = new XMLNode (X_("Automation"));
278 if (controls().empty()) {
282 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
283 boost::shared_ptr<AutomationList> l
284 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
286 node->add_child_nocopy (l->get_state ());
294 Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState s)
296 Glib::Mutex::Lock lm (control_lock());
298 boost::shared_ptr<Evoral::Control> c = control (param, true);
299 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
301 if (s != l->automation_state()) {
302 l->set_automation_state (s);
303 _a_session.set_dirty ();
308 Automatable::get_parameter_automation_state (Evoral::Parameter param, bool lock)
310 AutoState result = Off;
313 control_lock().lock();
315 boost::shared_ptr<Evoral::Control> c = control(param);
316 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
319 result = l->automation_state();
322 control_lock().unlock();
328 Automatable::set_parameter_automation_style (Evoral::Parameter param, AutoStyle s)
330 Glib::Mutex::Lock lm (control_lock());
332 boost::shared_ptr<Evoral::Control> c = control(param, true);
333 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
335 if (s != l->automation_style()) {
336 l->set_automation_style (s);
337 _a_session.set_dirty ();
342 Automatable::get_parameter_automation_style (Evoral::Parameter param)
344 Glib::Mutex::Lock lm (control_lock());
346 boost::shared_ptr<Evoral::Control> c = control(param);
347 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
350 return l->automation_style();
352 return Absolute; // whatever
357 Automatable::protect_automation ()
359 typedef set<Evoral::Parameter> ParameterSet;
360 ParameterSet automated_params;
362 what_has_data(automated_params);
364 for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
366 boost::shared_ptr<Evoral::Control> c = control(*i);
367 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
369 switch (l->automation_state()) {
371 l->set_automation_state (Off);
374 l->set_automation_state (Play);
383 Automatable::automation_snapshot (nframes_t now, bool force)
385 if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
387 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
388 boost::shared_ptr<AutomationControl> c
389 = boost::dynamic_pointer_cast<AutomationControl>(i->second);
390 if (c->automation_write()) {
391 c->list()->rt_add (now, i->second->user_float());
395 _last_automation_snapshot = now;
400 Automatable::transport_stopped (sframes_t now)
402 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
404 boost::shared_ptr<AutomationControl> c
405 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
406 boost::shared_ptr<AutomationList> l
407 = boost::dynamic_pointer_cast<AutomationList>(c->list());
409 c->list()->reposition_for_rt_add (now);
411 if (c->automation_state() != Off) {
412 c->set_value(c->list()->eval(now));
417 boost::shared_ptr<Evoral::Control>
418 Automatable::control_factory(const Evoral::Parameter& param)
420 boost::shared_ptr<AutomationList> list(new AutomationList(param));
421 Evoral::Control* control = NULL;
422 if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
423 MidiTrack* mt = dynamic_cast<MidiTrack*>(this);
425 control = new MidiTrack::MidiControl(mt, param);
427 warning << "MidiCCAutomation for non-MidiTrack" << endl;
429 } else if (param.type() == PluginAutomation) {
430 PluginInsert* pi = dynamic_cast<PluginInsert*>(this);
432 control = new PluginInsert::PluginControl(pi, param);
434 warning << "PluginAutomation for non-Plugin" << endl;
436 } else if (param.type() == GainAutomation) {
437 Amp* amp = dynamic_cast<Amp*>(this);
439 control = new Amp::GainControl(X_("gaincontrol"), _a_session, amp, param);
441 warning << "GainAutomation for non-Amp" << endl;
443 } else if (param.type() == PanAutomation) {
444 Panner* me = dynamic_cast<Panner*>(this);
446 control = new Panner::PanControllable(me->session(), X_("panner"), *me, param);
448 warning << "PanAutomation for non-Panner" << endl;
453 control = new AutomationControl(_a_session, param);
456 control->set_list(list);
457 return boost::shared_ptr<Evoral::Control>(control);
460 boost::shared_ptr<AutomationControl>
461 Automatable::automation_control (const Evoral::Parameter& id, bool create)
463 return boost::dynamic_pointer_cast<AutomationControl>(Evoral::ControlSet::control(id, create));
466 boost::shared_ptr<const AutomationControl>
467 Automatable::automation_control (const Evoral::Parameter& id) const
469 return boost::dynamic_pointer_cast<const AutomationControl>(Evoral::ControlSet::control(id));