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.
23 #include "ardour/automation_control.h"
24 #include "ardour/automation_watch.h"
25 #include "ardour/event_type_map.h"
26 #include "ardour/session.h"
28 #include "pbd/memento_command.h"
29 #include "pbd/stacktrace.h"
35 // C99 'isfinite()' is not available in MSVC.
36 #define isfinite_local(val) (bool)_finite((double)val)
38 #define isfinite_local isfinite
42 using namespace ARDOUR;
45 AutomationControl::AutomationControl(ARDOUR::Session& session,
46 const Evoral::Parameter& parameter,
47 const ParameterDescriptor& desc,
48 boost::shared_ptr<ARDOUR::AutomationList> list,
50 : Controllable (name.empty() ? EventTypeMap::instance().to_symbol(parameter) : name)
51 , Evoral::Control(parameter, desc, list)
57 AutomationControl::~AutomationControl ()
62 AutomationControl::writable() const
64 boost::shared_ptr<AutomationList> al = alist();
66 return al->automation_state() != Play;
71 /** Get the current effective `user' value based on automation state */
73 AutomationControl::get_value() const
75 bool from_list = _list && ((AutomationList*)_list.get())->automation_playback();
76 return Control::get_double (from_list, _session.transport_frame());
79 /** Set the value and do the right thing based on automation state
80 * (e.g. record if necessary, etc.)
81 * @param value `user' value
84 AutomationControl::set_value (double value, PBD::Controllable::GroupControlDisposition /* group_override */)
86 bool to_list = _list && ((AutomationList*)_list.get())->automation_write();
88 Control::set_double (value, _session.transport_frame(), to_list);
90 Changed(); /* EMIT SIGNAL */
94 AutomationControl::set_list (boost::shared_ptr<Evoral::ControlList> list)
96 Control::set_list (list);
97 Changed(); /* EMIT SIGNAL */
101 AutomationControl::set_automation_state (AutoState as)
103 if (_list && as != alist()->automation_state()) {
105 alist()->set_automation_state (as);
107 return; // No watch for boolean automation
111 AutomationWatch::instance().add_automation_watch (shared_from_this());
112 } else if (as == Touch) {
114 AutomationWatch::instance().remove_automation_watch (shared_from_this());
116 /* this seems unlikely, but the combination of
117 * a control surface and the mouse could make
118 * it possible to put the control into Touch
119 * mode *while* touching it.
121 AutomationWatch::instance().add_automation_watch (shared_from_this());
124 AutomationWatch::instance().remove_automation_watch (shared_from_this());
130 AutomationControl::set_automation_style (AutoStyle as)
133 alist()->set_automation_style (as);
137 AutomationControl::start_touch(double when)
145 if (alist()->automation_state() == Touch) {
146 /* subtle. aligns the user value with the playback */
147 set_value (get_value (), Controllable::NoGroup);
148 alist()->start_touch (when);
149 if (!_desc.toggled) {
150 AutomationWatch::instance().add_automation_watch (shared_from_this());
158 AutomationControl::stop_touch(bool mark, double when)
162 set_touching (false);
164 if (alist()->automation_state() == Touch) {
165 alist()->stop_touch (mark, when);
166 if (!_desc.toggled) {
167 AutomationWatch::instance().remove_automation_watch (shared_from_this());
175 AutomationControl::commit_transaction (bool did_write)
178 if (alist ()->before ()) {
179 _session.begin_reversible_command (string_compose (_("record %1 automation"), name ()));
180 _session.commit_reversible_command (new MementoCommand<AutomationList> (*alist ().get (), alist ()->before (), &alist ()->get_state ()));
183 alist ()->clear_history ();
188 AutomationControl::internal_to_interface (double val) const
190 if (_desc.integer_step) {
191 // both upper and lower are inclusive.
192 val = (val - lower()) / (1 + upper() - lower());
194 val = (val - lower()) / (upper() - lower());
197 if (_desc.logarithmic) {
199 val = pow (val, 1./2.0);
209 AutomationControl::interface_to_internal (double val) const
211 if (!isfinite_local (val)) {
214 if (_desc.logarithmic) {
218 val = pow (val, 2.0);
222 if (_desc.integer_step) {
223 val = lower() + val * (1 + upper() - lower());
225 val = lower() + val * (upper() - lower());
228 if (val < lower()) val = lower();
229 if (val > upper()) val = upper();