f1b2f9f112b97f9cf3a434df2e5d520066a3cf6c
[ardour.git] / libs / surfaces / control_protocol / control_protocol.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it
5     and/or modify it under the terms of the GNU Lesser
6     General Public License as published by the Free Software
7     Foundation; either version 2 of the License, or (at your
8     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 "pbd/error.h"
22
23 #include "ardour/gain_control.h"
24 #include "ardour/session.h"
25 #include "ardour/record_enable_control.h"
26 #include "ardour/route.h"
27 #include "ardour/audio_track.h"
28 #include "ardour/meter.h"
29 #include "ardour/amp.h"
30 #include "control_protocol/control_protocol.h"
31
32 using namespace ARDOUR;
33 using namespace std;
34 using namespace PBD;
35
36 Signal0<void>       ControlProtocol::ZoomToSession;
37 Signal0<void>       ControlProtocol::ZoomOut;
38 Signal0<void>       ControlProtocol::ZoomIn;
39 Signal0<void>       ControlProtocol::Enter;
40 Signal0<void>       ControlProtocol::Undo;
41 Signal0<void>       ControlProtocol::Redo;
42 Signal1<void,float> ControlProtocol::ScrollTimeline;
43 Signal1<void,uint32_t> ControlProtocol::GotoView;
44 Signal0<void> ControlProtocol::CloseDialog;
45 PBD::Signal0<void> ControlProtocol::VerticalZoomInAll;
46 PBD::Signal0<void> ControlProtocol::VerticalZoomOutAll;
47 PBD::Signal0<void> ControlProtocol::VerticalZoomInSelected;
48 PBD::Signal0<void> ControlProtocol::VerticalZoomOutSelected;
49 PBD::Signal0<void>          ControlProtocol::StepTracksDown;
50 PBD::Signal0<void>          ControlProtocol::StepTracksUp;
51
52 PBD::Signal1<void,StripableNotificationListPtr> ControlProtocol::StripableSelectionChanged;
53
54 const std::string ControlProtocol::state_node_name ("Protocol");
55
56 ControlProtocol::ControlProtocol (Session& s, string str)
57         : BasicUI (s)
58         , _name (str)
59         , _active (false)
60 {
61 }
62
63 ControlProtocol::~ControlProtocol ()
64 {
65 }
66
67 int
68 ControlProtocol::set_active (bool yn)
69 {
70         _active = yn;
71         return 0;
72 }
73
74 void
75 ControlProtocol::next_track (uint32_t initial_id)
76 {
77         // STRIPABLE route_table[0] = _session->get_nth_stripable (++initial_id, RemoteControlID::Route);
78 }
79
80 void
81 ControlProtocol::prev_track (uint32_t initial_id)
82 {
83         if (!initial_id) {
84                 return;
85         }
86         // STRIPABLE route_table[0] = _session->get_nth_stripable (--initial_id, RemoteControlID::Route);
87 }
88
89 void
90 ControlProtocol::set_route_table_size (uint32_t size)
91 {
92         while (route_table.size() < size) {
93                 route_table.push_back (boost::shared_ptr<Route> ((Route*) 0));
94         }
95 }
96
97 void
98 ControlProtocol::set_route_table (uint32_t table_index, boost::shared_ptr<ARDOUR::Route> r)
99 {
100         if (table_index >= route_table.size()) {
101                 return;
102         }
103
104         route_table[table_index] = r;
105
106         // XXX SHAREDPTR need to handle r->GoingAway
107 }
108
109 bool
110 ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_id)
111 {
112 #if 0 // STRIPABLE
113         boost::shared_ptr<Route> r = session->route_by_remote_id (remote_control_id);
114
115         if (!r) {
116                 return false;
117         }
118
119         set_route_table (table_index, r);
120 #endif
121         return true;
122 }
123
124 void
125 ControlProtocol::route_set_rec_enable (uint32_t table_index, bool yn)
126 {
127         if (table_index > route_table.size()) {
128                 return;
129         }
130
131         boost::shared_ptr<Route> r = route_table[table_index];
132
133         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
134
135         if (at) {
136                 at->rec_enable_control()->set_value (1.0, Controllable::UseGroup);
137         }
138 }
139
140 bool
141 ControlProtocol::route_get_rec_enable (uint32_t table_index)
142 {
143         if (table_index > route_table.size()) {
144                 return false;
145         }
146
147         boost::shared_ptr<Route> r = route_table[table_index];
148
149         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
150
151         if (at) {
152                 return at->rec_enable_control()->get_value();
153         }
154
155         return false;
156 }
157
158
159 float
160 ControlProtocol::route_get_gain (uint32_t table_index)
161 {
162         if (table_index > route_table.size()) {
163                 return 0.0f;
164         }
165
166         boost::shared_ptr<Route> r = route_table[table_index];
167
168         if (r == 0) {
169                 return 0.0f;
170         }
171
172         return r->gain_control()->get_value();
173 }
174
175 void
176 ControlProtocol::route_set_gain (uint32_t table_index, float gain)
177 {
178         if (table_index > route_table.size()) {
179                 return;
180         }
181
182         boost::shared_ptr<Route> r = route_table[table_index];
183
184         if (r != 0) {
185                 r->gain_control()->set_value (gain, Controllable::UseGroup);
186         }
187 }
188
189 float
190 ControlProtocol::route_get_effective_gain (uint32_t table_index)
191 {
192         if (table_index > route_table.size()) {
193                 return 0.0f;
194         }
195
196         boost::shared_ptr<Route> r = route_table[table_index];
197
198         if (r == 0) {
199                 return 0.0f;
200         }
201
202         return r->amp()->gain_control()->get_value();
203 }
204
205
206 float
207 ControlProtocol::route_get_peak_input_power (uint32_t table_index, uint32_t which_input)
208 {
209         if (table_index > route_table.size()) {
210                 return 0.0f;
211         }
212
213         boost::shared_ptr<Route> r = route_table[table_index];
214
215         if (r == 0) {
216                 return 0.0f;
217         }
218
219         return r->peak_meter()->meter_level (which_input, MeterPeak);
220 }
221
222 bool
223 ControlProtocol::route_get_muted (uint32_t table_index)
224 {
225         if (table_index > route_table.size()) {
226                 return false;
227         }
228
229         boost::shared_ptr<Route> r = route_table[table_index];
230
231         if (r == 0) {
232                 return false;
233         }
234
235         return r->mute_control()->muted ();
236 }
237
238 void
239 ControlProtocol::route_set_muted (uint32_t table_index, bool yn)
240 {
241         if (table_index > route_table.size()) {
242                 return;
243         }
244
245         boost::shared_ptr<Route> r = route_table[table_index];
246
247         if (r != 0) {
248                 r->mute_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup);
249         }
250 }
251
252
253 bool
254 ControlProtocol::route_get_soloed (uint32_t table_index)
255 {
256         if (table_index > route_table.size()) {
257                 return false;
258         }
259
260         boost::shared_ptr<Route> r = route_table[table_index];
261
262         if (r == 0) {
263                 return false;
264         }
265
266         return r->soloed ();
267 }
268
269 void
270 ControlProtocol::route_set_soloed (uint32_t table_index, bool yn)
271 {
272         if (table_index > route_table.size()) {
273                 return;
274         }
275
276         boost::shared_ptr<Route> r = route_table[table_index];
277
278         if (r != 0) {
279                 r->solo_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup);
280         }
281 }
282
283 string
284 ControlProtocol:: route_get_name (uint32_t table_index)
285 {
286         if (table_index > route_table.size()) {
287                 return "";
288         }
289
290         boost::shared_ptr<Route> r = route_table[table_index];
291
292         if (r == 0) {
293                 return "";
294         }
295
296         return r->name();
297 }
298
299 list<boost::shared_ptr<Bundle> >
300 ControlProtocol::bundles ()
301 {
302         return list<boost::shared_ptr<Bundle> > ();
303 }
304
305 XMLNode&
306 ControlProtocol::get_state ()
307 {
308         XMLNode* node = new XMLNode (state_node_name);
309
310         node->add_property ("name", _name);
311         node->add_property ("feedback", get_feedback() ? "yes" : "no");
312
313         return *node;
314 }
315
316 int
317 ControlProtocol::set_state (XMLNode const & node, int /* version */)
318 {
319         const XMLProperty* prop;
320
321         if ((prop = node.property ("feedback")) != 0) {
322                 set_feedback (string_is_affirmative (prop->value()));
323         }
324
325         return 0;
326 }