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