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