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