OSC: fix /strip/hidden feedback excess
[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 "pbd/control_math.h"
23 #include <glibmm.h>
24
25
26 #include "ardour/session.h"
27 #include "ardour/track.h"
28 #include "ardour/monitor_control.h"
29 #include "ardour/dB.h"
30 #include "ardour/meter.h"
31 #include "ardour/route.h"
32 #include "ardour/route_group.h"
33 #include "ardour/solo_isolate_control.h"
34
35 #include "osc.h"
36 #include "osc_route_observer.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace std;
41 using namespace PBD;
42 using namespace ARDOUR;
43 using namespace ArdourSurface;
44
45 OSCRouteObserver::OSCRouteObserver (OSC& o, uint32_t ss, ArdourSurface::OSC::OSCSurface* su)
46         : _osc (o)
47         ,ssid (ss)
48         ,sur (su)
49         ,_last_gain (-1.0)
50         ,_last_trim (-1.0)
51         ,_init (true)
52         ,_expand (2048)
53 {
54         addr = lo_address_new_from_url (sur->remote_url.c_str());
55         gainmode = sur->gainmode;
56         feedback = sur->feedback;
57         in_line = feedback[2];
58         uint32_t sid = sur->bank + ssid - 2;
59         uint32_t not_ready = 0;
60         if (sur->linkset) {
61                 not_ready = _osc.link_sets[sur->linkset].not_ready;
62         }
63         if (not_ready) {
64                 set_link_ready (not_ready);
65         } else if (sid >= sur->strips.size ()) {
66                 // this _should_ only occure if the number of strips is less than banksize
67                 _strip = boost::shared_ptr<ARDOUR::Stripable>();
68                 clear_strip ();
69         } else {
70                 _strip = sur->strips[sid];
71                 refresh_strip (_strip, true);
72         }
73         if (sur->expand_enable) {
74                 set_expand (sur->expand);
75         } else {
76                 set_expand (0);
77         }
78 }
79
80 OSCRouteObserver::~OSCRouteObserver ()
81 {
82         _init = true;
83         strip_connections.drop_connections ();
84
85         lo_address_free (addr);
86 }
87
88 void
89 OSCRouteObserver::no_strip ()
90 {
91         // This gets called on drop references
92         _init = true;
93
94         strip_connections.drop_connections ();
95         /*
96          * The strip will sit idle at this point doing nothing until
97          * the surface has recalculated it's strip list and then calls
98          * refresh_strip. Otherwise refresh strip will get a strip address
99          * that does not exist... Crash
100          */
101  }
102         
103 void
104 OSCRouteObserver::refresh_strip (boost::shared_ptr<ARDOUR::Stripable> new_strip, bool force)
105 {
106         _init = true;
107         if (_tick_busy) {
108                 Glib::usleep(100); // let tick finish
109         }
110         _last_gain =-1.0;
111         _last_trim =-1.0;
112
113         send_select_status (ARDOUR::Properties::selected);
114
115         if ((new_strip == _strip) && !force) {
116                 // no change don't send feedback
117                 _init = false;
118                 return;
119         }
120         strip_connections.drop_connections ();
121         _strip = new_strip;
122         if (!_strip) {
123                 // this strip is blank and should be cleared
124                 clear_strip ();
125                 return;
126         }
127         _strip->DropReferences.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::no_strip, this), OSC::instance());
128         as = ARDOUR::Off;
129
130         if (feedback[0]) { // buttons are separate feedback
131                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::name_changed, this, boost::lambda::_1), OSC::instance());
132                 name_changed (ARDOUR::Properties::name);
133
134                 boost::shared_ptr<Route> rt = boost::dynamic_pointer_cast<Route> (_strip);
135                 if (rt) {
136                         rt->route_group_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::group_name, this), OSC::instance());
137                         group_name ();
138                 }
139
140                 _strip->presentation_info().PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::pi_changed, this, _1), OSC::instance());
141                 _osc.int_message_with_id (X_("/strip/hide"), ssid, _strip->is_hidden (), in_line, addr);
142
143                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/mute"), _strip->mute_control()), OSC::instance());
144                 send_change_message (X_("/strip/mute"), _strip->mute_control());
145
146                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo"), _strip->solo_control()), OSC::instance());
147                 send_change_message (X_("/strip/solo"), _strip->solo_control());
148
149                 if (_strip->solo_isolate_control()) {
150                         _strip->solo_isolate_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo_iso"), _strip->solo_isolate_control()), OSC::instance());
151                         send_change_message (X_("/strip/solo_iso"), _strip->solo_isolate_control());
152                 }
153
154                 if (_strip->solo_safe_control()) {
155                         _strip->solo_safe_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo_safe"), _strip->solo_safe_control()), OSC::instance());
156                         send_change_message (X_("/strip/solo_safe"), _strip->solo_safe_control());
157                 }
158
159                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
160                 if (track) {
161                         track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_monitor_status, this, track->monitoring_control()), OSC::instance());
162                         send_monitor_status (track->monitoring_control());
163                 }
164
165                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
166                 if (rec_controllable) {
167                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/recenable"), _strip->rec_enable_control()), OSC::instance());
168                         send_change_message (X_("/strip/recenable"), _strip->rec_enable_control());
169                 }
170                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
171                 if (rec_controllable) {
172                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/record_safe"), _strip->rec_safe_control()), OSC::instance());
173                         send_change_message (X_("/strip/record_safe"), _strip->rec_safe_control());
174                 }
175                 _strip->presentation_info().PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_select_status, this, _1), OSC::instance());
176                 send_select_status (ARDOUR::Properties::selected);
177         }
178
179         if (feedback[1]) { // level controls
180                 boost::shared_ptr<GainControl> gain_cont = _strip->gain_control();
181                 gain_cont->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::gain_automation, this), OSC::instance());
182                 gain_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_gain_message, this), OSC::instance());
183                 gain_automation ();
184
185                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
186                 if (trim_controllable) {
187                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_trim_message, this), OSC::instance());
188                         send_trim_message ();
189                 }
190
191                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
192                 if (pan_controllable) {
193                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/pan_stereo_position"), _strip->pan_azimuth_control()), OSC::instance());
194                         send_change_message (X_("/strip/pan_stereo_position"), _strip->pan_azimuth_control());
195                 }
196         }
197         _init = false;
198         tick();
199
200 }
201
202 void
203 OSCRouteObserver::set_expand (uint32_t expand)
204 {
205         if (expand != _expand) {
206                 _expand = expand;
207                 if (expand == ssid) {
208                         _osc.float_message_with_id (X_("/strip/expand"), ssid, 1.0, in_line, addr);
209                 } else {
210                         _osc.float_message_with_id (X_("/strip/expand"), ssid, 0.0, in_line, addr);
211                 }
212         }
213 }
214
215 void
216 OSCRouteObserver::set_link_ready (uint32_t not_ready)
217 {
218         if (not_ready) {
219                 clear_strip ();
220                 switch (ssid) {
221                         case 1:
222                                 _osc.text_message_with_id (X_("/strip/name"), ssid, "Device", in_line, addr);
223                                 break;
224                         case 2:
225                                 _osc.text_message_with_id (X_("/strip/name"), ssid, string_compose ("%1", not_ready), in_line, addr);
226                                 break;
227                         case 3:
228                                 _osc.text_message_with_id (X_("/strip/name"), ssid, "Missing", in_line, addr);
229                                 break;
230                         case 4:
231                                 _osc.text_message_with_id (X_("/strip/name"), ssid, "from", in_line, addr);
232                                 break;
233                         case 5:
234                                 _osc.text_message_with_id (X_("/strip/name"), ssid, "Linkset", in_line, addr);
235                                 break;
236                         default:
237                                 break;
238                 }
239         } else {
240                 refresh_strip (_strip, true);
241         }
242 }
243
244 void
245 OSCRouteObserver::clear_strip ()
246 {
247         _init = true;
248
249         strip_connections.drop_connections ();
250
251         // all strip buttons should be off and faders 0 and etc.
252         _osc.float_message_with_id (X_("/strip/expand"), ssid, 0, in_line, addr);
253         if (feedback[0]) { // buttons are separate feedback
254                 _osc.text_message_with_id (X_("/strip/name"), ssid, " ", in_line, addr);
255                 _osc.text_message_with_id (X_("/strip/group"), ssid, "none", in_line, addr);
256                 _osc.float_message_with_id (X_("/strip/mute"), ssid, 0, in_line, addr);
257                 _osc.float_message_with_id (X_("/strip/solo"), ssid, 0, in_line, addr);
258                 _osc.float_message_with_id (X_("/strip/recenable"), ssid, 0, in_line, addr);
259                 _osc.float_message_with_id (X_("/strip/record_safe"), ssid, 0, in_line, addr);
260                 _osc.float_message_with_id (X_("/strip/monitor_input"), ssid, 0, in_line, addr);
261                 _osc.float_message_with_id (X_("/strip/monitor_disk"), ssid, 0, in_line, addr);
262                 _osc.float_message_with_id (X_("/strip/gui_select"), ssid, 0, in_line, addr);
263                 _osc.float_message_with_id (X_("/strip/select"), ssid, 0, in_line, addr);
264         }
265         if (feedback[1]) { // level controls
266                 if (gainmode) {
267                         _osc.float_message_with_id (X_("/strip/fader"), ssid, 0, in_line, addr);
268                 } else {
269                         _osc.float_message_with_id (X_("/strip/gain"), ssid, -193, in_line, addr);
270                 }
271                 _osc.float_message_with_id (X_("/strip/trimdB"), ssid, 0, in_line, addr);
272                 _osc.float_message_with_id (X_("/strip/pan_stereo_position"), ssid, 0.5, in_line, addr);
273         }
274         if (feedback[9]) {
275                 _osc.float_message_with_id (X_("/strip/signal"), ssid, 0, in_line, addr);
276         }
277         if (feedback[7]) {
278                 if (gainmode) {
279                         _osc.float_message_with_id (X_("/strip/meter"), ssid, 0, in_line, addr);
280                 } else {
281                         _osc.float_message_with_id (X_("/strip/meter"), ssid, -193, in_line, addr);
282                 }
283         }else if (feedback[8]) {
284                 _osc.float_message_with_id (X_("/strip/meter"), ssid, 0, in_line, addr);
285         }
286 }
287
288
289 void
290 OSCRouteObserver::tick ()
291 {
292         if (_init) {
293                 return;
294         }
295         _tick_busy = true;
296         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
297                 // the only meter here is master
298                 float now_meter;
299                 if (_strip->peak_meter()) {
300                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
301                 } else {
302                         now_meter = -193;
303                 }
304                 if (now_meter < -120) now_meter = -193;
305                 if (_last_meter != now_meter) {
306                         if (feedback[7] || feedback[8]) {
307                                 if (gainmode && feedback[7]) {
308                                         _osc.float_message_with_id (X_("/strip/meter"), ssid, ((now_meter + 94) / 100), in_line, addr);
309                                 } else if ((!gainmode) && feedback[7]) {
310                                         _osc.float_message_with_id (X_("/strip/meter"), ssid, now_meter, in_line, addr);
311                                 } else if (feedback[8]) {
312                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
313                                         uint16_t ledbits = ~(0xfff<<ledlvl);
314                                         _osc.int_message_with_id (X_("/strip/meter"), ssid, ledbits, in_line, addr);
315                                 }
316                         }
317                         if (feedback[9]) {
318                                 float signal;
319                                 if (now_meter < -40) {
320                                         signal = 0;
321                                 } else {
322                                         signal = 1;
323                                 }
324                                 _osc.float_message_with_id (X_("/strip/signal"), ssid, signal, in_line, addr);
325                         }
326                 }
327                 _last_meter = now_meter;
328
329         }
330         if (feedback[1]) {
331                 if (gain_timeout) {
332                         if (gain_timeout == 1) {
333                                 _osc.text_message_with_id (X_("/strip/name"), ssid, _strip->name(), in_line, addr);
334                         }
335                         gain_timeout--;
336                 }
337                 if (as == ARDOUR::Play ||  as == ARDOUR::Touch) {
338                         if(_last_gain != _strip->gain_control()->get_value()) {
339                                 _last_gain = _strip->gain_control()->get_value();
340                                 send_gain_message ();
341                         }
342                 }
343         }
344         _tick_busy = false;
345 }
346
347 void
348 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
349 {
350         if (!what_changed.contains (ARDOUR::Properties::name)) {
351             return;
352         }
353
354         if (_strip) {
355                 _osc.text_message_with_id (X_("/strip/name"), ssid, _strip->name(), in_line, addr);
356         }
357 }
358
359 void
360 OSCRouteObserver::group_name ()
361 {
362         boost::shared_ptr<Route> rt = boost::dynamic_pointer_cast<Route> (_strip);
363
364         RouteGroup *rg = rt->route_group();
365         if (rg) {
366                 _osc.text_message_with_id (X_("/strip/group"), ssid, rg->name(), in_line, addr);
367         } else {
368                 _osc.text_message_with_id (X_("/strip/group"), ssid, " ", in_line, addr);
369         }
370 }
371
372 void
373 OSCRouteObserver::pi_changed (PBD::PropertyChange const& what_changed)
374 {
375         if (!what_changed.contains (ARDOUR::Properties::hidden)) {
376                 return;
377         }
378         _osc.int_message_with_id (X_("/strip/hide"), ssid, _strip->is_hidden (), in_line, addr);
379 }
380
381 void
382 OSCRouteObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
383 {
384         float val = controllable->get_value();
385         _osc.float_message_with_id (path, ssid, (float) controllable->internal_to_interface (val), in_line, addr);
386 }
387
388 void
389 OSCRouteObserver::send_monitor_status (boost::shared_ptr<Controllable> controllable)
390 {
391         int disk, input;
392         float val = controllable->get_value();
393         switch ((int) val) {
394                 case 1:
395                         disk = 0;
396                         input = 1;
397                         break;
398                 case 2:
399                         disk = 1;
400                         input = 0;
401                         break;
402                 case 3:
403                         disk = 1;
404                         input = 1;
405                         break;
406                 default:
407                         disk = 0;
408                         input = 0;
409         }
410         _osc.int_message_with_id (X_("/strip/monitor_input"), ssid, input, in_line, addr);
411         _osc.int_message_with_id (X_("/strip/monitor_disk"), ssid, disk, in_line, addr);
412
413 }
414
415 void
416 OSCRouteObserver::send_trim_message ()
417 {
418         if (_last_trim != _strip->trim_control()->get_value()) {
419                 _last_trim = _strip->trim_control()->get_value();
420         } else {
421                 return;
422         }
423         _osc.float_message_with_id (X_("/strip/trimdB"), ssid, (float) accurate_coefficient_to_dB (_last_trim), in_line, addr);
424 }
425
426 void
427 OSCRouteObserver::send_gain_message ()
428 {
429         boost::shared_ptr<Controllable> controllable = _strip->gain_control();
430         if (_last_gain != controllable->get_value()) {
431                 _last_gain = controllable->get_value();
432         } else {
433                 return;
434         }
435
436         if (gainmode) {
437                 _osc.float_message_with_id (X_("/strip/fader"), ssid, controllable->internal_to_interface (_last_gain), in_line, addr);
438                 if (gainmode == 1) {
439                         _osc.text_message_with_id (X_("/strip/name"), ssid, string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())), in_line, addr);
440                         gain_timeout = 8;
441                 }
442         }
443         if (!gainmode || gainmode == 2) {
444                 if (controllable->get_value() < 1e-15) {
445                         _osc.float_message_with_id (X_("/strip/gain"), ssid, -200, in_line, addr);
446                 } else {
447                         _osc.float_message_with_id (X_("/strip/gain"), ssid, accurate_coefficient_to_dB (_last_gain), in_line, addr);
448                 }
449         }
450 }
451
452 void
453 OSCRouteObserver::gain_automation ()
454 {
455         string path = X_("/strip/gain");
456         if (gainmode) {
457                 path = X_("/strip/fader");
458         }
459         send_gain_message ();
460         as = _strip->gain_control()->alist()->automation_state();
461         string auto_name;
462         float output = 0;
463         switch (as) {
464                 case ARDOUR::Off:
465                         output = 0;
466                         auto_name = "Manual";
467                         break;
468                 case ARDOUR::Play:
469                         output = 1;
470                         auto_name = "Play";
471                         break;
472                 case ARDOUR::Write:
473                         output = 2;
474                         auto_name = "Write";
475                         break;
476                 case ARDOUR::Touch:
477                         output = 3;
478                         auto_name = "Touch";
479                         break;
480                 case ARDOUR::Latch:
481                         output = 4;
482                         auto_name = "Latch";
483                         break;
484                 default:
485                         break;
486         }
487         _osc.float_message_with_id (string_compose (X_("%1/automation"), path), ssid, output, in_line, addr);
488         _osc.text_message_with_id (string_compose (X_("%1/automation_name"), path), ssid, auto_name, in_line, addr);
489 }
490
491 void
492 OSCRouteObserver::send_select_status (const PropertyChange& what)
493 {
494         if (what == PropertyChange(ARDOUR::Properties::selected)) {
495                 if (_strip) {
496                         _osc.float_message_with_id (X_("/strip/select"), ssid, _strip->is_selected(), in_line, addr);
497                 }
498         }
499 }