2 Copyright (C) 2007 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "pbd/memento_command.h"
25 #include "pbd/stacktrace.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/automation_control.h"
29 #include "ardour/automation_watch.h"
30 #include "ardour/control_group.h"
31 #include "ardour/event_type_map.h"
32 #include "ardour/session.h"
38 // C99 'isfinite()' is not available in MSVC.
39 #define isfinite_local(val) (bool)_finite((double)val)
41 #define isfinite_local isfinite
45 using namespace ARDOUR;
48 AutomationControl::AutomationControl(ARDOUR::Session& session,
49 const Evoral::Parameter& parameter,
50 const ParameterDescriptor& desc,
51 boost::shared_ptr<ARDOUR::AutomationList> list,
53 : Controllable (name.empty() ? EventTypeMap::instance().to_symbol(parameter) : name)
54 , Evoral::Control(parameter, desc, list)
59 set_flags (Controllable::Toggle);
63 AutomationControl::~AutomationControl ()
65 DropReferences (); /* EMIT SIGNAL */
69 AutomationControl::writable() const
71 boost::shared_ptr<AutomationList> al = alist();
73 return al->automation_state() != Play;
78 /** Get the current effective `user' value based on automation state */
80 AutomationControl::get_value() const
82 bool from_list = _list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback();
83 return Control::get_double (from_list, _session.transport_frame());
87 AutomationControl::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
93 /* enforce strict double/boolean value mapping */
101 if (check_rt (val, gcd)) {
102 /* change has been queued to take place in an RT context */
106 if (_group && _group->use_me (gcd)) {
107 _group->set_group_value (shared_from_this(), val);
109 actually_set_value (val, gcd);
113 /** Set the value and do the right thing based on automation state
114 * (e.g. record if necessary, etc.)
115 * @param value `user' value
118 AutomationControl::actually_set_value (double value, PBD::Controllable::GroupControlDisposition gcd)
120 bool to_list = _list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_write();
121 //const double old_value = Control::user_double ();
123 Control::set_double (value, _session.transport_frame(), to_list);
125 //AutomationType at = (AutomationType) _parameter.type();
126 //std::cerr << "++++ Changed (" << enum_2_string (at) << ", " << enum_2_string (gcd) << ") = " << value
127 //<< " (was " << old_value << ") @ " << this << std::endl;
133 AutomationControl::set_list (boost::shared_ptr<Evoral::ControlList> list)
135 Control::set_list (list);
136 Changed (true, Controllable::NoGroup);
140 AutomationControl::set_automation_state (AutoState as)
142 if (flags() & NotAutomatable) {
145 if (_list && as != alist()->automation_state()) {
147 alist()->set_automation_state (as);
149 return; // No watch for boolean automation
153 AutomationWatch::instance().add_automation_watch (shared_from_this());
154 } else if (as == Touch) {
156 AutomationWatch::instance().remove_automation_watch (shared_from_this());
158 /* this seems unlikely, but the combination of
159 * a control surface and the mouse could make
160 * it possible to put the control into Touch
161 * mode *while* touching it.
163 AutomationWatch::instance().add_automation_watch (shared_from_this());
166 AutomationWatch::instance().remove_automation_watch (shared_from_this());
172 AutomationControl::set_automation_style (AutoStyle as)
175 alist()->set_automation_style (as);
179 AutomationControl::start_touch(double when)
187 if (alist()->automation_state() == Touch) {
188 /* subtle. aligns the user value with the playback */
189 set_value (get_value (), Controllable::NoGroup);
190 alist()->start_touch (when);
191 if (!_desc.toggled) {
192 AutomationWatch::instance().add_automation_watch (shared_from_this());
200 AutomationControl::stop_touch(bool mark, double when)
204 set_touching (false);
206 if (alist()->automation_state() == Touch) {
207 alist()->stop_touch (mark, when);
208 if (!_desc.toggled) {
209 AutomationWatch::instance().remove_automation_watch (shared_from_this());
217 AutomationControl::commit_transaction (bool did_write)
220 if (alist ()->before ()) {
221 _session.begin_reversible_command (string_compose (_("record %1 automation"), name ()));
222 _session.commit_reversible_command (new MementoCommand<AutomationList> (*alist ().get (), alist ()->before (), &alist ()->get_state ()));
225 alist ()->clear_history ();
230 AutomationControl::internal_to_interface (double val) const
232 if (_desc.integer_step) {
233 // both upper and lower are inclusive.
234 val = (val - lower()) / (1 + upper() - lower());
236 val = (val - lower()) / (upper() - lower());
239 if (_desc.logarithmic) {
241 val = pow (val, 1./2.0);
251 AutomationControl::interface_to_internal (double val) const
253 if (!isfinite_local (val)) {
256 if (_desc.logarithmic) {
260 val = pow (val, 2.0);
264 if (_desc.integer_step) {
265 val = lower() + val * (1 + upper() - lower());
267 val = lower() + val * (upper() - lower());
270 if (val < lower()) val = lower();
271 if (val > upper()) val = upper();
277 AutomationControl::set_group (boost::shared_ptr<ControlGroup> cg)
279 /* this method can only be called by a ControlGroup. We do not need
280 to ensure consistency by calling ControlGroup::remove_control(),
281 since we are guaranteed that the ControlGroup will take care of that
289 AutomationControl::check_rt (double val, Controllable::GroupControlDisposition gcd)
291 if (!_session.loading() && (flags() & Controllable::RealTime) && !AudioEngine::instance()->in_process_thread()) {
292 /* queue change in RT context */
293 _session.set_control (shared_from_this(), val, gcd);