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