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/session.h>
29 #include <ardour/automatable.h>
30 #include <ardour/midi_track.h>
31 #include <ardour/plugin_insert.h>
32 #include <ardour/panner.h>
37 using namespace ARDOUR;
40 nframes_t Automatable::_automation_interval = 0;
42 Automatable::Automatable(Session& session)
44 , _last_automation_snapshot(0)
49 Automatable::old_set_automation_state (const XMLNode& node)
51 const XMLProperty *prop;
53 if ((prop = node.property ("path")) != 0) {
54 load_automation (prop->value());
56 warning << _("Automation node has no path property") << endmsg;
59 if ((prop = node.property ("visible")) != 0) {
63 _visible_controls.clear ();
65 sstr << prop->value();
71 mark_automation_visible (Evoral::Parameter(PluginAutomation, 0, what), true);
75 _last_automation_snapshot = 0;
81 Automatable::load_automation (const string& path)
85 if (path[0] == '/') { // legacy
88 fullpath = _a_session.automation_dir();
91 ifstream in (fullpath.c_str());
94 warning << string_compose(_("cannot open %2 to load automation data (%3)")
95 , fullpath, strerror (errno)) << endmsg;
99 Glib::Mutex::Lock lm (control_lock());
100 set<Evoral::Parameter> tosave;
103 _last_automation_snapshot = 0;
110 in >> port; if (!in) break;
111 in >> when; if (!in) goto bad;
112 in >> value; if (!in) goto bad;
114 Evoral::Parameter param(PluginAutomation, 0, port);
115 /* FIXME: this is legacy and only used for plugin inserts? I think? */
116 boost::shared_ptr<Evoral::Control> c = control (param, true);
117 c->list()->add (when, value);
118 tosave.insert (param);
124 error << string_compose(_("cannot load automation data from %2"), fullpath) << endmsg;
130 Automatable::add_control(boost::shared_ptr<Evoral::Control> ac)
132 Evoral::Parameter param = ac->parameter();
134 ControlSet::add_control(ac);
135 _can_automate_list.insert(param);
136 auto_state_changed(param); // sync everything up
140 Automatable::what_has_visible_data(set<Evoral::Parameter>& s) const
142 Glib::Mutex::Lock lm (control_lock());
143 set<Evoral::Parameter>::const_iterator li;
145 for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
151 Automatable::describe_parameter (Evoral::Parameter param)
153 /* derived classes like PluginInsert should override this */
155 if (param == Evoral::Parameter(GainAutomation)) {
157 } else if (param.type() == PanAutomation) {
158 /* ID's are zero-based, present them as 1-based */
159 return (string_compose(_("Pan %1"), param.id() + 1));
160 } else if (param.type() == MidiCCAutomation) {
161 return string_compose("CC %1 (%2) [%3]",
162 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);
163 } else if (param.type() == MidiPgmChangeAutomation) {
164 return string_compose("Program [%1]", int(param.channel()) + 1);
165 } else if (param.type() == MidiPitchBenderAutomation) {
166 return string_compose("Bender [%1]", int(param.channel()) + 1);
167 } else if (param.type() == MidiChannelPressureAutomation) {
168 return string_compose("Pressure [%1]", int(param.channel()) + 1);
170 return EventTypeMap::instance().to_symbol(param);
175 Automatable::can_automate (Evoral::Parameter what)
177 _can_automate_list.insert (what);
181 Automatable::mark_automation_visible (Evoral::Parameter what, bool yn)
184 _visible_controls.insert (what);
186 set<Evoral::Parameter>::iterator i;
188 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
189 _visible_controls.erase (i);
194 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
195 * had a single automation parameter, with it's type implicit. Derived objects should
196 * pass that type and it will be used for the untyped AutomationList found.
199 Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy_param)
201 Glib::Mutex::Lock lm (control_lock());
203 /* Don't clear controls, since some may be special derived Controllable classes */
205 _visible_controls.clear ();
207 XMLNodeList nlist = node.children();
208 XMLNodeIterator niter;
210 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
212 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
213 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
217 if ((*niter)->name() == "AutomationList") {
219 const XMLProperty* id_prop = (*niter)->property("automation-id");
221 Evoral::Parameter param = (id_prop
222 ? EventTypeMap::instance().new_parameter(id_prop->value())
225 if (param.type() == NullAutomation) {
226 warning << "Automation has null type" << endl;
230 boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
233 warning << "AutomationList node without automation-id property, "
234 << "using default: " << EventTypeMap::instance().to_symbol(legacy_param) << endmsg;
237 boost::shared_ptr<Evoral::Control> existing = control(param);
239 existing->set_list(al);
241 boost::shared_ptr<Evoral::Control> newcontrol = control_factory(param);
242 add_control(newcontrol);
243 newcontrol->set_list(al);
244 warning << "Control did not exist";
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());
270 node->add_child_nocopy (l->get_state ());
277 Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState s)
279 Glib::Mutex::Lock lm (control_lock());
281 boost::shared_ptr<Evoral::Control> c = control (param, true);
282 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
284 if (s != l->automation_state()) {
285 l->set_automation_state (s);
286 _a_session.set_dirty ();
291 Automatable::get_parameter_automation_state (Evoral::Parameter param, bool lock)
293 AutoState result = Off;
296 control_lock().lock();
298 boost::shared_ptr<Evoral::Control> c = control(param);
299 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
302 result = l->automation_state();
305 control_lock().unlock();
311 Automatable::set_parameter_automation_style (Evoral::Parameter param, AutoStyle s)
313 Glib::Mutex::Lock lm (control_lock());
315 boost::shared_ptr<Evoral::Control> c = control(param, true);
316 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
318 if (s != l->automation_style()) {
319 l->set_automation_style (s);
320 _a_session.set_dirty ();
325 Automatable::get_parameter_automation_style (Evoral::Parameter param)
327 Glib::Mutex::Lock lm (control_lock());
329 boost::shared_ptr<Evoral::Control> c = control(param);
330 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
333 return l->automation_style();
335 return Absolute; // whatever
340 Automatable::protect_automation ()
342 typedef set<Evoral::Parameter> ParameterSet;
343 ParameterSet automated_params;
345 what_has_data(automated_params);
347 for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
349 boost::shared_ptr<Evoral::Control> c = control(*i);
350 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
352 switch (l->automation_state()) {
354 l->set_automation_state (Off);
357 l->set_automation_state (Play);
366 Automatable::automation_snapshot (nframes_t now, bool force)
368 if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
370 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
371 boost::shared_ptr<AutomationControl> c
372 = boost::dynamic_pointer_cast<AutomationControl>(i->second);
373 if (c->automation_write()) {
374 c->list()->rt_add (now, i->second->user_float());
378 _last_automation_snapshot = now;
383 Automatable::transport_stopped (nframes_t now)
385 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
387 boost::shared_ptr<AutomationControl> c
388 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
389 boost::shared_ptr<AutomationList> l
390 = boost::dynamic_pointer_cast<AutomationList>(c->list());
392 c->list()->reposition_for_rt_add (now);
394 if (c->automation_state() != Off) {
395 c->set_value(c->list()->eval(now));
400 boost::shared_ptr<Evoral::Control>
401 Automatable::control_factory(const Evoral::Parameter& param)
403 boost::shared_ptr<AutomationList> list(new AutomationList(param));
404 Evoral::Control* control = NULL;
405 if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
406 control = new MidiTrack::MidiControl((MidiTrack*)this, param);
407 } else if (param.type() == PluginAutomation) {
408 control = new PluginInsert::PluginControl((PluginInsert*)this, param);
409 } else if (param.type() == GainAutomation) {
410 control = new IO::GainControl( X_("gaincontrol"), (IO*)this, param);
411 } else if (param.type() == PanAutomation) {
412 control = new Panner::PanControllable( ((Panner *)this)->session(), X_("panner"), *(Panner *)this, param);
414 control = new AutomationControl(_a_session, param);
416 control->set_list(list);
417 return boost::shared_ptr<Evoral::Control>(control);