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