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 "midi++/names.h"
28 #include "ardour/automatable.h"
29 #include "ardour/event_type_map.h"
30 #include "ardour/midi_track.h"
31 #include "ardour/panner.h"
32 #include "ardour/plugin_insert.h"
33 #include "ardour/session.h"
38 using namespace ARDOUR;
41 nframes_t Automatable::_automation_interval = 0;
43 Automatable::Automatable(Session& session)
45 , _last_automation_snapshot(0)
50 Automatable::old_set_automation_state (const XMLNode& node)
52 const XMLProperty *prop;
54 if ((prop = node.property ("path")) != 0) {
55 load_automation (prop->value());
57 warning << _("Automation node has no path property") << endmsg;
60 if ((prop = node.property ("visible")) != 0) {
64 _visible_controls.clear ();
66 sstr << prop->value();
72 mark_automation_visible (Evoral::Parameter(PluginAutomation, 0, what), true);
76 _last_automation_snapshot = 0;
82 Automatable::load_automation (const string& path)
86 if (path[0] == '/') { // legacy
89 fullpath = _a_session.automation_dir();
92 ifstream in (fullpath.c_str());
95 warning << string_compose(_("cannot open %2 to load automation data (%3)")
96 , fullpath, strerror (errno)) << endmsg;
100 Glib::Mutex::Lock lm (control_lock());
101 set<Evoral::Parameter> tosave;
104 _last_automation_snapshot = 0;
111 in >> port; if (!in) break;
112 in >> when; if (!in) goto bad;
113 in >> value; if (!in) goto bad;
115 Evoral::Parameter param(PluginAutomation, 0, port);
116 /* FIXME: this is legacy and only used for plugin inserts? I think? */
117 boost::shared_ptr<Evoral::Control> c = control (param, true);
118 c->list()->add (when, value);
119 tosave.insert (param);
125 error << string_compose(_("cannot load automation data from %2"), fullpath) << endmsg;
131 Automatable::add_control(boost::shared_ptr<Evoral::Control> ac)
133 Evoral::Parameter param = ac->parameter();
135 ControlSet::add_control(ac);
136 _can_automate_list.insert(param);
137 auto_state_changed(param); // sync everything up
141 Automatable::what_has_visible_data(set<Evoral::Parameter>& s) const
143 Glib::Mutex::Lock lm (control_lock());
144 set<Evoral::Parameter>::const_iterator li;
146 for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
152 Automatable::describe_parameter (Evoral::Parameter param)
154 /* derived classes like PluginInsert should override this */
156 if (param == Evoral::Parameter(GainAutomation)) {
158 } else if (param.type() == PanAutomation) {
159 /* ID's are zero-based, present them as 1-based */
160 return (string_compose(_("Pan %1"), param.id() + 1));
161 } else if (param.type() == MidiCCAutomation) {
162 return string_compose("%2 [%3]",
163 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);
164 } else if (param.type() == MidiPgmChangeAutomation) {
165 return string_compose("Program [%1]", int(param.channel()) + 1);
166 } else if (param.type() == MidiPitchBenderAutomation) {
167 return string_compose("Bender [%1]", int(param.channel()) + 1);
168 } else if (param.type() == MidiChannelPressureAutomation) {
169 return string_compose("Pressure [%1]", int(param.channel()) + 1);
171 return EventTypeMap::instance().to_symbol(param);
176 Automatable::can_automate (Evoral::Parameter what)
178 _can_automate_list.insert (what);
182 Automatable::mark_automation_visible (Evoral::Parameter what, bool yn)
185 _visible_controls.insert (what);
187 set<Evoral::Parameter>::iterator i;
189 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
190 _visible_controls.erase (i);
195 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
196 * had a single automation parameter, with it's type implicit. Derived objects should
197 * pass that type and it will be used for the untyped AutomationList found.
200 Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy_param)
202 Glib::Mutex::Lock lm (control_lock());
204 /* Don't clear controls, since some may be special derived Controllable classes */
206 _visible_controls.clear ();
208 XMLNodeList nlist = node.children();
209 XMLNodeIterator niter;
211 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
213 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
214 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
218 if ((*niter)->name() == "AutomationList") {
220 const XMLProperty* id_prop = (*niter)->property("automation-id");
222 Evoral::Parameter param = (id_prop
223 ? EventTypeMap::instance().new_parameter(id_prop->value())
226 if (param.type() == NullAutomation) {
227 warning << "Automation has null type" << endl;
231 boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
234 warning << "AutomationList node without automation-id property, "
235 << "using default: " << EventTypeMap::instance().to_symbol(legacy_param) << endmsg;
238 boost::shared_ptr<Evoral::Control> existing = control(param);
240 existing->set_list(al);
242 boost::shared_ptr<Evoral::Control> newcontrol = control_factory(param);
243 add_control(newcontrol);
244 newcontrol->set_list(al);
248 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
252 _last_automation_snapshot = 0;
258 Automatable::get_automation_state ()
260 Glib::Mutex::Lock lm (control_lock());
261 XMLNode* node = new XMLNode (X_("Automation"));
263 if (controls().empty()) {
267 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
268 boost::shared_ptr<AutomationList> l
269 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
271 node->add_child_nocopy (l->get_state ());
279 Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState s)
281 Glib::Mutex::Lock lm (control_lock());
283 boost::shared_ptr<Evoral::Control> c = control (param, true);
284 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
286 if (s != l->automation_state()) {
287 l->set_automation_state (s);
288 _a_session.set_dirty ();
293 Automatable::get_parameter_automation_state (Evoral::Parameter param, bool lock)
295 AutoState result = Off;
298 control_lock().lock();
300 boost::shared_ptr<Evoral::Control> c = control(param);
301 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
304 result = l->automation_state();
307 control_lock().unlock();
313 Automatable::set_parameter_automation_style (Evoral::Parameter param, AutoStyle s)
315 Glib::Mutex::Lock lm (control_lock());
317 boost::shared_ptr<Evoral::Control> c = control(param, true);
318 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
320 if (s != l->automation_style()) {
321 l->set_automation_style (s);
322 _a_session.set_dirty ();
327 Automatable::get_parameter_automation_style (Evoral::Parameter param)
329 Glib::Mutex::Lock lm (control_lock());
331 boost::shared_ptr<Evoral::Control> c = control(param);
332 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
335 return l->automation_style();
337 return Absolute; // whatever
342 Automatable::protect_automation ()
344 typedef set<Evoral::Parameter> ParameterSet;
345 ParameterSet automated_params;
347 what_has_data(automated_params);
349 for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
351 boost::shared_ptr<Evoral::Control> c = control(*i);
352 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
354 switch (l->automation_state()) {
356 l->set_automation_state (Off);
359 l->set_automation_state (Play);
368 Automatable::automation_snapshot (nframes_t now, bool force)
370 if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
372 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
373 boost::shared_ptr<AutomationControl> c
374 = boost::dynamic_pointer_cast<AutomationControl>(i->second);
375 if (c->automation_write()) {
376 c->list()->rt_add (now, i->second->user_float());
380 _last_automation_snapshot = now;
385 Automatable::transport_stopped (nframes_t now)
387 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
389 boost::shared_ptr<AutomationControl> c
390 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
391 boost::shared_ptr<AutomationList> l
392 = boost::dynamic_pointer_cast<AutomationList>(c->list());
394 c->list()->reposition_for_rt_add (now);
396 if (c->automation_state() != Off) {
397 c->set_value(c->list()->eval(now));
402 boost::shared_ptr<Evoral::Control>
403 Automatable::control_factory(const Evoral::Parameter& param)
405 boost::shared_ptr<AutomationList> list(new AutomationList(param));
406 Evoral::Control* control = NULL;
407 if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
408 control = new MidiTrack::MidiControl((MidiTrack*)this, param);
409 } else if (param.type() == PluginAutomation) {
410 control = new PluginInsert::PluginControl((PluginInsert*)this, param);
411 } else if (param.type() == GainAutomation) {
412 control = new IO::GainControl( X_("gaincontrol"), (IO*)this, param);
413 } else if (param.type() == PanAutomation) {
414 Panner* me = dynamic_cast<Panner*>(this);
416 control = new Panner::PanControllable(me->session(), X_("panner"), *me, param);
418 cerr << "ERROR: PanAutomation for non-Panner" << endl;
421 control = new AutomationControl(_a_session, param);
423 control->set_list(list);
424 return boost::shared_ptr<Evoral::Control>(control);
427 boost::shared_ptr<AutomationControl>
428 Automatable::automation_control (const Evoral::Parameter& id, bool create)
430 return boost::dynamic_pointer_cast<AutomationControl>(Evoral::ControlSet::control(id, create));
433 boost::shared_ptr<const AutomationControl>
434 Automatable::automation_control (const Evoral::Parameter& id) const
436 return boost::dynamic_pointer_cast<const AutomationControl>(Evoral::ControlSet::control(id));