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