Merge branch 'cairocanvas'
[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(
33                 ARDOUR::Session& session,
34                 const Evoral::Parameter& parameter,
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, list)
39         , _session(session)
40 {
41 }
42
43 AutomationControl::~AutomationControl ()
44 {
45 }
46
47 /** Get the current effective `user' value based on automation state */
48 double
49 AutomationControl::get_value() const
50 {
51         bool from_list = _list && ((AutomationList*)_list.get())->automation_playback();
52         return Control::get_double (from_list, _session.transport_frame());
53 }
54
55 /** Set the value and do the right thing based on automation state
56  *  (e.g. record if necessary, etc.)
57  *  @param value `user' value
58  */
59 void
60 AutomationControl::set_value (double value)
61 {
62         bool to_list = _list && ((AutomationList*)_list.get())->automation_write();
63
64         if (to_list && parameter().toggled()) {
65
66                 // store the previous value just before this so any
67                 // interpolation works right
68
69
70                 _list->add (get_double(), _session.transport_frame()-1);
71         }
72
73         Control::set_double (value, _session.transport_frame(), to_list);
74
75         Changed(); /* EMIT SIGNAL */
76 }
77
78 void
79 AutomationControl::set_list (boost::shared_ptr<Evoral::ControlList> list)
80 {
81         Control::set_list (list);
82         Changed();  /* EMIT SIGNAL */
83 }
84
85 void
86 AutomationControl::set_automation_state (AutoState as)
87 {
88         if (_list && as != alist()->automation_state()) {
89
90                 alist()->set_automation_state (as);
91
92                 if (as == Write) {
93                         AutomationWatch::instance().add_automation_watch (shared_from_this());
94                 } else if (as == Touch) {
95                         if (!touching()) {
96                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
97                         } else {
98                                 /* this seems unlikely, but the combination of
99                                  * a control surface and the mouse could make
100                                  * it possible to put the control into Touch
101                                  * mode *while* touching it.
102                                  */
103                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
104                         }
105                 } else {
106                         AutomationWatch::instance().remove_automation_watch (shared_from_this());
107                 }
108         }
109 }
110
111 void
112 AutomationControl::set_automation_style (AutoStyle as)
113 {
114         alist()->set_automation_style (as);
115 }
116
117 void
118 AutomationControl::start_touch(double when)
119 {
120         if (!touching()) {
121                 if (alist()->automation_state() == Touch) {
122                         alist()->start_touch (when);
123                         AutomationWatch::instance().add_automation_watch (shared_from_this());
124                 }
125                 set_touching (true);
126         }
127 }
128
129 void
130 AutomationControl::stop_touch(bool mark, double when)
131 {
132         if (touching()) {
133                 set_touching (false);
134                 if (alist()->automation_state() == Touch) {
135                         alist()->stop_touch (mark, when);
136                         AutomationWatch::instance().remove_automation_watch (shared_from_this());
137                 }
138         }
139 }