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));
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 boost::shared_ptr<AutomationList> al = boost::dynamic_pointer_cast<AutomationList> (ac->list ());
153 al->automation_state_changed.connect_same_thread (
154 _list_connections, boost::bind (&Automatable::automation_list_automation_state_changed, this, ac->parameter(), _1)
157 ControlSet::add_control (ac);
158 _can_automate_list.insert (param);
160 automation_list_automation_state_changed (param, al->automation_state ()); // sync everything up
164 Automatable::what_has_visible_data(set<Evoral::Parameter>& s) const
166 Glib::Mutex::Lock lm (control_lock());
167 set<Evoral::Parameter>::const_iterator li;
169 for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
175 Automatable::describe_parameter (Evoral::Parameter param)
177 /* derived classes like PluginInsert should override this */
179 if (param == Evoral::Parameter(GainAutomation)) {
181 } else if (param.type() == PanAutomation) {
182 /* ID's are zero-based, present them as 1-based */
183 return (string_compose(_("Pan %1"), param.id() + 1));
184 } else if (param.type() == MidiCCAutomation) {
185 return string_compose("%1: %2 [%3]",
186 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);
187 } else if (param.type() == MidiPgmChangeAutomation) {
188 return string_compose("Program [%1]", int(param.channel()) + 1);
189 } else if (param.type() == MidiPitchBenderAutomation) {
190 return string_compose("Bender [%1]", int(param.channel()) + 1);
191 } else if (param.type() == MidiChannelPressureAutomation) {
192 return string_compose("Pressure [%1]", int(param.channel()) + 1);
194 return EventTypeMap::instance().to_symbol(param);
199 Automatable::can_automate (Evoral::Parameter what)
201 _can_automate_list.insert (what);
205 Automatable::mark_automation_visible (Evoral::Parameter what, bool yn)
208 _visible_controls.insert (what);
210 set<Evoral::Parameter>::iterator i;
212 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
213 _visible_controls.erase (i);
218 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
219 * had a single automation parameter, with it's type implicit. Derived objects should
220 * pass that type and it will be used for the untyped AutomationList found.
223 Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy_param)
225 Glib::Mutex::Lock lm (control_lock());
227 /* Don't clear controls, since some may be special derived Controllable classes */
229 _visible_controls.clear ();
231 XMLNodeList nlist = node.children();
232 XMLNodeIterator niter;
234 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
236 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
237 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
241 if ((*niter)->name() == "AutomationList") {
243 const XMLProperty* id_prop = (*niter)->property("automation-id");
245 Evoral::Parameter param = (id_prop
246 ? EventTypeMap::instance().new_parameter(id_prop->value())
249 if (param.type() == NullAutomation) {
250 warning << "Automation has null type" << endl;
254 boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
257 warning << "AutomationList node without automation-id property, "
258 << "using default: " << EventTypeMap::instance().to_symbol(legacy_param) << endmsg;
261 boost::shared_ptr<Evoral::Control> existing = control(param);
263 existing->set_list(al);
265 boost::shared_ptr<Evoral::Control> newcontrol = control_factory(param);
266 add_control(newcontrol);
267 newcontrol->set_list(al);
271 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
275 _last_automation_snapshot = 0;
281 Automatable::get_automation_state ()
283 Glib::Mutex::Lock lm (control_lock());
284 XMLNode* node = new XMLNode (X_("Automation"));
286 if (controls().empty()) {
290 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
291 boost::shared_ptr<AutomationList> l
292 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
294 node->add_child_nocopy (l->get_state ());
302 Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState s)
304 Glib::Mutex::Lock lm (control_lock());
306 boost::shared_ptr<Evoral::Control> c = control (param, true);
307 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
309 if (s != l->automation_state()) {
310 l->set_automation_state (s);
311 _a_session.set_dirty ();
316 Automatable::get_parameter_automation_state (Evoral::Parameter param)
318 AutoState result = Off;
320 boost::shared_ptr<Evoral::Control> c = control(param);
321 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
324 result = l->automation_state();
331 Automatable::set_parameter_automation_style (Evoral::Parameter param, AutoStyle s)
333 Glib::Mutex::Lock lm (control_lock());
335 boost::shared_ptr<Evoral::Control> c = control(param, true);
336 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
338 if (s != l->automation_style()) {
339 l->set_automation_style (s);
340 _a_session.set_dirty ();
345 Automatable::get_parameter_automation_style (Evoral::Parameter param)
347 Glib::Mutex::Lock lm (control_lock());
349 boost::shared_ptr<Evoral::Control> c = control(param);
350 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
353 return l->automation_style();
355 return Absolute; // whatever
360 Automatable::protect_automation ()
362 typedef set<Evoral::Parameter> ParameterSet;
363 ParameterSet automated_params;
365 what_has_data(automated_params);
367 for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
369 boost::shared_ptr<Evoral::Control> c = control(*i);
370 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
372 switch (l->automation_state()) {
374 l->set_automation_state (Off);
377 l->set_automation_state (Play);
386 Automatable::automation_snapshot (nframes_t now, bool force)
388 if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
390 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
391 boost::shared_ptr<AutomationControl> c
392 = boost::dynamic_pointer_cast<AutomationControl>(i->second);
393 if (c->automation_write()) {
394 c->list()->rt_add (now, i->second->user_double());
398 _last_automation_snapshot = now;
403 Automatable::transport_stopped (sframes_t now)
405 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
407 boost::shared_ptr<AutomationControl> c
408 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
409 boost::shared_ptr<AutomationList> l
410 = boost::dynamic_pointer_cast<AutomationList>(c->list());
412 c->list()->reposition_for_rt_add (now);
414 if (c->automation_state() != Off) {
415 c->set_value(c->list()->eval(now));
420 boost::shared_ptr<Evoral::Control>
421 Automatable::control_factory(const Evoral::Parameter& param)
423 boost::shared_ptr<AutomationList> list(new AutomationList(param));
424 Evoral::Control* control = NULL;
425 if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
426 MidiTrack* mt = dynamic_cast<MidiTrack*>(this);
428 control = new MidiTrack::MidiControl(mt, param);
430 warning << "MidiCCAutomation for non-MidiTrack" << endl;
432 } else if (param.type() == PluginAutomation) {
433 PluginInsert* pi = dynamic_cast<PluginInsert*>(this);
435 control = new PluginInsert::PluginControl(pi, param);
437 warning << "PluginAutomation for non-Plugin" << endl;
439 } else if (param.type() == GainAutomation) {
440 Amp* amp = dynamic_cast<Amp*>(this);
442 control = new Amp::GainControl(X_("gaincontrol"), _a_session, amp, param);
444 warning << "GainAutomation for non-Amp" << endl;
446 } else if (param.type() == PanAutomation) {
447 Panner* me = dynamic_cast<Panner*>(this);
449 control = new Panner::PanControllable(me->session(), X_("panner"), *me, param);
451 warning << "PanAutomation for non-Panner" << endl;
456 control = new AutomationControl(_a_session, param);
459 control->set_list(list);
460 return boost::shared_ptr<Evoral::Control>(control);
463 boost::shared_ptr<AutomationControl>
464 Automatable::automation_control (const Evoral::Parameter& id, bool create)
466 return boost::dynamic_pointer_cast<AutomationControl>(Evoral::ControlSet::control(id, create));
469 boost::shared_ptr<const AutomationControl>
470 Automatable::automation_control (const Evoral::Parameter& id) const
472 return boost::dynamic_pointer_cast<const AutomationControl>(Evoral::ControlSet::control(id));
476 Automatable::clear_controls ()
478 _control_connections.drop_connections ();
479 ControlSet::clear_controls ();