OSC: added group join, switch, remove function
[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                 rt->route_group_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::group_name, this), OSC::instance());
136                 group_name ();
137
138                 _strip->presentation_info().PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::pi_changed, this, _1), OSC::instance());
139                 _osc.int_message_with_id (X_("/strip/hide"), ssid, _strip->is_hidden (), in_line, addr);
140
141                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/mute"), _strip->mute_control()), OSC::instance());
142                 send_change_message (X_("/strip/mute"), _strip->mute_control());
143
144                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo"), _strip->solo_control()), OSC::instance());
145                 send_change_message (X_("/strip/solo"), _strip->solo_control());
146
147                 if (_strip->solo_isolate_control()) {
148                         _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());
149                         send_change_message (X_("/strip/solo_iso"), _strip->solo_isolate_control());
150                 }
151
152                 if (_strip->solo_safe_control()) {
153                         _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());
154                         send_change_message (X_("/strip/solo_safe"), _strip->solo_safe_control());
155                 }
156
157                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
158                 if (track) {
159                         track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_monitor_status, this, track->monitoring_control()), OSC::instance());
160                         send_monitor_status (track->monitoring_control());
161                 }
162
163                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
164                 if (rec_controllable) {
165                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/recenable"), _strip->rec_enable_control()), OSC::instance());
166                         send_change_message (X_("/strip/recenable"), _strip->rec_enable_control());
167                 }
168                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
169                 if (rec_controllable) {
170                         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());
171                         send_change_message (X_("/strip/record_safe"), _strip->rec_safe_control());
172                 }
173                 _strip->presentation_info().PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_select_status, this, _1), OSC::instance());
174                 send_select_status (ARDOUR::Properties::selected);
175         }
176
177         if (feedback[1]) { // level controls
178                 boost::shared_ptr<GainControl> gain_cont = _strip->gain_control();
179                 gain_cont->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::gain_automation, this), OSC::instance());
180                 gain_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_gain_message, this), OSC::instance());
181                 gain_automation ();
182
183                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
184                 if (trim_controllable) {
185                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_trim_message, this), OSC::instance());
186                         send_trim_message ();
187                 }
188
189                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
190                 if (pan_controllable) {
191                         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());
192                         send_change_message (X_("/strip/pan_stereo_position"), _strip->pan_azimuth_control());
193                 }
194         }
195         _init = false;
196         tick();
197
198 }
199
200 void
201 OSCRouteObserver::set_expand (uint32_t expand)
202 {
203         if (expand != _expand) {
204                 _expand = expand;
205                 if (expand == ssid) {
206                         _osc.float_message_with_id (X_("/strip/expand"), ssid, 1.0, in_line, addr);
207                 } else {
208                         _osc.float_message_with_id (X_("/strip/expand"), ssid, 0.0, in_line, addr);
209                 }
210         }
211 }
212
213 void
214 OSCRouteObserver::set_link_ready (uint32_t not_ready)
215 {
216         if (not_ready) {
217                 clear_strip ();
218                 switch (ssid) {
219                         case 1:
220                                 _osc.text_message_with_id (X_("/strip/name"), ssid, "Device", in_line, addr);
221                                 break;
222                         case 2:
223                                 _osc.text_message_with_id (X_("/strip/name"), ssid, string_compose ("%1", not_ready), in_line, addr);
224                                 break;
225                         case 3:
226                                 _osc.text_message_with_id (X_("/strip/name"), ssid, "Missing", in_line, addr);
227                                 break;
228                         case 4:
229                                 _osc.text_message_with_id (X_("/strip/name"), ssid, "from", in_line, addr);
230                                 break;
231                         case 5:
232                                 _osc.text_message_with_id (X_("/strip/name"), ssid, "Linkset", in_line, addr);
233                                 break;
234                         default:
235                                 break;
236                 }
237         } else {
238                 refresh_strip (_strip, true);
239         }
240 }
241
242 void
243 OSCRouteObserver::clear_strip ()
244 {
245         _init = true;
246
247         strip_connections.drop_connections ();
248
249         // all strip buttons should be off and faders 0 and etc.
250         _osc.float_message_with_id (X_("/strip/expand"), ssid, 0, in_line, addr);
251         if (feedback[0]) { // buttons are separate feedback
252                 _osc.text_message_with_id (X_("/strip/name"), ssid, " ", in_line, addr);
253                 _osc.text_message_with_id (X_("/strip/group"), ssid, "none", in_line, addr);
254                 _osc.float_message_with_id (X_("/strip/mute"), ssid, 0, in_line, addr);
255                 _osc.float_message_with_id (X_("/strip/solo"), ssid, 0, in_line, addr);
256                 _osc.float_message_with_id (X_("/strip/recenable"), ssid, 0, in_line, addr);
257                 _osc.float_message_with_id (X_("/strip/record_safe"), ssid, 0, in_line, addr);
258                 _osc.float_message_with_id (X_("/strip/monitor_input"), ssid, 0, in_line, addr);
259                 _osc.float_message_with_id (X_("/strip/monitor_disk"), ssid, 0, in_line, addr);
260                 _osc.float_message_with_id (X_("/strip/gui_select"), ssid, 0, in_line, addr);
261                 _osc.float_message_with_id (X_("/strip/select"), ssid, 0, in_line, addr);
262         }
263         if (feedback[1]) { // level controls
264                 if (gainmode) {
265                         _osc.float_message_with_id (X_("/strip/fader"), ssid, 0, in_line, addr);
266                 } else {
267                         _osc.float_message_with_id (X_("/strip/gain"), ssid, -193, in_line, addr);
268                 }
269                 _osc.float_message_with_id (X_("/strip/trimdB"), ssid, 0, in_line, addr);
270                 _osc.float_message_with_id (X_("/strip/pan_stereo_position"), ssid, 0.5, in_line, addr);
271         }
272         if (feedback[9]) {
273                 _osc.float_message_with_id (X_("/strip/signal"), ssid, 0, in_line, addr);
274         }
275         if (feedback[7]) {
276                 if (gainmode) {
277                         _osc.float_message_with_id (X_("/strip/meter"), ssid, 0, in_line, addr);
278                 } else {
279                         _osc.float_message_with_id (X_("/strip/meter"), ssid, -193, in_line, addr);
280                 }
281         }else if (feedback[8]) {
282                 _osc.float_message_with_id (X_("/strip/meter"), ssid, 0, in_line, addr);
283         }
284 }
285
286
287 void
288 OSCRouteObserver::tick ()
289 {
290         if (_init) {
291                 return;
292         }
293         _tick_busy = true;
294         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
295                 // the only meter here is master
296                 float now_meter;
297                 if (_strip->peak_meter()) {
298                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
299                 } else {
300                         now_meter = -193;
301                 }
302                 if (now_meter < -120) now_meter = -193;
303                 if (_last_meter != now_meter) {
304                         if (feedback[7] || feedback[8]) {
305                                 if (gainmode && feedback[7]) {
306                                         _osc.float_message_with_id (X_("/strip/meter"), ssid, ((now_meter + 94) / 100), in_line, addr);
307                                 } else if ((!gainmode) && feedback[7]) {
308                                         _osc.float_message_with_id (X_("/strip/meter"), ssid, now_meter, in_line, addr);
309                                 } else if (feedback[8]) {
310                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
311                                         uint16_t ledbits = ~(0xfff<<ledlvl);
312                                         _osc.int_message_with_id (X_("/strip/meter"), ssid, ledbits, in_line, addr);
313                                 }
314                         }
315                         if (feedback[9]) {
316                                 float signal;
317                                 if (now_meter < -40) {
318                                         signal = 0;
319                                 } else {
320                                         signal = 1;
321                                 }
322                                 _osc.float_message_with_id (X_("/strip/signal"), ssid, signal, in_line, addr);
323                         }
324                 }
325                 _last_meter = now_meter;
326
327         }
328         if (feedback[1]) {
329                 if (gain_timeout) {
330                         if (gain_timeout == 1) {
331                                 _osc.text_message_with_id (X_("/strip/name"), ssid, _strip->name(), in_line, addr);
332                         }
333                         gain_timeout--;
334                 }
335                 if (as == ARDOUR::Play ||  as == ARDOUR::Touch) {
336                         if(_last_gain != _strip->gain_control()->get_value()) {
337                                 _last_gain = _strip->gain_control()->get_value();
338                                 send_gain_message ();
339                         }
340                 }
341         }
342         _tick_busy = false;
343 }
344
345 void
346 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
347 {
348         if (!what_changed.contains (ARDOUR::Properties::name)) {
349             return;
350         }
351
352         if (_strip) {
353                 _osc.text_message_with_id (X_("/strip/name"), ssid, _strip->name(), in_line, addr);
354         }
355 }
356
357 void
358 OSCRouteObserver::group_name ()
359 {
360         boost::shared_ptr<Route> rt = boost::dynamic_pointer_cast<Route> (_strip);
361
362         RouteGroup *rg = rt->route_group();
363         if (rg) {
364                 _osc.text_message_with_id (X_("/strip/group"), ssid, rg->name(), in_line, addr);
365         } else {
366                 _osc.text_message_with_id (X_("/strip/group"), ssid, " ", in_line, addr);
367         }
368 }
369
370 void
371 OSCRouteObserver::pi_changed (PBD::PropertyChange const& what_changed)
372 {
373         _osc.int_message_with_id (X_("/strip/hide"), ssid, _strip->is_hidden (), in_line, addr);
374 }
375
376 void
377 OSCRouteObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
378 {
379         float val = controllable->get_value();
380         _osc.float_message_with_id (path, ssid, (float) controllable->internal_to_interface (val), in_line, addr);
381 }
382
383 void
384 OSCRouteObserver::send_monitor_status (boost::shared_ptr<Controllable> controllable)
385 {
386         int disk, input;
387         float val = controllable->get_value();
388         switch ((int) val) {
389                 case 1:
390                         disk = 0;
391                         input = 1;
392                         break;
393                 case 2:
394                         disk = 1;
395                         input = 0;
396                         break;
397                 case 3:
398                         disk = 1;
399                         input = 1;
400                         break;
401                 default:
402                         disk = 0;
403                         input = 0;
404         }
405         _osc.int_message_with_id (X_("/strip/monitor_input"), ssid, input, in_line, addr);
406         _osc.int_message_with_id (X_("/strip/monitor_disk"), ssid, disk, in_line, addr);
407
408 }
409
410 void
411 OSCRouteObserver::send_trim_message ()
412 {
413         if (_last_trim != _strip->trim_control()->get_value()) {
414                 _last_trim = _strip->trim_control()->get_value();
415         } else {
416                 return;
417         }
418         _osc.float_message_with_id (X_("/strip/trimdB"), ssid, (float) accurate_coefficient_to_dB (_last_trim), in_line, addr);
419 }
420
421 void
422 OSCRouteObserver::send_gain_message ()
423 {
424         boost::shared_ptr<Controllable> controllable = _strip->gain_control();
425         if (_last_gain != controllable->get_value()) {
426                 _last_gain = controllable->get_value();
427         } else {
428                 return;
429         }
430
431         if (gainmode) {
432                 _osc.float_message_with_id (X_("/strip/fader"), ssid, controllable->internal_to_interface (_last_gain), in_line, addr);
433                 if (gainmode == 1) {
434                         _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);
435                         gain_timeout = 8;
436                 }
437         }
438         if (!gainmode || gainmode == 2) {
439                 if (controllable->get_value() < 1e-15) {
440                         _osc.float_message_with_id (X_("/strip/gain"), ssid, -200, in_line, addr);
441                 } else {
442                         _osc.float_message_with_id (X_("/strip/gain"), ssid, accurate_coefficient_to_dB (_last_gain), in_line, addr);
443                 }
444         }
445 }
446
447 void
448 OSCRouteObserver::gain_automation ()
449 {
450         string path = X_("/strip/gain");
451         if (gainmode) {
452                 path = X_("/strip/fader");
453         }
454         send_gain_message ();
455         as = _strip->gain_control()->alist()->automation_state();
456         string auto_name;
457         float output = 0;
458         switch (as) {
459                 case ARDOUR::Off:
460                         output = 0;
461                         auto_name = "Manual";
462                         break;
463                 case ARDOUR::Play:
464                         output = 1;
465                         auto_name = "Play";
466                         break;
467                 case ARDOUR::Write:
468                         output = 2;
469                         auto_name = "Write";
470                         break;
471                 case ARDOUR::Touch:
472                         output = 3;
473                         auto_name = "Touch";
474                         break;
475                 case ARDOUR::Latch:
476                         output = 4;
477                         auto_name = "Latch";
478                         break;
479                 default:
480                         break;
481         }
482         _osc.float_message_with_id (string_compose (X_("%1/automation"), path), ssid, output, in_line, addr);
483         _osc.text_message_with_id (string_compose (X_("%1/automation_name"), path), ssid, auto_name, in_line, addr);
484 }
485
486 void
487 OSCRouteObserver::send_select_status (const PropertyChange& what)
488 {
489         if (what == PropertyChange(ARDOUR::Properties::selected)) {
490                 if (_strip) {
491                         _osc.float_message_with_id (X_("/strip/select"), ssid, _strip->is_selected(), in_line, addr);
492                 }
493         }
494 }