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