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