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