OSC: Only send new select send values
[ardour.git] / libs / surfaces / osc / osc_select_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 <vector>
21 #include "boost/lambda/lambda.hpp"
22
23 #include "pbd/control_math.h"
24
25 #include "ardour/session.h"
26 #include "ardour/track.h"
27 #include "ardour/monitor_control.h"
28 #include "ardour/dB.h"
29 #include "ardour/meter.h"
30 #include "ardour/phase_control.h"
31 #include "ardour/solo_isolate_control.h"
32 #include "ardour/solo_safe_control.h"
33 #include "ardour/route.h"
34 #include "ardour/send.h"
35 #include "ardour/plugin.h"
36 #include "ardour/plugin_insert.h"
37 #include "ardour/processor.h"
38 #include "ardour/readonly_control.h"
39
40 #include "osc.h"
41 #include "osc_select_observer.h"
42
43 #include <glibmm.h>
44
45 #include "pbd/i18n.h"
46
47 using namespace std;
48 using namespace PBD;
49 using namespace ARDOUR;
50 using namespace ArdourSurface;
51
52 OSCSelectObserver::OSCSelectObserver (OSC& o, ArdourSurface::OSC::OSCSurface* su)
53         : _osc (o)
54         ,sur (su)
55         ,nsends (0)
56         ,_last_gain (-1.0)
57         ,_last_trim (-1.0)
58         ,_init (true)
59         ,eq_bands (0)
60         ,_expand (2048)
61 {
62         addr = lo_address_new_from_url  (sur->remote_url.c_str());
63         gainmode = sur->gainmode;
64         feedback = sur->feedback;
65         in_line = feedback[2];
66         send_page_size = sur->send_page_size;
67         plug_page_size = sur->plug_page_size;
68         send_page = sur->send_page;
69         refresh_strip (sur->select, sur->nsends, true);
70         set_expand (sur->expand_enable);
71 }
72
73 OSCSelectObserver::~OSCSelectObserver ()
74 {
75         _init = true;
76         no_strip ();
77         lo_address_free (addr);
78 }
79
80 void
81 OSCSelectObserver::no_strip ()
82 {
83         // This gets called on drop references
84         _init = true;
85
86         strip_connections.drop_connections ();
87         send_connections.drop_connections ();
88         plugin_connections.drop_connections ();
89         eq_connections.drop_connections ();
90         /*
91          * The strip will sit idle at this point doing nothing until
92          * the surface has recalculated it's strip list and then calls
93          * refresh_strip. Otherwise refresh strip will get a strip address
94          * that does not exist... Crash
95          */
96  }
97
98 void
99 OSCSelectObserver::refresh_strip (boost::shared_ptr<ARDOUR::Stripable> new_strip, uint32_t s_nsends, bool force)
100 {
101         _init = true;
102         if (_tick_busy) {
103                 Glib::usleep(100); // let tick finish
104         }
105
106         if (_strip && (new_strip == _strip) && !force) {
107                 _init = false;
108                 return;
109         }
110
111         _strip = new_strip;
112         if (!_strip) {
113                 clear_observer ();
114                 return;
115         }
116
117         _strip->DropReferences.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::no_strip, this), OSC::instance());
118         as = ARDOUR::Off;
119         send_size = 0;
120         plug_size = 0;
121         _comp_redux = 1;
122         nsends = s_nsends;
123         _last_gain = -1.0;
124         _last_trim = -1.0;
125
126         _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::name_changed, this, boost::lambda::_1), OSC::instance());
127         name_changed (ARDOUR::Properties::name);
128
129         _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/mute"), _strip->mute_control()), OSC::instance());
130         change_message ("/select/mute", _strip->mute_control());
131
132         _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo"), _strip->solo_control()), OSC::instance());
133         change_message ("/select/solo", _strip->solo_control());
134
135         if (_strip->solo_isolate_control()) {
136                 _strip->solo_isolate_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo_iso"), _strip->solo_isolate_control()), OSC::instance());
137                 change_message ("/select/solo_iso", _strip->solo_isolate_control());
138         }
139
140         if (_strip->solo_safe_control()) {
141                 _strip->solo_safe_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo_safe"), _strip->solo_safe_control()), OSC::instance());
142                 change_message ("/select/solo_safe", _strip->solo_safe_control());
143         }
144
145         boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
146         if (track) {
147                 track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::monitor_status, this, track->monitoring_control()), OSC::instance());
148                 monitor_status (track->monitoring_control());
149         }
150
151         boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
152         if (rec_controllable) {
153                 rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/recenable"), _strip->rec_enable_control()), OSC::instance());
154                 change_message ("/select/recenable", _strip->rec_enable_control());
155         }
156
157         boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
158         if (recsafe_controllable) {
159                 recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/record_safe"), _strip->rec_safe_control()), OSC::instance());
160                 change_message ("/select/record_safe", _strip->rec_safe_control());
161         }
162
163         boost::shared_ptr<AutomationControl> phase_controllable = _strip->phase_control ();
164         if (phase_controllable) {
165                 phase_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/polarity"), _strip->phase_control()), OSC::instance());
166                 change_message ("/select/polarity", _strip->phase_control());
167         }
168
169         _strip->gain_control()->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_automation, this), OSC::instance());
170         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this), OSC::instance());
171         gain_automation ();
172
173         boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
174         if (trim_controllable) {
175                 trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::trim_message, this, X_("/select/trimdB"), _strip->trim_control()), OSC::instance());
176                 trim_message ("/select/trimdB", _strip->trim_control());
177         }
178
179         boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
180         if (pan_controllable) {
181                 pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_stereo_position"), _strip->pan_azimuth_control()), OSC::instance());
182                 change_message ("/select/pan_stereo_position", _strip->pan_azimuth_control());
183         }
184
185         boost::shared_ptr<Controllable> width_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_width_control());
186         if (width_controllable) {
187                 width_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_stereo_width"), _strip->pan_width_control()), OSC::instance());
188                 change_message ("/select/pan_stereo_width", _strip->pan_width_control());
189         }
190
191         // Rest of possible pan controls... Untested because I can't find a way to get them in the GUI :)
192         if (_strip->pan_elevation_control ()) {
193                 _strip->pan_elevation_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_elevation_position"), _strip->pan_elevation_control()), OSC::instance());
194                 change_message ("/select/pan_elevation_position", _strip->pan_elevation_control());
195         }
196         if (_strip->pan_frontback_control ()) {
197                 _strip->pan_frontback_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_frontback_position"), _strip->pan_frontback_control()), OSC::instance());
198                 change_message ("/select/pan_frontback_position", _strip->pan_frontback_control());
199         }
200         if (_strip->pan_lfe_control ()) {
201                 _strip->pan_lfe_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_lfe_control"), _strip->pan_lfe_control()), OSC::instance());
202                 change_message ("/select/pan_lfe_control", _strip->pan_lfe_control());
203         }
204
205         // sends, plugins and eq
206         // detecting processor changes is now in osc.cc
207
208         // but... MB master send enable is different
209         if (_strip->master_send_enable_controllable ()) {
210                 _strip->master_send_enable_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message, this, X_("/select/master_send_enable"), _strip->master_send_enable_controllable()), OSC::instance());
211                 enable_message ("/select/master_send_enable", _strip->master_send_enable_controllable());
212         }
213
214         // Compressor
215         if (_strip->comp_enable_controllable ()) {
216                 _strip->comp_enable_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message, this, X_("/select/comp_enable"), _strip->comp_enable_controllable()), OSC::instance());
217                 enable_message ("/select/comp_enable", _strip->comp_enable_controllable());
218         }
219         if (_strip->comp_threshold_controllable ()) {
220                 _strip->comp_threshold_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/comp_threshold"), _strip->comp_threshold_controllable()), OSC::instance());
221                 change_message ("/select/comp_threshold", _strip->comp_threshold_controllable());
222         }
223         if (_strip->comp_speed_controllable ()) {
224                 _strip->comp_speed_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/comp_speed"), _strip->comp_speed_controllable()), OSC::instance());
225                 change_message ("/select/comp_speed", _strip->comp_speed_controllable());
226         }
227         if (_strip->comp_mode_controllable ()) {
228                 _strip->comp_mode_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::comp_mode, this), OSC::instance());
229                 comp_mode ();
230         }
231         if (_strip->comp_makeup_controllable ()) {
232                 _strip->comp_makeup_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/comp_makeup"), _strip->comp_makeup_controllable()), OSC::instance());
233                 change_message ("/select/comp_makeup", _strip->comp_makeup_controllable());
234         }
235         renew_sends ();
236         renew_plugin ();
237         eq_restart(0);
238         _init = false;
239
240         tick();
241 }
242
243 void
244 OSCSelectObserver::set_expand (uint32_t expand)
245 {
246         if (expand != _expand) {
247                 _expand = expand;
248                 if (expand) {
249                         _osc.float_message ("/select/expand", 1.0, addr);
250                 } else {
251                         _osc.float_message ("/select/expand", 0.0, addr);
252                 }
253         }
254 }
255
256 void
257 OSCSelectObserver::set_send_page (uint32_t page)
258 {
259         send_page = page;
260         renew_sends ();
261 }
262
263 void
264 OSCSelectObserver::set_send_size (uint32_t size)
265 {
266         send_page_size = size;
267         renew_sends ();
268 }
269
270 void
271 OSCSelectObserver::clear_observer ()
272 {
273         _init = true;
274         strip_connections.drop_connections ();
275         // all strip buttons should be off and faders 0 and etc.
276         _osc.float_message ("/select/expand", 0, addr);
277         _osc.text_message ("/select/name", " ", addr);
278         _osc.text_message ("/select/comment", " ", addr);
279         _osc.float_message ("/select/mute", 0, addr);
280         _osc.float_message ("/select/solo", 0, addr);
281         _osc.float_message ("/select/recenable", 0, addr);
282         _osc.float_message ("/select/record_safe", 0, addr);
283         _osc.float_message ("/select/monitor_input", 0, addr);
284         _osc.float_message ("/select/monitor_disk", 0, addr);
285         _osc.float_message ("/select/polarity", 0, addr);
286         _osc.float_message ("/select/n_inputs", 0, addr);
287         _osc.float_message ("/select/n_outputs", 0, addr);
288         if (gainmode) {
289                 _osc.float_message ("/select/fader", 0, addr);
290         } else {
291                 _osc.float_message ("/select/gain", -193, addr);
292         }
293         _osc.float_message ("/select/trimdB", 0, addr);
294         _osc.float_message ("/select/pan_stereo_position", 0.5, addr);
295         _osc.float_message ("/select/pan_stereo_width", 1, addr);
296         if (feedback[9]) {
297                 _osc.float_message ("/select/signal", 0, addr);
298         }
299         if (feedback[7]) {
300                 if (gainmode) {
301                         _osc.float_message ("/select/meter", 0, addr);
302                 } else {
303                         _osc.float_message ("/select/meter", -193, addr);
304                 }
305         }else if (feedback[8]) {
306                 _osc.float_message ("/select/meter", 0, addr);
307         }
308         _osc.float_message ("/select/pan_elevation_position", 0, addr);
309         _osc.float_message ("/select/pan_frontback_position", .5, addr);
310         _osc.float_message ("/select/pan_lfe_control", 0, addr);
311         _osc.float_message ("/select/comp_enable", 0, addr);
312         _osc.float_message ("/select/comp_threshold", 0, addr);
313         _osc.float_message ("/select/comp_speed", 0, addr);
314         _osc.float_message ("/select/comp_mode", 0, addr);
315         _osc.text_message ("/select/comp_mode_name", " ", addr);
316         _osc.text_message ("/select/comp_speed_name", " ", addr);
317         _osc.float_message ("/select/comp_makeup", 0, addr);
318         _osc.float_message ("/select/expand", 0.0, addr);
319         send_end();
320         plugin_end();
321         eq_end();
322 }
323
324 void
325 OSCSelectObserver::renew_sends () {
326         send_connections.drop_connections ();
327         send_timeout.clear();
328         send_init();
329 }
330
331 void
332 OSCSelectObserver::renew_plugin () {
333         plugin_end();
334         plugin_init();
335 }
336
337 void
338 OSCSelectObserver::send_init()
339 {
340         send_size = nsends;
341         if (send_page_size) {
342                 send_size = send_page_size;
343         }
344         if (!send_size) {
345                 return;
346         }
347         uint32_t page_start = ((send_page - 1) * send_size);
348         uint32_t last_send = send_page * send_size;
349         uint32_t c = 1;
350         send_timeout.push_back (2);
351         _last_send.clear();
352         _last_send.push_back (0.0);
353
354         for (uint32_t s = page_start; s < last_send; ++s, ++c) {
355
356                 bool send_valid = false;
357                 if (_strip->send_level_controllable (s)) {
358                         _strip->send_level_controllable(s)->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain, this, c, _strip->send_level_controllable(s)), OSC::instance());
359                         send_timeout.push_back (2);
360                         _last_send.push_back (20.0);
361                         send_gain (c, _strip->send_level_controllable(s));
362                         send_valid = true;
363                 } else {
364                         send_gain (c, _strip->send_level_controllable(s));
365                         _osc.float_message_with_id ("/select/send_enable", c, 0, in_line, addr);
366                         _osc.text_message_with_id ("/select/send_name", c, " ", in_line, addr);
367                 }
368
369                 if (_strip->send_enable_controllable (s)) {
370                         _strip->send_enable_controllable(s)->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message_with_id, this, X_("/select/send_enable"), c, _strip->send_enable_controllable(s)), OSC::instance());
371                         enable_message_with_id ("/select/send_enable", c, _strip->send_enable_controllable(s));
372                 } else if (send_valid) {
373                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (_strip);
374                         if (!r) {
375                                 // should never get here
376                                 _osc.float_message_with_id ("/select/send_enable", c, 0, in_line, addr);
377                         }
378                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(s));
379                         if (snd) {
380                                 boost::shared_ptr<Processor> proc = boost::dynamic_pointer_cast<Processor> (snd);
381                                 proc->ActiveChanged.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_enable, this, X_("/select/send_enable"), c, proc), OSC::instance());
382                                 _osc.float_message_with_id ("/select/send_enable", c, proc->enabled(), in_line, addr);
383                         }
384                 }
385                 if (!gainmode && send_valid) {
386                         _osc.text_message_with_id ("/select/send_name", c, _strip->send_name(s), in_line, addr);
387                 }
388         }
389 }
390
391 void
392 OSCSelectObserver::plugin_init()
393 {
394         if (!sur->plugin_id || !sur->plugins.size ()) {
395                 return;
396         }
397
398         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(_strip);
399         if (!r) {
400                 return;
401         }
402
403         // we have a plugin number now get the processor
404         boost::shared_ptr<Processor> proc = r->nth_plugin (sur->plugins[sur->plugin_id - 1]);
405         boost::shared_ptr<PluginInsert> pi;
406         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
407                 return;
408         }
409         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
410
411         bool ok = false;
412         nplug_params = sur->plug_params.size ();
413
414         // default of 0 page size means show all
415         plug_size = nplug_params;
416         if (plug_page_size) {
417                 plug_size = plug_page_size;
418         }
419         _osc.text_message ("/select/plugin/name", pip->name(), addr);
420         uint32_t page_end = nplug_params;
421         uint32_t max_page = 1;
422         if (plug_size && nplug_params) {
423                 max_page = (uint32_t)((nplug_params - 1) / plug_size) + 1;
424         }
425
426         if (sur->plug_page < 1) {
427                 sur->plug_page = 1;
428         }
429         if ((uint32_t)sur->plug_page > max_page) {
430                 sur->plug_page = max_page;
431         }
432         uint32_t page_start = ((sur->plug_page - 1) * plug_size);
433         page_end = sur->plug_page * plug_size;
434
435         int pid = 1;
436         for ( uint32_t ppi = page_start;  ppi < page_end; ++ppi, ++pid) {
437                 if (ppi >= nplug_params) {
438                         _osc.text_message_with_id ("/select/plugin/parameter/name", pid, " ", in_line, addr);
439                         _osc.float_message_with_id ("/select/plugin/parameter", pid, 0, in_line, addr);
440                         continue;
441                 }
442
443                 uint32_t controlid = pip->nth_parameter(sur->plug_params[ppi], ok);
444                 if (!ok) {
445                         continue;
446                 }
447                 ParameterDescriptor pd;
448                 pip->get_parameter_descriptor(controlid, pd);
449                 _osc.text_message_with_id ("/select/plugin/parameter/name", pid, pd.label, in_line, addr);
450                 if ( pip->parameter_is_input(controlid)) {
451                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
452                         if (c) {
453                                 bool swtch = false;
454                                 if (pd.integer_step && pd.upper == 1) {
455                                         swtch = true;
456                                 }
457                                 c->Changed.connect (plugin_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::plugin_parameter_changed, this, pid, swtch, c), OSC::instance());
458                                 plugin_parameter_changed (pid, swtch, c);
459                         }
460                 }
461         }
462 }
463
464 void
465 OSCSelectObserver::send_end ()
466 {
467         send_connections.drop_connections ();
468         for (uint32_t i = 1; i <= send_size; i++) {
469                 if (gainmode) {
470                         _osc.float_message_with_id ("/select/send_fader", i, 0, in_line, addr);
471                 } else {
472                         _osc.float_message_with_id ("/select/send_gain", i, -193, in_line, addr);
473                 }
474                 // next enable
475                 _osc.float_message_with_id ("/select/send_enable", i, 0, in_line, addr);
476                 // next name
477                 _osc.text_message_with_id ("/select/send_name", i, " ", in_line, addr);
478         }
479         // need to delete or clear send_timeout
480         send_timeout.clear();
481 }
482
483 void
484 OSCSelectObserver::plugin_parameter_changed (int pid, bool swtch, boost::shared_ptr<PBD::Controllable> controllable)
485 {
486         if (swtch) {
487                 enable_message_with_id ("/select/plugin/parameter", pid, controllable);
488         } else {
489                 change_message_with_id ("/select/plugin/parameter", pid, controllable);
490         }
491 }
492
493 void
494 OSCSelectObserver::plugin_end ()
495 {
496         plugin_connections.drop_connections ();
497         _osc.text_message ("/select/plugin/name", " ", addr);
498         for (uint32_t i = 1; i <= plug_size; i++) {
499                 _osc.float_message_with_id ("/select/plugin/parameter", i, 0, in_line, addr);
500                 // next name
501                 _osc.text_message_with_id ("/select/plugin/parameter/name", i, " ", in_line, addr);
502         }
503         nplug_params = 0;
504 }
505
506 void
507 OSCSelectObserver::tick ()
508 {
509         if (_init) {
510                 return;
511         }
512         _tick_busy = true;
513         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
514                 float now_meter;
515                 if (_strip->peak_meter()) {
516                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
517                 } else {
518                         now_meter = -193;
519                 }
520                 if (now_meter < -144) now_meter = -193;
521                 if (_last_meter != now_meter) {
522                         if (feedback[7] || feedback[8]) {
523                                 string path = "/select/meter";
524                                 if (gainmode && feedback[7]) {
525                                         _osc.float_message (path, ((now_meter + 94) / 100), addr);
526                                 } else if ((!gainmode) && feedback[7]) {
527                                         _osc.float_message (path, now_meter, addr);
528                                 } else if (feedback[8]) {
529                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
530                                         uint16_t ledbits = ~(0xfff<<ledlvl);
531                                         _osc.float_message (path, ledbits, addr);
532                                 }
533                         }
534                         if (feedback[9]) {
535                                 string path = "/select/signal";
536                                 float signal;
537                                 if (now_meter < -40) {
538                                         signal = 0;
539                                 } else {
540                                         signal = 1;
541                                 }
542                                 _osc.float_message (path, signal, addr);
543                         }
544                 }
545                 _last_meter = now_meter;
546
547         }
548         if (gain_timeout) {
549                 if (gain_timeout == 1) {
550                         _osc.text_message ("/select/name", _strip->name(), addr);
551                 }
552                 gain_timeout--;
553         }
554
555         if (as == ARDOUR::Play ||  as == ARDOUR::Touch) {
556                 if(_last_gain != _strip->gain_control()->get_value()) {
557                         _last_gain = _strip->gain_control()->get_value();
558                                 gain_message ();
559                 }
560         }
561         if (_strip->comp_redux_controllable() && _strip->comp_enable_controllable() && _strip->comp_enable_controllable()->get_value()) {
562                 float new_value = _strip->comp_redux_controllable()->get_parameter();
563                 if (_comp_redux != new_value) {
564                         _osc.float_message ("/select/comp_redux", new_value, addr);
565                         _comp_redux = new_value;
566                 }
567         }
568         for (uint32_t i = 1; i <= send_timeout.size(); i++) {
569                 if (send_timeout[i]) {
570                         if (send_timeout[i] == 1) {
571                                 uint32_t pg_offset = (send_page - 1) * send_page_size;
572                                 _osc.text_message_with_id ("/select/send_name", i, _strip->send_name(pg_offset + i - 1), in_line, addr);
573                         }
574                         send_timeout[i]--;
575                 }
576         }
577         _tick_busy = false;
578 }
579
580 void
581 OSCSelectObserver::name_changed (const PBD::PropertyChange& what_changed)
582 {
583         if (!what_changed.contains (ARDOUR::Properties::name)) {
584                 return;
585         }
586
587         if (!_strip) {
588                 return;
589         }
590
591         _osc.text_message ("/select/name", _strip->name(), addr);
592         boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (_strip);
593         if (route) {
594                 //spit out the comment at the same time
595                 _osc.text_message ("/select/comment", route->comment(), addr);
596                 // lets tell the surface how many inputs this strip has
597                 _osc.float_message ("/select/n_inputs", (float) route->n_inputs().n_total(), addr);
598                 // lets tell the surface how many outputs this strip has
599                 _osc.float_message ("/select/n_outputs", (float) route->n_outputs().n_total(), addr);
600         }
601 }
602
603 void
604 OSCSelectObserver::change_message (string path, boost::shared_ptr<Controllable> controllable)
605 {
606         float val = controllable->get_value();
607
608         _osc.float_message (path, (float) controllable->internal_to_interface (val), addr);
609 }
610
611 void
612 OSCSelectObserver::enable_message (string path, boost::shared_ptr<Controllable> controllable)
613 {
614         float val = controllable->get_value();
615         if (val) {
616                 _osc.float_message (path, 1, addr);
617         } else {
618                 _osc.float_message (path, 0, addr);
619         }
620
621 }
622
623 void
624 OSCSelectObserver::change_message_with_id (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
625 {
626         float val = controllable->get_value();
627
628         _osc.float_message_with_id (path, id, (float) controllable->internal_to_interface (val), in_line, addr);
629 }
630
631 void
632 OSCSelectObserver::enable_message_with_id (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
633 {
634         float val = controllable->get_value();
635         if (val) {
636                 _osc.float_message_with_id (path, id, 1, in_line, addr);
637         } else {
638                 _osc.float_message_with_id (path, id, 0, in_line, addr);
639         }
640 }
641
642 void
643 OSCSelectObserver::monitor_status (boost::shared_ptr<Controllable> controllable)
644 {
645         int disk, input;
646         float val = controllable->get_value();
647         switch ((int) val) {
648                 case 1:
649                         disk = 0;
650                         input = 1;
651                         break;
652                 case 2:
653                         disk = 1;
654                         input = 0;
655                         break;
656                 default:
657                         disk = 0;
658                         input = 0;
659         }
660
661         _osc.float_message ("/select/monitor_input", (float) input, addr);
662         _osc.float_message ("/select/monitor_disk", (float) disk, addr);
663 }
664
665 void
666 OSCSelectObserver::trim_message (string path, boost::shared_ptr<Controllable> controllable)
667 {
668         if (_last_trim != controllable->get_value()) {
669                 _last_trim = controllable->get_value();
670         } else {
671                 return;
672         }
673
674         _osc.float_message (path, (float) accurate_coefficient_to_dB (controllable->get_value()), addr);
675 }
676
677 void
678 OSCSelectObserver::gain_message ()
679 {
680         float value = _strip->gain_control()->get_value();
681         if (_last_gain != value) {
682                 _last_gain = value;
683         } else {
684                 return;
685         }
686
687         if (gainmode) {
688                 _osc.text_message ("/select/name", string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (value)), addr);
689                 gain_timeout = 8;
690                 _osc.float_message ("/select/fader", _strip->gain_control()->internal_to_interface (value), addr);
691         } else {
692                 if (value < 1e-15) {
693                         _osc.float_message ("/select/gain", -200, addr);
694                 } else {
695                         _osc.float_message ("/select/gain", accurate_coefficient_to_dB (value), addr);
696                 }
697         }
698 }
699
700 void
701 OSCSelectObserver::gain_automation ()
702 {
703         float output = 0;
704         as = _strip->gain_control()->alist()->automation_state();
705         string auto_name;
706         switch (as) {
707                 case ARDOUR::Off:
708                         output = 0;
709                         auto_name = "Manual";
710                         break;
711                 case ARDOUR::Play:
712                         output = 1;
713                         auto_name = "Play";
714                         break;
715                 case ARDOUR::Write:
716                         output = 2;
717                         auto_name = "Write";
718                         break;
719                 case ARDOUR::Touch:
720                         output = 3;
721                         auto_name = "Touch";
722                         break;
723                 default:
724                         break;
725         }
726
727         if (gainmode) {
728                 _osc.float_message ("/select/fader/automation", output, addr);
729                 _osc.text_message ("/select/fader/automation_name", auto_name, addr);
730         } else {
731                 _osc.float_message ("/select/gain/automation", output, addr);
732                 _osc.text_message ("/select/gain/automation_name", auto_name, addr);
733         }
734
735         gain_message ();
736 }
737
738 void
739 OSCSelectObserver::send_gain (uint32_t id, boost::shared_ptr<PBD::Controllable> controllable)
740 {
741         float raw_value = 0.0;
742         if (controllable) {
743                 raw_value = controllable->get_value();
744         }
745         if (_last_send[id] != raw_value) {
746                 _last_send[id] = raw_value;
747         } else {
748                 return;
749         }
750         string path;
751         float value = 0.0;
752         float db;
753 #ifdef MIXBUS
754         if (controllable) {
755                 db = raw_value;
756         } else {
757                 db = -193;
758         }
759 #else
760         if (raw_value < 1e-15) {
761                 db = -193;
762         } else {
763                 db = accurate_coefficient_to_dB (raw_value);
764         }
765 #endif
766
767         if (gainmode) {
768                 path = "/select/send_fader";
769                 if (controllable) {
770                         value = controllable->internal_to_interface (raw_value);
771                 }
772                 _osc.text_message_with_id ("/select/send_name" , id, string_compose ("%1%2%3", std::fixed, std::setprecision(2), db), in_line, addr);
773                 if (send_timeout.size() > id) {
774                         send_timeout[id] = 8;
775                 }
776         } else {
777                 path = "/select/send_gain";
778                 value = db;
779         }
780
781         _osc.float_message_with_id (path, id, value, in_line, addr);
782 }
783
784 void
785 OSCSelectObserver::send_enable (string path, uint32_t id, boost::shared_ptr<Processor> proc)
786 {
787         // with no delay value is wrong
788         Glib::usleep(10);
789
790         _osc.float_message_with_id ("/select/send_enable", id, proc->enabled(), in_line, addr);
791 }
792
793 void
794 OSCSelectObserver::comp_mode ()
795 {
796         change_message ("/select/comp_mode", _strip->comp_mode_controllable());
797         _osc.text_message ("/select/comp_mode_name", _strip->comp_mode_name(_strip->comp_mode_controllable()->get_value()), addr);
798         _osc.text_message ("/select/comp_speed_name", _strip->comp_speed_name(_strip->comp_mode_controllable()->get_value()), addr);
799 }
800
801 void
802 OSCSelectObserver::eq_init()
803 {
804         // HPF and enable are special case, rest are in bands
805         if (_strip->filter_enable_controllable (true)) {
806                 _strip->filter_enable_controllable (true)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf/enable"), _strip->filter_enable_controllable (true)), OSC::instance());
807                 change_message ("/select/eq_hpf/enable", _strip->filter_enable_controllable(true));
808         }
809
810         if (_strip->filter_enable_controllable (false)) {
811                 _strip->filter_enable_controllable (false)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_lpf/enable"), _strip->filter_enable_controllable (false)), OSC::instance());
812                 change_message ("/select/eq_lpf/enable", _strip->filter_enable_controllable(false));
813         }
814
815         if (_strip->filter_freq_controllable (true)) {
816                 _strip->filter_freq_controllable (true)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf/freq"), _strip->filter_freq_controllable (true)), OSC::instance());
817                 change_message ("/select/eq_hpf/freq", _strip->filter_freq_controllable(true));
818         }
819
820         if (_strip->filter_freq_controllable (false)) {
821                 _strip->filter_freq_controllable (false)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_lpf/freq"), _strip->filter_freq_controllable (false)), OSC::instance());
822                 change_message ("/select/eq_lpf/freq", _strip->filter_freq_controllable(false));
823         }
824
825         if (_strip->filter_slope_controllable (true)) {
826                 _strip->filter_slope_controllable (true)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf/slope"), _strip->filter_slope_controllable (true)), OSC::instance());
827                 change_message ("/select/eq_hpf/slope", _strip->filter_slope_controllable(true));
828         }
829
830         if (_strip->filter_slope_controllable (false)) {
831                 _strip->filter_slope_controllable (false)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_lpf/slope"), _strip->filter_slope_controllable (false)), OSC::instance());
832                 change_message ("/select/eq_lpf/slope", _strip->filter_slope_controllable(false));
833         }
834
835         if (_strip->eq_enable_controllable ()) {
836                 _strip->eq_enable_controllable ()->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message, this, X_("/select/eq_enable"), _strip->eq_enable_controllable()), OSC::instance());
837                 enable_message ("/select/eq_enable", _strip->eq_enable_controllable());
838         }
839
840         eq_bands = _strip->eq_band_cnt ();
841         if (eq_bands < 0) {
842                 eq_bands = 0;
843         }
844         if (!eq_bands) {
845                 return;
846         }
847
848         for (int i = 0; i < eq_bands; i++) {
849                 if (_strip->eq_band_name(i).size()) {
850                         _osc.text_message_with_id ("/select/eq_band_name", i + 1, _strip->eq_band_name (i), in_line, addr);
851                 }
852                 if (_strip->eq_gain_controllable (i)) {
853                         _strip->eq_gain_controllable(i)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message_with_id, this, X_("/select/eq_gain"), i + 1, _strip->eq_gain_controllable(i)), OSC::instance());
854                         change_message_with_id ("/select/eq_gain", i + 1, _strip->eq_gain_controllable(i));
855                 }
856                 if (_strip->eq_freq_controllable (i)) {
857                         _strip->eq_freq_controllable(i)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message_with_id, this, X_("/select/eq_freq"), i + 1, _strip->eq_freq_controllable(i)), OSC::instance());
858                         change_message_with_id ("/select/eq_freq", i + 1, _strip->eq_freq_controllable(i));
859                 }
860                 if (_strip->eq_q_controllable (i)) {
861                         _strip->eq_q_controllable(i)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message_with_id, this, X_("/select/eq_q"), i + 1, _strip->eq_q_controllable(i)), OSC::instance());
862                         change_message_with_id ("/select/eq_q", i + 1, _strip->eq_q_controllable(i));
863                 }
864                 if (_strip->eq_shape_controllable (i)) {
865                         _strip->eq_shape_controllable(i)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message_with_id, this, X_("/select/eq_shape"), i + 1, _strip->eq_shape_controllable(i)), OSC::instance());
866                         change_message_with_id ("/select/eq_shape", i + 1, _strip->eq_shape_controllable(i));
867                 }
868         }
869 }
870
871 void
872 OSCSelectObserver::eq_end ()
873 {
874         eq_connections.drop_connections ();
875                 _osc.float_message ("/select/eq_hpf", 0, addr);
876                 _osc.float_message ("/select/eq_enable", 0, addr);
877
878         for (int i = 1; i <= eq_bands; i++) {
879                 _osc.text_message_with_id ("/select/eq_band_name", i, " ", in_line, addr);
880                 _osc.float_message_with_id ("/select/eq_gain", i, 0, in_line, addr);
881                 _osc.float_message_with_id ("/select/eq_freq", i, 0, in_line, addr);
882                 _osc.float_message_with_id ("/select/eq_q", i, 0, in_line, addr);
883                 _osc.float_message_with_id ("/select/eq_shape", i, 0, in_line, addr);
884
885
886         }
887 }
888
889 void
890 OSCSelectObserver::eq_restart(int x)
891 {
892         eq_connections.drop_connections ();
893         //eq_end();
894         eq_init();
895 }