Fix some workflow problems wrt automation.
[ardour.git] / libs / ardour / automation_control.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David Robillard
4
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.
9
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.
14
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.
18
19 */
20
21 #include <iostream>
22
23 #include "ardour/automation_control.h"
24 #include "ardour/automation_watch.h"
25 #include "ardour/event_type_map.h"
26 #include "ardour/session.h"
27
28 using namespace std;
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 AutomationControl::AutomationControl(ARDOUR::Session&                          session,
33                                      const Evoral::Parameter&                  parameter,
34                                      const ParameterDescriptor&                desc,
35                                      boost::shared_ptr<ARDOUR::AutomationList> list,
36                                      const string&                             name)
37         : Controllable (name.empty() ? EventTypeMap::instance().to_symbol(parameter) : name)
38         , Evoral::Control(parameter, desc, list)
39         , _session(session)
40         , _desc(desc)
41 {
42 }
43
44 AutomationControl::~AutomationControl ()
45 {
46 }
47
48 /** Get the current effective `user' value based on automation state */
49 double
50 AutomationControl::get_value() const
51 {
52         bool from_list = _list && ((AutomationList*)_list.get())->automation_playback();
53         return Control::get_double (from_list, _session.transport_frame());
54 }
55
56 /** Set the value and do the right thing based on automation state
57  *  (e.g. record if necessary, etc.)
58  *  @param value `user' value
59  */
60 void
61 AutomationControl::set_value (double value)
62 {
63         bool to_list = _list && ((AutomationList*)_list.get())->automation_write();
64
65         Control::set_double (value, _session.transport_frame(), to_list);
66
67         Changed(); /* EMIT SIGNAL */
68 }
69
70 void
71 AutomationControl::set_list (boost::shared_ptr<Evoral::ControlList> list)
72 {
73         Control::set_list (list);
74         Changed();  /* EMIT SIGNAL */
75 }
76
77 void
78 AutomationControl::set_automation_state (AutoState as)
79 {
80         if (_list && as != alist()->automation_state()) {
81
82                 alist()->set_automation_state (as);
83                 if (_desc.toggled) {
84                         return;  // No watch for boolean automation
85                 }
86
87                 if (as == Write) {
88                         AutomationWatch::instance().add_automation_watch (shared_from_this());
89                 } else if (as == Touch) {
90                         if (!touching()) {
91                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
92                         } else {
93                                 /* this seems unlikely, but the combination of
94                                  * a control surface and the mouse could make
95                                  * it possible to put the control into Touch
96                                  * mode *while* touching it.
97                                  */
98                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
99                         }
100                 } else {
101                         AutomationWatch::instance().remove_automation_watch (shared_from_this());
102                 }
103         }
104 }
105
106 void
107 AutomationControl::set_automation_style (AutoStyle as)
108 {
109         if (!_list) return;
110         alist()->set_automation_style (as);
111 }
112
113 void
114 AutomationControl::start_touch(double when)
115 {
116         if (!_list) return;
117         if (!touching()) {
118                 if (alist()->automation_state() == Touch) {
119                         /* subtle. aligns the user value with the playback */
120                         set_value (get_value ());
121                         alist()->start_touch (when);
122                         if (!_desc.toggled) {
123                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
124                         }
125                 }
126                 set_touching (true);
127         }
128 }
129
130 void
131 AutomationControl::stop_touch(bool mark, double when)
132 {
133         if (!_list) return;
134         if (touching()) {
135                 set_touching (false);
136                 if (alist()->automation_state() == Touch) {
137                         alist()->stop_touch (mark, when);
138                         if (!_desc.toggled) {
139                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
140                         }
141                 }
142         }
143 }
144
145 double
146 AutomationControl::internal_to_interface (double val) const
147 {
148         if (_desc.integer_step) {
149                 // both upper and lower are inclusive.
150                 val =  (val - lower()) / (1 + upper() - lower());
151         } else {
152                 val =  (val - lower()) / (upper() - lower());
153         }
154
155         if (_desc.logarithmic) {
156                 if (val > 0) {
157                         val = pow (val, 1/1.5);
158                 } else {
159                         val = 0;
160                 }
161         }
162
163         return val;
164 }
165
166 double
167 AutomationControl::interface_to_internal (double val) const
168 {
169         if (_desc.logarithmic) {
170                 if (val <= 0) {
171                         val = 0;
172                 } else {
173                         val = pow (val, 1.5);
174                 }
175         }
176
177         if (_desc.integer_step) {
178                 val =  lower() + val * (1 + upper() - lower());
179         } else {
180                 val =  lower() + val * (upper() - lower());
181         }
182
183         if (val < lower()) val = lower();
184         if (val > upper()) val = upper();
185
186         return val;
187 }
188
189