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"
26 #include <glibmm/miscutils.h>
28 #include "pbd/error.h"
29 #include "pbd/enumwriter.h"
31 #include "midi++/names.h"
33 #include "ardour/automatable.h"
34 #include "ardour/amp.h"
35 #include "ardour/event_type_map.h"
36 #include "ardour/midi_track.h"
37 #include "ardour/panner.h"
38 #include "ardour/plugin_insert.h"
39 #include "ardour/session.h"
44 using namespace ARDOUR;
47 nframes_t Automatable::_automation_interval = 0;
49 Automatable::Automatable(Session& session)
51 , _last_automation_snapshot(0)
55 Automatable::Automatable (const Automatable& other)
57 , _a_session (other._a_session)
58 , _last_automation_snapshot (0)
60 Glib::Mutex::Lock lm (other._control_lock);
62 for (Controls::const_iterator i = other._controls.begin(); i != other._controls.end(); ++i) {
63 boost::shared_ptr<Evoral::Control> ac (control_factory (i->first));
68 Automatable::old_set_automation_state (const XMLNode& node)
70 const XMLProperty *prop;
72 if ((prop = node.property ("path")) != 0) {
73 load_automation (prop->value());
75 warning << _("Automation node has no path property") << endmsg;
78 if ((prop = node.property ("visible")) != 0) {
82 _visible_controls.clear ();
84 sstr << prop->value();
90 mark_automation_visible (Evoral::Parameter(PluginAutomation, 0, what), true);
94 _last_automation_snapshot = 0;
100 Automatable::load_automation (const string& path)
104 if (Glib::path_is_absolute (path)) { // legacy
107 fullpath = _a_session.automation_dir();
110 ifstream in (fullpath.c_str());
113 warning << string_compose(_("cannot open %2 to load automation data (%3)")
114 , fullpath, strerror (errno)) << endmsg;
118 Glib::Mutex::Lock lm (control_lock());
119 set<Evoral::Parameter> tosave;
122 _last_automation_snapshot = 0;
129 in >> port; if (!in) break;
130 in >> when; if (!in) goto bad;
131 in >> value; if (!in) goto bad;
133 Evoral::Parameter param(PluginAutomation, 0, port);
134 /* FIXME: this is legacy and only used for plugin inserts? I think? */
135 boost::shared_ptr<Evoral::Control> c = control (param, true);
136 c->list()->add (when, value);
137 tosave.insert (param);
143 error << string_compose(_("cannot load automation data from %2"), fullpath) << endmsg;
149 Automatable::add_control(boost::shared_ptr<Evoral::Control> ac)
151 Evoral::Parameter param = ac->parameter();
153 boost::shared_ptr<AutomationList> al = boost::dynamic_pointer_cast<AutomationList> (ac->list ());
156 al->automation_state_changed.connect_same_thread (
157 _list_connections, boost::bind (&Automatable::automation_list_automation_state_changed, this, ac->parameter(), _1)
160 ControlSet::add_control (ac);
161 _can_automate_list.insert (param);
163 automation_list_automation_state_changed (param, al->automation_state ()); // sync everything up
167 Automatable::what_has_visible_data(set<Evoral::Parameter>& s) const
169 Glib::Mutex::Lock lm (control_lock());
170 set<Evoral::Parameter>::const_iterator li;
172 for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
178 Automatable::describe_parameter (Evoral::Parameter param)
180 /* derived classes like PluginInsert should override this */
182 if (param == Evoral::Parameter(GainAutomation)) {
184 } else if (param.type() == PanAutomation) {
185 /* ID's are zero-based, present them as 1-based */
186 return (string_compose(_("Pan %1"), param.id() + 1));
187 } else if (param.type() == MidiCCAutomation) {
188 return string_compose("%1: %2 [%3]",
189 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);
190 } else if (param.type() == MidiPgmChangeAutomation) {
191 return string_compose("Program [%1]", int(param.channel()) + 1);
192 } else if (param.type() == MidiPitchBenderAutomation) {
193 return string_compose("Bender [%1]", int(param.channel()) + 1);
194 } else if (param.type() == MidiChannelPressureAutomation) {
195 return string_compose("Pressure [%1]", int(param.channel()) + 1);
197 return EventTypeMap::instance().to_symbol(param);
202 Automatable::can_automate (Evoral::Parameter what)
204 _can_automate_list.insert (what);
208 Automatable::mark_automation_visible (Evoral::Parameter what, bool yn)
211 _visible_controls.insert (what);
213 set<Evoral::Parameter>::iterator i;
215 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
216 _visible_controls.erase (i);
221 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
222 * had a single automation parameter, with it's type implicit. Derived objects should
223 * pass that type and it will be used for the untyped AutomationList found.
226 Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy_param)
228 Glib::Mutex::Lock lm (control_lock());
230 /* Don't clear controls, since some may be special derived Controllable classes */
232 _visible_controls.clear ();
234 XMLNodeList nlist = node.children();
235 XMLNodeIterator niter;
237 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
239 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
240 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
244 if ((*niter)->name() == "AutomationList") {
246 const XMLProperty* id_prop = (*niter)->property("automation-id");
248 Evoral::Parameter param = (id_prop
249 ? EventTypeMap::instance().new_parameter(id_prop->value())
252 if (param.type() == NullAutomation) {
253 warning << "Automation has null type" << endl;
257 boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
260 warning << "AutomationList node without automation-id property, "
261 << "using default: " << EventTypeMap::instance().to_symbol(legacy_param) << endmsg;
264 boost::shared_ptr<Evoral::Control> existing = control(param);
266 existing->set_list(al);
268 boost::shared_ptr<Evoral::Control> newcontrol = control_factory(param);
269 add_control(newcontrol);
270 newcontrol->set_list(al);
274 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
278 _last_automation_snapshot = 0;
284 Automatable::get_automation_state ()
286 Glib::Mutex::Lock lm (control_lock());
287 XMLNode* node = new XMLNode (X_("Automation"));
289 if (controls().empty()) {
293 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
294 boost::shared_ptr<AutomationList> l
295 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
297 node->add_child_nocopy (l->get_state ());
305 Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState s)
307 Glib::Mutex::Lock lm (control_lock());
309 boost::shared_ptr<Evoral::Control> c = control (param, true);
310 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
312 if (s != l->automation_state()) {
313 l->set_automation_state (s);
314 _a_session.set_dirty ();
319 Automatable::get_parameter_automation_state (Evoral::Parameter param)
321 AutoState result = Off;
323 boost::shared_ptr<Evoral::Control> c = control(param);
324 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
327 result = l->automation_state();
334 Automatable::set_parameter_automation_style (Evoral::Parameter param, AutoStyle s)
336 Glib::Mutex::Lock lm (control_lock());
338 boost::shared_ptr<Evoral::Control> c = control(param, true);
339 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
341 if (s != l->automation_style()) {
342 l->set_automation_style (s);
343 _a_session.set_dirty ();
348 Automatable::get_parameter_automation_style (Evoral::Parameter param)
350 Glib::Mutex::Lock lm (control_lock());
352 boost::shared_ptr<Evoral::Control> c = control(param);
353 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
356 return l->automation_style();
358 return Absolute; // whatever
363 Automatable::protect_automation ()
365 typedef set<Evoral::Parameter> ParameterSet;
366 ParameterSet automated_params;
368 what_has_data(automated_params);
370 for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
372 boost::shared_ptr<Evoral::Control> c = control(*i);
373 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
375 switch (l->automation_state()) {
377 l->set_automation_state (Off);
380 l->set_automation_state (Play);
389 Automatable::automation_snapshot (nframes_t now, bool force)
391 if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
393 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
394 boost::shared_ptr<AutomationControl> c
395 = boost::dynamic_pointer_cast<AutomationControl>(i->second);
396 if (c->automation_write()) {
397 c->list()->rt_add (now, i->second->user_double());
401 _last_automation_snapshot = now;
406 Automatable::transport_stopped (sframes_t now)
408 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
410 boost::shared_ptr<AutomationControl> c
411 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
412 boost::shared_ptr<AutomationList> l
413 = boost::dynamic_pointer_cast<AutomationList>(c->list());
415 c->list()->reposition_for_rt_add (now);
417 if (c->automation_state() != Off) {
418 c->set_value(c->list()->eval(now));
423 boost::shared_ptr<Evoral::Control>
424 Automatable::control_factory(const Evoral::Parameter& param)
426 boost::shared_ptr<AutomationList> list(new AutomationList(param));
427 Evoral::Control* control = NULL;
428 if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
429 MidiTrack* mt = dynamic_cast<MidiTrack*>(this);
431 control = new MidiTrack::MidiControl(mt, param);
433 warning << "MidiCCAutomation for non-MidiTrack" << endl;
435 } else if (param.type() == PluginAutomation) {
436 PluginInsert* pi = dynamic_cast<PluginInsert*>(this);
438 control = new PluginInsert::PluginControl(pi, param);
440 warning << "PluginAutomation for non-Plugin" << endl;
442 } else if (param.type() == GainAutomation) {
443 Amp* amp = dynamic_cast<Amp*>(this);
445 control = new Amp::GainControl(X_("gaincontrol"), _a_session, amp, param);
447 warning << "GainAutomation for non-Amp" << endl;
449 } else if (param.type() == PanAutomation) {
450 Panner* me = dynamic_cast<Panner*>(this);
452 control = new Panner::PanControllable(me->session(), X_("panner"), *me, param);
454 warning << "PanAutomation for non-Panner" << endl;
459 control = new AutomationControl(_a_session, param);
462 control->set_list(list);
463 return boost::shared_ptr<Evoral::Control>(control);
466 boost::shared_ptr<AutomationControl>
467 Automatable::automation_control (const Evoral::Parameter& id, bool create)
469 return boost::dynamic_pointer_cast<AutomationControl>(Evoral::ControlSet::control(id, create));
472 boost::shared_ptr<const AutomationControl>
473 Automatable::automation_control (const Evoral::Parameter& id) const
475 return boost::dynamic_pointer_cast<const AutomationControl>(Evoral::ControlSet::control(id));
479 Automatable::clear_controls ()
481 _control_connections.drop_connections ();
482 ControlSet::clear_controls ();