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