f413ad82d60ab2068e00d617391121ba3589f06b
[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/route.h"
26 #include "ardour/audio_track.h"
27 #include "ardour/meter.h"
28 #include "ardour/amp.h"
29 #include "control_protocol/control_protocol.h"
30
31 using namespace ARDOUR;
32 using namespace std;
33 using namespace PBD;
34
35 Signal0<void>       ControlProtocol::ZoomToSession;
36 Signal0<void>       ControlProtocol::ZoomOut;
37 Signal0<void>       ControlProtocol::ZoomIn;
38 Signal0<void>       ControlProtocol::Enter;
39 Signal0<void>       ControlProtocol::Undo;
40 Signal0<void>       ControlProtocol::Redo;
41 Signal1<void,float> ControlProtocol::ScrollTimeline;
42 Signal1<void,uint32_t> ControlProtocol::GotoView;
43 Signal0<void> ControlProtocol::CloseDialog;
44 PBD::Signal0<void> ControlProtocol::VerticalZoomInAll;
45 PBD::Signal0<void> ControlProtocol::VerticalZoomOutAll;
46 PBD::Signal0<void> ControlProtocol::VerticalZoomInSelected;
47 PBD::Signal0<void> ControlProtocol::VerticalZoomOutSelected;
48 PBD::Signal1<void,RouteNotificationListPtr> ControlProtocol::TrackSelectionChanged;
49 PBD::Signal1<void,uint32_t> ControlProtocol::AddRouteToSelection;
50 PBD::Signal1<void,uint32_t> ControlProtocol::SetRouteSelection;
51 PBD::Signal1<void,uint32_t> ControlProtocol::ToggleRouteSelection;
52 PBD::Signal1<void,uint32_t> ControlProtocol::RemoveRouteFromSelection;
53 PBD::Signal0<void>          ControlProtocol::ClearRouteSelection;
54 PBD::Signal0<void>          ControlProtocol::StepTracksDown;
55 PBD::Signal0<void>          ControlProtocol::StepTracksUp;
56
57 const std::string ControlProtocol::state_node_name ("Protocol");
58
59 ControlProtocol::ControlProtocol (Session& s, string str)
60         : BasicUI (s)
61         , _name (str)
62         , _active (false)
63 {
64 }
65
66 ControlProtocol::~ControlProtocol ()
67 {
68 }
69
70 int
71 ControlProtocol::set_active (bool yn)
72 {
73         _active = yn;
74         return 0;
75 }
76
77 void
78 ControlProtocol::next_track (uint32_t initial_id)
79 {
80         uint32_t limit = session->nroutes();
81         boost::shared_ptr<Route> cr = route_table[0];
82         uint32_t id;
83
84         if (cr) {
85                 id = cr->remote_control_id ();
86         } else {
87                 id = 0;
88         }
89
90         if (id == limit) {
91                 id = 0;
92         } else {
93                 id++;
94         }
95
96         while (id <= limit) {
97                 if ((cr = session->route_by_remote_id (id)) != 0) {
98                         break;
99                 }
100                 id++;
101         }
102
103         if (id >= limit) {
104                 id = 0;
105                 while (id != initial_id) {
106                         if ((cr = session->route_by_remote_id (id)) != 0) {
107                                 break;
108                         }
109                         id++;
110                 }
111         }
112
113         route_table[0] = cr;
114 }
115
116 void
117 ControlProtocol::prev_track (uint32_t initial_id)
118 {
119         uint32_t limit = session->nroutes();
120         boost::shared_ptr<Route> cr = route_table[0];
121         int32_t id;
122
123         if (cr) {
124                 id = cr->remote_control_id ();
125         } else {
126                 id = 0;
127         }
128
129         if (id == 0) {
130                 id = limit;
131         } else {
132                 id--;
133         }
134
135         while (id >= 0) {
136                 if ((cr = session->route_by_remote_id (id)) != 0) {
137                         break;
138                 }
139                 id--;
140         }
141
142         if (id < 0) {
143                 uint32_t i = limit;
144                 while (i > initial_id) {
145                         if ((cr = session->route_by_remote_id (i)) != 0) {
146                                 break;
147                         }
148                         i--;
149                 }
150         }
151
152         route_table[0] = cr;
153 }
154
155
156 void
157 ControlProtocol::set_route_table_size (uint32_t size)
158 {
159         while (route_table.size() < size) {
160                 route_table.push_back (boost::shared_ptr<Route> ((Route*) 0));
161         }
162 }
163
164 void
165 ControlProtocol::set_route_table (uint32_t table_index, boost::shared_ptr<ARDOUR::Route> r)
166 {
167         if (table_index >= route_table.size()) {
168                 return;
169         }
170
171         route_table[table_index] = r;
172
173         // XXX SHAREDPTR need to handle r->GoingAway
174 }
175
176 bool
177 ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_id)
178 {
179         boost::shared_ptr<Route> r = session->route_by_remote_id (remote_control_id);
180
181         if (!r) {
182                 return false;
183         }
184
185         set_route_table (table_index, r);
186
187         return true;
188 }
189
190 void
191 ControlProtocol::route_set_rec_enable (uint32_t table_index, bool yn)
192 {
193         if (table_index > route_table.size()) {
194                 return;
195         }
196
197         boost::shared_ptr<Route> r = route_table[table_index];
198
199         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
200
201         if (at) {
202                 at->rec_enable_control()->set_value (1.0, Controllable::UseGroup);
203         }
204 }
205
206 bool
207 ControlProtocol::route_get_rec_enable (uint32_t table_index)
208 {
209         if (table_index > route_table.size()) {
210                 return false;
211         }
212
213         boost::shared_ptr<Route> r = route_table[table_index];
214
215         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
216
217         if (at) {
218                 return at->rec_enable_control()->get_value();
219         }
220
221         return false;
222 }
223
224
225 float
226 ControlProtocol::route_get_gain (uint32_t table_index)
227 {
228         if (table_index > route_table.size()) {
229                 return 0.0f;
230         }
231
232         boost::shared_ptr<Route> r = route_table[table_index];
233
234         if (r == 0) {
235                 return 0.0f;
236         }
237
238         return r->gain_control()->get_value();
239 }
240
241 void
242 ControlProtocol::route_set_gain (uint32_t table_index, float gain)
243 {
244         if (table_index > route_table.size()) {
245                 return;
246         }
247
248         boost::shared_ptr<Route> r = route_table[table_index];
249
250         if (r != 0) {
251                 r->gain_control()->set_value (gain, Controllable::UseGroup);
252         }
253 }
254
255 float
256 ControlProtocol::route_get_effective_gain (uint32_t table_index)
257 {
258         if (table_index > route_table.size()) {
259                 return 0.0f;
260         }
261
262         boost::shared_ptr<Route> r = route_table[table_index];
263
264         if (r == 0) {
265                 return 0.0f;
266         }
267
268         return r->amp()->gain_control()->get_value();
269 }
270
271
272 float
273 ControlProtocol::route_get_peak_input_power (uint32_t table_index, uint32_t which_input)
274 {
275         if (table_index > route_table.size()) {
276                 return 0.0f;
277         }
278
279         boost::shared_ptr<Route> r = route_table[table_index];
280
281         if (r == 0) {
282                 return 0.0f;
283         }
284
285         return r->peak_meter()->meter_level (which_input, MeterPeak);
286 }
287
288 bool
289 ControlProtocol::route_get_muted (uint32_t table_index)
290 {
291         if (table_index > route_table.size()) {
292                 return false;
293         }
294
295         boost::shared_ptr<Route> r = route_table[table_index];
296
297         if (r == 0) {
298                 return false;
299         }
300
301         return r->mute_control()->muted ();
302 }
303
304 void
305 ControlProtocol::route_set_muted (uint32_t table_index, bool yn)
306 {
307         if (table_index > route_table.size()) {
308                 return;
309         }
310
311         boost::shared_ptr<Route> r = route_table[table_index];
312
313         if (r != 0) {
314                 r->mute_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup);
315         }
316 }
317
318
319 bool
320 ControlProtocol::route_get_soloed (uint32_t table_index)
321 {
322         if (table_index > route_table.size()) {
323                 return false;
324         }
325
326         boost::shared_ptr<Route> r = route_table[table_index];
327
328         if (r == 0) {
329                 return false;
330         }
331
332         return r->soloed ();
333 }
334
335 void
336 ControlProtocol::route_set_soloed (uint32_t table_index, bool yn)
337 {
338         if (table_index > route_table.size()) {
339                 return;
340         }
341
342         boost::shared_ptr<Route> r = route_table[table_index];
343
344         if (r != 0) {
345                 r->solo_control()->set_value (yn ? 1.0 : 0.0, Controllable::UseGroup);
346         }
347 }
348
349 string
350 ControlProtocol:: route_get_name (uint32_t table_index)
351 {
352         if (table_index > route_table.size()) {
353                 return "";
354         }
355
356         boost::shared_ptr<Route> r = route_table[table_index];
357
358         if (r == 0) {
359                 return "";
360         }
361
362         return r->name();
363 }
364
365 list<boost::shared_ptr<Bundle> >
366 ControlProtocol::bundles ()
367 {
368         return list<boost::shared_ptr<Bundle> > ();
369 }
370
371 XMLNode&
372 ControlProtocol::get_state ()
373 {
374         XMLNode* node = new XMLNode (state_node_name);
375
376         node->add_property ("name", _name);
377         node->add_property ("feedback", get_feedback() ? "yes" : "no");
378
379         return *node;
380 }
381
382 int
383 ControlProtocol::set_state (XMLNode const & node, int /* version */)
384 {
385         const XMLProperty* prop;
386
387         if ((prop = node.property ("feedback")) != 0) {
388                 set_feedback (string_is_affirmative (prop->value()));
389         }
390
391         return 0;
392 }