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::Flag flags)
55 : Controllable (name.empty() ? EventTypeMap::instance().to_symbol(parameter) : name, flags)
56 , Evoral::Control(parameter, desc, list)
61 set_flags (Controllable::Toggle);
65 AutomationControl::~AutomationControl ()
67 DropReferences (); /* EMIT SIGNAL */
71 AutomationControl::writable() const
73 boost::shared_ptr<AutomationList> al = alist();
75 return al->automation_state() != Play;
80 /** Get the current effective `user' value based on automation state */
82 AutomationControl::get_value() const
84 bool from_list = _list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback();
85 return Control::get_double (from_list, _session.transport_frame());
89 AutomationControl::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
95 /* enforce strict double/boolean value mapping */
103 if (check_rt (val, gcd)) {
104 /* change has been queued to take place in an RT context */
108 if (_group && _group->use_me (gcd)) {
109 _group->set_group_value (shared_from_this(), val);
111 actually_set_value (val, gcd);
115 /** Set the value and do the right thing based on automation state
116 * (e.g. record if necessary, etc.)
117 * @param value `user' value
120 AutomationControl::actually_set_value (double value, PBD::Controllable::GroupControlDisposition gcd)
122 bool to_list = _list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_write();
123 //const double old_value = Control::user_double ();
125 Control::set_double (value, _session.transport_frame(), to_list);
127 //AutomationType at = (AutomationType) _parameter.type();
128 //std::cerr << "++++ Changed (" << enum_2_string (at) << ", " << enum_2_string (gcd) << ") = " << value
129 //<< " (was " << old_value << ") @ " << this << std::endl;
135 AutomationControl::set_list (boost::shared_ptr<Evoral::ControlList> list)
137 Control::set_list (list);
138 Changed (true, Controllable::NoGroup);
142 AutomationControl::set_automation_state (AutoState as)
144 if (flags() & NotAutomatable) {
147 if (_list && as != alist()->automation_state()) {
149 const double val = get_value ();
151 alist()->set_automation_state (as);
153 return; // No watch for boolean automation
157 AutomationWatch::instance().add_automation_watch (shared_from_this());
158 } else if (as == Touch) {
159 if (alist()->empty()) {
160 Control::set_double (val, _session.current_start_frame (), true);
161 Control::set_double (val, _session.current_end_frame (), true);
162 Changed (true, Controllable::NoGroup);
165 AutomationWatch::instance().remove_automation_watch (shared_from_this());
167 /* this seems unlikely, but the combination of
168 * a control surface and the mouse could make
169 * it possible to put the control into Touch
170 * mode *while* touching it.
172 AutomationWatch::instance().add_automation_watch (shared_from_this());
175 AutomationWatch::instance().remove_automation_watch (shared_from_this());
181 AutomationControl::set_automation_style (AutoStyle as)
184 alist()->set_automation_style (as);
188 AutomationControl::start_touch(double when)
196 if (alist()->automation_state() == Touch) {
197 /* subtle. aligns the user value with the playback */
198 set_value (get_value (), Controllable::NoGroup);
199 alist()->start_touch (when);
200 if (!_desc.toggled) {
201 AutomationWatch::instance().add_automation_watch (shared_from_this());
209 AutomationControl::stop_touch(bool mark, double when)
213 set_touching (false);
215 if (alist()->automation_state() == Touch) {
216 alist()->stop_touch (mark, when);
217 if (!_desc.toggled) {
218 AutomationWatch::instance().remove_automation_watch (shared_from_this());
226 AutomationControl::commit_transaction (bool did_write)
229 if (alist ()->before ()) {
230 _session.begin_reversible_command (string_compose (_("record %1 automation"), name ()));
231 _session.commit_reversible_command (new MementoCommand<AutomationList> (*alist ().get (), alist ()->before (), &alist ()->get_state ()));
234 alist ()->clear_history ();
239 AutomationControl::internal_to_interface (double val) const
241 if (_desc.integer_step) {
242 // both upper and lower are inclusive.
243 val = (val - lower()) / (1 + upper() - lower());
245 val = (val - lower()) / (upper() - lower());
248 if (_desc.logarithmic) {
250 val = pow (val, 1./2.0);
260 AutomationControl::interface_to_internal (double val) const
262 if (!isfinite_local (val)) {
265 if (_desc.logarithmic) {
269 val = pow (val, 2.0);
273 if (_desc.integer_step) {
274 val = lower() + val * (1 + upper() - lower());
276 val = lower() + val * (upper() - lower());
279 if (val < lower()) val = lower();
280 if (val > upper()) val = upper();
286 AutomationControl::set_group (boost::shared_ptr<ControlGroup> cg)
288 /* this method can only be called by a ControlGroup. We do not need
289 to ensure consistency by calling ControlGroup::remove_control(),
290 since we are guaranteed that the ControlGroup will take care of that
298 AutomationControl::check_rt (double val, Controllable::GroupControlDisposition gcd)
300 if (!_session.loading() && (flags() & Controllable::RealTime) && !AudioEngine::instance()->in_process_thread()) {
301 /* queue change in RT context */
302 _session.set_control (shared_from_this(), val, gcd);