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);
245 warning << "Control did not exist";
249 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
253 _last_automation_snapshot = 0;
259 Automatable::get_automation_state ()
261 Glib::Mutex::Lock lm (control_lock());
262 XMLNode* node = new XMLNode (X_("Automation"));
264 if (controls().empty()) {
268 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
269 boost::shared_ptr<AutomationList> l
270 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
271 node->add_child_nocopy (l->get_state ());
278 Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState s)
280 Glib::Mutex::Lock lm (control_lock());
282 boost::shared_ptr<Evoral::Control> c = control (param, true);
283 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
285 if (s != l->automation_state()) {
286 l->set_automation_state (s);
287 _a_session.set_dirty ();
292 Automatable::get_parameter_automation_state (Evoral::Parameter param, bool lock)
294 AutoState result = Off;
297 control_lock().lock();
299 boost::shared_ptr<Evoral::Control> c = control(param);
300 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
303 result = l->automation_state();
306 control_lock().unlock();
312 Automatable::set_parameter_automation_style (Evoral::Parameter param, AutoStyle s)
314 Glib::Mutex::Lock lm (control_lock());
316 boost::shared_ptr<Evoral::Control> c = control(param, true);
317 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
319 if (s != l->automation_style()) {
320 l->set_automation_style (s);
321 _a_session.set_dirty ();
326 Automatable::get_parameter_automation_style (Evoral::Parameter param)
328 Glib::Mutex::Lock lm (control_lock());
330 boost::shared_ptr<Evoral::Control> c = control(param);
331 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
334 return l->automation_style();
336 return Absolute; // whatever
341 Automatable::protect_automation ()
343 typedef set<Evoral::Parameter> ParameterSet;
344 ParameterSet automated_params;
346 what_has_data(automated_params);
348 for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
350 boost::shared_ptr<Evoral::Control> c = control(*i);
351 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
353 switch (l->automation_state()) {
355 l->set_automation_state (Off);
358 l->set_automation_state (Play);
367 Automatable::automation_snapshot (nframes_t now, bool force)
369 if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
371 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
372 boost::shared_ptr<AutomationControl> c
373 = boost::dynamic_pointer_cast<AutomationControl>(i->second);
374 if (c->automation_write()) {
375 c->list()->rt_add (now, i->second->user_float());
379 _last_automation_snapshot = now;
384 Automatable::transport_stopped (nframes_t now)
386 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
388 boost::shared_ptr<AutomationControl> c
389 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
390 boost::shared_ptr<AutomationList> l
391 = boost::dynamic_pointer_cast<AutomationList>(c->list());
393 c->list()->reposition_for_rt_add (now);
395 if (c->automation_state() != Off) {
396 c->set_value(c->list()->eval(now));
401 boost::shared_ptr<Evoral::Control>
402 Automatable::control_factory(const Evoral::Parameter& param)
404 boost::shared_ptr<AutomationList> list(new AutomationList(param));
405 Evoral::Control* control = NULL;
406 if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
407 control = new MidiTrack::MidiControl((MidiTrack*)this, param);
408 } else if (param.type() == PluginAutomation) {
409 control = new PluginInsert::PluginControl((PluginInsert*)this, param);
410 } else if (param.type() == GainAutomation) {
411 control = new IO::GainControl( X_("gaincontrol"), (IO*)this, param);
412 } else if (param.type() == PanAutomation) {
413 control = new Panner::PanControllable( ((Panner *)this)->session(), X_("panner"), *(Panner *)this, param);
415 control = new AutomationControl(_a_session, param);
417 control->set_list(list);
418 return boost::shared_ptr<Evoral::Control>(control);
421 boost::shared_ptr<AutomationControl>
422 Automatable::automation_control (const Evoral::Parameter& id, bool create)
424 return boost::dynamic_pointer_cast<AutomationControl>(Evoral::ControlSet::control(id, create));
427 boost::shared_ptr<const AutomationControl>
428 Automatable::automation_control (const Evoral::Parameter& id) const
430 return boost::dynamic_pointer_cast<const AutomationControl>(Evoral::ControlSet::control(id));