cd379b10dcdef026fa6ec5728c8a91a6f5ee9ee2
[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 const std::string ControlProtocol::state_node_name ("Protocol");
57
58 ControlProtocol::ControlProtocol (Session& s, string str)
59         : BasicUI (s)
60         , _name (str)
61         , _active (false)
62 {
63 }
64
65 ControlProtocol::~ControlProtocol ()
66 {
67 }
68
69 int
70 ControlProtocol::set_active (bool yn)
71 {
72         _active = yn;
73         return 0;
74 }
75
76 void
77 ControlProtocol::next_track (uint32_t initial_id)
78 {
79         uint32_t limit = session->nroutes();
80         boost::shared_ptr<Route> cr = route_table[0];
81         uint32_t id;
82
83         if (cr) {
84                 id = cr->remote_control_id ();
85         } else {
86                 id = 0;
87         }
88
89         if (id == limit) {
90                 id = 0;
91         } else {
92                 id++;
93         }
94
95         while (id <= limit) {
96                 if ((cr = session->route_by_remote_id (id)) != 0) {
97                         break;
98                 }
99                 id++;
100         }
101
102         if (id >= limit) {
103                 id = 0;
104                 while (id != initial_id) {
105                         if ((cr = session->route_by_remote_id (id)) != 0) {
106                                 break;
107                         }
108                         id++;
109                 }
110         }
111
112         route_table[0] = cr;
113 }
114
115 void
116 ControlProtocol::prev_track (uint32_t initial_id)
117 {
118         uint32_t limit = session->nroutes();
119         boost::shared_ptr<Route> cr = route_table[0];
120         int32_t id;
121
122         if (cr) {
123                 id = cr->remote_control_id ();
124         } else {
125                 id = 0;
126         }
127
128         if (id == 0) {
129                 id = limit;
130         } else {
131                 id--;
132         }
133
134         while (id >= 0) {
135                 if ((cr = session->route_by_remote_id (id)) != 0) {
136                         break;
137                 }
138                 id--;
139         }
140
141         if (id < 0) {
142                 uint32_t i = limit;
143                 while (i > initial_id) {
144                         if ((cr = session->route_by_remote_id (i)) != 0) {
145                                 break;
146                         }
147                         i--;
148                 }
149         }
150
151         route_table[0] = cr;
152 }
153
154
155 void
156 ControlProtocol::set_route_table_size (uint32_t size)
157 {
158         while (route_table.size() < size) {
159                 route_table.push_back (boost::shared_ptr<Route> ((Route*) 0));
160         }
161 }
162
163 void
164 ControlProtocol::set_route_table (uint32_t table_index, boost::shared_ptr<ARDOUR::Route> r)
165 {
166         if (table_index >= route_table.size()) {
167                 return;
168         }
169         
170         route_table[table_index] = r;
171
172         // XXX SHAREDPTR need to handle r->GoingAway
173 }
174
175 bool
176 ControlProtocol::set_route_table (uint32_t table_index, uint32_t remote_control_id)
177 {
178         boost::shared_ptr<Route> r = session->route_by_remote_id (remote_control_id);
179
180         if (!r) {
181                 return false;
182         }
183
184         set_route_table (table_index, r);
185
186         return true;
187 }
188
189 void
190 ControlProtocol::route_set_rec_enable (uint32_t table_index, bool yn)
191 {
192         if (table_index > route_table.size()) {
193                 return;
194         }
195
196         boost::shared_ptr<Route> r = route_table[table_index];
197
198         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
199
200         if (at) {
201                 at->set_record_enabled (yn, this);
202         }
203 }
204
205 bool
206 ControlProtocol::route_get_rec_enable (uint32_t table_index)
207 {
208         if (table_index > route_table.size()) {
209                 return false;
210         }
211
212         boost::shared_ptr<Route> r = route_table[table_index];
213
214         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(r);
215
216         if (at) {
217                 return at->record_enabled ();
218         }
219
220         return false;
221 }
222
223
224 float
225 ControlProtocol::route_get_gain (uint32_t table_index)
226 {
227         if (table_index > route_table.size()) {
228                 return 0.0f;
229         }
230
231         boost::shared_ptr<Route> r = route_table[table_index];
232
233         if (r == 0) {
234                 return 0.0f;
235         }
236
237         return r->amp()->gain ();
238 }
239
240 void
241 ControlProtocol::route_set_gain (uint32_t table_index, float gain)
242 {
243         if (table_index > route_table.size()) {
244                 return;
245         }
246
247         boost::shared_ptr<Route> r = route_table[table_index];
248         
249         if (r != 0) {
250                 r->set_gain (gain, this);
251         }
252 }
253
254 float
255 ControlProtocol::route_get_effective_gain (uint32_t table_index)
256 {
257         if (table_index > route_table.size()) {
258                 return 0.0f;
259         }
260
261         boost::shared_ptr<Route> r = route_table[table_index];
262
263         if (r == 0) {
264                 return 0.0f;
265         }
266
267         return r->amp()->gain_control()->get_value();
268 }
269
270
271 float
272 ControlProtocol::route_get_peak_input_power (uint32_t table_index, uint32_t which_input)
273 {
274         if (table_index > route_table.size()) {
275                 return 0.0f;
276         }
277
278         boost::shared_ptr<Route> r = route_table[table_index];
279
280         if (r == 0) {
281                 return 0.0f;
282         }
283
284         return r->peak_meter().peak_power (which_input);
285 }
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->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->set_mute (yn, this);
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->set_solo (yn, this);
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
378         return *node;
379 }