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