OSC: added solo isolate and solo safe/lock.
[ardour.git] / libs / surfaces / osc / osc_select_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 #include "ardour/phase_control.h"
28 #include "ardour/solo_isolate_control.h"
29 #include "ardour/solo_safe_control.h"
30
31 #include "osc.h"
32 #include "osc_select_observer.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace PBD;
38 using namespace ARDOUR;
39 using namespace ArdourSurface;
40
41 OSCSelectObserver::OSCSelectObserver (boost::shared_ptr<Stripable> s, lo_address a, uint32_t ss, uint32_t gm, std::bitset<32> fb)
42         : _strip (s)
43         ,ssid (ss)
44         ,gainmode (gm)
45         ,feedback (fb)
46         ,nsends (0)
47 {
48         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
49
50         if (feedback[0]) { // buttons are separate feedback
51                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::name_changed, this, boost::lambda::_1), OSC::instance());
52                 name_changed (ARDOUR::Properties::name);
53
54                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/mute"), _strip->mute_control()), OSC::instance());
55                 change_message ("/select/mute", _strip->mute_control());
56
57                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo"), _strip->solo_control()), OSC::instance());
58                 change_message ("/select/solo", _strip->solo_control());
59
60                 _strip->solo_isolate_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo_iso"), _strip->solo_isolate_control()), OSC::instance());
61                 change_message ("/select/solo_iso", _strip->solo_isolate_control());
62
63                 _strip->solo_safe_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo_safe"), _strip->solo_safe_control()), OSC::instance());
64                 change_message ("/select/solo_safe", _strip->solo_safe_control());
65
66                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
67                 if (track) {
68                 track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::monitor_status, this, track->monitoring_control()), OSC::instance());
69                 monitor_status (track->monitoring_control());
70                 }
71
72                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
73                 if (rec_controllable) {
74                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/recenable"), _strip->rec_enable_control()), OSC::instance());
75                         change_message ("/select/recenable", _strip->rec_enable_control());
76                 }
77
78                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
79                 if (recsafe_controllable) {
80                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/record_safe"), _strip->rec_safe_control()), OSC::instance());
81                         change_message ("/select/record_safe", _strip->rec_safe_control());
82                 }
83
84                 boost::shared_ptr<AutomationControl> phase_controllable = _strip->phase_control ();
85                 if (phase_controllable) {
86                         phase_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/polarity"), _strip->phase_control()), OSC::instance());
87                         change_message ("/select/polarity", _strip->phase_control());
88                 }
89
90         }
91
92         if (feedback[1]) { // level controls
93                 if (gainmode) {
94                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this, X_("/select/fader"), _strip->gain_control()), OSC::instance());
95                         gain_message ("/select/fader", _strip->gain_control());
96                 } else {
97                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this, X_("/select/gain"), _strip->gain_control()), OSC::instance());
98                         gain_message ("/select/gain", _strip->gain_control());
99                 }
100
101                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
102                 if (trim_controllable) {
103                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::trim_message, this, X_("/select/trimdB"), _strip->trim_control()), OSC::instance());
104                         trim_message ("/select/trimdB", _strip->trim_control());
105                 }
106
107                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
108                 if (pan_controllable) {
109                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_stereo_position"), _strip->pan_azimuth_control()), OSC::instance());
110                         change_message ("/select/pan_stereo_position", _strip->pan_azimuth_control());
111                 }
112
113                 boost::shared_ptr<Controllable> width_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_width_control());
114                 if (width_controllable) {
115                         width_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_stereo_width"), _strip->pan_width_control()), OSC::instance());
116                         change_message ("/select/pan_stereo_width", _strip->pan_width_control());
117                 }
118
119                 // detecting processor changes requires cast to route
120                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(_strip);
121                 r->processors_changed.connect  (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_restart, this, -1), OSC::instance());
122                 send_init();
123         }
124         tick();
125 }
126
127 OSCSelectObserver::~OSCSelectObserver ()
128 {
129
130         strip_connections.drop_connections ();
131         // all strip buttons should be off and faders 0 and etc.
132         if (feedback[0]) { // buttons are separate feedback
133                 lo_message msg = lo_message_new ();
134                 // name is a string do it first
135                 string path = "/select/name";
136                 lo_message_add_string (msg, " ");
137                 lo_send_message (addr, path.c_str(), msg);
138                 lo_message_free (msg);
139                 clear_strip ("/select/mute", 0);
140                 clear_strip ("/select/solo", 0);
141                 clear_strip ("/select/recenable", 0);
142                 clear_strip ("/select/record_safe", 0);
143                 clear_strip ("/select/monitor_input", 0);
144                 clear_strip ("/select/monitor_disk", 0);
145                 clear_strip ("/select/polarity", 0);
146         }
147         if (feedback[1]) { // level controls
148                 if (gainmode) {
149                         clear_strip ("/select/fader", 0);
150                 } else {
151                         clear_strip ("/select/gain", -193);
152                 }
153                 clear_strip ("/select/trimdB", 0);
154                 clear_strip ("/select/pan_stereo_position", 0.5);
155                 clear_strip ("/select/pan_stereo_width", 1);
156         }
157         if (feedback[9]) {
158                 clear_strip ("/select/signal", 0);
159         }
160         if (feedback[7]) {
161                 if (gainmode) {
162                         clear_strip ("/select/meter", 0);
163                 } else {
164                         clear_strip ("/select/meter", -193);
165                 }
166         }else if (feedback[8]) {
167                 clear_strip ("/select/meter", 0);
168         }
169         send_end();
170
171         lo_address_free (addr);
172 }
173
174 void
175 OSCSelectObserver::send_init()
176 {
177         // we don't know how many there are, so find out.
178         bool sends;
179         do {
180                 sends = false;
181                 if (_strip->send_level_controllable (nsends)) {
182                         _strip->send_level_controllable(nsends)->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain, this, nsends, _strip->send_level_controllable(nsends)), OSC::instance());
183                         send_gain (nsends, _strip->send_level_controllable(nsends));
184                         sends = true;
185                 }
186
187                 if (_strip->send_enable_controllable (nsends)) {
188                         _strip->send_enable_controllable(nsends)->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_enable, this, X_("/select/send_enable"), nsends, _strip->send_enable_controllable(nsends)), OSC::instance());
189                         send_enable ("/select/send_enable", nsends, _strip->send_enable_controllable(nsends));
190                         sends = true;
191                 } else if (sends) {
192                         // not used by Ardour, just mixbus so in Ardour always true
193                         lo_message msg = lo_message_new ();
194                         path = "/select/send_enable";
195                         if (feedback[2]) {
196                                 path = set_path (path, nsends + 1);
197                         } else {
198                                 lo_message_add_int32 (msg, nsends + 1);
199                         }
200                         lo_message_add_int32 (msg, 1);
201                         lo_send_message (addr, path.c_str(), msg);
202                         lo_message_free (msg);
203                 }
204                 // this should get signalled by the route the send goes to, (TODO)
205                 if (sends) { // if the gain control is there, this is too
206                         send_name ("/select/send_name", nsends, _strip->send_name(nsends));
207                 }
208                 // Send numbers are 0 based, OSC is 1 based so this gets incremented at the end
209                 if (sends) {
210                         nsends++;
211                 }
212         } while (sends);
213 }
214
215 void
216 OSCSelectObserver::send_end ()
217 {
218         send_connections.drop_connections ();
219         for (uint32_t i = 1; i <= nsends; i++) {
220                 lo_message msg = lo_message_new ();
221                 string path = "/select/send_gain";
222                 if (feedback[2]) {
223                         path = set_path (path, i);
224                 } else {
225                         lo_message_add_int32 (msg, i);
226                 }
227
228                 if (gainmode) {
229                         lo_message_add_int32 (msg, 0);
230                 } else {
231                         lo_message_add_float (msg, -193);
232                 }
233                 lo_send_message (addr, path.c_str(), msg);
234                 lo_message_free (msg);
235                 // next enable
236                 msg = lo_message_new ();
237                 path = "/select/send_enable";
238                 if (feedback[2]) {
239                         path = set_path (path, i);
240                 } else {
241                         lo_message_add_int32 (msg, i);
242                 }
243                 lo_message_add_int32 (msg, 0);
244                 lo_send_message (addr, path.c_str(), msg);
245                 lo_message_free (msg);
246                 // next name
247                 msg = lo_message_new ();
248                 path = "/select/send_name";
249                 if (feedback[2]) {
250                         path = set_path (path, i);
251                 } else {
252                         lo_message_add_int32 (msg, i);
253                 }
254                 lo_message_add_string (msg, " ");
255                 lo_send_message (addr, path.c_str(), msg);
256                 lo_message_free (msg);
257         }
258         nsends = 0;
259 }
260
261 void
262 OSCSelectObserver::send_restart(int x)
263 {
264         send_end();
265         send_init();
266 }
267
268 void
269 OSCSelectObserver::tick ()
270 {
271         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
272
273                 float now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
274                 if (now_meter < -193) now_meter = -193;
275                 if (_last_meter != now_meter) {
276                         if (feedback[7] || feedback[8]) {
277                                 string path = "/select/meter";
278                                 lo_message msg = lo_message_new ();
279                                 if (gainmode && feedback[7]) {
280                                         uint32_t lev1023 = (uint32_t)((now_meter + 54) * 17.05);
281                                         lo_message_add_int32 (msg, lev1023);
282                                         lo_send_message (addr, path.c_str(), msg);
283                                 } else if ((!gainmode) && feedback[7]) {
284                                         lo_message_add_float (msg, now_meter);
285                                         lo_send_message (addr, path.c_str(), msg);
286                                 } else if (feedback[8]) {
287                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
288                                         uint16_t ledbits = ~(0xfff<<ledlvl);
289                                         lo_message_add_int32 (msg, ledbits);
290                                         lo_send_message (addr, path.c_str(), msg);
291                                 }
292                                 lo_message_free (msg);
293                         }
294                         if (feedback[9]) {
295                                 string path = "/select/signal";
296                                 lo_message msg = lo_message_new ();
297                                 float signal;
298                                 if (now_meter < -40) {
299                                         signal = 0;
300                                 } else {
301                                         signal = 1;
302                                 }
303                                 lo_message_add_float (msg, signal);
304                                 lo_send_message (addr, path.c_str(), msg);
305                                 lo_message_free (msg);
306                         }
307                 }
308                 _last_meter = now_meter;
309
310         }
311
312 }
313
314 void
315 OSCSelectObserver::name_changed (const PBD::PropertyChange& what_changed)
316 {
317         if (!what_changed.contains (ARDOUR::Properties::name)) {
318             return;
319         }
320
321         if (!_strip) {
322                 return;
323         }
324
325         lo_message msg = lo_message_new ();
326
327         // ssid is the strip on the surface this observer refers to
328         // not part of the internal ordering.
329         string path = "/select/name";
330         /*if (feedback[2]) {
331                 path = set_path (path);
332         } else {
333                 lo_message_add_int32 (msg, ssid);
334         }*/
335         lo_message_add_string (msg, _strip->name().c_str());
336
337         lo_send_message (addr, path.c_str(), msg);
338         lo_message_free (msg);
339 }
340
341 void
342 OSCSelectObserver::change_message (string path, boost::shared_ptr<Controllable> controllable)
343 {
344         lo_message msg = lo_message_new ();
345
346         lo_message_add_float (msg, (float) controllable->get_value());
347
348         lo_send_message (addr, path.c_str(), msg);
349         lo_message_free (msg);
350 }
351
352 void
353 OSCSelectObserver::monitor_status (boost::shared_ptr<Controllable> controllable)
354 {
355         int disk, input;
356         float val = controllable->get_value();
357         switch ((int) val) {
358                 case 1:
359                         disk = 0;
360                         input = 1;
361                         break;
362                 case 2:
363                         disk = 1;
364                         input = 0;
365                         break;
366                 default:
367                         disk = 0;
368                         input = 0;
369         }
370
371         lo_message msg = lo_message_new ();
372         string path = "/select/monitor_input";
373         lo_message_add_int32 (msg, (float) input);
374         lo_send_message (addr, path.c_str(), msg);
375         lo_message_free (msg);
376
377         msg = lo_message_new ();
378         path = "/select/monitor_disk";
379         lo_message_add_int32 (msg, (float) disk);
380         lo_send_message (addr, path.c_str(), msg);
381         lo_message_free (msg);
382
383 }
384
385 void
386 OSCSelectObserver::trim_message (string path, boost::shared_ptr<Controllable> controllable)
387 {
388         lo_message msg = lo_message_new ();
389
390         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
391
392         lo_send_message (addr, path.c_str(), msg);
393         lo_message_free (msg);
394 }
395
396 void
397 OSCSelectObserver::gain_message (string path, boost::shared_ptr<Controllable> controllable)
398 {
399         lo_message msg = lo_message_new ();
400
401         if (gainmode) {
402                 if (controllable->get_value() == 1) {
403                         lo_message_add_int32 (msg, 800);
404                 } else {
405                         lo_message_add_int32 (msg, gain_to_slider_position (controllable->get_value()) * 1023);
406                 }
407         } else {
408                 if (controllable->get_value() < 1e-15) {
409                         lo_message_add_float (msg, -200);
410                 } else {
411                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
412                 }
413         }
414
415         lo_send_message (addr, path.c_str(), msg);
416         lo_message_free (msg);
417 }
418
419 void
420 OSCSelectObserver::send_gain (uint32_t id, boost::shared_ptr<PBD::Controllable> controllable)
421 {
422         lo_message msg = lo_message_new ();
423         string path;
424         float value;
425
426         if (gainmode) {
427                 path = "/select/send_fader";
428                 if (controllable->get_value() == 1) {
429                         value = 800;
430                 } else {
431                         value = gain_to_slider_position (controllable->get_value());
432                 }
433         } else {
434                 path = "/select/send_gain";
435                 if (controllable->get_value() < 1e-15) {
436                         value = -193;
437                 } else {
438                         value = accurate_coefficient_to_dB (controllable->get_value());
439                 }
440         }
441
442         if (feedback[2]) {
443                 path = set_path (path, id + 1);
444         } else {
445                 lo_message_add_int32 (msg, id + 1);
446         }
447
448         lo_message_add_float (msg, value);
449         lo_send_message (addr, path.c_str(), msg);
450         lo_message_free (msg);
451 }
452
453 void
454 OSCSelectObserver::send_enable (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
455 {
456         lo_message msg = lo_message_new ();
457         if (feedback[2]) {
458                 path = set_path (path, id + 1);
459         } else {
460                 lo_message_add_int32 (msg, id + 1);
461         }
462
463         lo_message_add_float (msg, (float) controllable->get_value());
464
465         lo_send_message (addr, path.c_str(), msg);
466         lo_message_free (msg);
467 }
468
469 void
470 OSCSelectObserver::send_name (string path, uint32_t id, string name)
471 {
472         lo_message msg = lo_message_new ();
473         if (feedback[2]) {
474                 path = set_path (path, id + 1);
475         } else {
476                 lo_message_add_int32 (msg, id + 1);
477         }
478
479         lo_message_add_string (msg, name.c_str());
480
481         lo_send_message (addr, path.c_str(), msg);
482         lo_message_free (msg);
483 }
484
485 string
486 OSCSelectObserver::set_path (string path, uint32_t id)
487 {
488         if (feedback[2]) {
489   ostringstream os;
490   os << path << "/" << id;
491   path = os.str();
492         }
493         return path;
494 }
495
496 void
497 OSCSelectObserver::clear_strip (string path, float val)
498 {
499         lo_message msg = lo_message_new ();
500         lo_message_add_float (msg, val);
501
502         lo_send_message (addr, path.c_str(), msg);
503         lo_message_free (msg);
504
505 }
506