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 <ardour/session.h>
28 #include <ardour/automatable.h>
29 #include <ardour/midi_track.h>
34 using namespace ARDOUR;
37 nframes_t Automatable::_automation_interval = 0;
39 Automatable::Automatable(Session& _session, const string& name)
40 : SessionObject(_session, name)
41 , _last_automation_snapshot(0)
45 Automatable::old_set_automation_state (const XMLNode& node)
47 const XMLProperty *prop;
49 if ((prop = node.property ("path")) != 0) {
50 load_automation (prop->value());
52 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
55 if ((prop = node.property ("visible")) != 0) {
59 _visible_controls.clear ();
61 sstr << prop->value();
67 mark_automation_visible (Parameter(PluginAutomation, what), true);
71 _last_automation_snapshot = 0;
77 Automatable::load_automation (const string& path)
81 if (path[0] == '/') { // legacy
84 fullpath = _session.automation_dir();
87 ifstream in (fullpath.c_str());
90 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
94 Glib::Mutex::Lock lm (_automation_lock);
95 set<Parameter> tosave;
98 _last_automation_snapshot = 0;
105 in >> port; if (!in) break;
106 in >> when; if (!in) goto bad;
107 in >> value; if (!in) goto bad;
109 /* FIXME: this is legacy and only used for plugin inserts? I think? */
110 boost::shared_ptr<AutomationControl> c = control (Parameter(PluginAutomation, port), true);
111 c->list()->add (when, value);
112 tosave.insert (Parameter(PluginAutomation, port));
118 error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
124 Automatable::add_control(boost::shared_ptr<AutomationControl> ac)
126 Parameter param = ac->parameter();
128 _controls[param] = ac;
130 _can_automate_list.insert(param);
132 // Sync everything (derived classes) up to initial values
133 auto_state_changed(param);
137 Automatable::what_has_automation (set<Parameter>& s) const
139 Glib::Mutex::Lock lm (_automation_lock);
140 Controls::const_iterator li;
142 // FIXME: correct semantics?
143 for (li = _controls.begin(); li != _controls.end(); ++li) {
144 s.insert ((*li).first);
149 Automatable::what_has_visible_automation (set<Parameter>& s) const
151 Glib::Mutex::Lock lm (_automation_lock);
152 set<Parameter>::const_iterator li;
154 for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
159 /** Returns NULL if we don't have an AutomationList for \a parameter.
161 boost::shared_ptr<AutomationControl>
162 Automatable::control (Parameter parameter, bool create_if_missing)
164 Controls::iterator i = _controls.find(parameter);
166 if (i != _controls.end()) {
169 } else if (create_if_missing) {
170 boost::shared_ptr<AutomationList> al (new AutomationList (
171 parameter, FLT_MIN, FLT_MAX, default_parameter_value (parameter)));
172 boost::shared_ptr<AutomationControl> ac(control_factory(al));
177 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
178 return boost::shared_ptr<AutomationControl>();
182 boost::shared_ptr<const AutomationControl>
183 Automatable::control (Parameter parameter) const
185 Controls::const_iterator i = _controls.find(parameter);
187 if (i != _controls.end()) {
190 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
191 return boost::shared_ptr<AutomationControl>();
197 Automatable::describe_parameter (Parameter param)
199 /* derived classes like PluginInsert should override this */
201 if (param == Parameter(GainAutomation)) {
203 } else if (param.type() == PanAutomation) {
204 return (string_compose(_("Pan %1"), param.id()));
205 } else if (param.type() == MidiCCAutomation) {
206 string name = get_name_for_cc_number(param.id());
207 if(name.length() != 0) {
208 return string_compose("%1 [%2]", name, int(param.channel()) + 1);
210 return string_compose("CC %1 [%2]", param.id(), int(param.channel()) + 1);
212 } else if (param.type() == MidiPgmChangeAutomation) {
213 return string_compose("Program [%1]", int(param.channel()) + 1);
214 } else if (param.type() == MidiPitchBenderAutomation) {
215 return string_compose("Bender [%1]", int(param.channel()) + 1);
216 } else if (param.type() == MidiChannelAftertouchAutomation) {
217 return string_compose("Aftertouch [%1]", int(param.channel()) + 1);
219 return param.to_string();
224 Automatable::get_name_for_cc_number (uint32_t cc_number)
238 name = "Modulation MSB";
242 name = "Breath Controller";
246 name = "Foot Controller";
250 name = "Portamento Time";
254 name = "RPN Controller";
258 name = "Main Volume";
285 name = string_compose("General Purpose %1", cc_number - 15);
289 name = "Sustain Pedal";
305 name = "Legato Footswitch";
317 name = string_compose("Sound Controller %1", cc_number - 69);
324 name = string_compose("General Purpose %1", cc_number - 75);
328 name = "Portamento Control";
336 name = string_compose("Effects %1 Depth", cc_number - 90);
340 name = "Data Increment RPN/NRPN";
344 name = "Data Decrement RPN/NRPN";
364 name = "all sounds off";
368 name = "Controller Reset";
372 name = "Local Control on/off";
376 name = "all notes off";
388 name = "mono on / poly off";
392 name = "poly on / mono off";
403 Automatable::can_automate (Parameter what)
405 _can_automate_list.insert (what);
409 Automatable::mark_automation_visible (Parameter what, bool yn)
412 _visible_controls.insert (what);
414 set<Parameter>::iterator i;
416 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
417 _visible_controls.erase (i);
423 Automatable::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
425 Controls::const_iterator li;
427 next_event.when = max_frames;
429 for (li = _controls.begin(); li != _controls.end(); ++li) {
431 AutomationList::const_iterator i;
432 boost::shared_ptr<const AutomationList> alist (li->second->list());
433 ControlEvent cp (now, 0.0f);
435 for (i = lower_bound (alist->const_begin(), alist->const_end(), &cp, AutomationList::time_comparator);
436 i != alist->const_end() && (*i)->when < end; ++i) {
437 if ((*i)->when > now) {
442 if (i != alist->const_end() && (*i)->when < end) {
444 if ((*i)->when < next_event.when) {
445 next_event.when = (*i)->when;
450 return next_event.when != max_frames;
453 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
454 * had a single automation parameter, with it's type implicit. Derived objects should
455 * pass that type and it will be used for the untyped AutomationList found.
458 Automatable::set_automation_state (const XMLNode& node, Parameter legacy_param)
460 Glib::Mutex::Lock lm (_automation_lock);
462 /* Don't clear controls, since some may be special derived Controllable classes */
464 _visible_controls.clear ();
466 XMLNodeList nlist = node.children();
467 XMLNodeIterator niter;
469 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
471 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, ¶m) != 1) {
472 error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
476 if ((*niter)->name() == "AutomationList") {
478 const XMLProperty* id_prop = (*niter)->property("automation-id");
480 Parameter param = (id_prop ? Parameter(id_prop->value()) : legacy_param);
482 boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
485 warning << "AutomationList node without automation-id property, "
486 << "using default: " << legacy_param.to_string() << endmsg;
487 al->set_parameter(legacy_param);
490 boost::shared_ptr<AutomationControl> existing = control(param);
492 existing->set_list(al);
494 add_control(control_factory(al));
497 error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
501 _last_automation_snapshot = 0;
507 Automatable::get_automation_state ()
509 Glib::Mutex::Lock lm (_automation_lock);
510 XMLNode* node = new XMLNode (X_("Automation"));
512 if (_controls.empty()) {
516 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li) {
517 node->add_child_nocopy (li->second->list()->get_state ());
524 Automatable::clear_automation ()
526 Glib::Mutex::Lock lm (_automation_lock);
528 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
529 li->second->list()->clear();
533 Automatable::set_parameter_automation_state (Parameter param, AutoState s)
535 Glib::Mutex::Lock lm (_automation_lock);
537 boost::shared_ptr<AutomationControl> c = control (param, true);
539 if (s != c->list()->automation_state()) {
540 c->list()->set_automation_state (s);
541 _session.set_dirty ();
546 Automatable::get_parameter_automation_state (Parameter param, bool lock)
548 AutoState result = Off;
551 _automation_lock.lock();
553 boost::shared_ptr<AutomationControl> c = control(param);
556 result = c->list()->automation_state();
559 _automation_lock.unlock();
565 Automatable::set_parameter_automation_style (Parameter param, AutoStyle s)
567 Glib::Mutex::Lock lm (_automation_lock);
569 boost::shared_ptr<AutomationControl> c = control(param, true);
571 if (s != c->list()->automation_style()) {
572 c->list()->set_automation_style (s);
573 _session.set_dirty ();
578 Automatable::get_parameter_automation_style (Parameter param)
580 Glib::Mutex::Lock lm (_automation_lock);
582 boost::shared_ptr<AutomationControl> c = control(param);
585 return c->list()->automation_style();
587 return Absolute; // whatever
592 Automatable::protect_automation ()
594 set<Parameter> automated_params;
596 what_has_automation (automated_params);
598 for (set<Parameter>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
600 boost::shared_ptr<AutomationControl> c = control(*i);
602 switch (c->list()->automation_state()) {
604 c->list()->set_automation_state (Off);
607 c->list()->set_automation_state (Play);
616 Automatable::automation_snapshot (nframes_t now, bool force)
618 if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
620 for (Controls::iterator i = _controls.begin(); i != _controls.end(); ++i) {
621 if (i->second->list()->automation_write()) {
622 i->second->list()->rt_add (now, i->second->user_value());
626 _last_automation_snapshot = now;
631 Automatable::transport_stopped (nframes_t now)
633 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li) {
635 boost::shared_ptr<AutomationControl> c = li->second;
637 c->list()->reposition_for_rt_add (now);
639 if (c->list()->automation_state() != Off) {
640 c->set_value(c->list()->eval(now));
645 /* FIXME: this probably doesn't belong here */
646 boost::shared_ptr<AutomationControl>
647 Automatable::control_factory(boost::shared_ptr<AutomationList> list)
650 list->parameter().type() == MidiCCAutomation ||
651 list->parameter().type() == MidiPgmChangeAutomation ||
652 list->parameter().type() == MidiChannelAftertouchAutomation
654 // FIXME: this will die horribly if this is not a MidiTrack
655 return boost::shared_ptr<AutomationControl>(new MidiTrack::MidiControl((MidiTrack*)this, list));
657 return boost::shared_ptr<AutomationControl>(new AutomationControl(_session, list));