OSC: Reworked select to follow Gui selected strip regardless of it's inclusion in...
[ardour.git] / libs / surfaces / osc / osc_route_observer.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "boost/lambda/lambda.hpp"
21
22 #include "ardour/session.h"
23 #include "ardour/track.h"
24 #include "ardour/monitor_control.h"
25 #include "ardour/dB.h"
26 #include "ardour/meter.h"
27
28 #include "osc.h"
29 #include "osc_route_observer.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace PBD;
35 using namespace ARDOUR;
36 using namespace ArdourSurface;
37
38 OSCRouteObserver::OSCRouteObserver (boost::shared_ptr<Stripable> s, lo_address a, uint32_t ss, uint32_t gm, std::bitset<32> fb)
39         : _strip (s)
40         ,ssid (ss)
41         ,gainmode (gm)
42         ,feedback (fb)
43 {
44         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
45
46         if (feedback[0]) { // buttons are separate feedback
47                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::name_changed, this, boost::lambda::_1), OSC::instance());
48                 name_changed (ARDOUR::Properties::name);
49
50                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/mute"), _strip->mute_control()), OSC::instance());
51                 send_change_message ("/strip/mute", _strip->mute_control());
52
53                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo"), _strip->solo_control()), OSC::instance());
54                 send_change_message ("/strip/solo", _strip->solo_control());
55
56                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
57                 if (track) {
58                 track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_monitor_status, this, track->monitoring_control()), OSC::instance());
59                 send_monitor_status (track->monitoring_control());
60                 }
61
62                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
63                 if (rec_controllable) {
64                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/recenable"), _strip->rec_enable_control()), OSC::instance());
65                         send_change_message ("/strip/recenable", _strip->rec_enable_control());
66                 }
67                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
68                 if (rec_controllable) {
69                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/record_safe"), _strip->rec_safe_control()), OSC::instance());
70                         send_change_message ("/strip/record_safe", _strip->rec_safe_control());
71                 }
72                 _strip->presentation_info().PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_select_status, this, _1), OSC::instance());
73                 send_select_status (ARDOUR::Properties::selected);
74         }
75
76         if (feedback[1]) { // level controls
77                 if (gainmode) {
78                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_gain_message, this, X_("/strip/fader"), _strip->gain_control()), OSC::instance());
79                         send_gain_message ("/strip/fader", _strip->gain_control());
80                 } else {
81                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_gain_message, this, X_("/strip/gain"), _strip->gain_control()), OSC::instance());
82                         send_gain_message ("/strip/gain", _strip->gain_control());
83                 }
84
85                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
86                 if (trim_controllable) {
87                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_trim_message, this, X_("/strip/trimdB"), _strip->trim_control()), OSC::instance());
88                         send_trim_message ("/strip/trimdB", _strip->trim_control());
89                 }
90
91                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
92                 if (pan_controllable) {
93                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/pan_stereo_position"), _strip->pan_azimuth_control()), OSC::instance());
94                         send_change_message ("/strip/pan_stereo_position", _strip->pan_azimuth_control());
95                 }
96         }
97         tick();
98 }
99
100 OSCRouteObserver::~OSCRouteObserver ()
101 {
102
103         strip_connections.drop_connections ();
104         // all strip buttons should be off and faders 0 and etc.
105         if (feedback[0]) { // buttons are separate feedback
106                 lo_message msg = lo_message_new ();
107                 // name is a string do it first
108                 string path = "/strip/name";
109                 if (feedback[2]) {
110                         path = set_path (path);
111                 } else {
112                         lo_message_add_int32 (msg, ssid);
113                 }
114                 lo_message_add_string (msg, " ");
115
116                 lo_send_message (addr, path.c_str(), msg);
117                 lo_message_free (msg);
118                 clear_strip ("/strip/mute", 0);
119                 clear_strip ("/strip/solo", 0);
120                 clear_strip ("/strip/recenable", 0);
121                 clear_strip ("/strip/record_safe", 0);
122                 clear_strip ("/strip/monitor_input", 0);
123                 clear_strip ("/strip/monitor_disk", 0);
124                 clear_strip ("/strip/gui_select", 0);
125                 clear_strip ("/strip/select", 0);
126         }
127         if (feedback[1]) { // level controls
128                 if (gainmode) {
129                         clear_strip ("/strip/fader", 0);
130                 } else {
131                         clear_strip ("/strip/gain", -193);
132                 }
133                 clear_strip ("/strip/trimdB", 0);
134                 clear_strip ("/strip/pan_stereo_position", 0.5);
135         }
136         if (feedback[9]) {
137                 clear_strip ("/strip/signal", 0);
138         }
139         if (feedback[7]) {
140                 if (gainmode) {
141                         clear_strip ("/strip/meter", 0);
142                 } else {
143                         clear_strip ("/strip/meter", -193);
144                 }
145         }else if (feedback[8]) {
146                 clear_strip ("/strip/meter", 0);
147         }
148
149         lo_address_free (addr);
150 }
151
152 void
153 OSCRouteObserver::tick ()
154 {
155         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
156                 // the only meter here is master
157                 float now_meter;
158                 if (_strip->peak_meter()) {
159                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
160                 } else {
161                         now_meter = -193;
162                 }
163                 if (now_meter < -193) now_meter = -193;
164                 if (_last_meter != now_meter) {
165                         if (feedback[7] || feedback[8]) {
166                                 string path = "/strip/meter";
167                                 lo_message msg = lo_message_new ();
168                                 if (feedback[2]) {
169                                         path = set_path (path);
170                                 } else {
171                                         lo_message_add_int32 (msg, ssid);
172                                 }
173                                 if (gainmode && feedback[7]) {
174                                         uint32_t lev1023 = (uint32_t)((now_meter + 54) * 17.05);
175                                         lo_message_add_int32 (msg, lev1023);
176                                         lo_send_message (addr, path.c_str(), msg);
177                                 } else if ((!gainmode) && feedback[7]) {
178                                         lo_message_add_float (msg, now_meter);
179                                         lo_send_message (addr, path.c_str(), msg);
180                                 } else if (feedback[8]) {
181                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
182                                         uint16_t ledbits = ~(0xfff<<ledlvl);
183                                         lo_message_add_int32 (msg, ledbits);
184                                         lo_send_message (addr, path.c_str(), msg);
185                                 }
186                                 lo_message_free (msg);
187                         }
188                         if (feedback[9]) {
189                                 string path = "/strip/signal";
190                                 lo_message msg = lo_message_new ();
191                                 if (feedback[2]) {
192                                         path = set_path (path);
193                                 } else {
194                                         lo_message_add_int32 (msg, ssid);
195                                 }
196                                 float signal;
197                                 if (now_meter < -40) {
198                                         signal = 0;
199                                 } else {
200                                         signal = 1;
201                                 }
202                                 lo_message_add_float (msg, signal);
203                                 lo_send_message (addr, path.c_str(), msg);
204                                 lo_message_free (msg);
205                         }
206                 }
207                 _last_meter = now_meter;
208
209         }
210
211 }
212
213 void
214 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
215 {
216         if (!what_changed.contains (ARDOUR::Properties::name)) {
217             return;
218         }
219
220         if (!_strip) {
221                 return;
222         }
223
224         lo_message msg = lo_message_new ();
225
226         // ssid is the strip on the surface this observer refers to
227         // not part of the internal ordering.
228         string path = "/strip/name";
229         if (feedback[2]) {
230                 path = set_path (path);
231         } else {
232                 lo_message_add_int32 (msg, ssid);
233         }
234         lo_message_add_string (msg, _strip->name().c_str());
235
236         lo_send_message (addr, path.c_str(), msg);
237         lo_message_free (msg);
238 }
239
240 void
241 OSCRouteObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
242 {
243         lo_message msg = lo_message_new ();
244
245         if (feedback[2]) {
246                 path = set_path (path);
247         } else {
248                 lo_message_add_int32 (msg, ssid);
249         }
250         lo_message_add_float (msg, (float) controllable->get_value());
251
252         lo_send_message (addr, path.c_str(), msg);
253         lo_message_free (msg);
254 }
255
256 void
257 OSCRouteObserver::send_monitor_status (boost::shared_ptr<Controllable> controllable)
258 {
259         int disk, input;
260         float val = controllable->get_value();
261         switch ((int) val) {
262                 case 1:
263                         disk = 0;
264                         input = 1;
265                         break;
266                 case 2:
267                         disk = 1;
268                         input = 0;
269                         break;
270                 default:
271                         disk = 0;
272                         input = 0;
273         }
274
275         lo_message msg = lo_message_new ();
276         string path = "/strip/monitor_input";
277         if (feedback[2]) {
278                 path = set_path (path);
279         } else {
280                 lo_message_add_int32 (msg, ssid);
281         }
282         lo_message_add_int32 (msg, (float) input);
283         lo_send_message (addr, path.c_str(), msg);
284         lo_message_free (msg);
285
286         msg = lo_message_new ();
287         path = "/strip/monitor_disk";
288         if (feedback[2]) {
289                 path = set_path (path);
290         } else {
291                 lo_message_add_int32 (msg, ssid);
292         }
293         lo_message_add_int32 (msg, (float) disk);
294         lo_send_message (addr, path.c_str(), msg);
295         lo_message_free (msg);
296
297 }
298
299 void
300 OSCRouteObserver::send_trim_message (string path, boost::shared_ptr<Controllable> controllable)
301 {
302         lo_message msg = lo_message_new ();
303
304         if (feedback[2]) {
305                 path = set_path (path);
306         } else {
307                 lo_message_add_int32 (msg, ssid);
308         }
309
310         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
311
312         lo_send_message (addr, path.c_str(), msg);
313         lo_message_free (msg);
314 }
315
316 void
317 OSCRouteObserver::send_gain_message (string path, boost::shared_ptr<Controllable> controllable)
318 {
319         lo_message msg = lo_message_new ();
320
321         if (feedback[2]) {
322                 path = set_path (path);
323         } else {
324                 lo_message_add_int32 (msg, ssid);
325         }
326
327         if (gainmode) {
328                 if (controllable->get_value() == 1) {
329                         lo_message_add_int32 (msg, 800);
330                 } else {
331                         lo_message_add_int32 (msg, gain_to_slider_position (controllable->get_value()) * 1023);
332                 }
333         } else {
334                 if (controllable->get_value() < 1e-15) {
335                         lo_message_add_float (msg, -200);
336                 } else {
337                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
338                 }
339         }
340
341         lo_send_message (addr, path.c_str(), msg);
342         lo_message_free (msg);
343 }
344
345 string
346 OSCRouteObserver::set_path (string path)
347 {
348         if (feedback[2]) {
349   ostringstream os;
350   os << path << "/" << ssid;
351   path = os.str();
352         }
353         return path;
354 }
355
356 void
357 OSCRouteObserver::clear_strip (string path, float val)
358 {
359         lo_message msg = lo_message_new ();
360         if (feedback[2]) {
361                 path = set_path (path);
362         } else {
363                 lo_message_add_int32 (msg, ssid);
364         }
365         lo_message_add_float (msg, val);
366
367         lo_send_message (addr, path.c_str(), msg);
368         lo_message_free (msg);
369
370 }
371
372 void
373 OSCRouteObserver::send_select_status (const PropertyChange& what)
374 {
375         if (what == PropertyChange(ARDOUR::Properties::selected)) {
376                 if (_strip) {
377                         string path = "/strip/select";
378
379                         lo_message msg = lo_message_new ();
380                         if (feedback[2]) {
381                                 path = set_path (path);
382                         } else {
383                                 lo_message_add_int32 (msg, ssid);
384                         }
385                         lo_message_add_float (msg, _strip->is_selected());
386                         lo_send_message (addr, path.c_str(), msg);
387                         lo_message_free (msg);
388                 }
389         }
390 }