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